aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2013-12-31 03:41:14 +0000
committerIngo Schwarze <schwarze@openbsd.org>2013-12-31 03:41:14 +0000
commita23935eaa0bc6038f06a3c8b59454d4282f1d1a0 (patch)
treeffc24c2145b821e2be31df4b8b5c37f91f93b149
parent05e81e9e4124ecfe3c1b096c9edebf065f703d8d (diff)
downloadmandoc-a23935eaa0bc6038f06a3c8b59454d4282f1d1a0.tar.gz
mandoc-a23935eaa0bc6038f06a3c8b59454d4282f1d1a0.tar.zst
mandoc-a23935eaa0bc6038f06a3c8b59454d4282f1d1a0.zip
Experimental feature to let apropos(1) show different keys than .Nd.
This really takes us beyond what grep -R /usr/*/man/ can do because now you can search for pages by *one* criterion and then display the contents of *another* macro from those pages, like in $ apropos -O Ox Fa~wchar to get an impression how long wide character handling is available.
-rw-r--r--apropos.c18
-rw-r--r--manpage.c6
-rw-r--r--mansearch.c68
-rw-r--r--mansearch.h5
4 files changed, 85 insertions, 12 deletions
diff --git a/apropos.c b/apropos.c
index f9f71138..c629257e 100644
--- a/apropos.c
+++ b/apropos.c
@@ -1,6 +1,7 @@
-/* $Id: apropos.c,v 1.35 2013/12/27 18:51:25 schwarze Exp $ */
+/* $Id: apropos.c,v 1.36 2013/12/31 03:41:14 schwarze Exp $ */
/*
* Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
+ * Copyright (c) 2013 Ingo Schwarze <schwarze@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -41,6 +42,7 @@ main(int argc, char *argv[])
char *defpaths, *auxpaths;
char *conf_file;
char *progname;
+ char *outkey;
extern char *optarg;
extern int optind;
@@ -57,8 +59,9 @@ main(int argc, char *argv[])
auxpaths = defpaths = NULL;
conf_file = NULL;
+ outkey = NULL;
- while (-1 != (ch = getopt(argc, argv, "C:M:m:S:s:")))
+ while (-1 != (ch = getopt(argc, argv, "C:M:m:O:S:s:")))
switch (ch) {
case ('C'):
conf_file = optarg;
@@ -69,6 +72,9 @@ main(int argc, char *argv[])
case ('m'):
auxpaths = optarg;
break;
+ case ('O'):
+ outkey = optarg;
+ break;
case ('S'):
search.arch = optarg;
break;
@@ -89,23 +95,27 @@ main(int argc, char *argv[])
search.flags = whatis ? MANSEARCH_WHATIS : 0;
manpath_parse(&paths, conf_file, defpaths, auxpaths);
- ch = mansearch(&search, &paths, argc, argv, &res, &sz);
+ ch = mansearch(&search, &paths, argc, argv, outkey, &res, &sz);
manpath_free(&paths);
if (0 == ch)
goto usage;
for (i = 0; i < sz; i++) {
- printf("%s - %s\n", res[i].names, res[i].desc);
+ printf("%s - %s\n", res[i].names,
+ NULL == outkey ? res[i].desc :
+ NULL == res[i].output ? "" : res[i].output);
free(res[i].file);
free(res[i].names);
free(res[i].desc);
+ free(res[i].output);
}
free(res);
return(sz ? EXIT_SUCCESS : EXIT_FAILURE);
usage:
fprintf(stderr, "usage: %s [-C file] [-M path] [-m path] "
+ "[-O outkey] "
"[-S arch] [-s section]%s ...\n", progname,
whatis ? " name" : "\n expression");
return(EXIT_FAILURE);
diff --git a/manpage.c b/manpage.c
index 9c98b21d..99824d18 100644
--- a/manpage.c
+++ b/manpage.c
@@ -1,6 +1,7 @@
-/* $Id: manpage.c,v 1.5 2013/12/27 18:51:25 schwarze Exp $ */
+/* $Id: manpage.c,v 1.6 2013/12/31 03:41:14 schwarze Exp $ */
/*
* Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
+ * Copyright (c) 2013 Ingo Schwarze <schwarze@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -89,7 +90,7 @@ main(int argc, char *argv[])
search.deftype = TYPE_Nm | TYPE_Nd;
manpath_parse(&paths, conf_file, defpaths, auxpaths);
- ch = mansearch(&search, &paths, argc, argv, &res, &sz);
+ ch = mansearch(&search, &paths, argc, argv, NULL, &res, &sz);
manpath_free(&paths);
if (0 == ch)
@@ -109,6 +110,7 @@ main(int argc, char *argv[])
i + 1, res[i].names, res[i].desc);
free(res[i].names);
free(res[i].desc);
+ free(res[i].output);
}
if (0 == term) {
diff --git a/mansearch.c b/mansearch.c
index 57c187b6..e84d784f 100644
--- a/mansearch.c
+++ b/mansearch.c
@@ -1,4 +1,4 @@
-/* $Id: mansearch.c,v 1.11 2013/12/31 02:42:29 schwarze Exp $ */
+/* $Id: mansearch.c,v 1.12 2013/12/31 03:41:14 schwarze Exp $ */
/*
* Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2013 Ingo Schwarze <schwarze@openbsd.org>
@@ -122,6 +122,8 @@ static const struct type types[] = {
};
static char *buildnames(sqlite3 *, sqlite3_stmt *, uint64_t);
+static char *buildoutput(sqlite3 *, sqlite3_stmt *,
+ uint64_t, uint64_t);
static void *hash_alloc(size_t, void *);
static void hash_free(void *, size_t, void *);
static void *hash_halloc(size_t, void *);
@@ -138,18 +140,20 @@ static char *sql_statement(const struct expr *,
int
mansearch(const struct mansearch *search,
- const struct manpaths *paths,
- int argc, char *argv[],
+ const struct manpaths *paths,
+ int argc, char *argv[],
+ const char *outkey,
struct manpage **res, size_t *sz)
{
- int fd, rc, c;
+ int fd, rc, c, ibit;
int64_t id;
+ uint64_t outbit;
char buf[PATH_MAX];
char *sql;
struct manpage *mpage;
struct expr *e, *ep;
sqlite3 *db;
- sqlite3_stmt *s;
+ sqlite3_stmt *s, *s2;
struct match *mp;
struct ohash_info info;
struct ohash htab;
@@ -175,6 +179,16 @@ mansearch(const struct mansearch *search,
if (NULL == (e = exprcomp(search, argc, argv)))
goto out;
+ outbit = 0;
+ if (NULL != outkey) {
+ for (ibit = 0; types[ibit].bits; ibit++) {
+ if (0 == strcasecmp(types[ibit].name, outkey)) {
+ outbit = types[ibit].bits;
+ break;
+ }
+ }
+ }
+
/*
* Save a descriptor to the current working directory.
* Since pathnames in the "paths" variable might be relative,
@@ -291,6 +305,12 @@ mansearch(const struct mansearch *search,
if (SQLITE_OK != c)
fprintf(stderr, "%s\n", sqlite3_errmsg(db));
+ c = sqlite3_prepare_v2(db,
+ "SELECT * FROM keys WHERE pageid=? AND bits & ?",
+ -1, &s2, NULL);
+ if (SQLITE_OK != c)
+ fprintf(stderr, "%s\n", sqlite3_errmsg(db));
+
for (mp = ohash_first(&htab, &idx);
NULL != mp;
mp = ohash_next(&htab, &idx)) {
@@ -308,6 +328,8 @@ mansearch(const struct mansearch *search,
mpage->desc = mp->desc;
mpage->form = mp->form;
mpage->names = buildnames(db, s, mp->id);
+ mpage->output = outbit ?
+ buildoutput(db, s2, mp->id, outbit) : NULL;
free(mp->file);
free(mp);
@@ -315,6 +337,7 @@ mansearch(const struct mansearch *search,
}
sqlite3_finalize(s);
+ sqlite3_finalize(s2);
sqlite3_close(db);
ohash_delete(&htab);
}
@@ -365,6 +388,41 @@ buildnames(sqlite3 *db, sqlite3_stmt *s, uint64_t id)
return(names);
}
+static char *
+buildoutput(sqlite3 *db, sqlite3_stmt *s, uint64_t id, uint64_t outbit)
+{
+ char *output, *newoutput;
+ const char *oldoutput, *sep1, *data;
+ size_t i;
+ int c;
+
+ output = NULL;
+ i = 1;
+ SQL_BIND_INT64(db, s, i, id);
+ SQL_BIND_INT64(db, s, i, outbit);
+ while (SQLITE_ROW == (c = sqlite3_step(s))) {
+ if (NULL == output) {
+ oldoutput = "";
+ sep1 = "";
+ } else {
+ oldoutput = output;
+ sep1 = " # ";
+ }
+ data = sqlite3_column_text(s, 1);
+ if (-1 == asprintf(&newoutput, "%s%s%s",
+ oldoutput, sep1, data)) {
+ perror(0);
+ exit((int)MANDOCLEVEL_SYSERR);
+ }
+ free(output);
+ output = newoutput;
+ }
+ if (SQLITE_DONE != c)
+ fprintf(stderr, "%s\n", sqlite3_errmsg(db));
+ sqlite3_reset(s);
+ return(output);
+}
+
/*
* Implement substring match as an application-defined SQL function.
* Using the SQL LIKE or GLOB operators instead would be a bad idea
diff --git a/mansearch.h b/mansearch.h
index d433e25d..405826f0 100644
--- a/mansearch.h
+++ b/mansearch.h
@@ -1,6 +1,7 @@
-/* $Id: mansearch.h,v 1.5 2013/12/27 18:51:25 schwarze Exp $ */
+/* $Id: mansearch.h,v 1.6 2013/12/31 03:41:14 schwarze Exp $ */
/*
* Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
+ * Copyright (c) 2013 Ingo Schwarze <schwarze@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -64,6 +65,7 @@ struct manpage {
char *file; /* to be prefixed by manpath */
char *names; /* a list of names with sections */
char *desc; /* description of manpage */
+ char *output; /* user-defined additional output */
int form; /* 0 == catpage */
};
@@ -79,6 +81,7 @@ int mansearch(const struct mansearch *cfg, /* options */
const struct manpaths *paths, /* manpaths */
int argc, /* size of argv */
char *argv[], /* search terms */
+ const char *outkey, /* name of additional output key */
struct manpage **res, /* results */
size_t *ressz); /* results returned */