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