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