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