Improve the process for GNU tools
[minix3.git] / external / bsd / mdocml / dist / apropos.c
blobf227f82db749f07bc607b0746543bc95db22e147
1 /* Id: apropos.c,v 1.36 2013/12/31 03:41:14 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"
34 int
35 main(int argc, char *argv[])
37 int ch, whatis;
38 struct mansearch search;
39 size_t i, sz;
40 struct manpage *res;
41 struct manpaths paths;
42 char *defpaths, *auxpaths;
43 char *conf_file;
44 char *progname;
45 char *outkey;
46 extern char *optarg;
47 extern int optind;
49 progname = strrchr(argv[0], '/');
50 if (progname == NULL)
51 progname = argv[0];
52 else
53 ++progname;
55 whatis = (0 == strncmp(progname, "whatis", 6));
57 memset(&paths, 0, sizeof(struct manpaths));
58 memset(&search, 0, sizeof(struct mansearch));
60 auxpaths = defpaths = NULL;
61 conf_file = NULL;
62 outkey = NULL;
64 while (-1 != (ch = getopt(argc, argv, "C:M:m:O:S:s:")))
65 switch (ch) {
66 case ('C'):
67 conf_file = optarg;
68 break;
69 case ('M'):
70 defpaths = optarg;
71 break;
72 case ('m'):
73 auxpaths = optarg;
74 break;
75 case ('O'):
76 outkey = optarg;
77 break;
78 case ('S'):
79 search.arch = optarg;
80 break;
81 case ('s'):
82 search.sec = optarg;
83 break;
84 default:
85 goto usage;
88 argc -= optind;
89 argv += optind;
91 if (0 == argc)
92 goto usage;
94 search.deftype = whatis ? TYPE_Nm : TYPE_Nm | TYPE_Nd;
95 search.flags = whatis ? MANSEARCH_WHATIS : 0;
97 manpath_parse(&paths, conf_file, defpaths, auxpaths);
98 ch = mansearch(&search, &paths, argc, argv, outkey, &res, &sz);
99 manpath_free(&paths);
101 if (0 == ch)
102 goto usage;
104 for (i = 0; i < sz; i++) {
105 printf("%s - %s\n", res[i].names,
106 NULL == outkey ? res[i].desc :
107 NULL == res[i].output ? "" : res[i].output);
108 free(res[i].file);
109 free(res[i].names);
110 free(res[i].desc);
111 free(res[i].output);
114 free(res);
115 return(sz ? EXIT_SUCCESS : EXIT_FAILURE);
116 usage:
117 fprintf(stderr, "usage: %s [-C file] [-M path] [-m path] "
118 "[-O outkey] "
119 "[-S arch] [-s section]%s ...\n", progname,
120 whatis ? " name" : "\n expression");
121 return(EXIT_FAILURE);