Annotation of mandoc/apropos.c, Revision 1.33
1.33 ! kristaps 1: /* $Id: apropos.c,v 1.32 2012/06/09 14:11:15 kristaps Exp $ */
1.1 kristaps 2: /*
1.31 kristaps 3: * Copyright (c) 2012 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
1.27 kristaps 20: #include <sys/param.h>
1.13 kristaps 21:
1.1 kristaps 22: #include <assert.h>
23: #include <getopt.h>
1.33 ! kristaps 24: #include <stdint.h>
1.1 kristaps 25: #include <stdio.h>
26: #include <stdlib.h>
27: #include <string.h>
1.29 kristaps 28: #include <unistd.h>
1.1 kristaps 29:
1.18 kristaps 30: #include "manpath.h"
1.31 kristaps 31: #include "mansearch.h"
1.1 kristaps 32:
33: int
34: main(int argc, char *argv[])
35: {
1.32 kristaps 36: int ch, whatis;
37: struct mansearch search;
1.31 kristaps 38: size_t i, sz;
39: struct manpage *res;
1.32 kristaps 40: char *conf_file, *defpaths, *auxpaths;
1.15 kristaps 41: struct manpaths paths;
1.31 kristaps 42: char *progname;
43: extern char *optarg;
1.1 kristaps 44: extern int optind;
45:
46: progname = strrchr(argv[0], '/');
47: if (progname == NULL)
48: progname = argv[0];
49: else
50: ++progname;
51:
1.32 kristaps 52: auxpaths = defpaths = conf_file = NULL;
1.15 kristaps 53: memset(&paths, 0, sizeof(struct manpaths));
1.32 kristaps 54: memset(&search, 0, sizeof(struct mansearch));
55: whatis = (0 == strcmp(progname, "whatis"));
1.15 kristaps 56:
1.24 schwarze 57: while (-1 != (ch = getopt(argc, argv, "C:M:m:S:s:")))
1.1 kristaps 58: switch (ch) {
1.24 schwarze 59: case ('C'):
60: conf_file = optarg;
61: break;
1.17 kristaps 62: case ('M'):
63: defpaths = optarg;
64: break;
1.15 kristaps 65: case ('m'):
1.17 kristaps 66: auxpaths = optarg;
1.15 kristaps 67: break;
1.9 kristaps 68: case ('S'):
1.32 kristaps 69: search.arch = optarg;
1.1 kristaps 70: break;
1.9 kristaps 71: case ('s'):
1.32 kristaps 72: search.sec = optarg;
1.1 kristaps 73: break;
74: default:
1.31 kristaps 75: goto usage;
1.1 kristaps 76: }
77:
78: argc -= optind;
79: argv += optind;
80:
1.31 kristaps 81: if (0 == argc)
82: goto usage;
1.15 kristaps 83:
1.32 kristaps 84: search.deftype = whatis ? TYPE_Nm : TYPE_Nm | TYPE_Nd;
85: search.flags = whatis ? MANSEARCH_WHATIS : 0;
86:
1.24 schwarze 87: manpath_parse(&paths, conf_file, defpaths, auxpaths);
1.32 kristaps 88: ch = mansearch(&search, &paths, argc, argv, &res, &sz);
1.18 kristaps 89: manpath_free(&paths);
1.28 kristaps 90:
1.31 kristaps 91: if (0 == ch)
92: goto usage;
1.28 kristaps 93:
1.31 kristaps 94: for (i = 0; i < sz; i++) {
95: printf("%s - %s\n", res[i].file, res[i].desc);
96: free(res[i].desc);
1.28 kristaps 97: }
98:
1.31 kristaps 99: free(res);
100: return(sz ? EXIT_SUCCESS : EXIT_FAILURE);
101: usage:
102: fprintf(stderr, "usage: %s [-C conf] "
103: "[-M paths] "
104: "[-m paths] "
105: "[-S arch] "
106: "[-s section] "
107: "expr ...\n",
108: progname);
1.28 kristaps 109: return(EXIT_FAILURE);
1.1 kristaps 110: }
CVSweb