Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.sbin/makemandb Simplify handling of the section argument...
details: https://anonhg.NetBSD.org/src/rev/9f17f5782e84
branches: trunk
changeset: 353354:9f17f5782e84
user: abhinav <abhinav%NetBSD.org@localhost>
date: Mon May 01 05:28:00 2017 +0000
description:
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.
diffstat:
usr.sbin/makemandb/apropos-utils.c | 44 +++++-------------------
usr.sbin/makemandb/apropos-utils.h | 4 +-
usr.sbin/makemandb/apropos.c | 66 ++++++++++++++++++++-----------------
3 files changed, 48 insertions(+), 66 deletions(-)
diffs (243 lines):
diff -r 3b3c2fec21c4 -r 9f17f5782e84 usr.sbin/makemandb/apropos-utils.c
--- a/usr.sbin/makemandb/apropos-utils.c Sun Apr 30 23:19:11 2017 +0000
+++ b/usr.sbin/makemandb/apropos-utils.c Mon May 01 05:28:00 2017 +0000
@@ -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.upadhyay%gmail.com@localhost>
* 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 @@
* 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(§ion_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(§ion_clause, " AND mandb.section IN (%s)", temp);
+ if (args->sections && args->sections[0]) {
+ concat(§ion_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(§ion_clause, temp);
free(temp);
}
}
diff -r 3b3c2fec21c4 -r 9f17f5782e84 usr.sbin/makemandb/apropos-utils.h
--- a/usr.sbin/makemandb/apropos-utils.h Sun Apr 30 23:19:11 2017 +0000
+++ b/usr.sbin/makemandb/apropos-utils.h Mon May 01 05:28:00 2017 +0000
@@ -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.upadhyay%gmail.com@localhost>
* All rights reserved.
@@ -73,7 +73,7 @@
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;
diff -r 3b3c2fec21c4 -r 9f17f5782e84 usr.sbin/makemandb/apropos.c
--- a/usr.sbin/makemandb/apropos.c Sun Apr 30 23:19:11 2017 +0000
+++ b/usr.sbin/makemandb/apropos.c Mon May 01 05:28:00 2017 +0000
@@ -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.upadhyay%gmail.com@localhost>
* 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 @@
#include "apropos-utils.h"
typedef struct apropos_flags {
- char *sec_nums;
+ char **sections;
int nresults;
int pager;
int no_context;
@@ -59,22 +59,30 @@
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 @@
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 @@
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 @@
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 @@
str = NULL;
while (argc--)
concat(&str, *argv++);
- /* Eliminate any stopwords from the query */
query = remove_stopwords(lower(str));
/*
@@ -231,7 +231,7 @@
}
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 @@
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);
Home |
Main Index |
Thread Index |
Old Index