summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--usr.bin/mandoc/apropos.c22
-rw-r--r--usr.bin/mandoc/apropos_db.c47
-rw-r--r--usr.bin/mandoc/apropos_db.h3
-rw-r--r--usr.bin/mandoc/main.c5
4 files changed, 63 insertions, 14 deletions
diff --git a/usr.bin/mandoc/apropos.c b/usr.bin/mandoc/apropos.c
index b1d84a42090..5e309236ca8 100644
--- a/usr.bin/mandoc/apropos.c
+++ b/usr.bin/mandoc/apropos.c
@@ -1,4 +1,4 @@
-/* $Id: apropos.c,v 1.8 2011/11/26 16:41:35 schwarze Exp $ */
+/* $Id: apropos.c,v 1.9 2011/11/28 00:16:38 schwarze Exp $ */
/*
* Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
@@ -34,7 +34,7 @@ static char *progname;
int
apropos(int argc, char *argv[])
{
- int ch, rc;
+ int ch, rc, whatis;
struct manpaths paths;
size_t terms;
struct opts opts;
@@ -49,12 +49,13 @@ apropos(int argc, char *argv[])
else
++progname;
+ whatis = 0 == strncmp(progname, "whatis", 6);
+
memset(&paths, 0, sizeof(struct manpaths));
memset(&opts, 0, sizeof(struct opts));
auxpaths = defpaths = NULL;
e = NULL;
- rc = 0;
while (-1 != (ch = getopt(argc, argv, "M:m:S:s:")))
switch (ch) {
@@ -72,20 +73,23 @@ apropos(int argc, char *argv[])
break;
default:
usage();
- goto out;
+ return(EXIT_FAILURE);
}
argc -= optind;
argv += optind;
- if (0 == argc) {
- rc = 1;
- goto out;
- }
+ if (0 == argc)
+ return(EXIT_SUCCESS);
+
+ rc = 0;
manpath_parse(&paths, defpaths, auxpaths);
- if (NULL == (e = exprcomp(argc, argv, &terms))) {
+ e = whatis ? termcomp(argc, argv, &terms) :
+ exprcomp(argc, argv, &terms);
+
+ if (NULL == e) {
fprintf(stderr, "%s: Bad expression\n", progname);
goto out;
}
diff --git a/usr.bin/mandoc/apropos_db.c b/usr.bin/mandoc/apropos_db.c
index 9cf4cbbd0c0..fe8a4ff283c 100644
--- a/usr.bin/mandoc/apropos_db.c
+++ b/usr.bin/mandoc/apropos_db.c
@@ -1,4 +1,4 @@
-/* $Id: apropos_db.c,v 1.9 2011/11/27 23:11:32 schwarze Exp $ */
+/* $Id: apropos_db.c,v 1.10 2011/11/28 00:16:38 schwarze Exp $ */
/*
* Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
@@ -580,6 +580,49 @@ recfree(struct rec *rec)
free(rec->matches);
}
+/*
+ * Compile a list of straight-up terms.
+ * The arguments are re-written into ~[[:<:]]term[[:>:]], or "term"
+ * surrounded by word boundaries, then pumped through exprterm().
+ * Terms are case-insensitive.
+ * This emulates whatis(1) behaviour.
+ */
+struct expr *
+termcomp(int argc, char *argv[], size_t *tt)
+{
+ char *buf;
+ int pos;
+ struct expr *e, *next;
+ size_t sz;
+
+ buf = NULL;
+ e = NULL;
+ *tt = 0;
+
+ for (pos = argc - 1; pos >= 0; pos--) {
+ sz = strlen(argv[pos]) + 18;
+ buf = mandoc_realloc(buf, sz);
+ strlcpy(buf, "Nm~[[:<:]]", sz);
+ strlcat(buf, argv[pos], sz);
+ strlcat(buf, "[[:>:]]", sz);
+ if (NULL == (next = exprterm(buf, 0))) {
+ free(buf);
+ exprfree(e);
+ return(NULL);
+ }
+ next->next = e;
+ e = next;
+ (*tt)++;
+ }
+
+ free(buf);
+ return(e);
+}
+
+/*
+ * Compile a sequence of logical expressions.
+ * See apropos.1 for a grammar of this sequence.
+ */
struct expr *
exprcomp(int argc, char *argv[], size_t *tt)
{
@@ -730,7 +773,7 @@ exprterm(char *buf, int cs)
e.mask = TYPE_Nm | TYPE_Nd;
if (e.regex) {
- i = REG_EXTENDED | REG_NOSUB | cs ? 0 : REG_ICASE;
+ i = REG_EXTENDED | REG_NOSUB | (cs ? 0 : REG_ICASE);
if (regcomp(&e.re, e.v, i))
return(NULL);
}
diff --git a/usr.bin/mandoc/apropos_db.h b/usr.bin/mandoc/apropos_db.h
index 13372517248..302d8435694 100644
--- a/usr.bin/mandoc/apropos_db.h
+++ b/usr.bin/mandoc/apropos_db.h
@@ -1,4 +1,4 @@
-/* $Id: apropos_db.h,v 1.8 2011/11/27 23:11:32 schwarze Exp $ */
+/* $Id: apropos_db.h,v 1.9 2011/11/28 00:16:38 schwarze Exp $ */
/*
* Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
*
@@ -46,6 +46,7 @@ int apropos_search(int, char **, const struct opts *,
void (*)(struct res *, size_t, void *));
struct expr *exprcomp(int, char *[], size_t *);
void exprfree(struct expr *);
+struct expr *termcomp(int, char *[], size_t *);
__END_DECLS
diff --git a/usr.bin/mandoc/main.c b/usr.bin/mandoc/main.c
index f8d4de1e58c..5c0b0091970 100644
--- a/usr.bin/mandoc/main.c
+++ b/usr.bin/mandoc/main.c
@@ -1,4 +1,4 @@
-/* $Id: main.c,v 1.80 2011/10/09 17:59:56 schwarze Exp $ */
+/* $Id: main.c,v 1.81 2011/11/28 00:16:38 schwarze Exp $ */
/*
* Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
@@ -86,7 +86,8 @@ main(int argc, char *argv[])
else
++progname;
- if (0 == strncmp(progname, "apropos", 7))
+ if (0 == strncmp(progname, "apropos", 7) ||
+ 0 == strncmp(progname, "whatis", 6))
return(apropos(argc, argv));
if (0 == strncmp(progname, "mandocdb", 8))
return(mandocdb(argc, argv));