1 /* $Id: manpage.c,v 1.13 2015/11/07 17:58:55 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>
32 #include "mansearch.h"
34 static void show(const char *, const char *);
37 main(int argc, char *argv[])
42 struct mansearch search;
44 char *conf_file, *defpaths, *auxpaths, *line;
52 term = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO);
54 progname = strrchr(argv[0], '/');
60 auxpaths = defpaths = conf_file = NULL;
61 memset(&conf, 0, sizeof(conf));
62 memset(&search, 0, sizeof(struct mansearch));
64 while (-1 != (ch = getopt(argc, argv, "C:M:m:S:s:")))
92 search.argmode = ARG_EXPR;
94 manconf_parse(&conf, conf_file, defpaths, auxpaths);
95 ch = mansearch(&search, &conf.manpath, argc, argv, &res, &sz);
104 } else if (1 == sz && term) {
107 } else if (NULL == res)
110 for (i = 0; i < sz; i++) {
111 printf("%6zu %s: %s\n",
112 i + 1, res[i].names, res[i].output);
118 for (i = 0; i < sz; i++)
125 printf("Enter a choice [1]: ");
130 if ((len = getline(&line, &linesz, stdin)) != -1) {
131 if ('\n' == line[--len] && len > 0) {
133 if ((i = atoi(line)) < 1 || i > sz)
140 for (i = 0; i < sz; i++)
146 cmd = res[i - 1].form ? "mandoc" : "cat";
147 strlcpy(buf, res[i - 1].file, PATH_MAX);
148 for (i = 0; i < sz; i++)
155 fprintf(stderr, "usage: %s [-C conf] "
166 show(const char *cmd, const char *file)
171 if (-1 == pipe(fds)) {
176 if (-1 == (pid = fork())) {
179 } else if (pid > 0) {
180 dup2(fds[0], STDIN_FILENO);
182 cmd = NULL != getenv("MANPAGER") ?
184 (NULL != getenv("PAGER") ?
185 getenv("PAGER") : "more");
186 execlp(cmd, cmd, (char *)NULL);
191 dup2(fds[1], STDOUT_FILENO);
193 execlp(cmd, cmd, file, (char *)NULL);