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