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