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