Annotation of mandoc/apropos.c, Revision 1.14
1.14 ! kristaps 1: /* $Id: apropos.c,v 1.13 2011/11/14 10:07:06 kristaps Exp $ */
1.1 kristaps 2: /*
1.8 kristaps 3: * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
1.13 kristaps 17: #ifdef HAVE_CONFIG_H
18: #include "config.h"
19: #endif
20:
1.1 kristaps 21: #include <assert.h>
22: #include <getopt.h>
23: #include <limits.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"
30:
1.8 kristaps 31: static int cmp(const void *, const void *);
1.14 ! kristaps 32: static void list(struct res *, size_t, void *);
1.1 kristaps 33: static void usage(void);
34:
1.2 kristaps 35: static char *progname;
1.1 kristaps 36:
37: int
38: main(int argc, char *argv[])
39: {
1.12 schwarze 40: int ch;
1.14 ! kristaps 41: size_t terms;
1.1 kristaps 42: struct opts opts;
1.10 kristaps 43: struct expr *e;
1.1 kristaps 44: extern int optind;
45: extern char *optarg;
46:
47: memset(&opts, 0, sizeof(struct opts));
48:
49: progname = strrchr(argv[0], '/');
50: if (progname == NULL)
51: progname = argv[0];
52: else
53: ++progname;
54:
1.12 schwarze 55: while (-1 != (ch = getopt(argc, argv, "S:s:")))
1.1 kristaps 56: switch (ch) {
1.9 kristaps 57: case ('S'):
1.1 kristaps 58: opts.arch = optarg;
59: break;
1.9 kristaps 60: case ('s'):
1.1 kristaps 61: opts.cat = optarg;
62: break;
63: default:
64: usage();
65: return(EXIT_FAILURE);
66: }
67:
68: argc -= optind;
69: argv += optind;
70:
1.10 kristaps 71: if (0 == argc)
1.8 kristaps 72: return(EXIT_SUCCESS);
1.1 kristaps 73:
1.14 ! kristaps 74: if (NULL == (e = exprcomp(argc, argv, &terms))) {
1.10 kristaps 75: fprintf(stderr, "Bad expression\n");
76: return(EXIT_FAILURE);
77: }
1.13 kristaps 78:
1.2 kristaps 79: /*
80: * Configure databases.
81: * The keyword database is a btree that allows for duplicate
82: * entries.
83: * The index database is a recno.
84: */
85:
1.14 ! kristaps 86: ch = apropos_search(&opts, e, terms, NULL, list);
1.10 kristaps 87: exprfree(e);
1.14 ! kristaps 88: if (0 == ch)
! 89: fprintf(stderr, "%s: Database error\n", progname);
! 90: return(ch ? EXIT_SUCCESS : EXIT_FAILURE);
1.1 kristaps 91: }
92:
1.8 kristaps 93: /* ARGSUSED */
1.1 kristaps 94: static void
1.14 ! kristaps 95: list(struct res *res, size_t sz, void *arg)
1.1 kristaps 96: {
1.8 kristaps 97: int i;
1.1 kristaps 98:
1.14 ! kristaps 99: qsort(res, sz, sizeof(struct res), cmp);
1.1 kristaps 100:
1.8 kristaps 101: for (i = 0; i < (int)sz; i++)
1.1 kristaps 102: printf("%s(%s%s%s) - %s\n", res[i].title,
103: res[i].cat,
104: *res[i].arch ? "/" : "",
105: *res[i].arch ? res[i].arch : "",
106: res[i].desc);
107: }
108:
1.8 kristaps 109: static int
110: cmp(const void *p1, const void *p2)
111: {
112:
1.14 ! kristaps 113: return(strcmp(((const struct res *)p1)->title,
! 114: ((const struct res *)p2)->title));
1.8 kristaps 115: }
116:
1.1 kristaps 117: static void
118: usage(void)
119: {
120:
1.14 ! kristaps 121: fprintf(stderr, "usage: %s [-S arch] [-s section] "
! 122: "expression...\n", progname);
1.1 kristaps 123: }
CVSweb