Module Name:    src
Committed By:   abhinav
Date:           Mon May  1 05:28:00 UTC 2017

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

Log Message:
Simplify handling of the section arguments in apropos(1).

Earlier, a white space separated string was generated containing all the section
numbers passed through command line arguments. Later on that would have to be
tokenized and processed. Instead of that, use a NULL terminated array of 
strings.

Thanks to christos@ for reviewing and suggesting further improvements.


To generate a diff of this commit:
cvs rdiff -u -r1.36 -r1.37 src/usr.sbin/makemandb/apropos-utils.c
cvs rdiff -u -r1.11 -r1.12 src/usr.sbin/makemandb/apropos-utils.h
cvs rdiff -u -r1.21 -r1.22 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.c
diff -u src/usr.sbin/makemandb/apropos-utils.c:1.36 src/usr.sbin/makemandb/apropos-utils.c:1.37
--- src/usr.sbin/makemandb/apropos-utils.c:1.36	Sun Apr 30 16:56:30 2017
+++ src/usr.sbin/makemandb/apropos-utils.c	Mon May  1 05:28:00 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: apropos-utils.c,v 1.36 2017/04/30 16:56:30 abhinav Exp $	*/
+/*	$NetBSD: apropos-utils.c,v 1.37 2017/05/01 05:28:00 abhinav 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.36 2017/04/30 16:56:30 abhinav Exp $");
+__RCSID("$NetBSD: apropos-utils.c,v 1.37 2017/05/01 05:28:00 abhinav Exp $");
 
 #include <sys/queue.h>
 #include <sys/stat.h>
@@ -530,38 +530,14 @@ generate_search_query(query_args *args, 
 	 *   2. The LIMIT portion will be there if the user has specified
 	 *      a limit using the -n option.
 	 */
