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