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