]> git.cameronkatri.com Git - mandoc.git/blob - apropos.c
Adjust -Tman SYNOPSIS .Nm indentation using .HP; requested by millert@.
[mandoc.git] / apropos.c
1 /* $Id: apropos.c,v 1.33 2012/06/09 17:49:13 kristaps Exp $ */
2 /*
3 * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
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 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 #include <sys/param.h>
21
22 #include <assert.h>
23 #include <getopt.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include "manpath.h"
31 #include "mansearch.h"
32
33 int
34 main(int argc, char *argv[])
35 {
36 int ch, whatis;
37 struct mansearch search;
38 size_t i, sz;
39 struct manpage *res;
40 char *conf_file, *defpaths, *auxpaths;
41 struct manpaths paths;
42 char *progname;
43 extern char *optarg;
44 extern int optind;
45
46 progname = strrchr(argv[0], '/');
47 if (progname == NULL)
48 progname = argv[0];
49 else
50 ++progname;
51
52 auxpaths = defpaths = conf_file = NULL;
53 memset(&paths, 0, sizeof(struct manpaths));
54 memset(&search, 0, sizeof(struct mansearch));
55 whatis = (0 == strcmp(progname, "whatis"));
56
57 while (-1 != (ch = getopt(argc, argv, "C:M:m:S:s:")))
58 switch (ch) {
59 case ('C'):
60 conf_file = optarg;
61 break;
62 case ('M'):
63 defpaths = optarg;
64 break;
65 case ('m'):
66 auxpaths = optarg;
67 break;
68 case ('S'):
69 search.arch = optarg;
70 break;
71 case ('s'):
72 search.sec = optarg;
73 break;
74 default:
75 goto usage;
76 }
77
78 argc -= optind;
79 argv += optind;
80
81 if (0 == argc)
82 goto usage;
83
84 search.deftype = whatis ? TYPE_Nm : TYPE_Nm | TYPE_Nd;
85 search.flags = whatis ? MANSEARCH_WHATIS : 0;
86
87 manpath_parse(&paths, conf_file, defpaths, auxpaths);
88 ch = mansearch(&search, &paths, argc, argv, &res, &sz);
89 manpath_free(&paths);
90
91 if (0 == ch)
92 goto usage;
93
94 for (i = 0; i < sz; i++) {
95 printf("%s - %s\n", res[i].file, res[i].desc);
96 free(res[i].desc);
97 }
98
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);
109 return(EXIT_FAILURE);
110 }