man.c revision 1.23 1 1.23 jdolecek /* $NetBSD: man.c,v 1.23 2000/05/27 21:33:26 jdolecek Exp $ */
2 1.7 tls
3 1.1 cgd /*
4 1.7 tls * Copyright (c) 1987, 1993, 1994, 1995
5 1.1 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.9 mikel #include <sys/cdefs.h>
37 1.9 mikel
38 1.1 cgd #ifndef lint
39 1.9 mikel __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994, 1995\n\
40 1.9 mikel The Regents of the University of California. All rights reserved.\n");
41 1.1 cgd #endif /* not lint */
42 1.1 cgd
43 1.1 cgd #ifndef lint
44 1.7 tls #if 0
45 1.7 tls static char sccsid[] = "@(#)man.c 8.17 (Berkeley) 1/31/95";
46 1.7 tls #else
47 1.23 jdolecek __RCSID("$NetBSD: man.c,v 1.23 2000/05/27 21:33:26 jdolecek Exp $");
48 1.7 tls #endif
49 1.1 cgd #endif /* not lint */
50 1.1 cgd
51 1.1 cgd #include <sys/param.h>
52 1.1 cgd #include <sys/queue.h>
53 1.22 tsutsui #include <sys/utsname.h>
54 1.1 cgd
55 1.1 cgd #include <ctype.h>
56 1.1 cgd #include <err.h>
57 1.1 cgd #include <errno.h>
58 1.1 cgd #include <fcntl.h>
59 1.1 cgd #include <fnmatch.h>
60 1.1 cgd #include <glob.h>
61 1.1 cgd #include <signal.h>
62 1.1 cgd #include <stdio.h>
63 1.1 cgd #include <stdlib.h>
64 1.1 cgd #include <string.h>
65 1.1 cgd #include <unistd.h>
66 1.1 cgd
67 1.1 cgd #include "config.h"
68 1.1 cgd #include "pathnames.h"
69 1.1 cgd
70 1.1 cgd int f_all, f_where;
71 1.1 cgd
72 1.9 mikel int main __P((int, char **));
73 1.1 cgd static void build_page __P((char *, char **));
74 1.1 cgd static void cat __P((char *));
75 1.23 jdolecek static const char *check_pager __P((const char *));
76 1.1 cgd static int cleanup __P((void));
77 1.1 cgd static void how __P((char *));
78 1.1 cgd static void jump __P((char **, char *, char *));
79 1.23 jdolecek static int manual __P((char *, TAG *, glob_t *, const char *));
80 1.1 cgd static void onsig __P((int));
81 1.1 cgd static void usage __P((void));
82 1.1 cgd
83 1.1 cgd int
84 1.1 cgd main(argc, argv)
85 1.1 cgd int argc;
86 1.1 cgd char *argv[];
87 1.1 cgd {
88 1.23 jdolecek TAG *defp, *section, *newpathp, *subp;
89 1.23 jdolecek ENTRY *e_defp, *e_subp;
90 1.1 cgd glob_t pg;
91 1.1 cgd size_t len;
92 1.23 jdolecek int ch, f_cat, f_how, found, abs_section;
93 1.23 jdolecek char **ap, *cmd, *p, *p_add, *p_path;
94 1.23 jdolecek const char *machine, *pager, *conffile, *pathsearch;
95 1.23 jdolecek char buf[MAXPATHLEN * 2];
96 1.1 cgd
97 1.9 mikel #ifdef __GNUC__
98 1.9 mikel pager = NULL; /* XXX gcc -Wuninitialized */
99 1.9 mikel #endif
100 1.9 mikel
101 1.1 cgd f_cat = f_how = 0;
102 1.1 cgd conffile = p_add = p_path = NULL;
103 1.23 jdolecek pathsearch = NULL;
104 1.23 jdolecek while ((ch = getopt(argc, argv, "-aC:cfhkM:m:P:S:w")) != -1)
105 1.1 cgd switch (ch) {
106 1.1 cgd case 'a':
107 1.1 cgd f_all = 1;
108 1.1 cgd break;
109 1.1 cgd case 'C':
110 1.1 cgd conffile = optarg;
111 1.1 cgd break;
112 1.1 cgd case 'c':
113 1.1 cgd case '-': /* Deprecated. */
114 1.1 cgd f_cat = 1;
115 1.1 cgd break;
116 1.1 cgd case 'h':
117 1.1 cgd f_how = 1;
118 1.1 cgd break;
119 1.1 cgd case 'm':
120 1.1 cgd p_add = optarg;
121 1.1 cgd break;
122 1.1 cgd case 'M':
123 1.1 cgd case 'P': /* Backward compatibility. */
124 1.23 jdolecek p_path = strdup(optarg);
125 1.1 cgd break;
126 1.1 cgd /*
127 1.1 cgd * The -f and -k options are backward compatible,
128 1.1 cgd * undocumented ways of calling whatis(1) and apropos(1).
129 1.1 cgd */
130 1.1 cgd case 'f':
131 1.1 cgd jump(argv, "-f", "whatis");
132 1.1 cgd /* NOTREACHED */
133 1.1 cgd case 'k':
134 1.1 cgd jump(argv, "-k", "apropos");
135 1.1 cgd /* NOTREACHED */
136 1.23 jdolecek case 'S':
137 1.23 jdolecek pathsearch = optarg;
138 1.23 jdolecek break;
139 1.1 cgd case 'w':
140 1.1 cgd f_all = f_where = 1;
141 1.1 cgd break;
142 1.1 cgd case '?':
143 1.1 cgd default:
144 1.1 cgd usage();
145 1.1 cgd }
146 1.1 cgd argc -= optind;
147 1.1 cgd argv += optind;
148 1.1 cgd
149 1.23 jdolecek if (!argc)
150 1.1 cgd usage();
151 1.1 cgd
152 1.13 ross if (!f_cat && !f_how && !f_where) {
153 1.16 kleink if (!isatty(STDOUT_FILENO)) {
154 1.1 cgd f_cat = 1;
155 1.16 kleink } else {
156 1.16 kleink if ((pager = getenv("PAGER")) != NULL &&
157 1.16 kleink pager[0] != '\0')
158 1.13 ross pager = check_pager(pager);
159 1.16 kleink else
160 1.16 kleink pager = _PATH_PAGER;
161 1.13 ross }
162 1.13 ross }
163 1.23 jdolecek
164 1.1 cgd /* Read the configuration file. */
165 1.1 cgd config(conffile);
166 1.1 cgd
167 1.1 cgd /* Get the machine type. */
168 1.22 tsutsui if ((machine = getenv("MACHINE")) == NULL) {
169 1.22 tsutsui struct utsname utsname;
170 1.22 tsutsui
171 1.22 tsutsui if (uname(&utsname) == -1) {
172 1.22 tsutsui perror("uname");
173 1.22 tsutsui exit(1);
174 1.22 tsutsui }
175 1.22 tsutsui machine = utsname.machine;
176 1.22 tsutsui }
177 1.1 cgd
178 1.23 jdolecek /* create an empty _default list if the config file didn't have one */
179 1.1 cgd if ((defp = getlist("_default")) == NULL)
180 1.1 cgd defp = addlist("_default");
181 1.1 cgd
182 1.23 jdolecek /* if -M wasn't specified, check for MANPATH */
183 1.23 jdolecek if (p_path == NULL)
184 1.23 jdolecek p_path = getenv("MANPATH");
185 1.23 jdolecek
186 1.1 cgd /*
187 1.23 jdolecek * get section. abs_section will be non-zero iff the user
188 1.23 jdolecek * specified a section and it had absolute (rather than
189 1.23 jdolecek * relative) paths in the man.conf file.
190 1.1 cgd */
191 1.23 jdolecek if (argc > 1 && (section = getlist(*argv)) != NULL) {
192 1.23 jdolecek argv++;
193 1.23 jdolecek argc--;
194 1.23 jdolecek abs_section = (TAILQ_FIRST(§ion->list) != NULL &&
195 1.23 jdolecek *(TAILQ_FIRST(§ion->list)->s) == '/');
196 1.23 jdolecek } else {
197 1.23 jdolecek section = NULL;
198 1.23 jdolecek abs_section = 0;
199 1.1 cgd }
200 1.1 cgd
201 1.23 jdolecek /* get subdir list */
202 1.23 jdolecek subp = getlist("_subdir");
203 1.23 jdolecek if (!subp)
204 1.23 jdolecek subp = addlist("_subdir");
205 1.23 jdolecek
206 1.1 cgd /*
207 1.23 jdolecek * now that we have all the inputs we must generate a search path.
208 1.1 cgd */
209 1.23 jdolecek
210 1.23 jdolecek /*
211 1.23 jdolecek * 1: If user specified a section and it has absolute paths
212 1.23 jdolecek * in the config file, then that overrides _default, MANPATH and
213 1.23 jdolecek * path passed via -M.
214 1.23 jdolecek */
215 1.23 jdolecek if (abs_section) {
216 1.23 jdolecek p_path = NULL; /* zap -M/MANPATH */
217 1.23 jdolecek defp = section; /* zap _default */
218 1.23 jdolecek section = NULL; /* promoted to defp */
219 1.1 cgd }
220 1.1 cgd
221 1.23 jdolecek
222 1.23 jdolecek /*
223 1.23 jdolecek * 2: Section can be non-null only if a section was specified
224 1.23 jdolecek * and the config file has relative paths - the section list
225 1.23 jdolecek * overrides _subdir in this case.
226 1.23 jdolecek */
227 1.23 jdolecek if (section)
228 1.23 jdolecek subp = section;
229 1.23 jdolecek
230 1.23 jdolecek
231 1.1 cgd /*
232 1.23 jdolecek * 3: now we either have text string path (p_path) or a tag
233 1.23 jdolecek * based path (defp). we need to append subp and machine
234 1.23 jdolecek * to each element in the path.
235 1.23 jdolecek *
236 1.23 jdolecek * for backward compat, we do not append subp if abs_section
237 1.23 jdolecek * and the path does not end in "/".
238 1.1 cgd */
239 1.23 jdolecek newpathp = addlist("_new_path");
240 1.23 jdolecek if (p_path) {
241 1.23 jdolecek /* use p_path */
242 1.23 jdolecek for (; (p = strtok(p_path, ":")) != NULL; p_path = NULL) {
243 1.23 jdolecek for ( e_subp = TAILQ_FIRST(&subp->list) ;
244 1.23 jdolecek e_subp != NULL ;
245 1.23 jdolecek e_subp = TAILQ_NEXT(e_subp, q)) {
246 1.23 jdolecek snprintf(buf, sizeof(buf), "%s/%s{/%s,}",
247 1.23 jdolecek p, e_subp->s, machine);
248 1.23 jdolecek addentry(newpathp, buf, 0);
249 1.1 cgd }
250 1.1 cgd }
251 1.23 jdolecek } else {
252 1.23 jdolecek /* use defp rather than p_path */
253 1.23 jdolecek for (e_defp = TAILQ_FIRST(&defp->list) ;
254 1.23 jdolecek e_defp != NULL ;
255 1.23 jdolecek e_defp = TAILQ_NEXT(e_defp, q)) {
256 1.23 jdolecek
257 1.23 jdolecek /* handle trailing "/" magic here ... */
258 1.23 jdolecek if (abs_section &&
259 1.23 jdolecek e_defp->s[strlen(e_defp->s) - 1] != '/') {
260 1.1 cgd
261 1.1 cgd (void)snprintf(buf, sizeof(buf),
262 1.23 jdolecek "%s{/%s,}", e_defp->s, machine);
263 1.23 jdolecek addentry(newpathp, buf, 0);
264 1.1 cgd continue;
265 1.1 cgd }
266 1.23 jdolecek
267 1.23 jdolecek for ( e_subp = TAILQ_FIRST(&subp->list) ;
268 1.23 jdolecek e_subp != NULL ;
269 1.23 jdolecek e_subp = TAILQ_NEXT(e_subp, q)) {
270 1.23 jdolecek snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
271 1.23 jdolecek e_defp->s, (abs_section) ? "" : "/",
272 1.23 jdolecek e_subp->s, machine);
273 1.23 jdolecek addentry(newpathp, buf, 0);
274 1.23 jdolecek }
275 1.23 jdolecek }
276 1.23 jdolecek } /* using defp ... */
277 1.23 jdolecek
278 1.23 jdolecek /* now replace the current path with the new one */
279 1.23 jdolecek defp = newpathp;
280 1.23 jdolecek
281 1.23 jdolecek /*
282 1.23 jdolecek * 4: prepend the "-m" path, if specified. we always add
283 1.23 jdolecek * subp and machine to this part of the path.
284 1.23 jdolecek */
285 1.23 jdolecek
286 1.23 jdolecek if (p_add) {
287 1.23 jdolecek for (p = strtok(p_add, ":") ; p ; p = strtok(NULL, ":")) {
288 1.23 jdolecek for ( e_subp = TAILQ_FIRST(&subp->list) ;
289 1.23 jdolecek e_subp != NULL ;
290 1.23 jdolecek e_subp = TAILQ_NEXT(e_subp, q)) {
291 1.23 jdolecek snprintf(buf, sizeof(buf), "%s/%s{/%s,}",
292 1.23 jdolecek p, e_subp->s, machine);
293 1.23 jdolecek addentry(newpathp, buf, 1);
294 1.1 cgd }
295 1.1 cgd }
296 1.1 cgd }
297 1.1 cgd
298 1.23 jdolecek
299 1.1 cgd /*
300 1.1 cgd * 5: Search for the files. Set up an interrupt handler, so the
301 1.1 cgd * temporary files go away.
302 1.1 cgd */
303 1.1 cgd (void)signal(SIGINT, onsig);
304 1.1 cgd (void)signal(SIGHUP, onsig);
305 1.17 itohy (void)signal(SIGPIPE, onsig);
306 1.1 cgd
307 1.1 cgd memset(&pg, 0, sizeof(pg));
308 1.1 cgd for (found = 0; *argv; ++argv)
309 1.23 jdolecek if (manual(*argv, defp, &pg, pathsearch))
310 1.1 cgd found = 1;
311 1.1 cgd
312 1.1 cgd /* 6: If nothing found, we're done. */
313 1.1 cgd if (!found) {
314 1.1 cgd (void)cleanup();
315 1.1 cgd exit (1);
316 1.1 cgd }
317 1.1 cgd
318 1.1 cgd /* 7: If it's simple, display it fast. */
319 1.1 cgd if (f_cat) {
320 1.1 cgd for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
321 1.1 cgd if (**ap == '\0')
322 1.1 cgd continue;
323 1.1 cgd cat(*ap);
324 1.1 cgd }
325 1.1 cgd exit (cleanup());
326 1.1 cgd }
327 1.1 cgd if (f_how) {
328 1.1 cgd for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
329 1.1 cgd if (**ap == '\0')
330 1.1 cgd continue;
331 1.1 cgd how(*ap);
332 1.1 cgd }
333 1.1 cgd exit(cleanup());
334 1.1 cgd }
335 1.1 cgd if (f_where) {
336 1.1 cgd for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
337 1.1 cgd if (**ap == '\0')
338 1.1 cgd continue;
339 1.1 cgd (void)printf("%s\n", *ap);
340 1.1 cgd }
341 1.1 cgd exit(cleanup());
342 1.1 cgd }
343 1.1 cgd
344 1.1 cgd /*
345 1.1 cgd * 8: We display things in a single command; build a list of things
346 1.1 cgd * to display.
347 1.1 cgd */
348 1.1 cgd for (ap = pg.gl_pathv, len = strlen(pager) + 1; *ap != NULL; ++ap) {
349 1.1 cgd if (**ap == '\0')
350 1.1 cgd continue;
351 1.1 cgd len += strlen(*ap) + 1;
352 1.1 cgd }
353 1.1 cgd if ((cmd = malloc(len)) == NULL) {
354 1.9 mikel warn("malloc");
355 1.1 cgd (void)cleanup();
356 1.1 cgd exit(1);
357 1.1 cgd }
358 1.1 cgd p = cmd;
359 1.1 cgd len = strlen(pager);
360 1.1 cgd memmove(p, pager, len);
361 1.1 cgd p += len;
362 1.1 cgd *p++ = ' ';
363 1.1 cgd for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
364 1.1 cgd if (**ap == '\0')
365 1.1 cgd continue;
366 1.1 cgd len = strlen(*ap);
367 1.1 cgd memmove(p, *ap, len);
368 1.1 cgd p += len;
369 1.1 cgd *p++ = ' ';
370 1.1 cgd }
371 1.14 wsanchez *--p = '\0';
372 1.1 cgd
373 1.1 cgd /* Use system(3) in case someone's pager is "pager arg1 arg2". */
374 1.1 cgd (void)system(cmd);
375 1.1 cgd
376 1.1 cgd exit(cleanup());
377 1.1 cgd }
378 1.1 cgd
379 1.1 cgd /*
380 1.1 cgd * manual --
381 1.1 cgd * Search the manuals for the pages.
382 1.1 cgd */
383 1.1 cgd static int
384 1.23 jdolecek manual(page, tag, pg, pathsearch)
385 1.1 cgd char *page;
386 1.1 cgd TAG *tag;
387 1.1 cgd glob_t *pg;
388 1.23 jdolecek const char *pathsearch;
389 1.1 cgd {
390 1.1 cgd ENTRY *ep, *e_sufp, *e_tag;
391 1.1 cgd TAG *missp, *sufp;
392 1.12 kleink int anyfound, cnt, error, found;
393 1.8 mikel char *p, buf[MAXPATHLEN];
394 1.1 cgd
395 1.1 cgd anyfound = 0;
396 1.1 cgd buf[0] = '*';
397 1.1 cgd
398 1.1 cgd /* For each element in the list... */
399 1.1 cgd e_tag = tag == NULL ? NULL : tag->list.tqh_first;
400 1.1 cgd for (; e_tag != NULL; e_tag = e_tag->q.tqe_next) {
401 1.1 cgd (void)snprintf(buf, sizeof(buf), "%s/%s.*", e_tag->s, page);
402 1.12 kleink if ((error = glob(buf,
403 1.12 kleink GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT, NULL, pg)) != 0) {
404 1.12 kleink if (error == GLOB_NOMATCH)
405 1.12 kleink continue;
406 1.12 kleink else {
407 1.12 kleink warn("globbing");
408 1.12 kleink (void)cleanup();
409 1.12 kleink exit(1);
410 1.12 kleink }
411 1.1 cgd }
412 1.1 cgd if (pg->gl_matchc == 0)
413 1.1 cgd continue;
414 1.1 cgd
415 1.1 cgd /* Find out if it's really a man page. */
416 1.1 cgd for (cnt = pg->gl_pathc - pg->gl_matchc;
417 1.1 cgd cnt < pg->gl_pathc; ++cnt) {
418 1.1 cgd
419 1.23 jdolecek if (pathsearch) {
420 1.23 jdolecek p = strstr(pg->gl_pathv[cnt], pathsearch);
421 1.23 jdolecek if (!p || strchr(p, '/') == NULL) {
422 1.23 jdolecek pg->gl_pathv[cnt] = "";
423 1.23 jdolecek continue;
424 1.23 jdolecek }
425 1.23 jdolecek }
426 1.23 jdolecek
427 1.1 cgd /*
428 1.1 cgd * Try the _suffix key words first.
429 1.1 cgd *
430 1.1 cgd * XXX
431 1.1 cgd * Older versions of man.conf didn't have the suffix
432 1.1 cgd * key words, it was assumed that everything was a .0.
433 1.1 cgd * We just test for .0 first, it's fast and probably
434 1.1 cgd * going to hit.
435 1.1 cgd */
436 1.1 cgd (void)snprintf(buf, sizeof(buf), "*/%s.0", page);
437 1.1 cgd if (!fnmatch(buf, pg->gl_pathv[cnt], 0))
438 1.3 cgd goto next;
439 1.1 cgd
440 1.1 cgd e_sufp = (sufp = getlist("_suffix")) == NULL ?
441 1.1 cgd NULL : sufp->list.tqh_first;
442 1.1 cgd for (found = 0;
443 1.1 cgd e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
444 1.1 cgd (void)snprintf(buf,
445 1.1 cgd sizeof(buf), "*/%s%s", page, e_sufp->s);
446 1.1 cgd if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
447 1.1 cgd found = 1;
448 1.1 cgd break;
449 1.1 cgd }
450 1.1 cgd }
451 1.3 cgd if (found)
452 1.3 cgd goto next;
453 1.1 cgd
454 1.1 cgd /* Try the _build key words next. */
455 1.1 cgd e_sufp = (sufp = getlist("_build")) == NULL ?
456 1.1 cgd NULL : sufp->list.tqh_first;
457 1.1 cgd for (found = 0;
458 1.1 cgd e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
459 1.1 cgd for (p = e_sufp->s;
460 1.15 christos *p != '\0' && !isspace((unsigned char)*p); ++p);
461 1.1 cgd if (*p == '\0')
462 1.1 cgd continue;
463 1.1 cgd *p = '\0';
464 1.1 cgd (void)snprintf(buf,
465 1.1 cgd sizeof(buf), "*/%s%s", page, e_sufp->s);
466 1.1 cgd if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
467 1.1 cgd if (!f_where)
468 1.1 cgd build_page(p + 1,
469 1.1 cgd &pg->gl_pathv[cnt]);
470 1.1 cgd *p = ' ';
471 1.1 cgd found = 1;
472 1.1 cgd break;
473 1.1 cgd }
474 1.1 cgd *p = ' ';
475 1.1 cgd }
476 1.1 cgd if (found) {
477 1.3 cgd next: anyfound = 1;
478 1.3 cgd if (!f_all) {
479 1.3 cgd /* Delete any other matches. */
480 1.3 cgd while (++cnt< pg->gl_pathc)
481 1.3 cgd pg->gl_pathv[cnt] = "";
482 1.1 cgd break;
483 1.3 cgd }
484 1.1 cgd continue;
485 1.1 cgd }
486 1.1 cgd
487 1.1 cgd /* It's not a man page, forget about it. */
488 1.1 cgd pg->gl_pathv[cnt] = "";
489 1.1 cgd }
490 1.1 cgd
491 1.1 cgd if (anyfound && !f_all)
492 1.1 cgd break;
493 1.1 cgd }
494 1.1 cgd
495 1.1 cgd /* If not found, enter onto the missing list. */
496 1.1 cgd if (!anyfound) {
497 1.1 cgd if ((missp = getlist("_missing")) == NULL)
498 1.1 cgd missp = addlist("_missing");
499 1.1 cgd if ((ep = malloc(sizeof(ENTRY))) == NULL ||
500 1.1 cgd (ep->s = strdup(page)) == NULL) {
501 1.9 mikel warn("malloc");
502 1.1 cgd (void)cleanup();
503 1.1 cgd exit(1);
504 1.1 cgd }
505 1.1 cgd TAILQ_INSERT_TAIL(&missp->list, ep, q);
506 1.1 cgd }
507 1.1 cgd return (anyfound);
508 1.1 cgd }
509 1.1 cgd
510 1.1 cgd /*
511 1.1 cgd * build_page --
512 1.1 cgd * Build a man page for display.
513 1.1 cgd */
514 1.1 cgd static void
515 1.1 cgd build_page(fmt, pathp)
516 1.1 cgd char *fmt, **pathp;
517 1.1 cgd {
518 1.1 cgd static int warned;
519 1.1 cgd ENTRY *ep;
520 1.1 cgd TAG *intmpp;
521 1.4 jtc int fd, n;
522 1.4 jtc char *p, *b;
523 1.19 kleink char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[MAXPATHLEN];
524 1.19 kleink const char *tmpdir;
525 1.1 cgd
526 1.1 cgd /* Let the user know this may take awhile. */
527 1.1 cgd if (!warned) {
528 1.1 cgd warned = 1;
529 1.1 cgd warnx("Formatting manual page...");
530 1.1 cgd }
531 1.1 cgd
532 1.7 tls /*
533 1.7 tls * Historically man chdir'd to the root of the man tree.
534 1.7 tls * This was used in man pages that contained relative ".so"
535 1.7 tls * directives (including other man pages for command aliases etc.)
536 1.7 tls * It even went one step farther, by examining the first line
537 1.7 tls * of the man page and parsing the .so filename so it would
538 1.7 tls * make hard(?) links to the cat'ted man pages for space savings.
539 1.7 tls * (We don't do that here, but we could).
540 1.7 tls */
541 1.7 tls
542 1.7 tls /* copy and find the end */
543 1.7 tls for (b = buf, p = *pathp; (*b++ = *p++) != '\0';)
544 1.7 tls continue;
545 1.7 tls
546 1.10 tv /* skip the last two path components, page name and man[n] */
547 1.10 tv for (--b, --p, n = 2; b != buf; b--, p--)
548 1.10 tv if (*b == '/')
549 1.10 tv if (--n == 0) {
550 1.10 tv *b = '\0';
551 1.10 tv (void) chdir(buf);
552 1.10 tv p++;
553 1.10 tv break;
554 1.10 tv }
555 1.4 jtc
556 1.4 jtc
557 1.1 cgd /* Add a remove-when-done list. */
558 1.1 cgd if ((intmpp = getlist("_intmp")) == NULL)
559 1.1 cgd intmpp = addlist("_intmp");
560 1.1 cgd
561 1.1 cgd /* Move to the printf(3) format string. */
562 1.15 christos for (; *fmt && isspace((unsigned char)*fmt); ++fmt)
563 1.15 christos continue;
564 1.1 cgd
565 1.1 cgd /*
566 1.1 cgd * Get a temporary file and build a version of the file
567 1.1 cgd * to display. Replace the old file name with the new one.
568 1.1 cgd */
569 1.19 kleink if ((tmpdir = getenv("TMPDIR")) == NULL)
570 1.19 kleink tmpdir = _PATH_TMP;
571 1.19 kleink (void)snprintf(tpath, sizeof (tpath), "%s/%s", tmpdir, TMPFILE);
572 1.1 cgd if ((fd = mkstemp(tpath)) == -1) {
573 1.1 cgd warn("%s", tpath);
574 1.1 cgd (void)cleanup();
575 1.1 cgd exit(1);
576 1.1 cgd }
577 1.1 cgd (void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath);
578 1.10 tv (void)snprintf(cmd, sizeof(cmd), buf, p);
579 1.1 cgd (void)system(cmd);
580 1.1 cgd (void)close(fd);
581 1.1 cgd if ((*pathp = strdup(tpath)) == NULL) {
582 1.9 mikel warn("malloc");
583 1.1 cgd (void)cleanup();
584 1.1 cgd exit(1);
585 1.1 cgd }
586 1.1 cgd
587 1.1 cgd /* Link the built file into the remove-when-done list. */
588 1.1 cgd if ((ep = malloc(sizeof(ENTRY))) == NULL) {
589 1.9 mikel warn("malloc");
590 1.1 cgd (void)cleanup();
591 1.1 cgd exit(1);
592 1.1 cgd }
593 1.1 cgd ep->s = *pathp;
594 1.1 cgd TAILQ_INSERT_TAIL(&intmpp->list, ep, q);
595 1.1 cgd }
596 1.1 cgd
597 1.1 cgd /*
598 1.1 cgd * how --
599 1.1 cgd * display how information
600 1.1 cgd */
601 1.1 cgd static void
602 1.1 cgd how(fname)
603 1.1 cgd char *fname;
604 1.1 cgd {
605 1.1 cgd FILE *fp;
606 1.1 cgd
607 1.1 cgd int lcnt, print;
608 1.1 cgd char *p, buf[256];
609 1.1 cgd
610 1.1 cgd if (!(fp = fopen(fname, "r"))) {
611 1.1 cgd warn("%s", fname);
612 1.1 cgd (void)cleanup();
613 1.1 cgd exit (1);
614 1.1 cgd }
615 1.1 cgd #define S1 "SYNOPSIS"
616 1.1 cgd #define S2 "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"
617 1.1 cgd #define D1 "DESCRIPTION"
618 1.1 cgd #define D2 "D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN"
619 1.1 cgd for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) {
620 1.1 cgd if (!strncmp(buf, S1, sizeof(S1) - 1) ||
621 1.1 cgd !strncmp(buf, S2, sizeof(S2) - 1)) {
622 1.1 cgd print = 1;
623 1.1 cgd continue;
624 1.1 cgd } else if (!strncmp(buf, D1, sizeof(D1) - 1) ||
625 1.1 cgd !strncmp(buf, D2, sizeof(D2) - 1))
626 1.1 cgd return;
627 1.1 cgd if (!print)
628 1.1 cgd continue;
629 1.1 cgd if (*buf == '\n')
630 1.1 cgd ++lcnt;
631 1.1 cgd else {
632 1.1 cgd for(; lcnt; --lcnt)
633 1.1 cgd (void)putchar('\n');
634 1.15 christos for (p = buf; isspace((unsigned char)*p); ++p)
635 1.15 christos continue;
636 1.1 cgd (void)fputs(p, stdout);
637 1.1 cgd }
638 1.1 cgd }
639 1.1 cgd (void)fclose(fp);
640 1.1 cgd }
641 1.1 cgd
642 1.1 cgd /*
643 1.1 cgd * cat --
644 1.1 cgd * cat out the file
645 1.1 cgd */
646 1.1 cgd static void
647 1.1 cgd cat(fname)
648 1.1 cgd char *fname;
649 1.1 cgd {
650 1.1 cgd int fd, n;
651 1.1 cgd char buf[2048];
652 1.1 cgd
653 1.1 cgd if ((fd = open(fname, O_RDONLY, 0)) < 0) {
654 1.1 cgd warn("%s", fname);
655 1.1 cgd (void)cleanup();
656 1.1 cgd exit(1);
657 1.1 cgd }
658 1.1 cgd while ((n = read(fd, buf, sizeof(buf))) > 0)
659 1.1 cgd if (write(STDOUT_FILENO, buf, n) != n) {
660 1.1 cgd warn("write");
661 1.1 cgd (void)cleanup();
662 1.1 cgd exit (1);
663 1.1 cgd }
664 1.1 cgd if (n == -1) {
665 1.1 cgd warn("read");
666 1.1 cgd (void)cleanup();
667 1.1 cgd exit(1);
668 1.1 cgd }
669 1.1 cgd (void)close(fd);
670 1.1 cgd }
671 1.1 cgd
672 1.1 cgd /*
673 1.1 cgd * check_pager --
674 1.1 cgd * check the user supplied page information
675 1.1 cgd */
676 1.23 jdolecek static const char *
677 1.1 cgd check_pager(name)
678 1.23 jdolecek const char *name;
679 1.1 cgd {
680 1.23 jdolecek const char *p, *save;
681 1.1 cgd
682 1.1 cgd /*
683 1.1 cgd * if the user uses "more", we make it "more -s"; watch out for
684 1.1 cgd * PAGER = "mypager /usr/ucb/more"
685 1.1 cgd */
686 1.15 christos for (p = name; *p && !isspace((unsigned char)*p); ++p)
687 1.15 christos continue;
688 1.1 cgd for (; p > name && *p != '/'; --p);
689 1.1 cgd if (p != name)
690 1.1 cgd ++p;
691 1.1 cgd
692 1.1 cgd /* make sure it's "more", not "morex" */
693 1.15 christos if (!strncmp(p, "more", 4) && (!p[4] || isspace((unsigned char)p[4]))){
694 1.23 jdolecek char *newname;
695 1.23 jdolecek (void)asprintf(&newname, "%s %s", save, "-s");
696 1.23 jdolecek name = newname;
697 1.1 cgd }
698 1.23 jdolecek
699 1.23 jdolecek return (name);
700 1.1 cgd }
701 1.1 cgd
702 1.1 cgd /*
703 1.1 cgd * jump --
704 1.1 cgd * strip out flag argument and jump
705 1.1 cgd */
706 1.1 cgd static void
707 1.1 cgd jump(argv, flag, name)
708 1.1 cgd char **argv, *flag, *name;
709 1.1 cgd {
710 1.1 cgd char **arg;
711 1.1 cgd
712 1.1 cgd argv[0] = name;
713 1.1 cgd for (arg = argv + 1; *arg; ++arg)
714 1.1 cgd if (!strcmp(*arg, flag))
715 1.1 cgd break;
716 1.1 cgd for (; *arg; ++arg)
717 1.1 cgd arg[0] = arg[1];
718 1.1 cgd execvp(name, argv);
719 1.1 cgd (void)fprintf(stderr, "%s: Command not found.\n", name);
720 1.1 cgd exit(1);
721 1.1 cgd }
722 1.1 cgd
723 1.1 cgd /*
724 1.1 cgd * onsig --
725 1.1 cgd * If signaled, delete the temporary files.
726 1.1 cgd */
727 1.1 cgd static void
728 1.1 cgd onsig(signo)
729 1.1 cgd int signo;
730 1.1 cgd {
731 1.18 itohy sigset_t set;
732 1.18 itohy
733 1.1 cgd (void)cleanup();
734 1.1 cgd
735 1.1 cgd (void)signal(signo, SIG_DFL);
736 1.18 itohy
737 1.18 itohy /* unblock the signal */
738 1.18 itohy sigemptyset(&set);
739 1.18 itohy sigaddset(&set, signo);
740 1.18 itohy sigprocmask(SIG_UNBLOCK, &set, (sigset_t *) NULL);
741 1.18 itohy
742 1.1 cgd (void)kill(getpid(), signo);
743 1.1 cgd
744 1.1 cgd /* NOTREACHED */
745 1.1 cgd exit (1);
746 1.1 cgd }
747 1.1 cgd
748 1.1 cgd /*
749 1.1 cgd * cleanup --
750 1.1 cgd * Clean up temporary files, show any error messages.
751 1.1 cgd */
752 1.1 cgd static int
753 1.1 cgd cleanup()
754 1.1 cgd {
755 1.1 cgd TAG *intmpp, *missp;
756 1.1 cgd ENTRY *ep;
757 1.1 cgd int rval;
758 1.1 cgd
759 1.1 cgd rval = 0;
760 1.1 cgd ep = (missp = getlist("_missing")) == NULL ?
761 1.1 cgd NULL : missp->list.tqh_first;
762 1.1 cgd if (ep != NULL)
763 1.1 cgd for (; ep != NULL; ep = ep->q.tqe_next) {
764 1.1 cgd warnx("no entry for %s in the manual.", ep->s);
765 1.1 cgd rval = 1;
766 1.1 cgd }
767 1.1 cgd
768 1.1 cgd ep = (intmpp = getlist("_intmp")) == NULL ?
769 1.1 cgd NULL : intmpp->list.tqh_first;
770 1.1 cgd for (; ep != NULL; ep = ep->q.tqe_next)
771 1.1 cgd (void)unlink(ep->s);
772 1.1 cgd return (rval);
773 1.1 cgd }
774 1.1 cgd
775 1.1 cgd /*
776 1.1 cgd * usage --
777 1.1 cgd * print usage message and die
778 1.1 cgd */
779 1.1 cgd static void
780 1.1 cgd usage()
781 1.1 cgd {
782 1.15 christos extern char *__progname;
783 1.1 cgd (void)fprintf(stderr,
784 1.23 jdolecek "Usage: %s [-achw] [-C file] [-M path] [-m path] [-S srch] [section] title ...\n",
785 1.15 christos __progname);
786 1.1 cgd exit(1);
787 1.1 cgd }
788