]>
git.cameronkatri.com Git - mandoc.git/blob - manpage.c
1 /* $Id: manpage.c,v 1.3 2012/06/09 17:49:13 kristaps Exp $ */
3 * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/param.h>
31 #include "mansearch.h"
33 static void show(const char *, const char *);
36 main(int argc
, char *argv
[])
40 struct mansearch search
;
42 char *conf_file
, *defpaths
, *auxpaths
, *cp
;
45 struct manpaths paths
;
50 term
= isatty(STDIN_FILENO
) && isatty(STDOUT_FILENO
);
52 progname
= strrchr(argv
[0], '/');
58 auxpaths
= defpaths
= conf_file
= NULL
;
59 memset(&paths
, 0, sizeof(struct manpaths
));
60 memset(&search
, 0, sizeof(struct mansearch
));
62 while (-1 != (ch
= getopt(argc
, argv
, "C:M:m:S:s:")))
89 search
.deftype
= TYPE_Nm
| TYPE_Nd
;
91 manpath_parse(&paths
, conf_file
, defpaths
, auxpaths
);
92 ch
= mansearch(&search
, &paths
, argc
, argv
, &res
, &sz
);
100 return(EXIT_FAILURE
);
101 } else if (1 == sz
&& term
) {
104 } else if (NULL
== res
)
105 return(EXIT_FAILURE
);
107 for (i
= 0; i
< sz
; i
++) {
108 printf("%6zu %s: %s\n",
109 i
+ 1, res
[i
].file
, res
[i
].desc
);
115 return(EXIT_SUCCESS
);
119 printf("Enter a choice [1]: ");
122 if (NULL
!= (cp
= fgetln(stdin
, &len
)))
123 if ('\n' == cp
[--len
] && len
> 0) {
125 if ((i
= atoi(cp
)) < 1 || i
> sz
)
131 return(EXIT_SUCCESS
);
134 cmd
= res
[i
- 1].form
? "mandoc" : "cat";
135 strlcpy(buf
, res
[i
- 1].file
, MAXPATHLEN
);
141 fprintf(stderr
, "usage: %s [-C conf] "
148 return(EXIT_FAILURE
);
152 show(const char *cmd
, const char *file
)
157 if (-1 == pipe(fds
)) {
162 if (-1 == (pid
= fork())) {
165 } else if (pid
> 0) {
166 dup2(fds
[0], STDIN_FILENO
);
168 cmd
= NULL
!= getenv("MANPAGER") ?
170 (NULL
!= getenv("PAGER") ?
171 getenv("PAGER") : "more");
172 execlp(cmd
, cmd
, (char *)NULL
);
177 dup2(fds
[1], STDOUT_FILENO
);
179 execlp(cmd
, cmd
, file
, (char *)NULL
);