]> git.cameronkatri.com Git - mandoc.git/blob - main.c
During validation, drop .br before a text line starting with a
[mandoc.git] / main.c
1 /* $Id: main.c,v 1.310 2018/11/22 12:01:46 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].sec = 10;
409 res[sz].form = FORM_SRC;
410 sz++;
411 }
412 }
413
414 if (sz == 0) {
415 if (search.argmode != ARG_NAME)
416 warnx("nothing appropriate");
417 rc = MANDOCLEVEL_BADARG;
418 goto out;
419 }
420
421 /*
422 * For standard man(1) and -a output mode,
423 * prepare for copying filename pointers
424 * into the program parameter array.
425 */
426
427 if (outmode == OUTMODE_ONE) {
428 argc = 1;
429 best_prio = 20;
430 } else if (outmode == OUTMODE_ALL)
431 argc = (int)sz;
432
433 /* Iterate all matching manuals. */
434
435 resp = res;
436 for (i = 0; i < sz; i++) {
437 if (outmode == OUTMODE_FLN)
438 puts(res[i].file);
439 else if (outmode == OUTMODE_LST)
440 printf("%s - %s\n", res[i].names,
441 res[i].output == NULL ? "" :
442 res[i].output);
443 else if (outmode == OUTMODE_ONE) {
444 /* Search for the best section. */
445 sec = res[i].file;
446 sec += strcspn(sec, "123456789");
447 if (sec[0] == '\0')
448 continue;
449 prio = sec_prios[sec[0] - '1'];
450 if (sec[1] != '/')
451 prio += 10;
452 if (prio >= best_prio)
453 continue;
454 best_prio = prio;
455 resp = res + i;
456 }
457 }
458
459 /*
460 * For man(1), -a and -i output mode, fall through
461 * to the main mandoc(1) code iterating files
462 * and running the parsers on each of them.
463 */
464
465 if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
466 goto out;
467 }
468
469 /* mandoc(1) */
470
471 #if HAVE_PLEDGE
472 if (use_pager) {
473 if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1)
474 err((int)MANDOCLEVEL_SYSERR, "pledge");
475 } else {
476 if (pledge("stdio rpath", NULL) == -1)
477 err((int)MANDOCLEVEL_SYSERR, "pledge");
478 }
479 #endif
480
481 if (search.argmode == ARG_FILE)
482 moptions(&options, auxpaths);
483
484 mchars_alloc();
485 curp.mp = mparse_alloc(options, curp.mmin, mmsg,
486 curp.os_e, curp.os_s);
487
488 if (argc < 1) {
489 if (use_pager)
490 tag_files = tag_init();
491 parse(&curp, STDIN_FILENO, "<stdin>");
492 }
493
494 /*
495 * Remember the original working directory, if possible.
496 * This will be needed if some names on the command line
497 * are page names and some are relative file names.
498 * Do not error out if the current directory is not
499 * readable: Maybe it won't be needed after all.
500 */
501 startdir = open(".", O_RDONLY | O_DIRECTORY);
502
503 while (argc > 0) {
504
505 /*
506 * Changing directories is not needed in ARG_FILE mode.
507 * Do it on a best-effort basis. Even in case of
508 * failure, some functionality may still work.
509 */
510 if (resp != NULL) {
511 if (resp->ipath != SIZE_MAX)
512 (void)chdir(conf.manpath.paths[resp->ipath]);
513 else if (startdir != -1)
514 (void)fchdir(startdir);
515 }
516
517 fd = mparse_open(curp.mp, resp != NULL ? resp->file : *argv);
518 if (fd != -1) {
519 if (use_pager) {
520 use_pager = 0;
521 tag_files = tag_init();
522 if (conf.output.tag != NULL &&
523 tag_files->tagname == NULL)
524 tag_files->tagname =
525 *conf.output.tag != '\0' ?
526 conf.output.tag : *argv;
527 }
528
529 if (resp == NULL)
530 parse(&curp, fd, *argv);
531 else if (resp->form == FORM_SRC)
532 parse(&curp, fd, resp->file);
533 else
534 passthrough(resp->file, fd,
535 conf.output.synopsisonly);
536
537 if (ferror(stdout)) {
538 if (tag_files != NULL) {
539 warn("%s", tag_files->ofn);
540 tag_unlink();
541 tag_files = NULL;
542 } else
543 warn("stdout");
544 rc = MANDOCLEVEL_SYSERR;
545 break;
546 }
547
548 if (argc > 1 && curp.outtype <= OUTT_UTF8) {
549 if (curp.outdata == NULL)
550 outdata_alloc(&curp);
551 terminal_sepline(curp.outdata);
552 }
553 } else if (rc < MANDOCLEVEL_ERROR)
554 rc = MANDOCLEVEL_ERROR;
555
556 if (MANDOCLEVEL_OK != rc && curp.wstop)
557 break;
558
559 if (resp != NULL)
560 resp++;
561 else
562 argv++;
563 if (--argc)
564 mparse_reset(curp.mp);
565 }
566 if (startdir != -1) {
567 (void)fchdir(startdir);
568 close(startdir);
569 }
570
571 if (curp.outdata != NULL) {
572 switch (curp.outtype) {
573 case OUTT_HTML:
574 html_free(curp.outdata);
575 break;
576 case OUTT_UTF8:
577 case OUTT_LOCALE:
578 case OUTT_ASCII:
579 ascii_free(curp.outdata);
580 break;
581 case OUTT_PDF:
582 case OUTT_PS:
583 pspdf_free(curp.outdata);
584 break;
585 default:
586 break;
587 }
588 }
589 mandoc_xr_free();
590 mparse_free(curp.mp);
591 mchars_free();
592
593 out:
594 if (search.argmode != ARG_FILE) {
595 manconf_free(&conf);
596 mansearch_free(res, sz);
597 }
598
599 free(curp.os_s);
600
601 /*
602 * When using a pager, finish writing both temporary files,
603 * fork it, wait for the user to close it, and clean up.
604 */
605
606 if (tag_files != NULL) {
607 fclose(stdout);
608 tag_write();
609 man_pgid = getpgid(0);
610 tag_files->tcpgid = man_pgid == getpid() ?
611 getpgid(getppid()) : man_pgid;
612 pager_pid = 0;
613 signum = SIGSTOP;
614 for (;;) {
615
616 /* Stop here until moved to the foreground. */
617
618 tc_pgid = tcgetpgrp(tag_files->ofd);
619 if (tc_pgid != man_pgid) {
620 if (tc_pgid == pager_pid) {
621 (void)tcsetpgrp(tag_files->ofd,
622 man_pgid);
623 if (signum == SIGTTIN)
624 continue;
625 } else
626 tag_files->tcpgid = tc_pgid;
627 kill(0, signum);
628 continue;
629 }
630
631 /* Once in the foreground, activate the pager. */
632
633 if (pager_pid) {
634 (void)tcsetpgrp(tag_files->ofd, pager_pid);
635 kill(pager_pid, SIGCONT);
636 } else
637 pager_pid = spawn_pager(tag_files);
638
639 /* Wait for the pager to stop or exit. */
640
641 while ((pid = waitpid(pager_pid, &status,
642 WUNTRACED)) == -1 && errno == EINTR)
643 continue;
644
645 if (pid == -1) {
646 warn("wait");
647 rc = MANDOCLEVEL_SYSERR;
648 break;
649 }
650 if (!WIFSTOPPED(status))
651 break;
652
653 signum = WSTOPSIG(status);
654 }
655 tag_unlink();
656 }
657
658 return (int)rc;
659 }
660
661 static void
662 usage(enum argmode argmode)
663 {
664
665 switch (argmode) {
666 case ARG_FILE:
667 fputs("usage: mandoc [-ac] [-I os=name] "
668 "[-K encoding] [-mdoc | -man] [-O options]\n"
669 "\t [-T output] [-W level] [file ...]\n", stderr);
670 break;
671 case ARG_NAME:
672 fputs("usage: man [-acfhklw] [-C file] [-M path] "
673 "[-m path] [-S subsection]\n"
674 "\t [[-s] section] name ...\n", stderr);
675 break;
676 case ARG_WORD:
677 fputs("usage: whatis [-afk] [-C file] "
678 "[-M path] [-m path] [-O outkey] [-S arch]\n"
679 "\t [-s section] name ...\n", stderr);
680 break;
681 case ARG_EXPR:
682 fputs("usage: apropos [-afk] [-C file] "
683 "[-M path] [-m path] [-O outkey] [-S arch]\n"
684 "\t [-s section] expression ...\n", stderr);
685 break;
686 }
687 exit((int)MANDOCLEVEL_BADARG);
688 }
689
690 static int
691 fs_lookup(const struct manpaths *paths, size_t ipath,
692 const char *sec, const char *arch, const char *name,
693 struct manpage **res, size_t *ressz)
694 {
695 glob_t globinfo;
696 struct manpage *page;
697 char *file;
698 int globres;
699 enum form form;
700
701 form = FORM_SRC;
702 mandoc_asprintf(&file, "%s/man%s/%s.%s",
703 paths->paths[ipath], sec, name, sec);
704 if (access(file, R_OK) != -1)
705 goto found;
706 free(file);
707
708 mandoc_asprintf(&file, "%s/cat%s/%s.0",
709 paths->paths[ipath], sec, name);
710 if (access(file, R_OK) != -1) {
711 form = FORM_CAT;
712 goto found;
713 }
714 free(file);
715
716 if (arch != NULL) {
717 mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
718 paths->paths[ipath], sec, arch, name, sec);
719 if (access(file, R_OK) != -1)
720 goto found;
721 free(file);
722 }
723
724 mandoc_asprintf(&file, "%s/man%s/%s.[01-9]*",
725 paths->paths[ipath], sec, name);
726 globres = glob(file, 0, NULL, &globinfo);
727 if (globres != 0 && globres != GLOB_NOMATCH)
728 warn("%s: glob", file);
729 free(file);
730 if (globres == 0)
731 file = mandoc_strdup(*globinfo.gl_pathv);
732 globfree(&globinfo);
733 if (globres == 0)
734 goto found;
735 if (res != NULL || ipath + 1 != paths->sz)
736 return 0;
737
738 mandoc_asprintf(&file, "%s.%s", name, sec);
739 globres = access(file, R_OK);
740 free(file);
741 return globres != -1;
742
743 found:
744 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
745 name, sec, BINM_MAKEWHATIS, paths->paths[ipath]);
746 if (res == NULL) {
747 free(file);
748 return 1;
749 }
750 *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
751 page = *res + (*ressz - 1);
752 page->file = file;
753 page->names = NULL;
754 page->output = NULL;
755 page->ipath = ipath;
756 page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
757 page->form = form;
758 return 1;
759 }
760
761 static int
762 fs_search(const struct mansearch *cfg, const struct manpaths *paths,
763 int argc, char **argv, struct manpage **res, size_t *ressz)
764 {
765 const char *const sections[] =
766 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
767 const size_t nsec = sizeof(sections)/sizeof(sections[0]);
768
769 size_t ipath, isec, lastsz;
770
771 assert(cfg->argmode == ARG_NAME);
772
773 if (res != NULL)
774 *res = NULL;
775 *ressz = lastsz = 0;
776 while (argc) {
777 for (ipath = 0; ipath < paths->sz; ipath++) {
778 if (cfg->sec != NULL) {
779 if (fs_lookup(paths, ipath, cfg->sec,
780 cfg->arch, *argv, res, ressz) &&
781 cfg->firstmatch)
782 return 1;
783 } else for (isec = 0; isec < nsec; isec++)
784 if (fs_lookup(paths, ipath, sections[isec],
785 cfg->arch, *argv, res, ressz) &&
786 cfg->firstmatch)
787 return 1;
788 }
789 if (res != NULL && *ressz == lastsz &&
790 strchr(*argv, '/') == NULL) {
791 if (cfg->sec == NULL)
792 warnx("No entry for %s in the manual.",
793 *argv);
794 else
795 warnx("No entry for %s in section %s "
796 "of the manual.", *argv, cfg->sec);
797 }
798 lastsz = *ressz;
799 argv++;
800 argc--;
801 }
802 return 0;
803 }
804
805 static void
806 parse(struct curparse *curp, int fd, const char *file)
807 {
808 enum mandoclevel rctmp;
809 struct roff_man *man;
810
811 /* Begin by parsing the file itself. */
812
813 assert(file);
814 assert(fd >= 0);
815
816 rctmp = mparse_readfd(curp->mp, fd, file);
817 if (fd != STDIN_FILENO)
818 close(fd);
819 if (rc < rctmp)
820 rc = rctmp;
821
822 /*
823 * With -Wstop and warnings or errors of at least the requested
824 * level, do not produce output.
825 */
826
827 if (rctmp != MANDOCLEVEL_OK && curp->wstop)
828 return;
829
830 if (curp->outdata == NULL)
831 outdata_alloc(curp);
832
833 mparse_result(curp->mp, &man, NULL);
834
835 /* Execute the out device, if it exists. */
836
837 if (man == NULL)
838 return;
839 mandoc_xr_reset();
840 if (man->macroset == MACROSET_MDOC) {
841 if (curp->outtype != OUTT_TREE || !curp->outopts->noval)
842 mdoc_validate(man);
843 switch (curp->outtype) {
844 case OUTT_HTML:
845 html_mdoc(curp->outdata, man);
846 break;
847 case OUTT_TREE:
848 tree_mdoc(curp->outdata, man);
849 break;
850 case OUTT_MAN:
851 man_mdoc(curp->outdata, man);
852 break;
853 case OUTT_PDF:
854 case OUTT_ASCII:
855 case OUTT_UTF8:
856 case OUTT_LOCALE:
857 case OUTT_PS:
858 terminal_mdoc(curp->outdata, man);
859 break;
860 case OUTT_MARKDOWN:
861 markdown_mdoc(curp->outdata, man);
862 break;
863 default:
864 break;
865 }
866 }
867 if (man->macroset == MACROSET_MAN) {
868 if (curp->outtype != OUTT_TREE || !curp->outopts->noval)
869 man_validate(man);
870 switch (curp->outtype) {
871 case OUTT_HTML:
872 html_man(curp->outdata, man);
873 break;
874 case OUTT_TREE:
875 tree_man(curp->outdata, man);
876 break;
877 case OUTT_MAN:
878 mparse_copy(curp->mp);
879 break;
880 case OUTT_PDF:
881 case OUTT_ASCII:
882 case OUTT_UTF8:
883 case OUTT_LOCALE:
884 case OUTT_PS:
885 terminal_man(curp->outdata, man);
886 break;
887 default:
888 break;
889 }
890 }
891 if (curp->mmin < MANDOCERR_STYLE)
892 check_xr(file);
893 mparse_updaterc(curp->mp, &rc);
894 }
895
896 static void
897 check_xr(const char *file)
898 {
899 static struct manpaths paths;
900 struct mansearch search;
901 struct mandoc_xr *xr;
902 char *cp;
903 size_t sz;
904
905 if (paths.sz == 0)
906 manpath_base(&paths);
907
908 for (xr = mandoc_xr_get(); xr != NULL; xr = xr->next) {
909 if (xr->line == -1)
910 continue;
911 search.arch = NULL;
912 search.sec = xr->sec;
913 search.outkey = NULL;
914 search.argmode = ARG_NAME;
915 search.firstmatch = 1;
916 if (mansearch(&search, &paths, 1, &xr->name, NULL, &sz))
917 continue;
918 if (fs_search(&search, &paths, 1, &xr->name, NULL, &sz))
919 continue;
920 if (xr->count == 1)
921 mandoc_asprintf(&cp, "Xr %s %s", xr->name, xr->sec);
922 else
923 mandoc_asprintf(&cp, "Xr %s %s (%d times)",
924 xr->name, xr->sec, xr->count);
925 mmsg(MANDOCERR_XR_BAD, MANDOCLEVEL_STYLE,
926 file, xr->line, xr->pos + 1, cp);
927 free(cp);
928 }
929 }
930
931 static void
932 outdata_alloc(struct curparse *curp)
933 {
934 switch (curp->outtype) {
935 case OUTT_HTML:
936 curp->outdata = html_alloc(curp->outopts);
937 break;
938 case OUTT_UTF8:
939 curp->outdata = utf8_alloc(curp->outopts);
940 break;
941 case OUTT_LOCALE:
942 curp->outdata = locale_alloc(curp->outopts);
943 break;
944 case OUTT_ASCII:
945 curp->outdata = ascii_alloc(curp->outopts);
946 break;
947 case OUTT_PDF:
948 curp->outdata = pdf_alloc(curp->outopts);
949 break;
950 case OUTT_PS:
951 curp->outdata = ps_alloc(curp->outopts);
952 break;
953 default:
954 break;
955 }
956 }
957
958 static void
959 passthrough(const char *file, int fd, int synopsis_only)
960 {
961 const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
962 const char synr[] = "SYNOPSIS";
963
964 FILE *stream;
965 const char *syscall;
966 char *line, *cp;
967 size_t linesz;
968 ssize_t len, written;
969 int print;
970
971 line = NULL;
972 linesz = 0;
973
974 if (fflush(stdout) == EOF) {
975 syscall = "fflush";
976 goto fail;
977 }
978
979 if ((stream = fdopen(fd, "r")) == NULL) {
980 close(fd);
981 syscall = "fdopen";
982 goto fail;
983 }
984
985 print = 0;
986 while ((len = getline(&line, &linesz, stream)) != -1) {
987 cp = line;
988 if (synopsis_only) {
989 if (print) {
990 if ( ! isspace((unsigned char)*cp))
991 goto done;
992 while (isspace((unsigned char)*cp)) {
993 cp++;
994 len--;
995 }
996 } else {
997 if (strcmp(cp, synb) == 0 ||
998 strcmp(cp, synr) == 0)
999 print = 1;
1000 continue;
1001 }
1002 }
1003 for (; len > 0; len -= written) {
1004 if ((written = write(STDOUT_FILENO, cp, len)) != -1)
1005 continue;
1006 fclose(stream);
1007 syscall = "write";
1008 goto fail;
1009 }
1010 }
1011
1012 if (ferror(stream)) {
1013 fclose(stream);
1014 syscall = "getline";
1015 goto fail;
1016 }
1017
1018 done:
1019 free(line);
1020 fclose(stream);
1021 return;
1022
1023 fail:
1024 free(line);
1025 warn("%s: SYSERR: %s", file, syscall);
1026 if (rc < MANDOCLEVEL_SYSERR)
1027 rc = MANDOCLEVEL_SYSERR;
1028 }
1029
1030 static int
1031 koptions(int *options, char *arg)
1032 {
1033
1034 if ( ! strcmp(arg, "utf-8")) {
1035 *options |= MPARSE_UTF8;
1036 *options &= ~MPARSE_LATIN1;
1037 } else if ( ! strcmp(arg, "iso-8859-1")) {
1038 *options |= MPARSE_LATIN1;
1039 *options &= ~MPARSE_UTF8;
1040 } else if ( ! strcmp(arg, "us-ascii")) {
1041 *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
1042 } else {
1043 warnx("-K %s: Bad argument", arg);
1044 return 0;
1045 }
1046 return 1;
1047 }
1048
1049 static void
1050 moptions(int *options, char *arg)
1051 {
1052
1053 if (arg == NULL)
1054 return;
1055 if (strcmp(arg, "doc") == 0)
1056 *options |= MPARSE_MDOC;
1057 else if (strcmp(arg, "an") == 0)
1058 *options |= MPARSE_MAN;
1059 }
1060
1061 static int
1062 toptions(struct curparse *curp, char *arg)
1063 {
1064
1065 if (0 == strcmp(arg, "ascii"))
1066 curp->outtype = OUTT_ASCII;
1067 else if (0 == strcmp(arg, "lint")) {
1068 curp->outtype = OUTT_LINT;
1069 curp->mmin = MANDOCERR_BASE;
1070 mmsg_stream = stdout;
1071 } else if (0 == strcmp(arg, "tree"))
1072 curp->outtype = OUTT_TREE;
1073 else if (0 == strcmp(arg, "man"))
1074 curp->outtype = OUTT_MAN;
1075 else if (0 == strcmp(arg, "html"))
1076 curp->outtype = OUTT_HTML;
1077 else if (0 == strcmp(arg, "markdown"))
1078 curp->outtype = OUTT_MARKDOWN;
1079 else if (0 == strcmp(arg, "utf8"))
1080 curp->outtype = OUTT_UTF8;
1081 else if (0 == strcmp(arg, "locale"))
1082 curp->outtype = OUTT_LOCALE;
1083 else if (0 == strcmp(arg, "ps"))
1084 curp->outtype = OUTT_PS;
1085 else if (0 == strcmp(arg, "pdf"))
1086 curp->outtype = OUTT_PDF;
1087 else {
1088 warnx("-T %s: Bad argument", arg);
1089 return 0;
1090 }
1091
1092 return 1;
1093 }
1094
1095 static int
1096 woptions(struct curparse *curp, char *arg)
1097 {
1098 char *v, *o;
1099 const char *toks[11];
1100
1101 toks[0] = "stop";
1102 toks[1] = "all";
1103 toks[2] = "base";
1104 toks[3] = "style";
1105 toks[4] = "warning";
1106 toks[5] = "error";
1107 toks[6] = "unsupp";
1108 toks[7] = "fatal";
1109 toks[8] = "openbsd";
1110 toks[9] = "netbsd";
1111 toks[10] = NULL;
1112
1113 while (*arg) {
1114 o = arg;
1115 switch (getsubopt(&arg, (char * const *)toks, &v)) {
1116 case 0:
1117 curp->wstop = 1;
1118 break;
1119 case 1:
1120 case 2:
1121 curp->mmin = MANDOCERR_BASE;
1122 break;
1123 case 3:
1124 curp->mmin = MANDOCERR_STYLE;
1125 break;
1126 case 4:
1127 curp->mmin = MANDOCERR_WARNING;
1128 break;
1129 case 5:
1130 curp->mmin = MANDOCERR_ERROR;
1131 break;
1132 case 6:
1133 curp->mmin = MANDOCERR_UNSUPP;
1134 break;
1135 case 7:
1136 curp->mmin = MANDOCERR_MAX;
1137 break;
1138 case 8:
1139 curp->mmin = MANDOCERR_BASE;
1140 curp->os_e = MANDOC_OS_OPENBSD;
1141 break;
1142 case 9:
1143 curp->mmin = MANDOCERR_BASE;
1144 curp->os_e = MANDOC_OS_NETBSD;
1145 break;
1146 default:
1147 warnx("-W %s: Bad argument", o);
1148 return 0;
1149 }
1150 }
1151 return 1;
1152 }
1153
1154 static void
1155 mmsg(enum mandocerr t, enum mandoclevel lvl,
1156 const char *file, int line, int col, const char *msg)
1157 {
1158 const char *mparse_msg;
1159
1160 fprintf(mmsg_stream, "%s: %s:", getprogname(),
1161 file == NULL ? "<stdin>" : file);
1162
1163 if (line)
1164 fprintf(mmsg_stream, "%d:%d:", line, col + 1);
1165
1166 fprintf(mmsg_stream, " %s", mparse_strlevel(lvl));
1167
1168 if ((mparse_msg = mparse_strerror(t)) != NULL)
1169 fprintf(mmsg_stream, ": %s", mparse_msg);
1170
1171 if (msg)
1172 fprintf(mmsg_stream, ": %s", msg);
1173
1174 fputc('\n', mmsg_stream);
1175 }
1176
1177 static pid_t
1178 spawn_pager(struct tag_files *tag_files)
1179 {
1180 const struct timespec timeout = { 0, 100000000 }; /* 0.1s */
1181 #define MAX_PAGER_ARGS 16
1182 char *argv[MAX_PAGER_ARGS];
1183 const char *pager;
1184 char *cp;
1185 size_t cmdlen;
1186 int argc, use_ofn;
1187 pid_t pager_pid;
1188
1189 pager = getenv("MANPAGER");
1190 if (pager == NULL || *pager == '\0')
1191 pager = getenv("PAGER");
1192 if (pager == NULL || *pager == '\0')
1193 pager = "more -s";
1194 cp = mandoc_strdup(pager);
1195
1196 /*
1197 * Parse the pager command into words.
1198 * Intentionally do not do anything fancy here.
1199 */
1200
1201 argc = 0;
1202 while (argc + 5 < MAX_PAGER_ARGS) {
1203 argv[argc++] = cp;
1204 cp = strchr(cp, ' ');
1205 if (cp == NULL)
1206 break;
1207 *cp++ = '\0';
1208 while (*cp == ' ')
1209 cp++;
1210 if (*cp == '\0')
1211 break;
1212 }
1213
1214 /* For less(1), use the tag file. */
1215
1216 use_ofn = 1;
1217 if ((cmdlen = strlen(argv[0])) >= 4) {
1218 cp = argv[0] + cmdlen - 4;
1219 if (strcmp(cp, "less") == 0) {
1220 argv[argc++] = mandoc_strdup("-T");
1221 argv[argc++] = tag_files->tfn;
1222 if (tag_files->tagname != NULL) {
1223 argv[argc++] = mandoc_strdup("-t");
1224 argv[argc++] = tag_files->tagname;
1225 use_ofn = 0;
1226 }
1227 }
1228 }
1229 if (use_ofn)
1230 argv[argc++] = tag_files->ofn;
1231 argv[argc] = NULL;
1232
1233 switch (pager_pid = fork()) {
1234 case -1:
1235 err((int)MANDOCLEVEL_SYSERR, "fork");
1236 case 0:
1237 break;
1238 default:
1239 (void)setpgid(pager_pid, 0);
1240 (void)tcsetpgrp(tag_files->ofd, pager_pid);
1241 #if HAVE_PLEDGE
1242 if (pledge("stdio rpath tmppath tty proc", NULL) == -1)
1243 err((int)MANDOCLEVEL_SYSERR, "pledge");
1244 #endif
1245 tag_files->pager_pid = pager_pid;
1246 return pager_pid;
1247 }
1248
1249 /* The child process becomes the pager. */
1250
1251 if (dup2(tag_files->ofd, STDOUT_FILENO) == -1)
1252 err((int)MANDOCLEVEL_SYSERR, "pager stdout");
1253 close(tag_files->ofd);
1254 assert(tag_files->tfd == -1);
1255
1256 /* Do not start the pager before controlling the terminal. */
1257
1258 while (tcgetpgrp(STDOUT_FILENO) != getpid())
1259 nanosleep(&timeout, NULL);
1260
1261 execvp(argv[0], argv);
1262 err((int)MANDOCLEVEL_SYSERR, "exec %s", argv[0]);
1263 }