1 /* $Id: manpage.c,v 1.7 2014/01/06 03:02:46 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.
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:")))
90 search
.deftype
= TYPE_Nm
| TYPE_Nd
;
92 manpath_parse(&paths
, conf_file
, defpaths
, auxpaths
);
93 ch
= mansearch(&search
, &paths
, argc
, argv
, "Nd", &res
, &sz
);
101 return(EXIT_FAILURE
);
102 } else if (1 == sz
&& term
) {
105 } else if (NULL
== res
)
106 return(EXIT_FAILURE
);
108 for (i
= 0; i
< sz
; i
++) {
109 printf("%6zu %s: %s\n",
110 i
+ 1, res
[i
].names
, res
[i
].output
);
116 for (i
= 0; i
< sz
; i
++)
119 return(EXIT_SUCCESS
);
123 printf("Enter a choice [1]: ");
126 if (NULL
!= (cp
= fgetln(stdin
, &len
)))
127 if ('\n' == cp
[--len
] && len
> 0) {
129 if ((i
= atoi(cp
)) < 1 || i
> sz
)
134 for (i
= 0; i
< sz
; i
++)
137 return(EXIT_SUCCESS
);
140 cmd
= res
[i
- 1].form
? "mandoc" : "cat";
141 strlcpy(buf
, res
[i
- 1].file
, PATH_MAX
);
142 for (i
= 0; i
< sz
; i
++)
149 fprintf(stderr
, "usage: %s [-C conf] "
156 return(EXIT_FAILURE
);
160 show(const char *cmd
, const char *file
)
165 if (-1 == pipe(fds
)) {
170 if (-1 == (pid
= fork())) {
173 } else if (pid
> 0) {
174 dup2(fds
[0], STDIN_FILENO
);
176 cmd
= NULL
!= getenv("MANPAGER") ?
178 (NULL
!= getenv("PAGER") ?
179 getenv("PAGER") : "more");
180 execlp(cmd
, cmd
, (char *)NULL
);
185 dup2(fds
[1], STDOUT_FILENO
);
187 execlp(cmd
, cmd
, file
, (char *)NULL
);