-	char *sections_str = args->sec_nums;
-	char *temp;
-	if (sections_str) {
-		while (*sections_str) {
-			size_t len = strcspn(sections_str, " ");
-			char *sec = sections_str;
-			if (sections_str[len] == 0) {
-				sections_str += len;
-			} else {
-				sections_str[len] = 0;
-				sections_str += len + 1;
-			}
-			easprintf(&temp, "\'%s\',", sec);
-
-			if (section_clause) {
-				concat(&section_clause, temp);
-				free(temp);
-			} else {
-				section_clause = temp;
-			}
-		}
-		if (section_clause) {
-			/*
-			 * At least one section requested, add glue for query.
-			 * Before doing that, remove the comma at the end of
-			 * section_clause
-			 */
-			size_t section_clause_len = strlen(section_clause);
-			if (section_clause[section_clause_len - 1] == ',')
-				section_clause[section_clause_len - 1] = 0;
-			temp = section_clause;
-			easprintf(&section_clause, " AND mandb.section IN (%s)", temp);
+	if (args->sections && args->sections[0]) {
+		concat(&section_clause, " AND mandb.section IN (");
+		for (size_t i = 0; args->sections[i]; i++) {
+			char *temp;
+			char c = args->sections[i + 1]? ',': ')';
+			if ((temp = sqlite3_mprintf("%Q%c", args->sections[i], c)) == NULL)
+				goto RETURN;
+			concat(&section_clause, temp);
 			free(temp);
 		}
 	}

Index: src/usr.sbin/makemandb/apropos-utils.h
diff -u src/usr.sbin/makemandb/apropos-utils.h:1.11 src/usr.sbin/makemandb/apropos-utils.h:1.12
--- src/usr.sbin/makemandb/apropos-utils.h:1.11	Wed Apr 13 11:48:29 2016
+++ src/usr.sbin/makemandb/apropos-utils.h	Mon May  1 05:28:00 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: apropos-utils.h,v 1.11 2016/04/13 11:48:29 christos Exp $	*/
+/*	$NetBSD: apropos-utils.h,v 1.12 2017/05/01 05:28:00 abhinav Exp $	*/
 /*-
  * Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadh...@gmail.com>
  * All rights reserved.
@@ -73,7 +73,7 @@ enum man_sec {
 
 typedef struct query_args {
 	const char *search_str;		// user query
-	char *sec_nums;		// Section in which to do the search
+	char **sections;		// Sections in which to do the search
 	int nrec;			// number of records to fetch
 	int offset;		//From which position to start processing the records
 	int legacy;

Index: src/usr.sbin/makemandb/apropos.c
diff -u src/usr.sbin/makemandb/apropos.c:1.21 src/usr.sbin/makemandb/apropos.c:1.22
--- src/usr.sbin/makemandb/apropos.c:1.21	Sun May 22 19:26:04 2016
+++ src/usr.sbin/makemandb/apropos.c	Mon May  1 05:28:00 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: apropos.c,v 1.21 2016/05/22 19:26:04 abhinav Exp $	*/
+/*	$NetBSD: apropos.c,v 1.22 2017/05/01 05:28:00 abhinav 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.21 2016/05/22 19:26:04 abhinav Exp $");
+__RCSID("$NetBSD: apropos.c,v 1.22 2017/05/01 05:28:00 abhinav Exp $");
 
 #include <err.h>
 #include <stdio.h>
@@ -43,7 +43,7 @@ __RCSID("$NetBSD: apropos.c,v 1.21 2016/
 #include "apropos-utils.h"
 
 typedef struct apropos_flags {
-	char *sec_nums;
+	char **sections;
 	int nresults;
 	int pager;
 	int no_context;
@@ -59,22 +59,30 @@ typedef struct callback_data {
 	apropos_flags *aflags;
 } callback_data;
 
-static const unsigned int sections_args_length = 16;
-
 static char *remove_stopwords(const char *);
 static int query_callback(void *, const char * , const char *, const char *,
 	const char *, size_t);
 __dead static void usage(void);
 
 #define _PATH_PAGER	"/usr/bin/more -s"
+#define SECTIONS_ARGS_LENGTH 4;
 
 static void
 parseargs(int argc, char **argv, struct apropos_flags *aflags)
 {
 	int ch;
-	char sec[2] = {0, 0};
+	size_t sections_offset = 0;
+	size_t sections_size = 0;
+	char **sections = NULL;
+	char *section;
 	aflags->manconf = MANCONF;
 
+#define RESIZE_SECTIONS(newsize) \
+	if (sections == NULL || sections_offset > sections_size - 1) { \
+		sections_size += newsize; \
+		sections = erealloc(sections, sections_size * sizeof(*sections)); \
+	}
+
 	while ((ch = getopt(argc, argv, "123456789C:hilMmn:PprS:s:")) != -1) {
 		switch (ch) {
 		case '1':
@@ -86,17 +94,11 @@ parseargs(int argc, char **argv, struct 
 		case '7':
 		case '8':
 		case '9':
-			/*
-			 *Generate a space separated list of all the
-			 * requested sections
-			 */
-			sec[0] = (char) ch ;
-			if (aflags->sec_nums == NULL) {
-				aflags->sec_nums =
-				    emalloc(sections_args_length);
-				memcpy(aflags->sec_nums, sec, 2);
-			} else
-				concat2(&aflags->sec_nums, sec, 1);
+			section = emalloc(2);
+			section[0] = ch;
+			section[1] = 0;
+			RESIZE_SECTIONS(SECTIONS_ARGS_LENGTH)
+			sections[sections_offset++] = section;
 			break;
 		case 'C':
 			aflags->manconf = optarg;
@@ -134,21 +136,19 @@ parseargs(int argc, char **argv, struct 
 			aflags->machine = optarg;
 			break;
 		case 's':
-			if (aflags->sec_nums == NULL) {
-				size_t arglen = strlen(optarg);
-				aflags->sec_nums =
-				    arglen > sections_args_length
-					? emalloc(arglen + 1)
-					: emalloc(sections_args_length);
-				memcpy(aflags->sec_nums, optarg, arglen + 1);
-			} else
-				concat(&aflags->sec_nums, optarg);
+			RESIZE_SECTIONS(SECTIONS_ARGS_LENGTH)
+			sections[sections_offset++] = estrdup(optarg);
 			break;
 		case '?':
 		default:
 			usage();
 		}
 	}
+	if (sections) {
+		RESIZE_SECTIONS(1)
+		sections[sections_offset] = NULL;
+	}
+	aflags->sections = sections;
 }
 
 int
@@ -159,12 +159,13 @@ main(int argc, char *argv[])
 	char *errmsg = NULL;
 	char *str;
 	int rc = 0;
+	size_t i;
 	int s;
 	callback_data cbdata;
 	cbdata.out = stdout;		// the default output stream
 	cbdata.count = 0;
 	apropos_flags aflags;
-	aflags.sec_nums = NULL;
+	aflags.sections = NULL;
 	cbdata.aflags = &aflags;
 	sqlite3 *db;
 	setprogname(argv[0]);
@@ -203,7 +204,6 @@ main(int argc, char *argv[])
 	str = NULL;
 	while (argc--)
 		concat(&str, *argv++);
-	/* Eliminate any stopwords from the query */
 	query = remove_stopwords(lower(str));
 
 	/*
@@ -231,7 +231,7 @@ main(int argc, char *argv[])
 	}
 
 	args.search_str = query;
-	args.sec_nums = aflags.sec_nums;
+	args.sections = aflags.sections;
 	args.legacy = aflags.legacy;
 	args.nrec = aflags.nresults ? aflags.nresults : -1;
 	args.offset = 0;
@@ -251,7 +251,13 @@ main(int argc, char *argv[])
 		fprintf(cbdata.out, "</table>\n</body>\n</html>\n");
 
 	free(query);
-	free(aflags.sec_nums);
+
+	if (aflags.sections) {
+		for(i = 0; aflags.sections[i]; i++)
+			free(aflags.sections[i]);
+		free(aflags.sections);
+	}
+
 	close_db(db);
 	if (errmsg) {
 		warnx("%s", errmsg);

Reply via email to