Annotation of mandoc/apropos.c, Revision 1.16
1.16 ! kristaps 1: /* $Id: apropos.c,v 1.15 2011/11/20 15:43:14 kristaps Exp $ */
1.1 kristaps 2: /*
1.8 kristaps 3: * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.15 kristaps 4: * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
1.13 kristaps 18: #ifdef HAVE_CONFIG_H
19: #include "config.h"
20: #endif
21:
1.1 kristaps 22: #include <assert.h>
23: #include <getopt.h>
24: #include <limits.h>
25: #include <stdio.h>
26: #include <stdlib.h>
27: #include <string.h>
28:
1.11 schwarze 29: #include "apropos_db.h"
1.1 kristaps 30: #include "mandoc.h"
31:
1.15 kristaps 32: /*
33: * List of paths to be searched for manual databases.
34: */
35: struct manpaths {
36: int sz;
37: char **paths;
38: };
39:
1.8 kristaps 40: static int cmp(const void *, const void *);
1.14 kristaps 41: static void list(struct res *, size_t, void *);
1.16 ! kristaps 42: static void manpath_add(struct manpaths *, const char *);
! 43: static void manpath_parse(struct manpaths *, char *);
1.1 kristaps 44: static void usage(void);
45:
1.2 kristaps 46: static char *progname;
1.1 kristaps 47:
48: int
49: main(int argc, char *argv[])
50: {
1.15 kristaps 51: int i, ch, rc;
52: struct manpaths paths;
1.14 kristaps 53: size_t terms;
1.1 kristaps 54: struct opts opts;
1.10 kristaps 55: struct expr *e;
1.1 kristaps 56: extern int optind;
57: extern char *optarg;
58:
59: progname = strrchr(argv[0], '/');
60: if (progname == NULL)
61: progname = argv[0];
62: else
63: ++progname;
64:
1.15 kristaps 65: memset(&paths, 0, sizeof(struct manpaths));
66: memset(&opts, 0, sizeof(struct opts));
67:
68: e = NULL;
69: rc = 0;
70:
71: while (-1 != (ch = getopt(argc, argv, "m:S:s:")))
1.1 kristaps 72: switch (ch) {
1.15 kristaps 73: case ('m'):
1.16 ! kristaps 74: manpath_parse(&paths, optarg);
1.15 kristaps 75: break;
1.9 kristaps 76: case ('S'):
1.1 kristaps 77: opts.arch = optarg;
78: break;
1.9 kristaps 79: case ('s'):
1.1 kristaps 80: opts.cat = optarg;
81: break;
82: default:
83: usage();
1.15 kristaps 84: goto out;
1.1 kristaps 85: }
86:
87: argc -= optind;
88: argv += optind;
89:
1.15 kristaps 90: if (0 == argc) {
91: rc = 1;
92: goto out;
93: }
94:
1.16 ! kristaps 95: /*
! 96: * Let MANPATH override our default paths.
! 97: */
! 98:
! 99: if (NULL != getenv("MANPATH"))
! 100: manpath_add(&paths, getenv("MANPATH"));
! 101: else
! 102: manpath_add(&paths, ".");
1.1 kristaps 103:
1.14 kristaps 104: if (NULL == (e = exprcomp(argc, argv, &terms))) {
1.16 ! kristaps 105: fprintf(stderr, "%s: Bad expression\n", progname);
1.15 kristaps 106: goto out;
1.10 kristaps 107: }
1.13 kristaps 108:
1.15 kristaps 109: rc = apropos_search
110: (paths.sz, paths.paths,
111: &opts, e, terms, NULL, list);
112:
1.16 ! kristaps 113: if (0 == rc)
! 114: fprintf(stderr, "%s: Error reading "
! 115: "manual database\n", progname);
1.2 kristaps 116:
1.15 kristaps 117: out:
118: for (i = 0; i < paths.sz; i++)
119: free(paths.paths[i]);
120:
121: free(paths.paths);
1.10 kristaps 122: exprfree(e);
1.15 kristaps 123:
124: return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
1.1 kristaps 125: }
126:
1.8 kristaps 127: /* ARGSUSED */
1.1 kristaps 128: static void
1.14 kristaps 129: list(struct res *res, size_t sz, void *arg)
1.1 kristaps 130: {
1.8 kristaps 131: int i;
1.1 kristaps 132:
1.14 kristaps 133: qsort(res, sz, sizeof(struct res), cmp);
1.1 kristaps 134:
1.8 kristaps 135: for (i = 0; i < (int)sz; i++)
1.1 kristaps 136: printf("%s(%s%s%s) - %s\n", res[i].title,
137: res[i].cat,
138: *res[i].arch ? "/" : "",
139: *res[i].arch ? res[i].arch : "",
140: res[i].desc);
141: }
142:
1.8 kristaps 143: static int
144: cmp(const void *p1, const void *p2)
145: {
146:
1.14 kristaps 147: return(strcmp(((const struct res *)p1)->title,
148: ((const struct res *)p2)->title));
1.8 kristaps 149: }
150:
1.1 kristaps 151: static void
152: usage(void)
153: {
154:
1.15 kristaps 155: fprintf(stderr, "usage: %s "
156: "[-m dirs] "
157: "[-S arch] "
158: "[-s section] "
1.14 kristaps 159: "expression...\n", progname);
1.15 kristaps 160: }
161:
162: /*
163: * Parse a FULL pathname from a colon-separated list of arrays.
164: */
1.16 ! kristaps 165: static void
1.15 kristaps 166: manpath_parse(struct manpaths *dirs, char *path)
167: {
168: char *dir;
169:
170: for (dir = strtok(path, ":"); dir; dir = strtok(NULL, ":"))
1.16 ! kristaps 171: manpath_add(dirs, dir);
1.15 kristaps 172: }
173:
174: /*
1.16 ! kristaps 175: * Add a directory to the array, ignoring bad directories.
1.15 kristaps 176: * Grow the array one-by-one for simplicity's sake.
177: */
1.16 ! kristaps 178: static void
1.15 kristaps 179: manpath_add(struct manpaths *dirs, const char *dir)
180: {
181: char buf[PATH_MAX];
182: char *cp;
183:
1.16 ! kristaps 184: if (NULL == (cp = realpath(dir, buf)))
! 185: return;
1.15 kristaps 186:
187: dirs->paths = mandoc_realloc
188: (dirs->paths,
189: ((size_t)dirs->sz + 1) * sizeof(char *));
190:
191: dirs->paths[dirs->sz++] = mandoc_strdup(cp);
1.1 kristaps 192: }
CVSweb