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