]> git.cameronkatri.com Git - mandoc.git/blob - main.c
Do not hardcode "makewhatis" in a warning message, use BINM_MAKEWHATIS.
[mandoc.git] / main.c
1 /* $Id: main.c,v 1.266 2016/05/18 22:36:00 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 #include <signal.h>
34 #include <stdio.h>
35 #include <stdint.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <time.h>
39 #include <unistd.h>
40
41 #include "mandoc_aux.h"
42 #include "mandoc.h"
43 #include "roff.h"
44 #include "mdoc.h"
45 #include "man.h"
46 #include "tag.h"
47 #include "main.h"
48 #include "manconf.h"
49 #include "mansearch.h"
50
51 #if !defined(__GNUC__) || (__GNUC__ < 2)
52 # if !defined(lint)
53 # define __attribute__(x)
54 # endif
55 #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
56
57 enum outmode {
58 OUTMODE_DEF = 0,
59 OUTMODE_FLN,
60 OUTMODE_LST,
61 OUTMODE_ALL,
62 OUTMODE_INT,
63 OUTMODE_ONE
64 };
65
66 enum outt {
67 OUTT_ASCII = 0, /* -Tascii */
68 OUTT_LOCALE, /* -Tlocale */
69 OUTT_UTF8, /* -Tutf8 */
70 OUTT_TREE, /* -Ttree */
71 OUTT_MAN, /* -Tman */
72 OUTT_HTML, /* -Thtml */
73 OUTT_LINT, /* -Tlint */
74 OUTT_PS, /* -Tps */
75 OUTT_PDF /* -Tpdf */
76 };
77
78 struct curparse {
79 struct mparse *mp;
80 enum mandoclevel wlevel; /* ignore messages below this */
81 int wstop; /* stop after a file with a warning */
82 enum outt outtype; /* which output to use */
83 void *outdata; /* data for output */
84 struct manoutput *outopts; /* output options */
85 };
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 #if HAVE_SQLITE3
96 int mandocdb(int, char**);
97 #endif
98 static int moptions(int *, char *);
99 static void mmsg(enum mandocerr, enum mandoclevel,
100 const char *, int, int, const char *);
101 static void parse(struct curparse *, int, const char *);
102 static void passthrough(const char *, int, int);
103 static pid_t spawn_pager(struct tag_files *);
104 static int toptions(struct curparse *, char *);
105 static void usage(enum argmode) __attribute__((noreturn));
106 static int woptions(struct curparse *, char *);
107
108 static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
109 static char help_arg[] = "help";
110 static char *help_argv[] = {help_arg, NULL};
111 static enum mandoclevel rc;
112
113
114 int
115 main(int argc, char *argv[])
116 {
117 struct manconf conf;
118 struct curparse curp;
119 struct mansearch search;
120 struct tag_files *tag_files;
121 const char *progname;
122 char *auxpaths;
123 char *defos;
124 unsigned char *uc;
125 struct manpage *res, *resp;
126 char *conf_file, *defpaths;
127 const char *sec;
128 size_t i, sz;
129 int prio, best_prio;
130 enum outmode outmode;
131 int fd;
132 int show_usage;
133 int options;
134 int use_pager;
135 int status, signum;
136 int c;
137 pid_t pager_pid, tc_pgid, man_pgid, pid;
138
139 #if HAVE_PROGNAME
140 progname = getprogname();
141 #else
142 if (argc < 1)
143 progname = mandoc_strdup("mandoc");
144 else if ((progname = strrchr(argv[0], '/')) == NULL)
145 progname = argv[0];
146 else
147 ++progname;
148 setprogname(progname);
149 #endif
150
151 #if HAVE_SQLITE3
152 if (strncmp(progname, "mandocdb", 8) == 0 ||
153 strcmp(progname, BINM_MAKEWHATIS) == 0)
154 return mandocdb(argc, argv);
155 #endif
156
157 #if HAVE_PLEDGE
158 if (pledge("stdio rpath tmppath tty proc exec flock", NULL) == -1)
159 err((int)MANDOCLEVEL_SYSERR, "pledge");
160 #endif
161
162 /* Search options. */
163
164 memset(&conf, 0, sizeof(conf));
165 conf_file = defpaths = NULL;
166 auxpaths = NULL;
167
168 memset(&search, 0, sizeof(struct mansearch));
169 search.outkey = "Nd";
170
171 if (strcmp(progname, BINM_MAN) == 0)
172 search.argmode = ARG_NAME;
173 else if (strcmp(progname, BINM_APROPOS) == 0)
174 search.argmode = ARG_EXPR;
175 else if (strcmp(progname, BINM_WHATIS) == 0)
176 search.argmode = ARG_WORD;
177 else if (strncmp(progname, "help", 4) == 0)
178 search.argmode = ARG_NAME;
179 else
180 search.argmode = ARG_FILE;
181
182 /* Parser and formatter options. */
183
184 memset(&curp, 0, sizeof(struct curparse));
185 curp.outtype = OUTT_LOCALE;
186 curp.wlevel = MANDOCLEVEL_BADARG;
187 curp.outopts = &conf.output;
188 options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
189 defos = NULL;
190
191 use_pager = 1;
192 tag_files = NULL;
193 show_usage = 0;
194 outmode = OUTMODE_DEF;
195
196 while (-1 != (c = getopt(argc, argv,
197 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
198 switch (c) {
199 case 'a':
200 outmode = OUTMODE_ALL;
201 break;
202 case 'C':
203 conf_file = optarg;
204 break;
205 case 'c':
206 use_pager = 0;
207 break;
208 case 'f':
209 search.argmode = ARG_WORD;
210 break;
211 case 'h':
212 conf.output.synopsisonly = 1;
213 use_pager = 0;
214 outmode = OUTMODE_ALL;
215 break;
216 case 'I':
217 if (strncmp(optarg, "os=", 3)) {
218 warnx("-I %s: Bad argument", optarg);
219 return (int)MANDOCLEVEL_BADARG;
220 }
221 if (defos) {
222 warnx("-I %s: Duplicate argument", optarg);
223 return (int)MANDOCLEVEL_BADARG;
224 }
225 defos = mandoc_strdup(optarg + 3);
226 break;
227 case 'i':
228 outmode = OUTMODE_INT;
229 break;
230 case 'K':
231 if ( ! koptions(&options, optarg))
232 return (int)MANDOCLEVEL_BADARG;
233 break;
234 case 'k':
235 search.argmode = ARG_EXPR;
236 break;
237 case 'l':
238 search.argmode = ARG_FILE;
239 outmode = OUTMODE_ALL;
240 break;
241 case 'M':
242 defpaths = optarg;
243 break;
244 case 'm':
245 auxpaths = optarg;
246 break;
247 case 'O':
248 search.outkey = optarg;
249 while (optarg != NULL)
250 manconf_output(&conf.output,
251 strsep(&optarg, ","));
252 break;
253 case 'S':
254 search.arch = optarg;
255 break;
256 case 's':
257 search.sec = optarg;
258 break;
259 case 'T':
260 if ( ! toptions(&curp, optarg))
261 return (int)MANDOCLEVEL_BADARG;
262 break;
263 case 'W':
264 if ( ! woptions(&curp, optarg))
265 return (int)MANDOCLEVEL_BADARG;
266 break;
267 case 'w':
268 outmode = OUTMODE_FLN;
269 break;
270 default:
271 show_usage = 1;
272 break;
273 }
274 }
275
276 if (show_usage)
277 usage(search.argmode);
278
279 /* Postprocess options. */
280
281 if (outmode == OUTMODE_DEF) {
282 switch (search.argmode) {
283 case ARG_FILE:
284 outmode = OUTMODE_ALL;
285 use_pager = 0;
286 break;
287 case ARG_NAME:
288 outmode = OUTMODE_ONE;
289 break;
290 default:
291 outmode = OUTMODE_LST;
292 break;
293 }
294 }
295
296 if (outmode == OUTMODE_FLN ||
297 outmode == OUTMODE_LST ||
298 !isatty(STDOUT_FILENO))
299 use_pager = 0;
300
301 #if HAVE_PLEDGE
302 if (!use_pager)
303 if (pledge("stdio rpath flock", NULL) == -1)
304 err((int)MANDOCLEVEL_SYSERR, "pledge");
305 #endif
306
307 /* Parse arguments. */
308
309 if (argc > 0) {
310 argc -= optind;
311 argv += optind;
312 }
313 resp = NULL;
314
315 /*
316 * Quirks for help(1)
317 * and for a man(1) section argument without -s.
318 */
319
320 if (search.argmode == ARG_NAME) {
321 if (*progname == 'h') {
322 if (argc == 0) {
323 argv = help_argv;
324 argc = 1;
325 }
326 } else if (argc > 1 &&
327 ((uc = (unsigned char *)argv[0]) != NULL) &&
328 ((isdigit(uc[0]) && (uc[1] == '\0' ||
329 (isalpha(uc[1]) && uc[2] == '\0'))) ||
330 (uc[0] == 'n' && uc[1] == '\0'))) {
331 search.sec = (char *)uc;
332 argv++;
333 argc--;
334 }
335 if (search.arch == NULL)
336 search.arch = getenv("MACHINE");
337 #ifdef MACHINE
338 if (search.arch == NULL)
339 search.arch = MACHINE;
340 #endif
341 }
342
343 rc = MANDOCLEVEL_OK;
344
345 /* man(1), whatis(1), apropos(1) */
346
347 if (search.argmode != ARG_FILE) {
348 if (argc == 0)
349 usage(search.argmode);
350
351 if (search.argmode == ARG_NAME &&
352 outmode == OUTMODE_ONE)
353 search.firstmatch = 1;
354
355 /* Access the mandoc database. */
356
357 manconf_parse(&conf, conf_file, defpaths, auxpaths);
358 #if HAVE_SQLITE3
359 mansearch_setup(1);
360 if ( ! mansearch(&search, &conf.manpath,
361 argc, argv, &res, &sz))
362 usage(search.argmode);
363 #else
364 if (search.argmode != ARG_NAME) {
365 fputs("mandoc: database support not compiled in\n",
366 stderr);
367 return (int)MANDOCLEVEL_BADARG;
368 }
369 sz = 0;
370 #endif
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 ascii_sepline(curp.outdata);
483 } else if (rc < MANDOCLEVEL_ERROR)
484 rc = MANDOCLEVEL_ERROR;
485
486 if (MANDOCLEVEL_OK != rc && curp.wstop)
487 break;
488
489 if (resp != NULL)
490 resp++;
491 else
492 argv++;
493 if (--argc)
494 mparse_reset(curp.mp);
495 }
496
497 if (curp.outdata != NULL) {
498 switch (curp.outtype) {
499 case OUTT_HTML:
500 html_free(curp.outdata);
501 break;
502 case OUTT_UTF8:
503 case OUTT_LOCALE:
504 case OUTT_ASCII:
505 ascii_free(curp.outdata);
506 break;
507 case OUTT_PDF:
508 case OUTT_PS:
509 pspdf_free(curp.outdata);
510 break;
511 default:
512 break;
513 }
514 }
515 mparse_free(curp.mp);
516 mchars_free();
517
518 out:
519 if (search.argmode != ARG_FILE) {
520 manconf_free(&conf);
521 #if HAVE_SQLITE3
522 mansearch_free(res, sz);
523 mansearch_setup(0);
524 #endif
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(STDIN_FILENO);
547 if (tc_pgid != man_pgid) {
548 if (tc_pgid == pager_pid) {
549 (void)tcsetpgrp(STDIN_FILENO,
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(STDIN_FILENO, 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 form, globres;
629
630 form = FORM_SRC;
631 mandoc_asprintf(&file, "%s/man%s/%s.%s",
632 paths->paths[ipath], sec, name, sec);
633 if (access(file, R_OK) != -1)
634 goto found;
635 free(file);
636
637 mandoc_asprintf(&file, "%s/cat%s/%s.0",
638 paths->paths[ipath], sec, name);
639 if (access(file, R_OK) != -1) {
640 form = FORM_CAT;
641 goto found;
642 }
643 free(file);
644
645 if (arch != NULL) {
646 mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
647 paths->paths[ipath], sec, arch, name, sec);
648 if (access(file, R_OK) != -1)
649 goto found;
650 free(file);
651 }
652
653 mandoc_asprintf(&file, "%s/man%s/%s.[01-9]*",
654 paths->paths[ipath], sec, name);
655 globres = glob(file, 0, NULL, &globinfo);
656 if (globres != 0 && globres != GLOB_NOMATCH)
657 warn("%s: glob", file);
658 free(file);
659 if (globres == 0)
660 file = mandoc_strdup(*globinfo.gl_pathv);
661 globfree(&globinfo);
662 if (globres != 0)
663 return 0;
664
665 found:
666 #if HAVE_SQLITE3
667 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
668 name, sec, BINM_MAKEWHATIS, paths->paths[ipath]);
669 #endif
670 *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
671 page = *res + (*ressz - 1);
672 page->file = file;
673 page->names = NULL;
674 page->output = NULL;
675 page->ipath = ipath;
676 page->bits = NAME_FILE & NAME_MASK;
677 page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
678 page->form = form;
679 return 1;
680 }
681
682 static void
683 fs_search(const struct mansearch *cfg, const struct manpaths *paths,
684 int argc, char **argv, struct manpage **res, size_t *ressz)
685 {
686 const char *const sections[] =
687 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
688 const size_t nsec = sizeof(sections)/sizeof(sections[0]);
689
690 size_t ipath, isec, lastsz;
691
692 assert(cfg->argmode == ARG_NAME);
693
694 *res = NULL;
695 *ressz = lastsz = 0;
696 while (argc) {
697 for (ipath = 0; ipath < paths->sz; ipath++) {
698 if (cfg->sec != NULL) {
699 if (fs_lookup(paths, ipath, cfg->sec,
700 cfg->arch, *argv, res, ressz) &&
701 cfg->firstmatch)
702 return;
703 } else for (isec = 0; isec < nsec; isec++)
704 if (fs_lookup(paths, ipath, sections[isec],
705 cfg->arch, *argv, res, ressz) &&
706 cfg->firstmatch)
707 return;
708 }
709 if (*ressz == lastsz)
710 warnx("No entry for %s in the manual.", *argv);
711 lastsz = *ressz;
712 argv++;
713 argc--;
714 }
715 }
716
717 static void
718 parse(struct curparse *curp, int fd, const char *file)
719 {
720 enum mandoclevel rctmp;
721 struct roff_man *man;
722
723 /* Begin by parsing the file itself. */
724
725 assert(file);
726 assert(fd >= 0);
727
728 rctmp = mparse_readfd(curp->mp, fd, file);
729 if (fd != STDIN_FILENO)
730 close(fd);
731 if (rc < rctmp)
732 rc = rctmp;
733
734 /*
735 * With -Wstop and warnings or errors of at least the requested
736 * level, do not produce output.
737 */
738
739 if (rctmp != MANDOCLEVEL_OK && curp->wstop)
740 return;
741
742 /* If unset, allocate output dev now (if applicable). */
743
744 if (curp->outdata == NULL) {
745 switch (curp->outtype) {
746 case OUTT_HTML:
747 curp->outdata = html_alloc(curp->outopts);
748 break;
749 case OUTT_UTF8:
750 curp->outdata = utf8_alloc(curp->outopts);
751 break;
752 case OUTT_LOCALE:
753 curp->outdata = locale_alloc(curp->outopts);
754 break;
755 case OUTT_ASCII:
756 curp->outdata = ascii_alloc(curp->outopts);
757 break;
758 case OUTT_PDF:
759 curp->outdata = pdf_alloc(curp->outopts);
760 break;
761 case OUTT_PS:
762 curp->outdata = ps_alloc(curp->outopts);
763 break;
764 default:
765 break;
766 }
767 }
768
769 mparse_result(curp->mp, &man, NULL);
770
771 /* Execute the out device, if it exists. */
772
773 if (man == NULL)
774 return;
775 if (man->macroset == MACROSET_MDOC) {
776 mdoc_validate(man);
777 switch (curp->outtype) {
778 case OUTT_HTML:
779 html_mdoc(curp->outdata, man);
780 break;
781 case OUTT_TREE:
782 tree_mdoc(curp->outdata, man);
783 break;
784 case OUTT_MAN:
785 man_mdoc(curp->outdata, man);
786 break;
787 case OUTT_PDF:
788 case OUTT_ASCII:
789 case OUTT_UTF8:
790 case OUTT_LOCALE:
791 case OUTT_PS:
792 terminal_mdoc(curp->outdata, man);
793 break;
794 default:
795 break;
796 }
797 }
798 if (man->macroset == MACROSET_MAN) {
799 man_validate(man);
800 switch (curp->outtype) {
801 case OUTT_HTML:
802 html_man(curp->outdata, man);
803 break;
804 case OUTT_TREE:
805 tree_man(curp->outdata, man);
806 break;
807 case OUTT_MAN:
808 man_man(curp->outdata, man);
809 break;
810 case OUTT_PDF:
811 case OUTT_ASCII:
812 case OUTT_UTF8:
813 case OUTT_LOCALE:
814 case OUTT_PS:
815 terminal_man(curp->outdata, man);
816 break;
817 default:
818 break;
819 }
820 }
821 }
822
823 static void
824 passthrough(const char *file, int fd, int synopsis_only)
825 {
826 const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
827 const char synr[] = "SYNOPSIS";
828
829 FILE *stream;
830 const char *syscall;
831 char *line, *cp;
832 size_t linesz;
833 int print;
834
835 line = NULL;
836 linesz = 0;
837
838 if ((stream = fdopen(fd, "r")) == NULL) {
839 close(fd);
840 syscall = "fdopen";
841 goto fail;
842 }
843
844 print = 0;
845 while (getline(&line, &linesz, stream) != -1) {
846 cp = line;
847 if (synopsis_only) {
848 if (print) {
849 if ( ! isspace((unsigned char)*cp))
850 goto done;
851 while (isspace((unsigned char)*cp))
852 cp++;
853 } else {
854 if (strcmp(cp, synb) == 0 ||
855 strcmp(cp, synr) == 0)
856 print = 1;
857 continue;
858 }
859 }
860 if (fputs(cp, stdout)) {
861 fclose(stream);
862 syscall = "fputs";
863 goto fail;
864 }
865 }
866
867 if (ferror(stream)) {
868 fclose(stream);
869 syscall = "getline";
870 goto fail;
871 }
872
873 done:
874 free(line);
875 fclose(stream);
876 return;
877
878 fail:
879 free(line);
880 warn("%s: SYSERR: %s", file, syscall);
881 if (rc < MANDOCLEVEL_SYSERR)
882 rc = MANDOCLEVEL_SYSERR;
883 }
884
885 static int
886 koptions(int *options, char *arg)
887 {
888
889 if ( ! strcmp(arg, "utf-8")) {
890 *options |= MPARSE_UTF8;
891 *options &= ~MPARSE_LATIN1;
892 } else if ( ! strcmp(arg, "iso-8859-1")) {
893 *options |= MPARSE_LATIN1;
894 *options &= ~MPARSE_UTF8;
895 } else if ( ! strcmp(arg, "us-ascii")) {
896 *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
897 } else {
898 warnx("-K %s: Bad argument", arg);
899 return 0;
900 }
901 return 1;
902 }
903
904 static int
905 moptions(int *options, char *arg)
906 {
907
908 if (arg == NULL)
909 /* nothing to do */;
910 else if (0 == strcmp(arg, "doc"))
911 *options |= MPARSE_MDOC;
912 else if (0 == strcmp(arg, "andoc"))
913 /* nothing to do */;
914 else if (0 == strcmp(arg, "an"))
915 *options |= MPARSE_MAN;
916 else {
917 warnx("-m %s: Bad argument", arg);
918 return 0;
919 }
920
921 return 1;
922 }
923
924 static int
925 toptions(struct curparse *curp, char *arg)
926 {
927
928 if (0 == strcmp(arg, "ascii"))
929 curp->outtype = OUTT_ASCII;
930 else if (0 == strcmp(arg, "lint")) {
931 curp->outtype = OUTT_LINT;
932 curp->wlevel = MANDOCLEVEL_WARNING;
933 } else if (0 == strcmp(arg, "tree"))
934 curp->outtype = OUTT_TREE;
935 else if (0 == strcmp(arg, "man"))
936 curp->outtype = OUTT_MAN;
937 else if (0 == strcmp(arg, "html"))
938 curp->outtype = OUTT_HTML;
939 else if (0 == strcmp(arg, "utf8"))
940 curp->outtype = OUTT_UTF8;
941 else if (0 == strcmp(arg, "locale"))
942 curp->outtype = OUTT_LOCALE;
943 else if (0 == strcmp(arg, "xhtml"))
944 curp->outtype = OUTT_HTML;
945 else if (0 == strcmp(arg, "ps"))
946 curp->outtype = OUTT_PS;
947 else if (0 == strcmp(arg, "pdf"))
948 curp->outtype = OUTT_PDF;
949 else {
950 warnx("-T %s: Bad argument", arg);
951 return 0;
952 }
953
954 return 1;
955 }
956
957 static int
958 woptions(struct curparse *curp, char *arg)
959 {
960 char *v, *o;
961 const char *toks[7];
962
963 toks[0] = "stop";
964 toks[1] = "all";
965 toks[2] = "warning";
966 toks[3] = "error";
967 toks[4] = "unsupp";
968 toks[5] = "fatal";
969 toks[6] = NULL;
970
971 while (*arg) {
972 o = arg;
973 switch (getsubopt(&arg, UNCONST(toks), &v)) {
974 case 0:
975 curp->wstop = 1;
976 break;
977 case 1:
978 case 2:
979 curp->wlevel = MANDOCLEVEL_WARNING;
980 break;
981 case 3:
982 curp->wlevel = MANDOCLEVEL_ERROR;
983 break;
984 case 4:
985 curp->wlevel = MANDOCLEVEL_UNSUPP;
986 break;
987 case 5:
988 curp->wlevel = MANDOCLEVEL_BADARG;
989 break;
990 default:
991 warnx("-W %s: Bad argument", o);
992 return 0;
993 }
994 }
995
996 return 1;
997 }
998
999 static void
1000 mmsg(enum mandocerr t, enum mandoclevel lvl,
1001 const char *file, int line, int col, const char *msg)
1002 {
1003 const char *mparse_msg;
1004
1005 fprintf(stderr, "%s: %s:", getprogname(), file);
1006
1007 if (line)
1008 fprintf(stderr, "%d:%d:", line, col + 1);
1009
1010 fprintf(stderr, " %s", mparse_strlevel(lvl));
1011
1012 if (NULL != (mparse_msg = mparse_strerror(t)))
1013 fprintf(stderr, ": %s", mparse_msg);
1014
1015 if (msg)
1016 fprintf(stderr, ": %s", msg);
1017
1018 fputc('\n', stderr);
1019 }
1020
1021 static pid_t
1022 spawn_pager(struct tag_files *tag_files)
1023 {
1024 const struct timespec timeout = { 0, 100000000 }; /* 0.1s */
1025 #define MAX_PAGER_ARGS 16
1026 char *argv[MAX_PAGER_ARGS];
1027 const char *pager;
1028 char *cp;
1029 size_t cmdlen;
1030 int argc;
1031 pid_t pager_pid;
1032
1033 pager = getenv("MANPAGER");
1034 if (pager == NULL || *pager == '\0')
1035 pager = getenv("PAGER");
1036 if (pager == NULL || *pager == '\0')
1037 pager = "more -s";
1038 cp = mandoc_strdup(pager);
1039
1040 /*
1041 * Parse the pager command into words.
1042 * Intentionally do not do anything fancy here.
1043 */
1044
1045 argc = 0;
1046 while (argc + 4 < MAX_PAGER_ARGS) {
1047 argv[argc++] = cp;
1048 cp = strchr(cp, ' ');
1049 if (cp == NULL)
1050 break;
1051 *cp++ = '\0';
1052 while (*cp == ' ')
1053 cp++;
1054 if (*cp == '\0')
1055 break;
1056 }
1057
1058 /* For more(1) and less(1), use the tag file. */
1059
1060 if ((cmdlen = strlen(argv[0])) >= 4) {
1061 cp = argv[0] + cmdlen - 4;
1062 if (strcmp(cp, "less") == 0 || strcmp(cp, "more") == 0) {
1063 argv[argc++] = mandoc_strdup("-T");
1064 argv[argc++] = tag_files->tfn;
1065 }
1066 }
1067 argv[argc++] = tag_files->ofn;
1068 argv[argc] = NULL;
1069
1070 switch (pager_pid = fork()) {
1071 case -1:
1072 err((int)MANDOCLEVEL_SYSERR, "fork");
1073 case 0:
1074 break;
1075 default:
1076 (void)setpgid(pager_pid, 0);
1077 (void)tcsetpgrp(STDIN_FILENO, pager_pid);
1078 #if HAVE_PLEDGE
1079 if (pledge("stdio rpath tmppath tty proc", NULL) == -1)
1080 err((int)MANDOCLEVEL_SYSERR, "pledge");
1081 #endif
1082 tag_files->pager_pid = pager_pid;
1083 return pager_pid;
1084 }
1085
1086 /* The child process becomes the pager. */
1087
1088 if (dup2(tag_files->ofd, STDOUT_FILENO) == -1)
1089 err((int)MANDOCLEVEL_SYSERR, "pager stdout");
1090 close(tag_files->ofd);
1091 close(tag_files->tfd);
1092
1093 /* Do not start the pager before controlling the terminal. */
1094
1095 while (tcgetpgrp(STDIN_FILENO) != getpid())
1096 nanosleep(&timeout, NULL);
1097
1098 execvp(argv[0], argv);
1099 err((int)MANDOCLEVEL_SYSERR, "exec %s", argv[0]);
1100 }