Module Name:    src
Committed By:   wiz
Date:           Fri May  8 12:48:43 UTC 2009

Modified Files:
        src/usr.bin/apropos: apropos.1 apropos.c

Log Message:
Add support for -S and -s option, which limit the results to a specific
architecture or section, respectively.
Based on a patch for OpenBSD from Ingo Schwarze, modified for NetBSD by me.

Add some constification.

Ok lukem@ (to the non-constified version of the patch).


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/usr.bin/apropos/apropos.1
cvs rdiff -u -r1.29 -r1.30 src/usr.bin/apropos/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.bin/apropos/apropos.1
diff -u src/usr.bin/apropos/apropos.1:1.10 src/usr.bin/apropos/apropos.1:1.11
--- src/usr.bin/apropos/apropos.1:1.10	Thu Aug  7 11:13:07 2003
+++ src/usr.bin/apropos/apropos.1	Fri May  8 12:48:43 2009
@@ -1,4 +1,4 @@
-.\"	$NetBSD: apropos.1,v 1.10 2003/08/07 11:13:07 agc Exp $
+.\"	$NetBSD: apropos.1,v 1.11 2009/05/08 12:48:43 wiz Exp $
 .\"
 .\" Copyright (c) 1989, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"	@(#)apropos.1	8.1 (Berkeley) 6/29/93
 .\"
-.Dd June 29, 1993
+.Dd May 7, 2009
 .Dt APROPOS 1
 .Os
 .Sh NAME
@@ -40,6 +40,8 @@
 .Op Fl C Ar path
 .Op Fl M Ar path
 .Op Fl m Ar path
+.Op Fl S Ar subsection
+.Op Fl s Ar section
 .Ar keyword ...
 .Sh DESCRIPTION
 .Nm
@@ -96,6 +98,12 @@
 option or the
 .Ev MANPATH
 environment variable.
+.It Fl S Ar subsection
+Restrict the search to pages for the specified machine architecture.
+By default, pages for all architectures are shown.
+.It Fl s Ar section
+Restrict the search to the specified section of the manual.
+By default, pages from all sections are shown.
 .El
 .Sh ENVIRONMENT
 .Bl -tag -width MANPATH

Index: src/usr.bin/apropos/apropos.c
diff -u src/usr.bin/apropos/apropos.c:1.29 src/usr.bin/apropos/apropos.c:1.30
--- src/usr.bin/apropos/apropos.c:1.29	Mon Jul 21 14:19:20 2008
+++ src/usr.bin/apropos/apropos.c	Fri May  8 12:48:43 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: apropos.c,v 1.29 2008/07/21 14:19:20 lukem Exp $	*/
+/*	$NetBSD: apropos.c,v 1.30 2009/05/08 12:48:43 wiz Exp $	*/
 
 /*
  * Copyright (c) 1987, 1993, 1994
@@ -40,7 +40,7 @@
 #if 0
 static char sccsid[] = "@(#)apropos.c	8.8 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: apropos.c,v 1.29 2008/07/21 14:19:20 lukem Exp $");
+__RCSID("$NetBSD: apropos.c,v 1.30 2009/05/08 12:48:43 wiz Exp $");
 #endif
 #endif /* not lint */
 
@@ -65,9 +65,9 @@
 
 #define	MAXLINELEN	8192		/* max line handled */
 
-static void apropos(char **, char *, bool);
-static void lowstr(char *, char *);
-static bool match(char *, char *);
+static void apropos(char **, const char *, bool, const char *, const char *);
+static void lowstr(const char *, char *);
+static bool match(const char *, const char *);
 static void usage(void) __dead;
 
 int
@@ -76,12 +76,13 @@
 	ENTRY *ep;
 	TAG *tp;
 	int ch, rv;
-	char *conffile, **p, *p_augment, *p_path;
+	char *conffile, *machine, **p, *p_augment, *p_path, *sflag;
 	glob_t pg;
 
 	conffile = NULL;
 	p_augment = p_path = NULL;
-	while ((ch = getopt(argc, argv, "C:M:m:P:")) != -1) {
+	machine = sflag = NULL;
+	while ((ch = getopt(argc, argv, "C:M:m:P:S:s:")) != -1) {
 		switch (ch) {
 		case 'C':
 			conffile = optarg;
@@ -93,6 +94,14 @@
 		case 'm':
 			p_augment = optarg;
 			break;
+		case 'S':
+			machine = optarg;
+			lowstr(machine, machine);
+			break;
+		case 's':
+			sflag = optarg;
+			lowstr(sflag, sflag);
+			break;
 		case '?':
 		default:
 			usage();
@@ -112,9 +121,9 @@
 		lowstr(*p, *p);
 
 	if (p_augment)
-		apropos(argv, p_augment, true);
+		apropos(argv, p_augment, true, sflag, machine);
 	if (p_path || (p_path = getenv("MANPATH")))
-		apropos(argv, p_path, true);
+		apropos(argv, p_path, true, sflag, machine);
 	else {
 		config(conffile);
 		tp = gettag("_whatdb", 1);
@@ -130,7 +139,8 @@
 			}
 			if (pg.gl_pathc)
 				for (p = pg.gl_pathv; *p; p++)
-					apropos(argv, *p, false);
+					apropos(argv, *p, false, sflag,
+						machine);
 			globfree(&pg);
 		}
 	}
@@ -148,12 +158,20 @@
 }
 
 static void
-apropos(char **argv, char *path, bool buildpath)
+apropos(char **argv, const char *path, bool buildpath,
+	const char *sflag, const char *machine)
 {
-	char *end, *name, **p;
+	char *end, **p;
+	const char *name;
 	char buf[MAXLINELEN + 1];
 	char hold[MAXPATHLEN + 1];
 	char wbuf[MAXLINELEN + 1];
+	size_t slen = 0, mlen = 0;
+
+	if (sflag)
+		slen = strlen(sflag);
+	if (machine)
+		mlen = strlen(machine);
 
 	for (name = path; name; name = end) {	/* through name list */
 		if ((end = strchr(name, ':')) != NULL)
@@ -177,6 +195,19 @@
 				continue;
 			}
 			lowstr(buf, wbuf);
+			if (machine) {
+				if ((strncmp(wbuf, machine, mlen) != 0) ||
+				    strlen(wbuf) <= mlen || wbuf[mlen] != '/')
+					continue;
+			}
+			if (sflag) {
+				char *s = strchr(wbuf, '(');
+
+				if (!s)
+					continue;
+				if (strncmp(s+1, sflag, slen) != 0)
+					continue;
+			}
 			for (p = argv; *p; ++p) {
 				if (match(wbuf, *p)) {
 					(void)printf("%s", buf);
@@ -198,7 +229,7 @@
  *	match anywhere the string appears
  */
 static bool
-match(char *bp, char *str)
+match(const char *bp, const char *str)
 {
 	size_t len;
 	char test;
@@ -219,7 +250,7 @@
  *	convert a string to lower case
  */
 static void
-lowstr(char *from, char *to)
+lowstr(const char *from, char *to)
 {
 	char ch;
 
@@ -238,7 +269,9 @@
 {
 
 	(void)fprintf(stderr,
-	    "usage: %s [-C file] [-M path] [-m path] keyword ...\n",
+	    "usage: %s [-C file] [-M path] [-m path] "
+	    "[-S subsection] [-s section]\n"
+	    "       keyword ...\n",
 	    getprogname());
 	exit(1);
 }

Reply via email to