1 /* $Id: manpage.c,v 1.14 2016/07/09 15:24:19 schwarze Exp $ */
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.
20 #include <sys/types.h>
31 #include "mansearch.h"
33 static void show(const char *, const char *);
36 main(int argc, char *argv[])
41 struct mansearch search;
43 char *conf_file, *defpaths, *auxpaths, *line;
51 term = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO);
53 progname = strrchr(argv[0], '/');
59 auxpaths = defpaths = conf_file = NULL;
60 memset(&conf, 0, sizeof(conf));
61 memset(&search, 0, sizeof(struct mansearch));
63 while (-1 != (ch = getopt(argc, argv, "C:M:m:S:s:")))
91 search.argmode = ARG_EXPR;
93 manconf_parse(&conf, conf_file, defpaths, auxpaths);
94 ch = mansearch(&search, &conf.manpath, argc, argv, &res, &sz);
103 } else if (1 == sz && term) {
106 } else if (NULL == res)
109 for (i = 0; i < sz; i++) {
110 printf("%6zu %s: %s\n",
111 i + 1, res[i].names, res[i].output);
117 for (i = 0; i < sz; i++)
124 printf("Enter a choice [1]: ");
129 if ((len = getline(&line, &linesz, stdin)) != -1) {
130 if ('\n' == line[--len] && len > 0) {
132 if ((i = atoi(line)) < 1 || i > sz)
139 for (i = 0; i < sz; i++)
145 cmd = res[i - 1].form ? "mandoc" : "cat";
146 strlcpy(buf, res[i - 1].file, PATH_MAX);
147 for (i = 0; i < sz; i++)
154 fprintf(stderr, "usage: %s [-C conf] "
165 show(const char *cmd, const char *file)
170 if (-1 == pipe(fds)) {
175 if (-1 == (pid = fork())) {
178 } else if (pid > 0) {
179 dup2(fds[0], STDIN_FILENO);
181 cmd = NULL != getenv("MANPAGER") ?
183 (NULL != getenv("PAGER") ?
184 getenv("PAGER") : "more");
185 execlp(cmd, cmd, (char *)NULL);
190 dup2(fds[1], STDOUT_FILENO);
192 execlp(cmd, cmd, file, (char *)NULL);