1 /* $Id: manpage.c,v 1.10 2015/02/10 08:05:30 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[])
41 struct mansearch search;
43 char *conf_file, *defpaths, *auxpaths, *cp;
46 struct manpaths paths;
51 term = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO);
53 progname = strrchr(argv[0], '/');
59 auxpaths = defpaths = conf_file = NULL;
60 memset(&paths, 0, sizeof(struct manpaths));
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 manpath_parse(&paths, conf_file, defpaths, auxpaths);
94 ch = mansearch(&search, &paths, argc, argv, &res, &sz);
102 return(EXIT_FAILURE);
103 } else if (1 == sz && term) {
106 } else if (NULL == res)
107 return(EXIT_FAILURE);
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++)
120 return(EXIT_SUCCESS);
124 printf("Enter a choice [1]: ");
127 if (NULL != (cp = fgetln(stdin, &len)))
128 if ('\n' == cp[--len] && len > 0) {
130 if ((i = atoi(cp)) < 1 || i > sz)
135 for (i = 0; i < sz; i++)
138 return(EXIT_SUCCESS);
141 cmd = res[i - 1].form ? "mandoc" : "cat";
142 strlcpy(buf, res[i - 1].file, PATH_MAX);
143 for (i = 0; i < sz; i++)
150 fprintf(stderr, "usage: %s [-C conf] "
157 return(EXIT_FAILURE);
161 show(const char *cmd, const char *file)
166 if (-1 == pipe(fds)) {
171 if (-1 == (pid = fork())) {
174 } else if (pid > 0) {
175 dup2(fds[0], STDIN_FILENO);
177 cmd = NULL != getenv("MANPAGER") ?
179 (NULL != getenv("PAGER") ?
180 getenv("PAGER") : "more");
181 execlp(cmd, cmd, (char *)NULL);
186 dup2(fds[1], STDOUT_FILENO);
188 execlp(cmd, cmd, file, (char *)NULL);