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