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