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