Annotation of mandoc/apropos.c, Revision 1.38
1.38 ! schwarze 1: /* $Id: apropos.c,v 1.37 2014/01/06 03:02:46 schwarze Exp $ */
1.1 kristaps 2: /*
1.31 kristaps 3: * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.36 schwarze 4: * Copyright (c) 2013 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>
1.33 kristaps 25: #include <stdint.h>
1.1 kristaps 26: #include <stdio.h>
27: #include <stdlib.h>
28: #include <string.h>
1.29 kristaps 29: #include <unistd.h>
1.1 kristaps 30:
1.18 kristaps 31: #include "manpath.h"
1.31 kristaps 32: #include "mansearch.h"
1.1 kristaps 33:
34: int
35: main(int argc, char *argv[])
36: {
1.32 kristaps 37: int ch, whatis;
38: struct mansearch search;
1.31 kristaps 39: size_t i, sz;
40: struct manpage *res;
1.15 kristaps 41: struct manpaths paths;
1.34 schwarze 42: char *defpaths, *auxpaths;
43: char *conf_file;
1.31 kristaps 44: char *progname;
1.37 schwarze 45: const char *outkey;
1.31 kristaps 46: extern char *optarg;
1.1 kristaps 47: extern int optind;
48:
49: progname = strrchr(argv[0], '/');
50: if (progname == NULL)
51: progname = argv[0];
52: else
53: ++progname;
54:
1.34 schwarze 55: whatis = (0 == strncmp(progname, "whatis", 6));
56:
1.15 kristaps 57: memset(&paths, 0, sizeof(struct manpaths));
1.32 kristaps 58: memset(&search, 0, sizeof(struct mansearch));
1.34 schwarze 59:
60: auxpaths = defpaths = NULL;
61: conf_file = NULL;
1.37 schwarze 62: outkey = "Nd";
1.15 kristaps 63:
1.36 schwarze 64: while (-1 != (ch = getopt(argc, argv, "C:M:m:O:S:s:")))
1.1 kristaps 65: switch (ch) {
1.24 schwarze 66: case ('C'):
67: conf_file = optarg;
68: break;
1.17 kristaps 69: case ('M'):
70: defpaths = optarg;
71: break;
1.15 kristaps 72: case ('m'):
1.17 kristaps 73: auxpaths = optarg;
1.15 kristaps 74: break;
1.36 schwarze 75: case ('O'):
76: outkey = optarg;
77: break;
1.9 kristaps 78: case ('S'):
1.32 kristaps 79: search.arch = optarg;
1.1 kristaps 80: break;
1.9 kristaps 81: case ('s'):
1.32 kristaps 82: search.sec = optarg;
1.1 kristaps 83: break;
84: default:
1.31 kristaps 85: goto usage;
1.1 kristaps 86: }
87:
88: argc -= optind;
89: argv += optind;
90:
1.31 kristaps 91: if (0 == argc)
92: goto usage;
1.15 kristaps 93:
1.32 kristaps 94: search.deftype = whatis ? TYPE_Nm : TYPE_Nm | TYPE_Nd;
95: search.flags = whatis ? MANSEARCH_WHATIS : 0;
96:
1.24 schwarze 97: manpath_parse(&paths, conf_file, defpaths, auxpaths);
1.38 ! schwarze 98: mansearch_setup(1);
1.36 schwarze 99: ch = mansearch(&search, &paths, argc, argv, outkey, &res, &sz);
1.18 kristaps 100: manpath_free(&paths);
1.28 kristaps 101:
1.31 kristaps 102: if (0 == ch)
103: goto usage;
1.28 kristaps 104:
1.31 kristaps 105: for (i = 0; i < sz; i++) {
1.36 schwarze 106: printf("%s - %s\n", res[i].names,
107: NULL == res[i].output ? "" : res[i].output);
1.35 schwarze 108: free(res[i].file);
109: free(res[i].names);
1.36 schwarze 110: free(res[i].output);
1.28 kristaps 111: }
112:
1.31 kristaps 113: free(res);
1.38 ! schwarze 114: mansearch_setup(0);
1.31 kristaps 115: return(sz ? EXIT_SUCCESS : EXIT_FAILURE);
116: usage:
1.34 schwarze 117: fprintf(stderr, "usage: %s [-C file] [-M path] [-m path] "
1.36 schwarze 118: "[-O outkey] "
1.34 schwarze 119: "[-S arch] [-s section]%s ...\n", progname,
120: whatis ? " name" : "\n expression");
1.28 kristaps 121: return(EXIT_FAILURE);
1.1 kristaps 122: }
CVSweb