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