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