Module Name:    src
Committed By:   christos
Date:           Tue Apr  2 17:16:50 UTC 2013

Modified Files:
        src/usr.sbin/makemandb: apropos-utils.3 apropos-utils.c apropos-utils.h
            apropos.1 apropos.c

Log Message:
instead of having a format and no format flag, and exposing various formatters,
provide a format enum and expose html formatting too.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/usr.sbin/makemandb/apropos-utils.3
cvs rdiff -u -r1.14 -r1.15 src/usr.sbin/makemandb/apropos-utils.c
cvs rdiff -u -r1.8 -r1.9 src/usr.sbin/makemandb/apropos-utils.h
cvs rdiff -u -r1.11 -r1.12 src/usr.sbin/makemandb/apropos.1
cvs rdiff -u -r1.15 -r1.16 src/usr.sbin/makemandb/apropos.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/makemandb/apropos-utils.3
diff -u src/usr.sbin/makemandb/apropos-utils.3:1.1 src/usr.sbin/makemandb/apropos-utils.3:1.2
--- src/usr.sbin/makemandb/apropos-utils.3:1.1	Tue Feb  7 14:13:32 2012
+++ src/usr.sbin/makemandb/apropos-utils.3	Tue Apr  2 13:16:50 2013
@@ -1,4 +1,4 @@
-.\" $NetBSD: apropos-utils.3,v 1.1 2012/02/07 19:13:32 joerg Exp $
+.\" $NetBSD: apropos-utils.3,v 1.2 2013/04/02 17:16:50 christos Exp $
 .\"
 .\" Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadh...@gmail.com>
 .\" All rights reserved.
@@ -42,11 +42,7 @@
 .Ft void
 .Fn close_db "sqlite3 *db"
 .Ft int
-.Fn run_query "sqlite3 *db" "const char *snippet_args[3]" "query_args *args"
-.Ft int
-.Fn run_query_html "sqlite3 *db" "query_args *args"
-.Ft int
-.Fn run_query_pager "sqlite3 *db" "query_args *args"
+.Fn run_query "sqlite3 *db" "query_format fmt" "query_args *args"
 .Sh DESCRIPTION
 These functions all live in the
 .Pa apropos-utils.h
@@ -61,7 +57,5 @@ develop applications on top of it.
 .Xr close_db 3 ,
 .Xr init_db 3 ,
 .Xr run_query 3 ,
-.Xr run_query_html 3 ,
-.Xr run_query_pager 3
 .Sh AUTHORS
 .An Abhinav Upadhyay

Index: src/usr.sbin/makemandb/apropos-utils.c
diff -u src/usr.sbin/makemandb/apropos-utils.c:1.14 src/usr.sbin/makemandb/apropos-utils.c:1.15
--- src/usr.sbin/makemandb/apropos-utils.c:1.14	Fri Mar 29 16:46:07 2013
+++ src/usr.sbin/makemandb/apropos-utils.c	Tue Apr  2 13:16:50 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: apropos-utils.c,v 1.14 2013/03/29 20:46:07 christos Exp $	*/
+/*	$NetBSD: apropos-utils.c,v 1.15 2013/04/02 17:16:50 christos Exp $	*/
 /*-
  * Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadh...@gmail.com>
  * All rights reserved.
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: apropos-utils.c,v 1.14 2013/03/29 20:46:07 christos Exp $");
+__RCSID("$NetBSD: apropos-utils.c,v 1.15 2013/04/02 17:16:50 christos Exp $");
 
 #include <sys/queue.h>
 #include <sys/stat.h>
@@ -468,8 +468,8 @@ rank_func(sqlite3_context *pctx, int nva
  *  arpopos-utils.h for the description of individual fields.
  *
  */
