]> git.cameronkatri.com Git - mandoc.git/blob - main.c
Massively reduce the amount of text, cutting it down to what is needed
[mandoc.git] / main.c
1 /* $Id: main.c,v 1.307 2018/08/09 17:30:36 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2012, 2014-2018 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/ioctl.h>
23 #include <sys/param.h> /* MACHINE */
24 #include <sys/termios.h>
25 #include <sys/wait.h>
26
27 #include <assert.h>
28 #include <ctype.h>
29 #if HAVE_ERR
30 #include <err.h>
31 #endif
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <glob.h>
35 #if HAVE_SANDBOX_INIT
36 #include <sandbox.h>
37 #endif
38 #include <signal.h>
39 #include <stdio.h>
40 #include <stdint.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <time.h>
44 #include <unistd.h>
45
46 #include "mandoc_aux.h"
47 #include "mandoc.h"
48 #include "mandoc_xr.h"
49 #include "roff.h"
50 #include "mdoc.h"
51 #include "man.h"
52 #include "tag.h"
53 #include "main.h"
54 #include "manconf.h"
55 #include "mansearch.h"
56
57 enum outmode {
58 OUTMODE_DEF = 0,
59 OUTMODE_FLN,
60 OUTMODE_LST,
61 OUTMODE_ALL,
62 OUTMODE_ONE
63 };
64
65 enum outt {
66 OUTT_ASCII = 0, /* -Tascii */
67 OUTT_LOCALE, /* -Tlocale */
68 OUTT_UTF8, /* -Tutf8 */
69 OUTT_TREE, /* -Ttree */
70 OUTT_MAN, /* -Tman */
71 OUTT_HTML, /* -Thtml */
72 OUTT_MARKDOWN, /* -Tmarkdown */
73 OUTT_LINT, /* -Tlint */
74 OUTT_PS, /* -Tps */
75 OUTT_PDF /* -Tpdf */
76 };
77
78 struct curparse {
79 struct mparse *mp;
80 struct manoutput *outopts; /* output options */
81 void *outdata; /* data for output */
82 char *os_s; /* operating system for display */
83 int wstop; /* stop after a file with a warning */
84 enum mandocerr mmin; /* ignore messages below this */
85 enum mandoc_os os_e; /* check base system conventions */
86 enum outt outtype; /* which output to use */
87 };
88
89
90 int mandocdb(int, char *[]);
91
92 static void check_xr(const char *);
93 static int fs_lookup(const struct manpaths *,
94 size_t ipath, const char *,
95 const char *, const char *,
96 struct manpage **, size_t *);
97 static int fs_search(const struct mansearch *,
98 const struct manpaths *, int, char**,
99 struct manpage **, size_t *);
100 static int koptions(int *, char *);
101 static void moptions(int *, char *);
102 static void mmsg(enum mandocerr, enum mandoclevel,
103 const char *, int, int, const char *);
104 static void outdata_alloc(struct curparse *);
105 static void parse(struct curparse *, int, const char *);
106 static void passthrough(const char *, int, int);
107 static pid_t spawn_pager(struct tag_files *);
108 static int toptions(struct curparse *, char *);
109 static void usage(enum argmode) __attribute__((__noreturn__));
110 static int woptions(struct curparse *, char *);
111
112 static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
113 static char help_arg[] = "help";
114 static char *help_argv[] = {help_arg, NULL};
115 static enum mandoclevel rc;
116 static FILE *mmsg_stream;
117
118
119 int
120 main(int argc, char *argv[])
121 {
122 struct manconf conf;
123 struct mansearch search;
124 struct curparse curp;
125 struct winsize ws;
126 struct tag_files *tag_files;
127 struct manpage *res, *resp;
128 const char *progname, *sec, *thisarg;
129 char *conf_file, *defpaths, *auxpaths;
130 char *oarg;
131 unsigned char *uc;
132 size_t i, sz;
133 int prio, best_prio;
134 enum outmode outmode;
135 int fd, startdir;
136 int show_usage;
137 int options;
138 int use_pager;
139 int status, signum;
140 int c;
141 pid_t pager_pid, tc_pgid, man_pgid, pid;
142
143 #if HAVE_PROGNAME
144 progname = getprogname();
145 #else
146 if (argc < 1)
147 progname = mandoc_strdup("mandoc");
148 else if ((progname = strrchr(argv[0], '/')) == NULL)
149 progname = argv[0];
150 else
151 ++progname;
152 setprogname(progname);
153 #endif
154
155 if (strncmp(progname, "mandocdb", 8) == 0 ||
156 strcmp(progname, BINM_MAKEWHATIS) == 0)
157 return mandocdb(argc, argv);
158
159 #if HAVE_PLEDGE
160 if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1)
161 err((int)MANDOCLEVEL_SYSERR, "pledge");
162 #endif
163
164 #if HAVE_SANDBOX_INIT
165 if (sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, NULL) == -1)
166 errx((int)MANDOCLEVEL_SYSERR, "sandbox_init");
167 #endif
168
169 /* Search options. */
170
171 memset(&conf, 0, sizeof(conf));
172 conf_file = defpaths = NULL;
173 auxpaths = NULL;
174
175 memset(&search, 0, sizeof(struct mansearch));
176 search.outkey = "Nd";
177 oarg = NULL;
178
179 if (strcmp(progname, BINM_MAN) == 0)
180 search.argmode = ARG_NAME;
181 else if (strcmp(progname, BINM_APROPOS) == 0)
182 search.argmode = ARG_EXPR;
183 else if (strcmp(progname, BINM_WHATIS) == 0)
184 search.argmode = ARG_WORD;
185 else if (strncmp(progname, "help", 4) == 0)
186 search.argmode = ARG_NAME;
187 else
188 search.argmode = ARG_FILE;
189
190 /* Parser and formatter options. */
191
192 memset(&curp, 0, sizeof(struct curparse));
193 curp.outtype = OUTT_LOCALE;
194 curp.mmin = MANDOCERR_MAX;
195 curp.outopts = &conf.output;
196 options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
197 mmsg_stream = stderr;
198
199 use_pager = 1;
200 tag_files = NULL;
201 show_usage = 0;
202 outmode = OUTMODE_DEF;
203
204 while ((c = getopt(argc, argv,
205 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w")) != -1) {
206 if (c == 'i' && search.argmode == ARG_EXPR) {
207 optind--;
208 break;
209 }
210 switch (c) {
211 case 'a':
212 outmode = OUTMODE_ALL;
213 break;
214 case 'C':
215 conf_file = optarg;
216 break;
217 case 'c':
218 use_pager = 0;
219 break;
220 case 'f':
221 search.argmode = ARG_WORD;
222 break;
223 case 'h':
224 conf.output.synopsisonly = 1;
225 use_pager = 0;
226 outmode = OUTMODE_ALL;
227 break;
228 case 'I':
229 if (strncmp(optarg, "os=", 3)) {
230 warnx("-I %s: Bad argument", optarg);
231 return (int)MANDOCLEVEL_BADARG;
232 }
233 if (curp.os_s != NULL) {
234 warnx("-I %s: Duplicate argument", optarg);
235 return (int)MANDOCLEVEL_BADARG;
236 }
237 curp.os_s = mandoc_strdup(optarg + 3);
238 break;
239 case 'K':
240 if ( ! koptions(&options, optarg))
241 return (int)MANDOCLEVEL_BADARG;
242 break;
243 case 'k':
244 search.argmode = ARG_EXPR;
245 break;
246 case 'l':
247 search.argmode = ARG_FILE;
248 outmode = OUTMODE_ALL;
249 break;
250 case 'M':
251 defpaths = optarg;
252 break;
253 case 'm':
254 auxpaths = optarg;
255 break;
256 case 'O':
257 oarg = optarg;
258 break;
259 case 'S':
260 search.arch = optarg;
261 break;
262 case 's':
263 search.sec = optarg;
264 break;
265 case 'T':
266 if ( ! toptions(&curp, optarg))
267 return (int)MANDOCLEVEL_BADARG;
268 break;
269 case 'W':
270 if ( ! woptions(&curp, optarg))
271 return (int)MANDOCLEVEL_BADARG;
272 break;
273 case 'w':
274 outmode = OUTMODE_FLN;
275 break;
276 default:
277 show_usage = 1;
278 break;
279 }
280 }
281
282 if (show_usage)
283 usage(search.argmode);
284
285 /* Postprocess options. */
286
287 if (outmode == OUTMODE_DEF) {
288 switch (search.argmode) {
289 case ARG_FILE:
290 outmode = OUTMODE_ALL;
291 use_pager = 0;
292 break;
293 case ARG_NAME:
294 outmode = OUTMODE_ONE;
295 break;
296 default:
297 outmode = OUTMODE_LST;
298 break;
299 }
300 }
301
302 if (oarg != NULL) {
303 if (outmode == OUTMODE_LST)
304 search.outkey = oarg;
305 else {
306 while (oarg != NULL) {
307 thisarg = oarg;
308 if (manconf_output(&conf.output,
309 strsep(&oarg, ","), 0) == 0)
310 continue;
311 warnx("-O %s: Bad argument", thisarg);
312 return (int)MANDOCLEVEL_BADARG;
313 }
314 }
315 }
316
317 if (outmode == OUTMODE_FLN ||
318 outmode == OUTMODE_LST ||
319 !isatty(STDOUT_FILENO))
320 use_pager = 0;
321
322 if (use_pager &&
323 (conf.output.width == 0 || conf.output.indent == 0) &&
324 ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1 &&
325 ws.ws_col > 1) {
326 if (conf.output.width == 0 && ws.ws_col < 79)
327 conf.output.width = ws.ws_col - 1;
328 if (conf.output.indent == 0 && ws.ws_col < 66)
329 conf.output.indent = 3;
330 }
331
332 #if HAVE_PLEDGE
333 if (!use_pager)
334 if (pledge("stdio rpath", NULL) == -1)
335 err((int)MANDOCLEVEL_SYSERR, "pledge");
336 #endif
337
338 /* Parse arguments. */
339
340 if (argc > 0) {
341 argc -= optind;
342 argv += optind;
343 }
344 resp = NULL;
345
346 /*
347 * Quirks for help(1)
348 * and for a man(1) section argument without -s.
349 */
350
351 if (search.argmode == ARG_NAME) {
352 if (*progname == 'h') {
353 if (argc == 0) {
354 argv = help_argv;
355 argc = 1;
356 }
357 } else if (argc > 1 &&
358 ((uc = (unsigned char *)argv[0]) != NULL) &&
359 ((isdigit(uc[0]) && (uc[1] == '\0' ||
360 (isalpha(uc[1]) && uc[2] == '\0'))) ||
361 (uc[0] == 'n' && uc[1] == '\0'))) {
362 search.sec = (char *)uc;
363 argv++;
364 argc--;
365 }
366 if (search.arch == NULL)
367 search.arch = getenv("MACHINE");
368 #ifdef MACHINE
369 if (search.arch == NULL)
370 search.arch = MACHINE;
371 #endif
372 }
373
374 rc = MANDOCLEVEL_OK;
375
376 /* man(1), whatis(1), apropos(1) */
377
378 if (search.argmode != ARG_FILE) {
379 if (search.argmode == ARG_NAME &&
380 outmode == OUTMODE_ONE)
381 search.firstmatch = 1;
382
383 /* Access the mandoc database. */
384
385 manconf_parse(&conf, conf_file, defpaths, auxpaths);
386 if ( ! mansearch(&search, &conf.manpath,
387 argc, argv, &res, &sz))
388 usage(search.argmode);
389
390 if (sz == 0 && search.argmode == ARG_NAME)
391 fs_search(&search, &conf.manpath,
392 argc, argv, &res, &sz);
393
394 if (search.argmode == ARG_NAME) {
395 for (c = 0; c < argc; c++) {
396 if (strchr(argv[c], '/') == NULL)
397 continue;
398 if (access(argv[c], R_OK) == -1) {
399 warn("%s", argv[c]);
400 continue;
401 }
402 res = mandoc_reallocarray(res,
403 sz + 1, sizeof(*res));
404 res[sz].file = mandoc_strdup(argv[c]);
405 res[sz].names = NULL;
406 res[sz].output = NULL;
407 res[sz].ipath = SIZE_MAX;
408 res[sz].bits = 0;
409 res[sz].sec = 10;
410 res[sz].form = FORM_SRC;
411 sz++;
412 }
413 }
414
415 if (sz == 0) {
416 if (search.argmode != ARG_NAME)
417 warnx("nothing appropriate");
418 rc = MANDOCLEVEL_BADARG;
419 goto out;
420 }
421
422 /*
423 * For standard man(1) and -a output mode,
424 * prepare for copying filename pointers
425 * into the program parameter array.
426 */
427
428 if (outmode == OUTMODE_ONE) {
429 argc = 1;
430 best_prio = 20;
431 } else if (outmode == OUTMODE_ALL)
432 argc = (int)sz;
433
434 /* Iterate all matching manuals. */
435
436 resp = res;
437 for (i = 0; i < sz; i++) {
438 if (outmode == OUTMODE_FLN)
439 puts(res[i].file);
440 else if (outmode == OUTMODE_LST)
441 printf("%s - %s\n", res[i].names,
442 res[i].output == NULL ? "" :
443 res[i].output);
444 else if (outmode == OUTMODE_ONE) {
445 /* Search for the best section. */
446 sec = res[i].file;
447 sec += strcspn(sec, "123456789");
448 if (sec[0] == '\0')
449 continue;
450 prio = sec_prios[sec[0] - '1'];
451 if (sec[1] != '/')
452 prio += 10;
453 if (prio >= best_prio)
454 continue;
455 best_prio = prio;
456 resp = res + i;
457 }
458 }
459
460 /*
461 * For man(1), -a and -i output mode, fall through
462 * to the main mandoc(1) code iterating files
463 * and running the parsers on each of them.
464 */
465
466 if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
467 goto out;
468 }
469
470 /* mandoc(1) */
471
472 #if HAVE_PLEDGE
473 if (use_pager) {
474 if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1)
475 err((int)MANDOCLEVEL_SYSERR, "pledge");
476 } else {
477 if (pledge("stdio rpath", NULL) == -1)
478 err((int)MANDOCLEVEL_SYSERR, "pledge");
479 }
480 #endif
481
482 if (search.argmode == ARG_FILE)
483 moptions(&options, auxpaths);
484
485 mchars_alloc();
486 curp.mp = mparse_alloc(options, curp.mmin, mmsg,
487 curp.os_e, curp.os_s);
488
489 /*
490 * Conditionally start up the lookaside buffer before parsing.
491 */
492 if (OUTT_MAN == curp.outtype)
493 mparse_keep(curp.mp);
494
495 if (argc < 1) {
496 if (use_pager)
497 tag_files = tag_init();
498 parse(&curp, STDIN_FILENO, "<stdin>");
499 }
500
501 /*
502 * Remember the original working directory, if possible.
503 * This will be needed if some names on the command line
504 * are page names and some are relative file names.
505 * Do not error out if the current directory is not
506 * readable: Maybe it won't be needed after all.
507 */
508 startdir = open(".", O_RDONLY | O_DIRECTORY);
509
510 while (argc > 0) {
511
512 /*
513 * Changing directories is not needed in ARG_FILE mode.
514 * Do it on a best-effort basis. Even in case of
515 * failure, some functionality may still work.
516 */
517 if (resp != NULL) {
518 if (resp->ipath != SIZE_MAX)
519 (void)chdir(conf.manpath.paths[resp->ipath]);
520 else if (startdir != -1)
521 (void)fchdir(startdir);
522 }
523
524 fd = mparse_open(curp.mp, resp != NULL ? resp->file : *argv);
525 if (fd != -1) {
526 if (use_pager) {
527 tag_files = tag_init();
528 use_pager = 0;
529 }
530
531 if (resp == NULL)
532 parse(&curp, fd, *argv);
533 else if (resp->form == FORM_SRC)
534 parse(&curp, fd, resp->file);
535 else
536 passthrough(resp->file, fd,
537 conf.output.synopsisonly);
538
539 if (ferror(stdout)) {
540 if (tag_files != NULL) {
541 warn("%s", tag_files->ofn);
542 tag_unlink();
543 tag_files = NULL;
544 } else
545 warn("stdout");
546 rc = MANDOCLEVEL_SYSERR;
547 break;
548 }
549
550 if (argc > 1 && curp.outtype <= OUTT_UTF8) {
551 if (curp.outdata == NULL)
552 outdata_alloc(&curp);
553 terminal_sepline(curp.outdata);
554 }
555 } else if (rc < MANDOCLEVEL_ERROR)
556 rc = MANDOCLEVEL_ERROR;
557
558 if (MANDOCLEVEL_OK != rc && curp.wstop)
559 break;
560
561 if (resp != NULL)
562 resp++;
563 else
564 argv++;
565 if (--argc)
566 mparse_reset(curp.mp);
567 }
568 if (startdir != -1) {
569 (void)fchdir(startdir);
570 close(startdir);
571 }
572
573 if (curp.outdata != NULL) {
574 switch (curp.outtype) {
575 case OUTT_HTML:
576 html_free(curp.outdata);
577 break;
578 case OUTT_UTF8:
579 case OUTT_LOCALE:
580 case OUTT_ASCII:
581 ascii_free(curp.outdata);
582 break;
583 case OUTT_PDF:
584 case OUTT_PS:
585 pspdf_free(curp.outdata);
586 break;
587 default:
588 break;
589 }
590 }
591 mandoc_xr_free();
592 mparse_free(curp.mp);
593 mchars_free();
594
595 out:
596 if (search.argmode != ARG_FILE) {
597 manconf_free(&conf);
598 mansearch_free(res, sz);
599 }
600
601 free(curp.os_s);
602
603 /*
604 * When using a pager, finish writing both temporary files,
605 * fork it, wait for the user to close it, and clean up.
606 */
607
608 if (tag_files != NULL) {
609 fclose(stdout);
610 tag_write();
611 man_pgid = getpgid(0);
612 tag_files->tcpgid = man_pgid == getpid() ?
613 getpgid(getppid()) : man_pgid;
614 pager_pid = 0;
615 signum = SIGSTOP;
616 for (;;) {
617
618 /* Stop here until moved to the foreground. */
619
620 tc_pgid = tcgetpgrp(tag_files->ofd);
621 if (tc_pgid != man_pgid) {
622 if (tc_pgid == pager_pid) {
623 (void)tcsetpgrp(tag_files->ofd,
624 man_pgid);
625 if (signum == SIGTTIN)
626 continue;
627 } else
628 tag_files->tcpgid = tc_pgid;
629 kill(0, signum);
630 continue;
631 }
632
633 /* Once in the foreground, activate the pager. */
634
635 if (pager_pid) {
636 (void)tcsetpgrp(tag_files->ofd, pager_pid);
637 kill(pager_pid, SIGCONT);
638 } else
639 pager_pid = spawn_pager(tag_files);
640
641 /* Wait for the pager to stop or exit. */
642
643 while ((pid = waitpid(pager_pid, &status,
644 WUNTRACED)) == -1 && errno == EINTR)
645 continue;
646
647 if (pid == -1) {
648 warn("wait");
649 rc = MANDOCLEVEL_SYSERR;
650 break;
651 }
652 if (!WIFSTOPPED(status))
653 break;
654
655 signum = WSTOPSIG(status);
656 }
657 tag_unlink();
658 }
659
660 return (int)rc;
661 }
662
663 static void
664 usage(enum argmode argmode)
665 {
666
667 switch (argmode) {
668 case ARG_FILE:
669 fputs("usage: mandoc [-ac] [-I os=name] "
670 "[-K encoding] [-mdoc | -man] [-O options]\n"
671 "\t [-T output] [-W level] [file ...]\n", stderr);
672 break;
673 case ARG_NAME:
674 fputs("usage: man [-acfhklw] [-C file] [-M path] "
675 "[-m path] [-S subsection]\n"
676 "\t [[-s] section] name ...\n", stderr);
677 break;
678 case ARG_WORD:
679 fputs("usage: whatis [-afk] [-C file] "
680 "[-M path] [-m path] [-O outkey] [-S arch]\n"
681 "\t [-s section] name ...\n", stderr);
682 break;
683 case ARG_EXPR:
684 fputs("usage: apropos [-afk] [-C file] "
685 "[-M path] [-m path] [-O outkey] [-S arch]\n"
686 "\t [-s section] expression ...\n", stderr);
687 break;
688 }
689 exit((int)MANDOCLEVEL_BADARG);
690 }
691
692 static int
693 fs_lookup(const struct manpaths *paths, size_t ipath,
694 const char *sec, const char *arch, const char *name,
695 struct manpage **res, size_t *ressz)
696 {
697 glob_t globinfo;
698 struct manpage *page;
699 char *file;
700 int globres;
701 enum form form;
702
703 form = FORM_SRC;
704 mandoc_asprintf(&file, "%s/man%s/%s.%s",
705 paths->paths[ipath], sec, name, sec);
706 if (access(file, R_OK) != -1)
707 goto found;
708 free(file);
709
710 mandoc_asprintf(&file, "%s/cat%s/%s.0",
711 paths->paths[ipath], sec, name);
712 if (access(file, R_OK) != -1) {
713 form = FORM_CAT;
714 goto found;
715 }
716 free(file);
717
718 if (arch != NULL) {
719 mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
720 paths->paths[ipath], sec, arch, name, sec);
721 if (access(file, R_OK) != -1)
722 goto found;
723 free(file);
724 }
725
726 mandoc_asprintf(&file, "%s/man%s/%s.[01-9]*",
727 paths->paths[ipath], sec, name);
728 globres = glob(file, 0, NULL, &globinfo);
729 if (globres != 0 && globres != GLOB_NOMATCH)
730 warn("%s: glob", file);
731 free(file);
732 if (globres == 0)
733 file = mandoc_strdup(*globinfo.gl_pathv);
734 globfree(&globinfo);
735 if (globres == 0)
736 goto found;
737 if (res != NULL || ipath + 1 != paths->sz)
738 return 0;
739
740 mandoc_asprintf(&file, "%s.%s", name, sec);
741 globres = access(file, R_OK);
742 free(file);
743 return globres != -1;
744
745 found:
746 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
747 name, sec, BINM_MAKEWHATIS, paths->paths[ipath]);
748 if (res == NULL) {
749 free(file);
750 return 1;
751 }
752 *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
753 page = *res + (*ressz - 1);
754 page->file = file;
755 page->names = NULL;
756 page->output = NULL;
757 page->ipath = ipath;
758 page->bits = NAME_FILE & NAME_MASK;
759 page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
760 page->form = form;
761 return 1;
762 }
763
764 static int
765 fs_search(const struct mansearch *cfg, const struct manpaths *paths,
766 int argc, char **argv, struct manpage **res, size_t *ressz)
767 {
768 const char *const sections[] =
769 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
770 const size_t nsec = sizeof(sections)/sizeof(sections[0]);
771
772 size_t ipath, isec, lastsz;
773
774 assert(cfg->argmode == ARG_NAME);
775
776 if (res != NULL)
777 *res = NULL;
778 *ressz = lastsz = 0;
779 while (argc) {
780 for (ipath = 0; ipath < paths->sz; ipath++) {
781 if (cfg->sec != NULL) {
782 if (fs_lookup(paths, ipath, cfg->sec,
783 cfg->arch, *argv, res, ressz) &&
784 cfg->firstmatch)
785 return 1;
786 } else for (isec = 0; isec < nsec; isec++)
787 if (fs_lookup(paths, ipath, sections[isec],
788 cfg->arch, *argv, res, ressz) &&
789 cfg->firstmatch)
790 return 1;
791 }
792 if (res != NULL && *ressz == lastsz &&
793 strchr(*argv, '/') == NULL) {
794 if (cfg->sec == NULL)
795 warnx("No entry for %s in the manual.",
796 *argv);
797 else
798 warnx("No entry for %s in section %s "
799 "of the manual.", *argv, cfg->sec);
800 }
801 lastsz = *ressz;
802 argv++;
803 argc--;
804 }
805 return 0;
806 }
807
808 static void
809 parse(struct curparse *curp, int fd, const char *file)
810 {
811 enum mandoclevel rctmp;
812 struct roff_man *man;
813
814 /* Begin by parsing the file itself. */
815
816 assert(file);
817 assert(fd >= 0);
818
819 rctmp = mparse_readfd(curp->mp, fd, file);
820 if (fd != STDIN_FILENO)
821 close(fd);
822 if (rc < rctmp)
823 rc = rctmp;
824
825 /*
826 * With -Wstop and warnings or errors of at least the requested
827 * level, do not produce output.
828 */
829
830 if (rctmp != MANDOCLEVEL_OK && curp->wstop)
831 return;
832
833 if (curp->outdata == NULL)
834 outdata_alloc(curp);
835
836 mparse_result(curp->mp, &man, NULL);
837
838 /* Execute the out device, if it exists. */
839
840 if (man == NULL)
841 return;
842 mandoc_xr_reset();
843 if (man->macroset == MACROSET_MDOC) {
844 if (curp->outtype != OUTT_TREE || !curp->outopts->noval)
845 mdoc_validate(man);
846 switch (curp->outtype) {
847 case OUTT_HTML:
848 html_mdoc(curp->outdata, man);
849 break;
850 case OUTT_TREE:
851 tree_mdoc(curp->outdata, man);
852 break;
853 case OUTT_MAN:
854 man_mdoc(curp->outdata, man);
855 break;
856 case OUTT_PDF:
857 case OUTT_ASCII:
858 case OUTT_UTF8:
859 case OUTT_LOCALE:
860 case OUTT_PS:
861 terminal_mdoc(curp->outdata, man);
862 break;
863 case OUTT_MARKDOWN:
864 markdown_mdoc(curp->outdata, man);
865 break;
866 default:
867 break;
868 }
869 }
870 if (man->macroset == MACROSET_MAN) {
871 if (curp->outtype != OUTT_TREE || !curp->outopts->noval)
872 man_validate(man);
873 switch (curp->outtype) {
874 case OUTT_HTML:
875 html_man(curp->outdata, man);
876 break;
877 case OUTT_TREE:
878 tree_man(curp->outdata, man);
879 break;
880 case OUTT_MAN:
881 man_man(curp->outdata, man);
882 break;
883 case OUTT_PDF:
884 case OUTT_ASCII:
885 case OUTT_UTF8:
886 case OUTT_LOCALE:
887 case OUTT_PS:
888 terminal_man(curp->outdata, man);
889 break;
890 default:
891 break;
892 }
893 }
894 if (curp->mmin < MANDOCERR_STYLE)
895 check_xr(file);
896 mparse_updaterc(curp->mp, &rc);
897 }
898
899 static void
900 check_xr(const char *file)
901 {
902 static struct manpaths paths;
903 struct mansearch search;
904 struct mandoc_xr *xr;
905 char *cp;
906 size_t sz;
907
908 if (paths.sz == 0)
909 manpath_base(&paths);
910
911 for (xr = mandoc_xr_get(); xr != NULL; xr = xr->next) {
912 if (xr->line == -1)
913 continue;
914 search.arch = NULL;
915 search.sec = xr->sec;
916 search.outkey = NULL;
917 search.argmode = ARG_NAME;
918 search.firstmatch = 1;
919 if (mansearch(&search, &paths, 1, &xr->name, NULL, &sz))
920 continue;
921 if (fs_search(&search, &paths, 1, &xr->name, NULL, &sz))
922 continue;
923 if (xr->count == 1)
924 mandoc_asprintf(&cp, "Xr %s %s", xr->name, xr->sec);
925 else
926 mandoc_asprintf(&cp, "Xr %s %s (%d times)",
927 xr->name, xr->sec, xr->count);
928 mmsg(MANDOCERR_XR_BAD, MANDOCLEVEL_STYLE,
929 file, xr->line, xr->pos + 1, cp);
930 free(cp);
931 }
932 }
933
934 static void
935 outdata_alloc(struct curparse *curp)
936 {
937 switch (curp->outtype) {
938 case OUTT_HTML:
939 curp->outdata = html_alloc(curp->outopts);
940 break;
941 case OUTT_UTF8:
942 curp->outdata = utf8_alloc(curp->outopts);
943 break;
944 case OUTT_LOCALE:
945 curp->outdata = locale_alloc(curp->outopts);
946 break;
947 case OUTT_ASCII:
948 curp->outdata = ascii_alloc(curp->outopts);
949 break;
950 case OUTT_PDF:
951 curp->outdata = pdf_alloc(curp->outopts);
952 break;
953 case OUTT_PS:
954 curp->outdata = ps_alloc(curp->outopts);
955 break;
956 default:
957 break;
958 }
959 }
960
961 static void
962 passthrough(const char *file, int fd, int synopsis_only)
963 {
964 const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
965 const char synr[] = "SYNOPSIS";
966
967 FILE *stream;
968 const char *syscall;
969 char *line, *cp;
970 size_t linesz;
971 ssize_t len, written;
972 int print;
973
974 line = NULL;
975 linesz = 0;
976
977 if (fflush(stdout) == EOF) {
978 syscall = "fflush";
979 goto fail;
980 }
981
982 if ((stream = fdopen(fd, "r")) == NULL) {
983 close(fd);
984 syscall = "fdopen";
985 goto fail;
986 }
987
988 print = 0;
989 while ((len = getline(&line, &linesz, stream)) != -1) {
990 cp = line;
991 if (synopsis_only) {
992 if (print) {
993 if ( ! isspace((unsigned char)*cp))
994 goto done;
995 while (isspace((unsigned char)*cp)) {
996 cp++;
997 len--;
998 }
999 } else {
1000 if (strcmp(cp, synb) == 0 ||
1001 strcmp(cp, synr) == 0)
1002 print = 1;
1003 continue;
1004 }
1005 }
1006 for (; len > 0; len -= written) {
1007 if ((written = write(STDOUT_FILENO, cp, len)) != -1)
1008 continue;
1009 fclose(stream);
1010 syscall = "write";
1011 goto fail;
1012 }
1013 }
1014
1015 if (ferror(stream)) {
1016 fclose(stream);
1017 syscall = "getline";
1018 goto fail;
1019 }
1020
1021 done:
1022 free(line);
1023 fclose(stream);
1024 return;
1025
1026 fail:
1027 free(line);
1028 warn("%s: SYSERR: %s", file, syscall);
1029 if (rc < MANDOCLEVEL_SYSERR)
1030 rc = MANDOCLEVEL_SYSERR;
1031 }
1032
1033 static int
1034 koptions(int *options, char *arg)
1035 {
1036
1037 if ( ! strcmp(arg, "utf-8")) {
1038 *options |= MPARSE_UTF8;
1039 *options &= ~MPARSE_LATIN1;
1040 } else if ( ! strcmp(arg, "iso-8859-1")) {
1041 *options |= MPARSE_LATIN1;
1042 *options &= ~MPARSE_UTF8;
1043 } else if ( ! strcmp(arg, "us-ascii")) {
1044 *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
1045 } else {
1046 warnx("-K %s: Bad argument", arg);
1047 return 0;
1048 }
1049 return 1;
1050 }
1051
1052 static void
1053 moptions(int *options, char *arg)
1054 {
1055
1056 if (arg == NULL)
1057 return;
1058 if (strcmp(arg, "doc") == 0)
1059 *options |= MPARSE_MDOC;
1060 else if (strcmp(arg, "an") == 0)
1061 *options |= MPARSE_MAN;
1062 }
1063
1064 static int
1065 toptions(struct curparse *curp, char *arg)
1066 {
1067
1068 if (0 == strcmp(arg, "ascii"))
1069 curp->outtype = OUTT_ASCII;
1070 else if (0 == strcmp(arg, "lint")) {
1071 curp->outtype = OUTT_LINT;
1072 curp->mmin = MANDOCERR_BASE;
1073 mmsg_stream = stdout;
1074 } else if (0 == strcmp(arg, "tree"))
1075 curp->outtype = OUTT_TREE;
1076 else if (0 == strcmp(arg, "man"))
1077 curp->outtype = OUTT_MAN;
1078 else if (0 == strcmp(arg, "html"))
1079 curp->outtype = OUTT_HTML;
1080 else if (0 == strcmp(arg, "markdown"))
1081 curp->outtype = OUTT_MARKDOWN;
1082 else if (0 == strcmp(arg, "utf8"))
1083 curp->outtype = OUTT_UTF8;
1084 else if (0 == strcmp(arg, "locale"))
1085 curp->outtype = OUTT_LOCALE;
1086 else if (0 == strcmp(arg, "ps"))
1087 curp->outtype = OUTT_PS;
1088 else if (0 == strcmp(arg, "pdf"))
1089 curp->outtype = OUTT_PDF;
1090 else {
1091 warnx("-T %s: Bad argument", arg);
1092 return 0;
1093 }
1094
1095 return 1;
1096 }
1097
1098 static int
1099 woptions(struct curparse *curp, char *arg)
1100 {
1101 char *v, *o;
1102 const char *toks[11];
1103
1104 toks[0] = "stop";
1105 toks[1] = "all";
1106 toks[2] = "base";
1107 toks[3] = "style";
1108 toks[4] = "warning";
1109 toks[5] = "error";
1110 toks[6] = "unsupp";
1111 toks[7] = "fatal";
1112 toks[8] = "openbsd";
1113 toks[9] = "netbsd";
1114 toks[10] = NULL;
1115
1116 while (*arg) {
1117 o = arg;
1118 switch (getsubopt(&arg, (char * const *)toks, &v)) {
1119 case 0:
1120 curp->wstop = 1;
1121 break;
1122 case 1:
1123 case 2:
1124 curp->mmin = MANDOCERR_BASE;
1125 break;
1126 case 3:
1127 curp->mmin = MANDOCERR_STYLE;
1128 break;
1129 case 4:
1130 curp->mmin = MANDOCERR_WARNING;
1131 break;
1132 case 5:
1133 curp->mmin = MANDOCERR_ERROR;
1134 break;
1135 case 6:
1136 curp->mmin = MANDOCERR_UNSUPP;
1137 break;
1138 case 7:
1139 curp->mmin = MANDOCERR_MAX;
1140 break;
1141 case 8:
1142 curp->mmin = MANDOCERR_BASE;
1143 curp->os_e = MANDOC_OS_OPENBSD;
1144 break;
1145 case 9:
1146 curp->mmin = MANDOCERR_BASE;
1147 curp->os_e = MANDOC_OS_NETBSD;
1148 break;
1149 default:
1150 warnx("-W %s: Bad argument", o);
1151 return 0;
1152 }
1153 }
1154 return 1;
1155 }
1156
1157 static void
1158 mmsg(enum mandocerr t, enum mandoclevel lvl,
1159 const char *file, int line, int col, const char *msg)
1160 {
1161 const char *mparse_msg;
1162
1163 fprintf(mmsg_stream, "%s: %s:", getprogname(),
1164 file == NULL ? "<stdin>" : file);
1165
1166 if (line)
1167 fprintf(mmsg_stream, "%d:%d:", line, col + 1);
1168
1169 fprintf(mmsg_stream, " %s", mparse_strlevel(lvl));
1170
1171 if ((mparse_msg = mparse_strerror(t)) != NULL)
1172 fprintf(mmsg_stream, ": %s", mparse_msg);
1173
1174 if (msg)
1175 fprintf(mmsg_stream, ": %s", msg);
1176
1177 fputc('\n', mmsg_stream);
1178 }
1179
1180 static pid_t
1181 spawn_pager(struct tag_files *tag_files)
1182 {
1183 const struct timespec timeout = { 0, 100000000 }; /* 0.1s */
1184 #define MAX_PAGER_ARGS 16
1185 char *argv[MAX_PAGER_ARGS];
1186 const char *pager;
1187 char *cp;
1188 size_t cmdlen;
1189 int argc;
1190 pid_t pager_pid;
1191
1192 pager = getenv("MANPAGER");
1193 if (pager == NULL || *pager == '\0')
1194 pager = getenv("PAGER");
1195 if (pager == NULL || *pager == '\0')
1196 pager = "more -s";
1197 cp = mandoc_strdup(pager);
1198
1199 /*
1200 * Parse the pager command into words.
1201 * Intentionally do not do anything fancy here.
1202 */
1203
1204 argc = 0;
1205 while (argc + 4 < MAX_PAGER_ARGS) {
1206 argv[argc++] = cp;
1207 cp = strchr(cp, ' ');
1208 if (cp == NULL)
1209 break;
1210 *cp++ = '\0';
1211 while (*cp == ' ')
1212 cp++;
1213 if (*cp == '\0')
1214 break;
1215 }
1216
1217 /* For less(1), use the tag file. */
1218
1219 if ((cmdlen = strlen(argv[0])) >= 4) {
1220 cp = argv[0] + cmdlen - 4;
1221 if (strcmp(cp, "less") == 0) {
1222 argv[argc++] = mandoc_strdup("-T");
1223 argv[argc++] = tag_files->tfn;
1224 }
1225 }
1226 argv[argc++] = tag_files->ofn;
1227 argv[argc] = NULL;
1228
1229 switch (pager_pid = fork()) {
1230 case -1:
1231 err((int)MANDOCLEVEL_SYSERR, "fork");
1232 case 0:
1233 break;
1234 default:
1235 (void)setpgid(pager_pid, 0);
1236 (void)tcsetpgrp(tag_files->ofd, pager_pid);
1237 #if HAVE_PLEDGE
1238 if (pledge("stdio rpath tmppath tty proc", NULL) == -1)
1239 err((int)MANDOCLEVEL_SYSERR, "pledge");
1240 #endif
1241 tag_files->pager_pid = pager_pid;
1242 return pager_pid;
1243 }
1244
1245 /* The child process becomes the pager. */
1246
1247 if (dup2(tag_files->ofd, STDOUT_FILENO) == -1)
1248 err((int)MANDOCLEVEL_SYSERR, "pager stdout");
1249 close(tag_files->ofd);
1250 assert(tag_files->tfd == -1);
1251
1252 /* Do not start the pager before controlling the terminal. */
1253
1254 while (tcgetpgrp(STDOUT_FILENO) != getpid())
1255 nanosleep(&timeout, NULL);
1256
1257 execvp(argv[0], argv);
1258 err((int)MANDOCLEVEL_SYSERR, "exec %s", argv[0]);
1259 }