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