]> git.cameronkatri.com Git - mandoc.git/blob - main.c
Initialize the local variable "act" in print_mdoc_node().
[mandoc.git] / main.c
1 /* $Id: main.c,v 1.327 2019/05/03 18:39:34 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2012, 2014-2019 Ingo Schwarze <schwarze@openbsd.org>
5 * Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19 #include "config.h"
20
21 #include <sys/types.h>
22 #include <sys/ioctl.h>
23 #include <sys/param.h> /* MACHINE */
24 #include <sys/stat.h>
25 #include <sys/wait.h>
26
27 #include <assert.h>
28 #include <ctype.h>
29 #if HAVE_ERR
30 #include <err.h>
31 #endif
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <glob.h>
35 #if HAVE_SANDBOX_INIT
36 #include <sandbox.h>
37 #endif
38 #include <signal.h>
39 #include <stdio.h>
40 #include <stdint.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <termios.h>
44 #include <time.h>
45 #include <unistd.h>
46
47 #include "mandoc_aux.h"
48 #include "mandoc.h"
49 #include "mandoc_xr.h"
50 #include "roff.h"
51 #include "mdoc.h"
52 #include "man.h"
53 #include "mandoc_parse.h"
54 #include "tag.h"
55 #include "main.h"
56 #include "manconf.h"
57 #include "mansearch.h"
58
59 enum outmode {
60 OUTMODE_DEF = 0,
61 OUTMODE_FLN,
62 OUTMODE_LST,
63 OUTMODE_ALL,
64 OUTMODE_ONE
65 };
66
67 enum outt {
68 OUTT_ASCII = 0, /* -Tascii */
69 OUTT_LOCALE, /* -Tlocale */
70 OUTT_UTF8, /* -Tutf8 */
71 OUTT_TREE, /* -Ttree */
72 OUTT_MAN, /* -Tman */
73 OUTT_HTML, /* -Thtml */
74 OUTT_MARKDOWN, /* -Tmarkdown */
75 OUTT_LINT, /* -Tlint */
76 OUTT_PS, /* -Tps */
77 OUTT_PDF /* -Tpdf */
78 };
79
80 struct curparse {
81 struct mparse *mp;
82 struct manoutput *outopts; /* output options */
83 void *outdata; /* data for output */
84 char *os_s; /* operating system for display */
85 int wstop; /* stop after a file with a warning */
86 enum mandoc_os os_e; /* check base system conventions */
87 enum outt outtype; /* which output to use */
88 };
89
90
91 int mandocdb(int, char *[]);
92
93 static void check_xr(void);
94 static int fs_lookup(const struct manpaths *,
95 size_t ipath, const char *,
96 const char *, const char *,
97 struct manpage **, size_t *);
98 static int fs_search(const struct mansearch *,
99 const struct manpaths *, int, char**,
100 struct manpage **, size_t *);
101 static int koptions(int *, char *);
102 static void moptions(int *, char *);
103 static void outdata_alloc(struct curparse *);
104 static void parse(struct curparse *, int, const char *);
105 static void passthrough(const char *, int, int);
106 static pid_t spawn_pager(struct tag_files *);
107 static int toptions(struct curparse *, char *);
108 static void usage(enum argmode) __attribute__((__noreturn__));
109 static int woptions(struct curparse *, char *);
110
111 static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
112 static char help_arg[] = "help";
113 static char *help_argv[] = {help_arg, NULL};
114
115
116 int
117 main(int argc, char *argv[])
118 {
119 struct manconf conf;
120 struct mansearch search;
121 struct curparse curp;
122 struct winsize ws;
123 struct tag_files *tag_files;
124 struct manpage *res, *resp;
125 const char *progname, *sec, *thisarg;
126 char *conf_file, *defpaths, *auxpaths;
127 char *oarg, *tagarg;
128 unsigned char *uc;
129 size_t i, sz, ssz;
130 int prio, best_prio;
131 enum outmode outmode;
132 int fd, startdir;
133 int show_usage;
134 int options;
135 int use_pager;
136 int status, signum;
137 int c;
138 pid_t pager_pid, tc_pgid, man_pgid, pid;
139
140 #if HAVE_PROGNAME
141 progname = getprogname();
142 #else
143 if (argc < 1)
144 progname = mandoc_strdup("mandoc");
145 else if ((progname = strrchr(argv[0], '/')) == NULL)
146 progname = argv[0];
147 else
148 ++progname;
149 setprogname(progname);
150 #endif
151
152 mandoc_msg_setoutfile(stderr);
153 if (strncmp(progname, "mandocdb", 8) == 0 ||
154 strcmp(progname, BINM_MAKEWHATIS) == 0)
155 return mandocdb(argc, argv);
156
157 #if HAVE_PLEDGE
158 if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1)
159 err((int)MANDOCLEVEL_SYSERR, "pledge");
160 #endif
161
162 #if HAVE_SANDBOX_INIT
163 if (sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, NULL) == -1)
164 errx((int)MANDOCLEVEL_SYSERR, "sandbox_init");
165 #endif
166
167 /* Search options. */
168
169 memset(&conf, 0, sizeof(conf));
170 conf_file = defpaths = NULL;
171 auxpaths = NULL;
172
173 memset(&search, 0, sizeof(struct mansearch));
174 search.outkey = "Nd";
175 oarg = NULL;
176
177 if (strcmp(progname, BINM_MAN) == 0)
178 search.argmode = ARG_NAME;
179 else if (strcmp(progname, BINM_APROPOS) == 0)
180 search.argmode = ARG_EXPR;
181 else if (strcmp(progname, BINM_WHATIS) == 0)
182 search.argmode = ARG_WORD;
183 else if (strncmp(progname, "help", 4) == 0)
184 search.argmode = ARG_NAME;
185 else
186 search.argmode = ARG_FILE;
187
188 /* Parser and formatter options. */
189
190 memset(&curp, 0, sizeof(struct curparse));
191 curp.outtype = OUTT_LOCALE;
192 curp.outopts = &conf.output;
193 options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
194
195 use_pager = 1;
196 tag_files = NULL;
197 show_usage = 0;
198 outmode = OUTMODE_DEF;
199
200 while ((c = getopt(argc, argv,
201 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w")) != -1) {
202 if (c == 'i' && search.argmode == ARG_EXPR) {
203 optind--;
204 break;
205 }
206 switch (c) {
207 case 'a':
208 outmode = OUTMODE_ALL;
209 break;
210 case 'C':
211 conf_file = optarg;
212 break;
213 case 'c':
214 use_pager = 0;
215 break;
216 case 'f':
217 search.argmode = ARG_WORD;
218 break;
219 case 'h':
220 conf.output.synopsisonly = 1;
221 use_pager = 0;
222 outmode = OUTMODE_ALL;
223 break;
224 case 'I':
225 if (strncmp(optarg, "os=", 3)) {
226 warnx("-I %s: Bad argument", optarg);
227 return (int)MANDOCLEVEL_BADARG;
228 }
229 if (curp.os_s != NULL) {
230 warnx("-I %s: Duplicate argument", optarg);
231 return (int)MANDOCLEVEL_BADARG;
232 }
233 curp.os_s = mandoc_strdup(optarg + 3);
234 break;
235 case 'K':
236 if ( ! koptions(&options, optarg))
237 return (int)MANDOCLEVEL_BADARG;
238 break;
239 case 'k':
240 search.argmode = ARG_EXPR;
241 break;
242 case 'l':
243 search.argmode = ARG_FILE;
244 outmode = OUTMODE_ALL;
245 break;
246 case 'M':
247 defpaths = optarg;
248 break;
249 case 'm':
250 auxpaths = optarg;
251 break;
252 case 'O':
253 oarg = optarg;
254 break;
255 case 'S':
256 search.arch = optarg;
257 break;
258 case 's':
259 search.sec = optarg;
260 break;
261 case 'T':
262 if ( ! toptions(&curp, optarg))
263 return (int)MANDOCLEVEL_BADARG;
264 break;
265 case 'W':
266 if ( ! woptions(&curp, optarg))
267 return (int)MANDOCLEVEL_BADARG;
268 break;
269 case 'w':
270 outmode = OUTMODE_FLN;
271 break;
272 default:
273 show_usage = 1;
274 break;
275 }
276 }
277
278 if (show_usage)
279 usage(search.argmode);
280
281 /* Postprocess options. */
282
283 if (outmode == OUTMODE_DEF) {
284 switch (search.argmode) {
285 case ARG_FILE:
286 outmode = OUTMODE_ALL;
287 use_pager = 0;
288 break;
289 case ARG_NAME:
290 outmode = OUTMODE_ONE;
291 break;
292 default:
293 outmode = OUTMODE_LST;
294 break;
295 }
296 }
297
298 if (oarg != NULL) {
299 if (outmode == OUTMODE_LST)
300 search.outkey = oarg;
301 else {
302 while (oarg != NULL) {
303 if (manconf_output(&conf.output,
304 strsep(&oarg, ","), 0) == -1)
305 return (int)MANDOCLEVEL_BADARG;
306 }
307 }
308 }
309
310 if (curp.outtype != OUTT_TREE || !curp.outopts->noval)
311 options |= MPARSE_VALIDATE;
312
313 if (outmode == OUTMODE_FLN ||
314 outmode == OUTMODE_LST ||
315 !isatty(STDOUT_FILENO))
316 use_pager = 0;
317
318 if (use_pager &&
319 (conf.output.width == 0 || conf.output.indent == 0) &&
320 ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1 &&
321 ws.ws_col > 1) {
322 if (conf.output.width == 0 && ws.ws_col < 79)
323 conf.output.width = ws.ws_col - 1;
324 if (conf.output.indent == 0 && ws.ws_col < 66)
325 conf.output.indent = 3;
326 }
327
328 #if HAVE_PLEDGE
329 if (!use_pager)
330 if (pledge("stdio rpath", NULL) == -1)
331 err((int)MANDOCLEVEL_SYSERR, "pledge");
332 #endif
333
334 /* Parse arguments. */
335
336 if (argc > 0) {
337 argc -= optind;
338 argv += optind;
339 }
340 resp = NULL;
341
342 /*
343 * Quirks for help(1)
344 * and for a man(1) section argument without -s.
345 */
346
347 if (search.argmode == ARG_NAME) {
348 if (*progname == 'h') {
349 if (argc == 0) {
350 argv = help_argv;
351 argc = 1;
352 }
353 } else if (argc > 1 &&
354 ((uc = (unsigned char *)argv[0]) != NULL) &&
355 ((isdigit(uc[0]) && (uc[1] == '\0' ||
356 isalpha(uc[1]))) ||
357 (uc[0] == 'n' && uc[1] == '\0'))) {
358 search.sec = (char *)uc;
359 argv++;
360 argc--;
361 }
362 if (search.arch == NULL)
363 search.arch = getenv("MACHINE");
364 #ifdef MACHINE
365 if (search.arch == NULL)
366 search.arch = MACHINE;
367 #endif
368 }
369
370 /*
371 * Use the first argument for -O tag in addition to
372 * using it as a search term for man(1) or apropos(1).
373 */
374
375 if (conf.output.tag != NULL && *conf.output.tag == '\0') {
376 tagarg = argc > 0 && search.argmode == ARG_EXPR ?
377 strchr(*argv, '=') : NULL;
378 conf.output.tag = tagarg == NULL ? *argv : tagarg + 1;
379 }
380
381 /* man(1), whatis(1), apropos(1) */
382
383 if (search.argmode != ARG_FILE) {
384 if (search.argmode == ARG_NAME &&
385 outmode == OUTMODE_ONE)
386 search.firstmatch = 1;
387
388 /* Access the mandoc database. */
389
390 manconf_parse(&conf, conf_file, defpaths, auxpaths);
391 if ( ! mansearch(&search, &conf.manpath,
392 argc, argv, &res, &sz))
393 usage(search.argmode);
394
395 if (sz == 0 && search.argmode == ARG_NAME)
396 fs_search(&search, &conf.manpath,
397 argc, argv, &res, &sz);
398
399 if (search.argmode == ARG_NAME) {
400 for (c = 0; c < argc; c++) {
401 if (strchr(argv[c], '/') == NULL)
402 continue;
403 if (access(argv[c], R_OK) == -1) {
404 warn("%s", argv[c]);
405 continue;
406 }
407 res = mandoc_reallocarray(res,
408 sz + 1, sizeof(*res));
409 res[sz].file = mandoc_strdup(argv[c]);
410 res[sz].names = NULL;
411 res[sz].output = NULL;
412 res[sz].bits = 0;
413 res[sz].ipath = SIZE_MAX;
414 res[sz].sec = 10;
415 res[sz].form = FORM_SRC;
416 sz++;
417 }
418 }
419
420 if (sz == 0) {
421 if (search.argmode != ARG_NAME)
422 warnx("nothing appropriate");
423 mandoc_msg_setrc(MANDOCLEVEL_BADARG);
424 goto out;
425 }
426
427 /*
428 * For standard man(1) and -a output mode,
429 * prepare for copying filename pointers
430 * into the program parameter array.
431 */
432
433 if (outmode == OUTMODE_ONE) {
434 argc = 1;
435 best_prio = 40;
436 } else if (outmode == OUTMODE_ALL)
437 argc = (int)sz;
438
439 /* Iterate all matching manuals. */
440
441 resp = res;
442 for (i = 0; i < sz; i++) {
443 if (outmode == OUTMODE_FLN)
444 puts(res[i].file);
445 else if (outmode == OUTMODE_LST)
446 printf("%s - %s\n", res[i].names,
447 res[i].output == NULL ? "" :
448 res[i].output);
449 else if (outmode == OUTMODE_ONE) {
450 /* Search for the best section. */
451 sec = res[i].file;
452 sec += strcspn(sec, "123456789");
453 if (sec[0] == '\0')
454 continue; /* No section at all. */
455 prio = sec_prios[sec[0] - '1'];
456 if (search.sec != NULL) {
457 ssz = strlen(search.sec);
458 if (strncmp(sec, search.sec, ssz) == 0)
459 sec += ssz;
460 } else
461 sec++; /* Prefer without suffix. */
462 if (*sec != '/')
463 prio += 10; /* Wrong dir name. */
464 if (search.sec != NULL &&
465 (strlen(sec) <= ssz + 3 ||
466 strcmp(sec + strlen(sec) - ssz,
467 search.sec) != 0))
468 prio += 20; /* Wrong file ext. */
469 if (prio >= best_prio)
470 continue;
471 best_prio = prio;
472 resp = res + i;
473 }
474 }
475
476 /*
477 * For man(1), -a and -i output mode, fall through
478 * to the main mandoc(1) code iterating files
479 * and running the parsers on each of them.
480 */
481
482 if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
483 goto out;
484 }
485
486 /* mandoc(1) */
487
488 #if HAVE_PLEDGE
489 if (use_pager) {
490 if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1)
491 err((int)MANDOCLEVEL_SYSERR, "pledge");
492 } else {
493 if (pledge("stdio rpath", NULL) == -1)
494 err((int)MANDOCLEVEL_SYSERR, "pledge");
495 }
496 #endif
497
498 if (search.argmode == ARG_FILE)
499 moptions(&options, auxpaths);
500
501 mchars_alloc();
502 curp.mp = mparse_alloc(options, curp.os_e, curp.os_s);
503
504 if (argc < 1) {
505 if (use_pager) {
506 tag_files = tag_init();
507 tag_files->tagname = conf.output.tag;
508 }
509 thisarg = "<stdin>";
510 mandoc_msg_setinfilename(thisarg);
511 parse(&curp, STDIN_FILENO, thisarg);
512 mandoc_msg_setinfilename(NULL);
513 }
514
515 /*
516 * Remember the original working directory, if possible.
517 * This will be needed if some names on the command line
518 * are page names and some are relative file names.
519 * Do not error out if the current directory is not
520 * readable: Maybe it won't be needed after all.
521 */
522 startdir = open(".", O_RDONLY | O_DIRECTORY);
523
524 while (argc > 0) {
525
526 /*
527 * Changing directories is not needed in ARG_FILE mode.
528 * Do it on a best-effort basis. Even in case of
529 * failure, some functionality may still work.
530 */
531 if (resp != NULL) {
532 if (resp->ipath != SIZE_MAX)
533 (void)chdir(conf.manpath.paths[resp->ipath]);
534 else if (startdir != -1)
535 (void)fchdir(startdir);
536 thisarg = resp->file;
537 } else
538 thisarg = *argv;
539
540 fd = mparse_open(curp.mp, thisarg);
541 if (fd != -1) {
542 if (use_pager) {
543 use_pager = 0;
544 tag_files = tag_init();
545 tag_files->tagname = conf.output.tag;
546 }
547
548 mandoc_msg_setinfilename(thisarg);
549 if (resp == NULL || resp->form == FORM_SRC)
550 parse(&curp, fd, thisarg);
551 else
552 passthrough(resp->file, fd,
553 conf.output.synopsisonly);
554 mandoc_msg_setinfilename(NULL);
555
556 if (ferror(stdout)) {
557 if (tag_files != NULL) {
558 warn("%s", tag_files->ofn);
559 tag_unlink();
560 tag_files = NULL;
561 } else
562 warn("stdout");
563 mandoc_msg_setrc(MANDOCLEVEL_SYSERR);
564 break;
565 }
566
567 if (argc > 1 && curp.outtype <= OUTT_UTF8) {
568 if (curp.outdata == NULL)
569 outdata_alloc(&curp);
570 terminal_sepline(curp.outdata);
571 }
572 } else
573 mandoc_msg(MANDOCERR_FILE, 0, 0,
574 "%s: %s", thisarg, strerror(errno));
575
576 if (curp.wstop && mandoc_msg_getrc() != MANDOCLEVEL_OK)
577 break;
578
579 if (resp != NULL)
580 resp++;
581 else
582 argv++;
583 if (--argc)
584 mparse_reset(curp.mp);
585 }
586 if (startdir != -1) {
587 (void)fchdir(startdir);
588 close(startdir);
589 }
590
591 if (curp.outdata != NULL) {
592 switch (curp.outtype) {
593 case OUTT_HTML:
594 html_free(curp.outdata);
595 break;
596 case OUTT_UTF8:
597 case OUTT_LOCALE:
598 case OUTT_ASCII:
599 ascii_free(curp.outdata);
600 break;
601 case OUTT_PDF:
602 case OUTT_PS:
603 pspdf_free(curp.outdata);
604 break;
605 default:
606 break;
607 }
608 }
609 mandoc_xr_free();
610 mparse_free(curp.mp);
611 mchars_free();
612
613 out:
614 if (search.argmode != ARG_FILE) {
615 manconf_free(&conf);
616 mansearch_free(res, sz);
617 }
618
619 free(curp.os_s);
620
621 /*
622 * When using a pager, finish writing both temporary files,
623 * fork it, wait for the user to close it, and clean up.
624 */
625
626 if (tag_files != NULL) {
627 fclose(stdout);
628 tag_write();
629 man_pgid = getpgid(0);
630 tag_files->tcpgid = man_pgid == getpid() ?
631 getpgid(getppid()) : man_pgid;
632 pager_pid = 0;
633 signum = SIGSTOP;
634 for (;;) {
635
636 /* Stop here until moved to the foreground. */
637
638 tc_pgid = tcgetpgrp(tag_files->ofd);
639 if (tc_pgid != man_pgid) {
640 if (tc_pgid == pager_pid) {
641 (void)tcsetpgrp(tag_files->ofd,
642 man_pgid);
643 if (signum == SIGTTIN)
644 continue;
645 } else
646 tag_files->tcpgid = tc_pgid;
647 kill(0, signum);
648 continue;
649 }
650
651 /* Once in the foreground, activate the pager. */
652
653 if (pager_pid) {
654 (void)tcsetpgrp(tag_files->ofd, pager_pid);
655 kill(pager_pid, SIGCONT);
656 } else
657 pager_pid = spawn_pager(tag_files);
658
659 /* Wait for the pager to stop or exit. */
660
661 while ((pid = waitpid(pager_pid, &status,
662 WUNTRACED)) == -1 && errno == EINTR)
663 continue;
664
665 if (pid == -1) {
666 warn("wait");
667 mandoc_msg_setrc(MANDOCLEVEL_SYSERR);
668 break;
669 }
670 if (!WIFSTOPPED(status))
671 break;
672
673 signum = WSTOPSIG(status);
674 }
675 tag_unlink();
676 }
677 return (int)mandoc_msg_getrc();
678 }
679
680 static void
681 usage(enum argmode argmode)
682 {
683
684 switch (argmode) {
685 case ARG_FILE:
686 fputs("usage: mandoc [-ac] [-I os=name] "
687 "[-K encoding] [-mdoc | -man] [-O options]\n"
688 "\t [-T output] [-W level] [file ...]\n", stderr);
689 break;
690 case ARG_NAME:
691 fputs("usage: man [-acfhklw] [-C file] [-M path] "
692 "[-m path] [-S subsection]\n"
693 "\t [[-s] section] name ...\n", stderr);
694 break;
695 case ARG_WORD:
696 fputs("usage: whatis [-afk] [-C file] "
697 "[-M path] [-m path] [-O outkey] [-S arch]\n"
698 "\t [-s section] name ...\n", stderr);
699 break;
700 case ARG_EXPR:
701 fputs("usage: apropos [-afk] [-C file] "
702 "[-M path] [-m path] [-O outkey] [-S arch]\n"
703 "\t [-s section] expression ...\n", stderr);
704 break;
705 }
706 exit((int)MANDOCLEVEL_BADARG);
707 }
708
709 static int
710 fs_lookup(const struct manpaths *paths, size_t ipath,
711 const char *sec, const char *arch, const char *name,
712 struct manpage **res, size_t *ressz)
713 {
714 struct stat sb;
715 glob_t globinfo;
716 struct manpage *page;
717 char *file;
718 int globres;
719 enum form form;
720
721 form = FORM_SRC;
722 mandoc_asprintf(&file, "%s/man%s/%s.%s",
723 paths->paths[ipath], sec, name, sec);
724 if (stat(file, &sb) != -1)
725 goto found;
726 free(file);
727
728 mandoc_asprintf(&file, "%s/cat%s/%s.0",
729 paths->paths[ipath], sec, name);
730 if (stat(file, &sb) != -1) {
731 form = FORM_CAT;
732 goto found;
733 }
734 free(file);
735
736 if (arch != NULL) {
737 mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
738 paths->paths[ipath], sec, arch, name, sec);
739 if (stat(file, &sb) != -1)
740 goto found;
741 free(file);
742 }
743
744 mandoc_asprintf(&file, "%s/man%s/%s.[01-9]*",
745 paths->paths[ipath], sec, name);
746 globres = glob(file, 0, NULL, &globinfo);
747 if (globres != 0 && globres != GLOB_NOMATCH)
748 warn("%s: glob", file);
749 free(file);
750 if (globres == 0)
751 file = mandoc_strdup(*globinfo.gl_pathv);
752 globfree(&globinfo);
753 if (globres == 0) {
754 if (stat(file, &sb) != -1)
755 goto found;
756 free(file);
757 }
758 if (res != NULL || ipath + 1 != paths->sz)
759 return 0;
760
761 mandoc_asprintf(&file, "%s.%s", name, sec);
762 globres = stat(file, &sb);
763 free(file);
764 return globres != -1;
765
766 found:
767 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
768 name, sec, BINM_MAKEWHATIS, paths->paths[ipath]);
769 if (res == NULL) {
770 free(file);
771 return 1;
772 }
773 *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
774 page = *res + (*ressz - 1);
775 page->file = file;
776 page->names = NULL;
777 page->output = NULL;
778 page->bits = NAME_FILE & NAME_MASK;
779 page->ipath = ipath;
780 page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
781 page->form = form;
782 return 1;
783 }
784
785 static int
786 fs_search(const struct mansearch *cfg, const struct manpaths *paths,
787 int argc, char **argv, struct manpage **res, size_t *ressz)
788 {
789 const char *const sections[] =
790 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
791 const size_t nsec = sizeof(sections)/sizeof(sections[0]);
792
793 size_t ipath, isec, lastsz;
794
795 assert(cfg->argmode == ARG_NAME);
796
797 if (res != NULL)
798 *res = NULL;
799 *ressz = lastsz = 0;
800 while (argc) {
801 for (ipath = 0; ipath < paths->sz; ipath++) {
802 if (cfg->sec != NULL) {
803 if (fs_lookup(paths, ipath, cfg->sec,
804 cfg->arch, *argv, res, ressz) &&
805 cfg->firstmatch)
806 return 1;
807 } else for (isec = 0; isec < nsec; isec++)
808 if (fs_lookup(paths, ipath, sections[isec],
809 cfg->arch, *argv, res, ressz) &&
810 cfg->firstmatch)
811 return 1;
812 }
813 if (res != NULL && *ressz == lastsz &&
814 strchr(*argv, '/') == NULL) {
815 if (cfg->arch != NULL &&
816 arch_valid(cfg->arch, OSENUM) == 0)
817 warnx("Unknown architecture \"%s\".",
818 cfg->arch);
819 else if (cfg->sec == NULL)
820 warnx("No entry for %s in the manual.",
821 *argv);
822 else
823 warnx("No entry for %s in section %s "
824 "of the manual.", *argv, cfg->sec);
825 }
826 lastsz = *ressz;
827 argv++;
828 argc--;
829 }
830 return 0;
831 }
832
833 static void
834 parse(struct curparse *curp, int fd, const char *file)
835 {
836 struct roff_meta *meta;
837
838 /* Begin by parsing the file itself. */
839
840 assert(file);
841 assert(fd >= 0);
842
843 mparse_readfd(curp->mp, fd, file);
844 if (fd != STDIN_FILENO)
845 close(fd);
846
847 /*
848 * With -Wstop and warnings or errors of at least the requested
849 * level, do not produce output.
850 */
851
852 if (curp->wstop && mandoc_msg_getrc() != MANDOCLEVEL_OK)
853 return;
854
855 if (curp->outdata == NULL)
856 outdata_alloc(curp);
857 else if (curp->outtype == OUTT_HTML)
858 html_reset(curp);
859
860 mandoc_xr_reset();
861 meta = mparse_result(curp->mp);
862
863 /* Execute the out device, if it exists. */
864
865 if (meta->macroset == MACROSET_MDOC) {
866 switch (curp->outtype) {
867 case OUTT_HTML:
868 html_mdoc(curp->outdata, meta);
869 break;
870 case OUTT_TREE:
871 tree_mdoc(curp->outdata, meta);
872 break;
873 case OUTT_MAN:
874 man_mdoc(curp->outdata, meta);
875 break;
876 case OUTT_PDF:
877 case OUTT_ASCII:
878 case OUTT_UTF8:
879 case OUTT_LOCALE:
880 case OUTT_PS:
881 terminal_mdoc(curp->outdata, meta);
882 break;
883 case OUTT_MARKDOWN:
884 markdown_mdoc(curp->outdata, meta);
885 break;
886 default:
887 break;
888 }
889 }
890 if (meta->macroset == MACROSET_MAN) {
891 switch (curp->outtype) {
892 case OUTT_HTML:
893 html_man(curp->outdata, meta);
894 break;
895 case OUTT_TREE:
896 tree_man(curp->outdata, meta);
897 break;
898 case OUTT_MAN:
899 mparse_copy(curp->mp);
900 break;
901 case OUTT_PDF:
902 case OUTT_ASCII:
903 case OUTT_UTF8:
904 case OUTT_LOCALE:
905 case OUTT_PS:
906 terminal_man(curp->outdata, meta);
907 break;
908 default:
909 break;
910 }
911 }
912 if (mandoc_msg_getmin() < MANDOCERR_STYLE)
913 check_xr();
914 }
915
916 static void
917 check_xr(void)
918 {
919 static struct manpaths paths;
920 struct mansearch search;
921 struct mandoc_xr *xr;
922 size_t sz;
923
924 if (paths.sz == 0)
925 manpath_base(&paths);
926
927 for (xr = mandoc_xr_get(); xr != NULL; xr = xr->next) {
928 if (xr->line == -1)
929 continue;
930 search.arch = NULL;
931 search.sec = xr->sec;
932 search.outkey = NULL;
933 search.argmode = ARG_NAME;
934 search.firstmatch = 1;
935 if (mansearch(&search, &paths, 1, &xr->name, NULL, &sz))
936 continue;
937 if (fs_search(&search, &paths, 1, &xr->name, NULL, &sz))
938 continue;
939 if (xr->count == 1)
940 mandoc_msg(MANDOCERR_XR_BAD, xr->line,
941 xr->pos + 1, "Xr %s %s", xr->name, xr->sec);
942 else
943 mandoc_msg(MANDOCERR_XR_BAD, xr->line,
944 xr->pos + 1, "Xr %s %s (%d times)",
945 xr->name, xr->sec, xr->count);
946 }
947 }
948
949 static void
950 outdata_alloc(struct curparse *curp)
951 {
952 switch (curp->outtype) {
953 case OUTT_HTML:
954 curp->outdata = html_alloc(curp->outopts);
955 break;
956 case OUTT_UTF8:
957 curp->outdata = utf8_alloc(curp->outopts);
958 break;
959 case OUTT_LOCALE:
960 curp->outdata = locale_alloc(curp->outopts);
961 break;
962 case OUTT_ASCII:
963 curp->outdata = ascii_alloc(curp->outopts);
964 break;
965 case OUTT_PDF:
966 curp->outdata = pdf_alloc(curp->outopts);
967 break;
968 case OUTT_PS:
969 curp->outdata = ps_alloc(curp->outopts);
970 break;
971 default:
972 break;
973 }
974 }
975
976 static void
977 passthrough(const char *file, int fd, int synopsis_only)
978 {
979 const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
980 const char synr[] = "SYNOPSIS";
981
982 FILE *stream;
983 const char *syscall;
984 char *line, *cp;
985 size_t linesz;
986 ssize_t len, written;
987 int print;
988
989 line = NULL;
990 linesz = 0;
991
992 if (fflush(stdout) == EOF) {
993 syscall = "fflush";
994 goto fail;
995 }
996
997 if ((stream = fdopen(fd, "r")) == NULL) {
998 close(fd);
999 syscall = "fdopen";
1000 goto fail;
1001 }
1002
1003 print = 0;
1004 while ((len = getline(&line, &linesz, stream)) != -1) {
1005 cp = line;
1006 if (synopsis_only) {
1007 if (print) {
1008 if ( ! isspace((unsigned char)*cp))
1009 goto done;
1010 while (isspace((unsigned char)*cp)) {
1011 cp++;
1012 len--;
1013 }
1014 } else {
1015 if (strcmp(cp, synb) == 0 ||
1016 strcmp(cp, synr) == 0)
1017 print = 1;
1018 continue;
1019 }
1020 }
1021 for (; len > 0; len -= written) {
1022 if ((written = write(STDOUT_FILENO, cp, len)) != -1)
1023 continue;
1024 fclose(stream);
1025 syscall = "write";
1026 goto fail;
1027 }
1028 }
1029
1030 if (ferror(stream)) {
1031 fclose(stream);
1032 syscall = "getline";
1033 goto fail;
1034 }
1035
1036 done:
1037 free(line);
1038 fclose(stream);
1039 return;
1040
1041 fail:
1042 free(line);
1043 warn("%s: SYSERR: %s", file, syscall);
1044 mandoc_msg_setrc(MANDOCLEVEL_SYSERR);
1045 }
1046
1047 static int
1048 koptions(int *options, char *arg)
1049 {
1050
1051 if ( ! strcmp(arg, "utf-8")) {
1052 *options |= MPARSE_UTF8;
1053 *options &= ~MPARSE_LATIN1;
1054 } else if ( ! strcmp(arg, "iso-8859-1")) {
1055 *options |= MPARSE_LATIN1;
1056 *options &= ~MPARSE_UTF8;
1057 } else if ( ! strcmp(arg, "us-ascii")) {
1058 *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
1059 } else {
1060 warnx("-K %s: Bad argument", arg);
1061 return 0;
1062 }
1063 return 1;
1064 }
1065
1066 static void
1067 moptions(int *options, char *arg)
1068 {
1069
1070 if (arg == NULL)
1071 return;
1072 if (strcmp(arg, "doc") == 0)
1073 *options |= MPARSE_MDOC;
1074 else if (strcmp(arg, "an") == 0)
1075 *options |= MPARSE_MAN;
1076 }
1077
1078 static int
1079 toptions(struct curparse *curp, char *arg)
1080 {
1081
1082 if (0 == strcmp(arg, "ascii"))
1083 curp->outtype = OUTT_ASCII;
1084 else if (0 == strcmp(arg, "lint")) {
1085 curp->outtype = OUTT_LINT;
1086 mandoc_msg_setoutfile(stdout);
1087 mandoc_msg_setmin(MANDOCERR_BASE);
1088 } else if (0 == strcmp(arg, "tree"))
1089 curp->outtype = OUTT_TREE;
1090 else if (0 == strcmp(arg, "man"))
1091 curp->outtype = OUTT_MAN;
1092 else if (0 == strcmp(arg, "html"))
1093 curp->outtype = OUTT_HTML;
1094 else if (0 == strcmp(arg, "markdown"))
1095 curp->outtype = OUTT_MARKDOWN;
1096 else if (0 == strcmp(arg, "utf8"))
1097 curp->outtype = OUTT_UTF8;
1098 else if (0 == strcmp(arg, "locale"))
1099 curp->outtype = OUTT_LOCALE;
1100 else if (0 == strcmp(arg, "ps"))
1101 curp->outtype = OUTT_PS;
1102 else if (0 == strcmp(arg, "pdf"))
1103 curp->outtype = OUTT_PDF;
1104 else {
1105 warnx("-T %s: Bad argument", arg);
1106 return 0;
1107 }
1108
1109 return 1;
1110 }
1111
1112 static int
1113 woptions(struct curparse *curp, char *arg)
1114 {
1115 char *v, *o;
1116 const char *toks[11];
1117
1118 toks[0] = "stop";
1119 toks[1] = "all";
1120 toks[2] = "base";
1121 toks[3] = "style";
1122 toks[4] = "warning";
1123 toks[5] = "error";
1124 toks[6] = "unsupp";
1125 toks[7] = "fatal";
1126 toks[8] = "openbsd";
1127 toks[9] = "netbsd";
1128 toks[10] = NULL;
1129
1130 while (*arg) {
1131 o = arg;
1132 switch (getsubopt(&arg, (char * const *)toks, &v)) {
1133 case 0:
1134 curp->wstop = 1;
1135 break;
1136 case 1:
1137 case 2:
1138 mandoc_msg_setmin(MANDOCERR_BASE);
1139 break;
1140 case 3:
1141 mandoc_msg_setmin(MANDOCERR_STYLE);
1142 break;
1143 case 4:
1144 mandoc_msg_setmin(MANDOCERR_WARNING);
1145 break;
1146 case 5:
1147 mandoc_msg_setmin(MANDOCERR_ERROR);
1148 break;
1149 case 6:
1150 mandoc_msg_setmin(MANDOCERR_UNSUPP);
1151 break;
1152 case 7:
1153 mandoc_msg_setmin(MANDOCERR_MAX);
1154 break;
1155 case 8:
1156 mandoc_msg_setmin(MANDOCERR_BASE);
1157 curp->os_e = MANDOC_OS_OPENBSD;
1158 break;
1159 case 9:
1160 mandoc_msg_setmin(MANDOCERR_BASE);
1161 curp->os_e = MANDOC_OS_NETBSD;
1162 break;
1163 default:
1164 warnx("-W %s: Bad argument", o);
1165 return 0;
1166 }
1167 }
1168 return 1;
1169 }
1170
1171 static pid_t
1172 spawn_pager(struct tag_files *tag_files)
1173 {
1174 const struct timespec timeout = { 0, 100000000 }; /* 0.1s */
1175 #define MAX_PAGER_ARGS 16
1176 char *argv[MAX_PAGER_ARGS];
1177 const char *pager;
1178 char *cp;
1179 #if HAVE_LESS_T
1180 size_t cmdlen;
1181 #endif
1182 int argc, use_ofn;
1183 pid_t pager_pid;
1184
1185 pager = getenv("MANPAGER");
1186 if (pager == NULL || *pager == '\0')
1187 pager = getenv("PAGER");
1188 if (pager == NULL || *pager == '\0')
1189 pager = "more -s";
1190 cp = mandoc_strdup(pager);
1191
1192 /*
1193 * Parse the pager command into words.
1194 * Intentionally do not do anything fancy here.
1195 */
1196
1197 argc = 0;
1198 while (argc + 5 < MAX_PAGER_ARGS) {
1199 argv[argc++] = cp;
1200 cp = strchr(cp, ' ');
1201 if (cp == NULL)
1202 break;
1203 *cp++ = '\0';
1204 while (*cp == ' ')
1205 cp++;
1206 if (*cp == '\0')
1207 break;
1208 }
1209
1210 /* For less(1), use the tag file. */
1211
1212 use_ofn = 1;
1213 #if HAVE_LESS_T
1214 if ((cmdlen = strlen(argv[0])) >= 4) {
1215 cp = argv[0] + cmdlen - 4;
1216 if (strcmp(cp, "less") == 0) {
1217 argv[argc++] = mandoc_strdup("-T");
1218 argv[argc++] = tag_files->tfn;
1219 if (tag_files->tagname != NULL) {
1220 argv[argc++] = mandoc_strdup("-t");
1221 argv[argc++] = tag_files->tagname;
1222 use_ofn = 0;
1223 }
1224 }
1225 }
1226 #endif
1227 if (use_ofn)
1228 argv[argc++] = tag_files->ofn;
1229 argv[argc] = NULL;
1230
1231 switch (pager_pid = fork()) {
1232 case -1:
1233 err((int)MANDOCLEVEL_SYSERR, "fork");
1234 case 0:
1235 break;
1236 default:
1237 (void)setpgid(pager_pid, 0);
1238 (void)tcsetpgrp(tag_files->ofd, pager_pid);
1239 #if HAVE_PLEDGE
1240 if (pledge("stdio rpath tmppath tty proc", NULL) == -1)
1241 err((int)MANDOCLEVEL_SYSERR, "pledge");
1242 #endif
1243 tag_files->pager_pid = pager_pid;
1244 return pager_pid;
1245 }
1246
1247 /* The child process becomes the pager. */
1248
1249 if (dup2(tag_files->ofd, STDOUT_FILENO) == -1)
1250 err((int)MANDOCLEVEL_SYSERR, "pager stdout");
1251 close(tag_files->ofd);
1252 assert(tag_files->tfd == -1);
1253
1254 /* Do not start the pager before controlling the terminal. */
1255
1256 while (tcgetpgrp(STDOUT_FILENO) != getpid())
1257 nanosleep(&timeout, NULL);
1258
1259 execvp(argv[0], argv);
1260 err((int)MANDOCLEVEL_SYSERR, "exec %s", argv[0]);
1261 }