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