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