kmalloc: Avoid code duplication.
[dragonfly.git] / contrib / mdocml / apropos.c
blob80b6bc6d036e965fa85926ceb1303c3360e6b1c6
1 /* $Id: apropos.c,v 1.39 2014/04/20 16:46:04 schwarze Exp $ */
2 /*
3 * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21 #include <sys/param.h>
23 #include <assert.h>
24 #include <getopt.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include "manpath.h"
32 #include "mansearch.h"
35 int
36 main(int argc, char *argv[])
38 int ch, whatis;
39 struct mansearch search;
40 size_t i, sz;
41 struct manpage *res;
42 struct manpaths paths;
43 char *defpaths, *auxpaths;
44 char *conf_file;
45 char *progname;
46 const char *outkey;
47 extern char *optarg;
48 extern int optind;
50 progname = strrchr(argv[0], '/');
51 if (progname == NULL)
52 progname = argv[0];
53 else
54 ++progname;
56 whatis = (0 == strncmp(progname, "whatis", 6));
58 memset(&paths, 0, sizeof(struct manpaths));
59 memset(&search, 0, sizeof(struct mansearch));
61 auxpaths = defpaths = NULL;
62 conf_file = NULL;
63 outkey = "Nd";
65 while (-1 != (ch = getopt(argc, argv, "C:M:m:O:S:s:")))
66 switch (ch) {
67 case 'C':
68 conf_file = optarg;
69 break;
70 case 'M':
71 defpaths = optarg;
72 break;
73 case 'm':
74 auxpaths = optarg;
75 break;
76 case 'O':
77 outkey = optarg;
78 break;
79 case 'S':
80 search.arch = optarg;
81 break;
82 case 's':
83 search.sec = optarg;
84 break;
85 default:
86 goto usage;
89 argc -= optind;
90 argv += optind;
92 if (0 == argc)
93 goto usage;
95 search.deftype = whatis ? TYPE_Nm : TYPE_Nm | TYPE_Nd;
96 search.flags = whatis ? MANSEARCH_WHATIS : 0;
98 manpath_parse(&paths, conf_file, defpaths, auxpaths);
99 mansearch_setup(1);
100 ch = mansearch(&search, &paths, argc, argv, outkey, &res, &sz);
101 manpath_free(&paths);
103 if (0 == ch)
104 goto usage;
106 for (i = 0; i < sz; i++) {
107 printf("%s - %s\n", res[i].names,
108 NULL == res[i].output ? "" : res[i].output);
109 free(res[i].file);
110 free(res[i].names);
111 free(res[i].output);
114 free(res);
115 mansearch_setup(0);
116 return(sz ? EXIT_SUCCESS : EXIT_FAILURE);
117 usage:
118 fprintf(stderr, "usage: %s [-C file] [-M path] [-m path] "
119 "[-O outkey] "
120 "[-S arch] [-s section]%s ...\n", progname,
121 whatis ? " name" : "\n expression");
122 return(EXIT_FAILURE);