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