-int
-run_query(sqlite3 *db, const char *snippet_args[3], query_args *args)
+static int
+run_query_internal(sqlite3 *db, const char *snippet_args[3], query_args *args)
 {
 	const char *default_snippet_args[3];
 	char *section_clause = NULL;
@@ -719,7 +719,7 @@ callback_html(void *data, const char *se
  *  inline HTML fragments.
  *  After that it delegates the call the actual user supplied callback function.
  */
-int
+static int
 run_query_html(sqlite3 *db, query_args *args)
 {
 	struct orig_callback_data orig_data;
@@ -728,7 +728,7 @@ run_query_html(sqlite3 *db, query_args *
 	const char *snippet_args[] = {"\002", "\003", "..."};
 	args->callback = &callback_html;
 	args->callback_data = (void *) &orig_data;
-	return run_query(db, snippet_args, args);
+	return run_query_internal(db, snippet_args, args);
 }
 
 /*
@@ -879,24 +879,16 @@ callback_term(void *data, const char *se
  *  For this purpose it first calls it's own callback function callback_pager
  *  which then delegates the call to the user supplied callback.
  */
-int
+static int
 run_query_pager(sqlite3 *db, query_args *args)
 {
 	struct orig_callback_data orig_data;
 	orig_data.callback = args->callback;
 	orig_data.data = args->callback_data;
-	const char *snippet_args[3];
-
-	if (args->flags & APROPOS_NOFORMAT) {
-		snippet_args[0] = snippet_args[1] = "";
-	} else {
-		snippet_args[0] = "\002";
-		snippet_args[1] = "\003";
-	}
-	snippet_args[2] = "...";
+	const char *snippet_args[3] = { "\002", "\003", "..." };
 	args->callback = &callback_pager;
 	args->callback_data = (void *) &orig_data;
-	return run_query(db, snippet_args, args);
+	return run_query_internal(db, snippet_args, args);
 }
 
 static void
@@ -944,7 +936,7 @@ term_init(int fd, const char *sa[5])
  *  For this purpose it first calls it's own callback function callback_pager
  *  which then delegates the call to the user supplied callback.
  */
-int
+static int
 run_query_term(sqlite3 *db, query_args *args)
 {
 	struct orig_callback_data orig_data;
@@ -952,17 +944,43 @@ run_query_term(sqlite3 *db, query_args *
 	orig_data.callback = args->callback;
 	orig_data.data = args->callback_data;
 	const char *snippet_args[5];
-	if (args->flags & APROPOS_NOFORMAT) {
-		snippet_args[0] = snippet_args[1] = snippet_args[3] =
-		    snippet_args[4] = "";
-		snippet_args[2] = "...";
-	} else
-		term_init(STDOUT_FILENO, snippet_args);
+
+	term_init(STDOUT_FILENO, snippet_args);
 	ta.smul = snippet_args[3];
 	ta.rmul = snippet_args[4];
 	ta.orig_data = (void *) &orig_data;
 
 	args->callback = &callback_term;
 	args->callback_data = &ta;
-	return run_query(db, snippet_args, args);
+	return run_query_internal(db, snippet_args, args);
+}
+
+static int
+run_query_none(sqlite3 *db, query_args *args)
+{
+	struct orig_callback_data orig_data;
+	orig_data.callback = args->callback;
+	orig_data.data = args->callback_data;
+	const char *snippet_args[3] = { "", "", "..." };
+	args->callback = &callback_pager;
+	args->callback_data = (void *) &orig_data;
+	return run_query_internal(db, snippet_args, args);
+}
+
+int
+run_query(sqlite3 *db, query_format fmt, query_args *args)
+{
+	switch (fmt) {
+	case APROPOS_NONE:
+		return run_query_none(db, args);
+	case APROPOS_HTML:
+		return run_query_html(db, args);
+	case APROPOS_TERM:
+		return run_query_term(db, args);
+	case APROPOS_PAGER:
+		return run_query_pager(db, args);
+	default:
+		warnx("Unknown query format %d", (int)fmt);
+		return -1;
+	}
 }

Index: src/usr.sbin/makemandb/apropos-utils.h
diff -u src/usr.sbin/makemandb/apropos-utils.h:1.8 src/usr.sbin/makemandb/apropos-utils.h:1.9
--- src/usr.sbin/makemandb/apropos-utils.h:1.8	Fri Mar 29 16:07:31 2013
+++ src/usr.sbin/makemandb/apropos-utils.h	Tue Apr  2 13:16:50 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: apropos-utils.h,v 1.8 2013/03/29 20:07:31 christos Exp $	*/
+/*	$NetBSD: apropos-utils.h,v 1.9 2013/04/02 17:16:50 christos Exp $	*/
 /*-
  * Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadh...@gmail.com>
  * All rights reserved.
@@ -80,18 +80,20 @@ typedef struct query_args {
 		const char *, size_t);	// The callback function
 	void *callback_data;	// data to pass to the callback function
 	char **errmsg;		// buffer for storing the error msg
-	int  flags;
-#define APROPOS_NOFORMAT 1
 } query_args;
 
+typedef enum query_format {
+    APROPOS_NONE,
+    APROPOS_PAGER,
+    APROPOS_TERM,
+    APROPOS_HTML
+} query_format;
+
 char *lower(char *);
 void concat(char **, const char *);
 void concat2(char **, const char *, size_t);
 sqlite3 *init_db(int, const char *);
 void close_db(sqlite3 *);
 char *get_dbpath(const char *);
-int run_query(sqlite3 *, const char *[3], query_args *);
-int run_query_html(sqlite3 *, query_args *);
-int run_query_pager(sqlite3 *, query_args *);
-int run_query_term(sqlite3 *, query_args *);
+int run_query(sqlite3 *, query_format, query_args *);
 #endif

Index: src/usr.sbin/makemandb/apropos.1
diff -u src/usr.sbin/makemandb/apropos.1:1.11 src/usr.sbin/makemandb/apropos.1:1.12
--- src/usr.sbin/makemandb/apropos.1:1.11	Fri Mar 29 17:39:09 2013
+++ src/usr.sbin/makemandb/apropos.1	Tue Apr  2 13:16:50 2013
@@ -1,4 +1,4 @@
-.\" $NetBSD: apropos.1,v 1.11 2013/03/29 21:39:09 wiz Exp $
+.\" $NetBSD: apropos.1,v 1.12 2013/04/02 17:16:50 christos Exp $
 .\"
 .\" Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadh...@gmail.com>
 .\" All rights reserved.
@@ -37,7 +37,7 @@
 .Nd search the complete content of all man pages
 .Sh SYNOPSIS
 .Nm
-.Op Fl 123456789Ccilpr
+.Op Fl 123456789CchilPpr
 .Op Fl n Ar results
 .Op Fl S Ar machine
 .Op Fl s Ar section
@@ -72,19 +72,23 @@ Search only within the specified section
 Do not show the context of the match.
 .It Fl c
 Do show the context of the match (default).
+.It Fl h
+Turn on html formatting.
 .It Fl i
-Turn on escape code formatting.
+Turn on terminal escape code formatting.
 .It Fl l
 Legacy mode: Only searches name and name description.
-Does not print context, or escape format the text.
+Does not print context and turns off formatting.
 .It Fl n Ar results
 Output up to the specified number of search results.
 The default limit is infinity.
+.It Fl P
+Turn on pager formatting.
 .It Fl p
-Pipe the results through a pager (defaulting to
+Turn on pager formatting, and pipe the results through a pager (defaulting to
 .Xr more 1 ) .
 .It Fl r
-On tty output don't issue any formatting escape codes.
+Turn off formatting.
 .It Fl S Ar machine
 Limit the search to the pages for the specified machine architecture.
 By default pages for all architectures are shown in the search results.

Index: src/usr.sbin/makemandb/apropos.c
diff -u src/usr.sbin/makemandb/apropos.c:1.15 src/usr.sbin/makemandb/apropos.c:1.16
--- src/usr.sbin/makemandb/apropos.c:1.15	Fri Mar 29 17:39:16 2013
+++ src/usr.sbin/makemandb/apropos.c	Tue Apr  2 13:16:50 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: apropos.c,v 1.15 2013/03/29 21:39:16 wiz Exp $	*/
+/*	$NetBSD: apropos.c,v 1.16 2013/04/02 17:16:50 christos Exp $	*/
 /*-
  * Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadh...@gmail.com>
  * All rights reserved.
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: apropos.c,v 1.15 2013/03/29 21:39:16 wiz Exp $");
+__RCSID("$NetBSD: apropos.c,v 1.16 2013/04/02 17:16:50 christos Exp $");
 
 #include <err.h>
 #include <search.h>
@@ -49,7 +49,7 @@ typedef struct apropos_flags {
 	int nresults;
 	int pager;
 	int no_context;
-	int no_format;
+	query_format format;
 	int legacy;
 	const char *machine;
 } apropos_flags;
@@ -71,7 +71,7 @@ static void
 parseargs(int argc, char **argv, struct apropos_flags *aflags)
 {
 	int ch;
-	while ((ch = getopt(argc, argv, "123456789Cciln:prS:s:")) != -1) {
+	while ((ch = getopt(argc, argv, "123456789Cchiln:PprS:s:")) != -1) {
 		switch (ch) {
 		case '1':
 		case '2':
@@ -90,22 +90,28 @@ parseargs(int argc, char **argv, struct 
 		case 'c':
 			aflags->no_context = 0;
 			break;
+		case 'h':
+			aflags->format = APROPOS_HTML;
+			break;
 		case 'i':
-			aflags->no_format = 0;
+			aflags->format = APROPOS_TERM;
 			break;
 		case 'l':
 			aflags->legacy = 1;
 			aflags->no_context = 1;
-			aflags->no_format = 1;
+			aflags->format = APROPOS_NONE;
 			break;
 		case 'n':
 			aflags->nresults = atoi(optarg);
 			break;
 		case 'p':	// user wants a pager
 			aflags->pager = 1;
+			/*FALLTHROUGH*/
+		case 'P':
+			aflags->format = APROPOS_PAGER;
 			break;
 		case 'r':
-			aflags->no_format = 1;
+			aflags->format = APROPOS_NONE;
 			break;
 		case 'S':
 			aflags->machine = optarg;
@@ -145,7 +151,9 @@ main(int argc, char *argv[])
 	memset(&aflags, 0, sizeof(aflags));
 
 	if (!isatty(STDOUT_FILENO))
-		aflags.no_format = 1;
+		aflags.format = APROPOS_NONE;
+	else
+		aflags.format = APROPOS_TERM;
 
 	if ((str = getenv("APROPOS")) != NULL) {
 		char **ptr = emalloc((strlen(str) + 2) * sizeof(*ptr));
@@ -210,12 +218,16 @@ main(int argc, char *argv[])
 	args.callback = &query_callback;
 	args.callback_data = &cbdata;
 	args.errmsg = &errmsg;
-	args.flags = aflags.no_format ? APROPOS_NOFORMAT : 0;
 
-	if (aflags.pager)
-		rc = run_query_pager(db, &args);
-	else
-		rc = run_query_term(db, &args);
+	if (aflags.format == APROPOS_HTML) {
+		fprintf(cbdata.out, "<html>\n<header>\n<title>apropos results "
+		    "for %s</title></header>\n<body>\n<table cellpadding=\"4\""
+		    "style=\"border: 1px solid #000000; border-collapse:"
+		    "collapse;\" border=\"1\">\n", query);
+	}
+	rc = run_query(db, aflags.format, &args);
+	if (aflags.format == APROPOS_HTML)
+		fprintf(cbdata.out, "</table>\n</body>\n</html>\n");
 
 	free(query);
 	close_db(db);
@@ -252,11 +264,17 @@ query_callback(void *data, const char *s
 	callback_data *cbdata = (callback_data *) data;
 	FILE *out = cbdata->out;
 	cbdata->count++;
-	fprintf(out, cbdata->aflags->legacy ? "%s(%s) - %s\n" :
-	    "%s (%s)\t%s\n", name, section, name_desc);
-
-	if (cbdata->aflags->no_context == 0)
-		fprintf(out, "%s\n\n", snippet);
+	if (cbdata->aflags->format != APROPOS_HTML) {
+	    fprintf(out, cbdata->aflags->legacy ? "%s(%s) - %s\n" :
+		"%s (%s)\t%s\n", name, section, name_desc);
+	    if (cbdata->aflags->no_context == 0)
+		    fprintf(out, "%s\n\n", snippet);
+	} else {
+	    fprintf(out, "<tr><td>%s(%s)</td><td>%s</td></tr>\n", name,
+		section, name_desc);
+	    if (cbdata->aflags->no_context == 0)
+		    fprintf(out, "<tr><td colspan=2>%s</td></tr>\n", snippet);
+	}
 
 	return 0;
 }

Reply via email to