Annotation of mandoc/apropos.c, Revision 1.18
1.18 ! kristaps 1: /* $Id: apropos.c,v 1.17 2011/11/20 21:36:00 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>
1.17 kristaps 23: #include <ctype.h>
1.1 kristaps 24: #include <getopt.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"
1.18 ! kristaps 31: #include "manpath.h"
1.15 kristaps 32:
1.8 kristaps 33: static int cmp(const void *, const void *);
1.14 kristaps 34: static void list(struct res *, size_t, void *);
1.1 kristaps 35: static void usage(void);
36:
1.2 kristaps 37: static char *progname;
1.1 kristaps 38:
39: int
40: main(int argc, char *argv[])
41: {
1.18 ! kristaps 42: int ch, rc;
1.15 kristaps 43: struct manpaths paths;
1.14 kristaps 44: size_t terms;
1.1 kristaps 45: struct opts opts;
1.10 kristaps 46: struct expr *e;
1.17 kristaps 47: char *defpaths, *auxpaths;
1.1 kristaps 48: extern int optind;
49: extern char *optarg;
50:
51: progname = strrchr(argv[0], '/');
52: if (progname == NULL)
53: progname = argv[0];
54: else
55: ++progname;
56:
1.15 kristaps 57: memset(&paths, 0, sizeof(struct manpaths));
58: memset(&opts, 0, sizeof(struct opts));
59:
1.17 kristaps 60: auxpaths = defpaths = NULL;
1.15 kristaps 61: e = NULL;
62: rc = 0;
63:
1.17 kristaps 64: while (-1 != (ch = getopt(argc, argv, "M:m:S:s:")))
1.1 kristaps 65: switch (ch) {
1.17 kristaps 66: case ('M'):
67: defpaths = optarg;
68: break;
1.15 kristaps 69: case ('m'):
1.17 kristaps 70: auxpaths = optarg;
1.15 kristaps 71: break;
1.9 kristaps 72: case ('S'):
1.1 kristaps 73: opts.arch = optarg;
74: break;
1.9 kristaps 75: case ('s'):
1.1 kristaps 76: opts.cat = optarg;
77: break;
78: default:
79: usage();
1.15 kristaps 80: goto out;
1.1 kristaps 81: }
82:
83: argc -= optind;
84: argv += optind;
85:
1.15 kristaps 86: if (0 == argc) {
87: rc = 1;
88: goto out;
89: }
90:
1.18 ! kristaps 91: manpath_parse(&paths, defpaths, auxpaths);
1.1 kristaps 92:
1.14 kristaps 93: if (NULL == (e = exprcomp(argc, argv, &terms))) {
1.16 kristaps 94: fprintf(stderr, "%s: Bad expression\n", progname);
1.15 kristaps 95: goto out;
1.10 kristaps 96: }
1.13 kristaps 97:
1.15 kristaps 98: rc = apropos_search
99: (paths.sz, paths.paths,
100: &opts, e, terms, NULL, list);
101:
1.16 kristaps 102: if (0 == rc)
103: fprintf(stderr, "%s: Error reading "
104: "manual database\n", progname);
1.2 kristaps 105:
1.15 kristaps 106: out:
1.18 ! kristaps 107: manpath_free(&paths);
1.10 kristaps 108: exprfree(e);
1.15 kristaps 109:
110: return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
1.1 kristaps 111: }
112:
1.8 kristaps 113: /* ARGSUSED */
1.1 kristaps 114: static void
1.14 kristaps 115: list(struct res *res, size_t sz, void *arg)
1.1 kristaps 116: {
1.8 kristaps 117: int i;
1.1 kristaps 118:
1.14 kristaps 119: qsort(res, sz, sizeof(struct res), cmp);
1.1 kristaps 120:
1.8 kristaps 121: for (i = 0; i < (int)sz; i++)
1.1 kristaps 122: printf("%s(%s%s%s) - %s\n", res[i].title,
123: res[i].cat,
124: *res[i].arch ? "/" : "",
125: *res[i].arch ? res[i].arch : "",
126: res[i].desc);
127: }
128:
1.8 kristaps 129: static int
130: cmp(const void *p1, const void *p2)
131: {
132:
1.14 kristaps 133: return(strcmp(((const struct res *)p1)->title,
134: ((const struct res *)p2)->title));
1.8 kristaps 135: }
136:
1.1 kristaps 137: static void
138: usage(void)
139: {
140:
1.15 kristaps 141: fprintf(stderr, "usage: %s "
1.17 kristaps 142: "[-M dirs] "
1.15 kristaps 143: "[-m dirs] "
144: "[-S arch] "
145: "[-s section] "
1.14 kristaps 146: "expression...\n", progname);
1.1 kristaps 147: }
CVSweb