Annotation of src/usr.bin/mandoc/main.c, Revision 1.130
1.130 ! schwarze 1: /* $OpenBSD: main.c,v 1.129 2015/03/10 03:00:48 schwarze Exp $ */
1.1 kristaps 2: /*
1.95 schwarze 3: * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.118 schwarze 4: * Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
1.86 schwarze 5: * Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
1.1 kristaps 6: *
7: * Permission to use, copy, modify, and distribute this software for any
1.2 schwarze 8: * purpose with or without fee is hereby granted, provided that the above
9: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 10: *
1.2 schwarze 11: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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.
1.1 kristaps 18: */
19:
1.95 schwarze 20: #include <sys/types.h>
1.122 schwarze 21: #include <sys/param.h> /* MACHINE */
1.129 schwarze 22: #include <sys/wait.h>
1.95 schwarze 23:
1.1 kristaps 24: #include <assert.h>
1.95 schwarze 25: #include <ctype.h>
26: #include <errno.h>
27: #include <fcntl.h>
1.127 schwarze 28: #include <glob.h>
1.1 kristaps 29: #include <stdio.h>
1.17 schwarze 30: #include <stdint.h>
1.1 kristaps 31: #include <stdlib.h>
32: #include <string.h>
33: #include <unistd.h>
34:
1.30 schwarze 35: #include "mandoc.h"
1.89 schwarze 36: #include "mandoc_aux.h"
1.38 schwarze 37: #include "main.h"
1.1 kristaps 38: #include "mdoc.h"
39: #include "man.h"
1.95 schwarze 40: #include "manpath.h"
41: #include "mansearch.h"
42:
43: enum outmode {
44: OUTMODE_DEF = 0,
45: OUTMODE_FLN,
46: OUTMODE_LST,
47: OUTMODE_ALL,
48: OUTMODE_INT,
49: OUTMODE_ONE
50: };
1.1 kristaps 51:
1.16 schwarze 52: typedef void (*out_mdoc)(void *, const struct mdoc *);
53: typedef void (*out_man)(void *, const struct man *);
1.1 kristaps 54: typedef void (*out_free)(void *);
55:
56: enum outt {
1.76 schwarze 57: OUTT_ASCII = 0, /* -Tascii */
1.77 schwarze 58: OUTT_LOCALE, /* -Tlocale */
59: OUTT_UTF8, /* -Tutf8 */
1.76 schwarze 60: OUTT_TREE, /* -Ttree */
1.78 schwarze 61: OUTT_MAN, /* -Tman */
1.76 schwarze 62: OUTT_HTML, /* -Thtml */
63: OUTT_LINT, /* -Tlint */
64: OUTT_PS, /* -Tps */
65: OUTT_PDF /* -Tpdf */
1.1 kristaps 66: };
67:
68: struct curparse {
1.76 schwarze 69: struct mparse *mp;
1.102 schwarze 70: struct mchars *mchars; /* character table */
1.75 schwarze 71: enum mandoclevel wlevel; /* ignore messages below this */
72: int wstop; /* stop after a file with a warning */
1.90 schwarze 73: enum outt outtype; /* which output to use */
1.33 schwarze 74: out_mdoc outmdoc; /* mdoc output ptr */
1.90 schwarze 75: out_man outman; /* man output ptr */
1.33 schwarze 76: out_free outfree; /* free output ptr */
77: void *outdata; /* data for output */
78: char outopts[BUFSIZ]; /* buf of output opts */
79: };
80:
1.119 schwarze 81: static int fs_lookup(const struct manpaths *,
82: size_t ipath, const char *,
83: const char *, const char *,
84: struct manpage **, size_t *);
85: static void fs_search(const struct mansearch *,
86: const struct manpaths *, int, char**,
87: struct manpage **, size_t *);
1.103 schwarze 88: static int koptions(int *, char *);
1.79 schwarze 89: int mandocdb(int, char**);
1.87 schwarze 90: static int moptions(int *, char *);
1.76 schwarze 91: static void mmsg(enum mandocerr, enum mandoclevel,
92: const char *, int, int, const char *);
1.90 schwarze 93: static void parse(struct curparse *, int,
1.76 schwarze 94: const char *, enum mandoclevel *);
1.105 schwarze 95: static enum mandoclevel passthrough(const char *, int, int);
1.130 ! schwarze 96: static pid_t spawn_pager(void);
1.27 schwarze 97: static int toptions(struct curparse *, char *);
1.95 schwarze 98: static void usage(enum argmode) __attribute__((noreturn));
1.45 schwarze 99: static int woptions(struct curparse *, char *);
1.1 kristaps 100:
1.95 schwarze 101: static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
1.110 schwarze 102: static char help_arg[] = "help";
103: static char *help_argv[] = {help_arg, NULL};
1.19 schwarze 104: static const char *progname;
1.1 kristaps 105:
1.90 schwarze 106:
1.1 kristaps 107: int
108: main(int argc, char *argv[])
109: {
110: struct curparse curp;
1.95 schwarze 111: struct mansearch search;
112: struct manpaths paths;
1.111 schwarze 113: char *auxpaths;
1.95 schwarze 114: char *defos;
1.113 schwarze 115: unsigned char *uc;
1.95 schwarze 116: struct manpage *res, *resp;
1.111 schwarze 117: char *conf_file, *defpaths;
1.95 schwarze 118: size_t isec, i, sz;
1.111 schwarze 119: int prio, best_prio, synopsis_only;
1.95 schwarze 120: char sec;
1.120 schwarze 121: enum mandoclevel rc, rctmp;
1.95 schwarze 122: enum outmode outmode;
1.100 schwarze 123: int fd;
1.95 schwarze 124: int show_usage;
1.87 schwarze 125: int options;
1.95 schwarze 126: int c;
1.130 ! schwarze 127: pid_t pager_pid; /* 0: don't use; 1: not yet spawned. */
1.1 kristaps 128:
1.124 schwarze 129: if (argc < 1)
130: progname = "mandoc";
131: else if ((progname = strrchr(argv[0], '/')) == NULL)
1.19 schwarze 132: progname = argv[0];
133: else
134: ++progname;
1.79 schwarze 135:
1.82 schwarze 136: if (0 == strncmp(progname, "mandocdb", 8) ||
137: 0 == strncmp(progname, "makewhatis", 10))
1.79 schwarze 138: return(mandocdb(argc, argv));
1.19 schwarze 139:
1.95 schwarze 140: /* Search options. */
141:
142: memset(&paths, 0, sizeof(struct manpaths));
1.111 schwarze 143: conf_file = defpaths = NULL;
144: auxpaths = NULL;
1.95 schwarze 145:
146: memset(&search, 0, sizeof(struct mansearch));
147: search.outkey = "Nd";
148:
149: if (strcmp(progname, "man") == 0)
150: search.argmode = ARG_NAME;
151: else if (strncmp(progname, "apropos", 7) == 0)
152: search.argmode = ARG_EXPR;
153: else if (strncmp(progname, "whatis", 6) == 0)
154: search.argmode = ARG_WORD;
1.110 schwarze 155: else if (strncmp(progname, "help", 4) == 0)
156: search.argmode = ARG_NAME;
1.95 schwarze 157: else
158: search.argmode = ARG_FILE;
159:
160: /* Parser and formatter options. */
161:
1.19 schwarze 162: memset(&curp, 0, sizeof(struct curparse));
1.109 schwarze 163: curp.outtype = OUTT_LOCALE;
1.121 schwarze 164: curp.wlevel = MANDOCLEVEL_BADARG;
1.103 schwarze 165: options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
1.83 schwarze 166: defos = NULL;
1.1 kristaps 167:
1.130 ! schwarze 168: pager_pid = 1;
1.95 schwarze 169: show_usage = 0;
1.105 schwarze 170: synopsis_only = 0;
1.95 schwarze 171: outmode = OUTMODE_DEF;
172:
1.103 schwarze 173: while (-1 != (c = getopt(argc, argv,
174: "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
1.1 kristaps 175: switch (c) {
1.95 schwarze 176: case 'a':
177: outmode = OUTMODE_ALL;
178: break;
179: case 'C':
180: conf_file = optarg;
181: break;
182: case 'c':
1.130 ! schwarze 183: pager_pid = 0;
1.95 schwarze 184: break;
185: case 'f':
186: search.argmode = ARG_WORD;
187: break;
1.98 schwarze 188: case 'h':
189: (void)strlcat(curp.outopts, "synopsis,", BUFSIZ);
1.105 schwarze 190: synopsis_only = 1;
1.130 ! schwarze 191: pager_pid = 0;
1.98 schwarze 192: outmode = OUTMODE_ALL;
193: break;
1.90 schwarze 194: case 'I':
1.83 schwarze 195: if (strncmp(optarg, "os=", 3)) {
1.90 schwarze 196: fprintf(stderr,
1.115 schwarze 197: "%s: -I %s: Bad argument\n",
1.93 schwarze 198: progname, optarg);
1.83 schwarze 199: return((int)MANDOCLEVEL_BADARG);
200: }
201: if (defos) {
1.90 schwarze 202: fprintf(stderr,
1.115 schwarze 203: "%s: -I %s: Duplicate argument\n",
1.93 schwarze 204: progname, optarg);
1.83 schwarze 205: return((int)MANDOCLEVEL_BADARG);
206: }
207: defos = mandoc_strdup(optarg + 3);
208: break;
1.95 schwarze 209: case 'i':
210: outmode = OUTMODE_INT;
211: break;
1.103 schwarze 212: case 'K':
213: if ( ! koptions(&options, optarg))
214: return((int)MANDOCLEVEL_BADARG);
215: break;
1.95 schwarze 216: case 'k':
217: search.argmode = ARG_EXPR;
218: break;
1.96 schwarze 219: case 'l':
220: search.argmode = ARG_FILE;
221: outmode = OUTMODE_ALL;
222: break;
1.95 schwarze 223: case 'M':
224: defpaths = optarg;
225: break;
1.90 schwarze 226: case 'm':
1.95 schwarze 227: auxpaths = optarg;
1.1 kristaps 228: break;
1.90 schwarze 229: case 'O':
1.95 schwarze 230: search.outkey = optarg;
1.18 schwarze 231: (void)strlcat(curp.outopts, optarg, BUFSIZ);
232: (void)strlcat(curp.outopts, ",", BUFSIZ);
1.17 schwarze 233: break;
1.95 schwarze 234: case 'S':
235: search.arch = optarg;
236: break;
237: case 's':
238: search.sec = optarg;
239: break;
1.90 schwarze 240: case 'T':
1.22 schwarze 241: if ( ! toptions(&curp, optarg))
1.47 schwarze 242: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 243: break;
1.90 schwarze 244: case 'W':
1.45 schwarze 245: if ( ! woptions(&curp, optarg))
1.47 schwarze 246: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 247: break;
1.95 schwarze 248: case 'w':
249: outmode = OUTMODE_FLN;
250: break;
1.1 kristaps 251: default:
1.95 schwarze 252: show_usage = 1;
253: break;
254: }
255: }
256:
257: if (show_usage)
258: usage(search.argmode);
259:
260: /* Postprocess options. */
261:
262: if (outmode == OUTMODE_DEF) {
263: switch (search.argmode) {
264: case ARG_FILE:
265: outmode = OUTMODE_ALL;
1.130 ! schwarze 266: pager_pid = 0;
1.95 schwarze 267: break;
268: case ARG_NAME:
269: outmode = OUTMODE_ONE;
270: break;
271: default:
272: outmode = OUTMODE_LST;
273: break;
274: }
275: }
276:
277: /* Parse arguments. */
278:
1.124 schwarze 279: if (argc > 0) {
280: argc -= optind;
281: argv += optind;
282: }
1.95 schwarze 283: resp = NULL;
284:
1.110 schwarze 285: /*
286: * Quirks for help(1)
287: * and for a man(1) section argument without -s.
288: */
289:
290: if (search.argmode == ARG_NAME) {
291: if (*progname == 'h') {
292: if (argc == 0) {
293: argv = help_argv;
294: argc = 1;
295: }
1.117 schwarze 296: } else if (argc > 1 &&
1.128 schwarze 297: ((uc = (unsigned char *)argv[0]) != NULL) &&
1.113 schwarze 298: ((isdigit(uc[0]) && (uc[1] == '\0' ||
299: (isalpha(uc[1]) && uc[2] == '\0'))) ||
300: (uc[0] == 'n' && uc[1] == '\0'))) {
1.128 schwarze 301: search.sec = (char *)uc;
1.110 schwarze 302: argv++;
303: argc--;
304: }
1.122 schwarze 305: if (search.arch == NULL)
306: search.arch = getenv("MACHINE");
307: if (search.arch == NULL)
308: search.arch = MACHINE;
1.95 schwarze 309: }
310:
311: rc = MANDOCLEVEL_OK;
312:
313: /* man(1), whatis(1), apropos(1) */
314:
315: if (search.argmode != ARG_FILE) {
316: if (argc == 0)
317: usage(search.argmode);
1.107 schwarze 318:
319: if (search.argmode == ARG_NAME &&
320: outmode == OUTMODE_ONE)
321: search.firstmatch = 1;
1.95 schwarze 322:
323: /* Access the mandoc database. */
324:
325: manpath_parse(&paths, conf_file, defpaths, auxpaths);
326: mansearch_setup(1);
327: if( ! mansearch(&search, &paths, argc, argv, &res, &sz))
328: usage(search.argmode);
1.119 schwarze 329:
330: if (sz == 0 && search.argmode == ARG_NAME)
331: fs_search(&search, &paths, argc, argv, &res, &sz);
1.95 schwarze 332:
333: if (sz == 0) {
334: rc = MANDOCLEVEL_BADARG;
335: goto out;
336: }
337:
338: /*
339: * For standard man(1) and -a output mode,
340: * prepare for copying filename pointers
341: * into the program parameter array.
342: */
343:
344: if (outmode == OUTMODE_ONE) {
345: argc = 1;
346: best_prio = 10;
347: } else if (outmode == OUTMODE_ALL)
348: argc = (int)sz;
349:
350: /* Iterate all matching manuals. */
351:
1.119 schwarze 352: resp = res;
1.95 schwarze 353: for (i = 0; i < sz; i++) {
354: if (outmode == OUTMODE_FLN)
355: puts(res[i].file);
356: else if (outmode == OUTMODE_LST)
357: printf("%s - %s\n", res[i].names,
358: res[i].output == NULL ? "" :
359: res[i].output);
360: else if (outmode == OUTMODE_ONE) {
361: /* Search for the best section. */
362: isec = strcspn(res[i].file, "123456789");
363: sec = res[i].file[isec];
364: if ('\0' == sec)
365: continue;
366: prio = sec_prios[sec - '1'];
367: if (prio >= best_prio)
368: continue;
369: best_prio = prio;
370: resp = res + i;
371: }
1.1 kristaps 372: }
373:
1.95 schwarze 374: /*
375: * For man(1), -a and -i output mode, fall through
376: * to the main mandoc(1) code iterating files
377: * and running the parsers on each of them.
378: */
379:
380: if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
381: goto out;
382: }
383:
384: /* mandoc(1) */
385:
1.115 schwarze 386: if (search.argmode == ARG_FILE && ! moptions(&options, auxpaths))
1.95 schwarze 387: return((int)MANDOCLEVEL_BADARG);
388:
1.102 schwarze 389: curp.mchars = mchars_alloc();
390: curp.mp = mparse_alloc(options, curp.wlevel, mmsg,
391: curp.mchars, defos);
1.76 schwarze 392:
1.80 schwarze 393: /*
394: * Conditionally start up the lookaside buffer before parsing.
395: */
396: if (OUTT_MAN == curp.outtype)
397: mparse_keep(curp.mp);
398:
1.124 schwarze 399: if (argc < 1) {
1.130 ! schwarze 400: if (pager_pid == 1 && isatty(STDOUT_FILENO))
! 401: pager_pid = spawn_pager();
1.76 schwarze 402: parse(&curp, STDIN_FILENO, "<stdin>", &rc);
1.118 schwarze 403: }
1.27 schwarze 404:
1.124 schwarze 405: while (argc > 0) {
1.120 schwarze 406: rctmp = mparse_open(curp.mp, &fd,
1.118 schwarze 407: resp != NULL ? resp->file : *argv);
1.120 schwarze 408: if (rc < rctmp)
409: rc = rctmp;
1.118 schwarze 410:
411: if (fd != -1) {
1.130 ! schwarze 412: if (pager_pid == 1 && isatty(STDOUT_FILENO))
! 413: pager_pid = spawn_pager();
1.118 schwarze 414:
415: if (resp == NULL)
416: parse(&curp, fd, *argv, &rc);
1.100 schwarze 417: else if (resp->form & FORM_SRC) {
1.97 schwarze 418: /* For .so only; ignore failure. */
419: chdir(paths.paths[resp->ipath]);
1.100 schwarze 420: parse(&curp, fd, resp->file, &rc);
1.120 schwarze 421: } else {
422: rctmp = passthrough(resp->file, fd,
1.105 schwarze 423: synopsis_only);
1.120 schwarze 424: if (rc < rctmp)
425: rc = rctmp;
426: }
1.118 schwarze 427:
1.120 schwarze 428: rctmp = mparse_wait(curp.mp);
429: if (rc < rctmp)
430: rc = rctmp;
1.118 schwarze 431:
432: if (argc > 1 && curp.outtype <= OUTT_UTF8)
433: ascii_sepline(curp.outdata);
1.100 schwarze 434: }
435:
1.76 schwarze 436: if (MANDOCLEVEL_OK != rc && curp.wstop)
1.27 schwarze 437: break;
1.116 schwarze 438:
1.118 schwarze 439: if (resp != NULL)
440: resp++;
441: else
442: argv++;
443: if (--argc)
444: mparse_reset(curp.mp);
1.1 kristaps 445: }
446:
447: if (curp.outfree)
448: (*curp.outfree)(curp.outdata);
1.102 schwarze 449: mparse_free(curp.mp);
450: mchars_free(curp.mchars);
1.95 schwarze 451:
452: out:
453: if (search.argmode != ARG_FILE) {
1.97 schwarze 454: manpath_free(&paths);
1.95 schwarze 455: mansearch_free(res, sz);
456: mansearch_setup(0);
457: }
458:
1.83 schwarze 459: free(defos);
1.1 kristaps 460:
1.129 schwarze 461: /*
1.130 ! schwarze 462: * If a pager is attached, flush the pipe leading to it
! 463: * and signal end of file such that the user can browse
! 464: * to the end. Then wait for the user to close the pager.
1.129 schwarze 465: */
466:
1.130 ! schwarze 467: if (pager_pid != 0 && pager_pid != 1) {
! 468: fclose(stdout);
! 469: waitpid(pager_pid, NULL, 0);
! 470: }
1.129 schwarze 471:
1.76 schwarze 472: return((int)rc);
1.1 kristaps 473: }
474:
1.20 schwarze 475: static void
1.95 schwarze 476: usage(enum argmode argmode)
1.1 kristaps 477: {
478:
1.95 schwarze 479: switch (argmode) {
480: case ARG_FILE:
1.126 schwarze 481: fputs("usage: mandoc [-acfhkl] [-Ios=name] "
1.104 jmc 482: "[-Kencoding] [-mformat] [-Ooption]\n"
483: "\t [-Toutput] [-Wlevel] [file ...]\n", stderr);
1.95 schwarze 484: break;
485: case ARG_NAME:
1.126 schwarze 486: fputs("usage: man [-acfhklw] [-C file] [-I os=name] "
1.114 jmc 487: "[-K encoding] [-M path] [-m path]\n"
488: "\t [-O option=value] [-S subsection] [-s section] "
489: "[-T output] [-W level]\n"
1.95 schwarze 490: "\t [section] name ...\n", stderr);
491: break;
492: case ARG_WORD:
1.126 schwarze 493: fputs("usage: whatis [-acfhklw] [-C file] "
1.96 schwarze 494: "[-M path] [-m path] [-O outkey] [-S arch]\n"
495: "\t [-s section] name ...\n", stderr);
1.95 schwarze 496: break;
497: case ARG_EXPR:
1.126 schwarze 498: fputs("usage: apropos [-acfhklw] [-C file] "
1.96 schwarze 499: "[-M path] [-m path] [-O outkey] [-S arch]\n"
1.95 schwarze 500: "\t [-s section] expression ...\n", stderr);
501: break;
502: }
1.47 schwarze 503: exit((int)MANDOCLEVEL_BADARG);
1.119 schwarze 504: }
505:
506: static int
507: fs_lookup(const struct manpaths *paths, size_t ipath,
508: const char *sec, const char *arch, const char *name,
509: struct manpage **res, size_t *ressz)
510: {
1.127 schwarze 511: glob_t globinfo;
1.119 schwarze 512: struct manpage *page;
513: char *file;
1.127 schwarze 514: int form, globres;
1.119 schwarze 515:
1.127 schwarze 516: form = FORM_SRC;
1.119 schwarze 517: mandoc_asprintf(&file, "%s/man%s/%s.%s",
518: paths->paths[ipath], sec, name, sec);
1.127 schwarze 519: if (access(file, R_OK) != -1)
1.119 schwarze 520: goto found;
521: free(file);
522:
523: mandoc_asprintf(&file, "%s/cat%s/%s.0",
524: paths->paths[ipath], sec, name);
525: if (access(file, R_OK) != -1) {
526: form = FORM_CAT;
527: goto found;
528: }
529: free(file);
530:
531: if (arch != NULL) {
532: mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
533: paths->paths[ipath], sec, arch, name, sec);
1.127 schwarze 534: if (access(file, R_OK) != -1)
1.119 schwarze 535: goto found;
536: free(file);
537: }
1.127 schwarze 538:
539: mandoc_asprintf(&file, "%s/man%s/%s.*",
540: paths->paths[ipath], sec, name);
541: globres = glob(file, 0, NULL, &globinfo);
542: if (globres != 0 && globres != GLOB_NOMATCH)
543: fprintf(stderr, "%s: %s: glob: %s\n",
544: progname, file, strerror(errno));
545: free(file);
546: if (globres == 0)
547: file = mandoc_strdup(*globinfo.gl_pathv);
548: globfree(&globinfo);
549: if (globres != 0)
550: return(0);
1.119 schwarze 551:
552: found:
553: fprintf(stderr, "%s: outdated mandoc.db lacks %s(%s) entry,\n"
554: " consider running # makewhatis %s\n",
555: progname, name, sec, paths->paths[ipath]);
1.125 schwarze 556:
1.119 schwarze 557: *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
558: page = *res + (*ressz - 1);
559: page->file = file;
560: page->names = NULL;
561: page->output = NULL;
562: page->ipath = ipath;
563: page->bits = NAME_FILE & NAME_MASK;
564: page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
565: page->form = form;
566: return(1);
567: }
568:
569: static void
570: fs_search(const struct mansearch *cfg, const struct manpaths *paths,
571: int argc, char **argv, struct manpage **res, size_t *ressz)
572: {
573: const char *const sections[] =
574: {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
575: const size_t nsec = sizeof(sections)/sizeof(sections[0]);
576:
577: size_t ipath, isec, lastsz;
578:
579: assert(cfg->argmode == ARG_NAME);
580:
581: *res = NULL;
582: *ressz = lastsz = 0;
583: while (argc) {
584: for (ipath = 0; ipath < paths->sz; ipath++) {
585: if (cfg->sec != NULL) {
586: if (fs_lookup(paths, ipath, cfg->sec,
587: cfg->arch, *argv, res, ressz) &&
588: cfg->firstmatch)
589: return;
590: } else for (isec = 0; isec < nsec; isec++)
591: if (fs_lookup(paths, ipath, sections[isec],
592: cfg->arch, *argv, res, ressz) &&
593: cfg->firstmatch)
594: return;
595: }
596: if (*ressz == lastsz)
597: fprintf(stderr,
598: "%s: No entry for %s in the manual.\n",
599: progname, *argv);
600: lastsz = *ressz;
601: argv++;
602: argc--;
603: }
1.1 kristaps 604: }
605:
1.27 schwarze 606: static void
1.90 schwarze 607: parse(struct curparse *curp, int fd, const char *file,
608: enum mandoclevel *level)
1.52 schwarze 609: {
1.76 schwarze 610: enum mandoclevel rc;
611: struct mdoc *mdoc;
612: struct man *man;
1.52 schwarze 613:
1.76 schwarze 614: /* Begin by parsing the file itself. */
1.52 schwarze 615:
1.76 schwarze 616: assert(file);
617: assert(fd >= -1);
1.52 schwarze 618:
1.76 schwarze 619: rc = mparse_readfd(curp->mp, fd, file);
1.1 kristaps 620:
1.51 schwarze 621: /*
1.76 schwarze 622: * With -Wstop and warnings or errors of at least the requested
623: * level, do not produce output.
1.51 schwarze 624: */
625:
1.76 schwarze 626: if (MANDOCLEVEL_OK != rc && curp->wstop)
1.51 schwarze 627: goto cleanup;
628:
629: /* If unset, allocate output dev now (if applicable). */
630:
631: if ( ! (curp->outman && curp->outmdoc)) {
632: switch (curp->outtype) {
1.90 schwarze 633: case OUTT_HTML:
1.102 schwarze 634: curp->outdata = html_alloc(curp->mchars,
635: curp->outopts);
1.77 schwarze 636: curp->outfree = html_free;
637: break;
1.90 schwarze 638: case OUTT_UTF8:
1.102 schwarze 639: curp->outdata = utf8_alloc(curp->mchars,
640: curp->outopts);
1.77 schwarze 641: curp->outfree = ascii_free;
642: break;
1.90 schwarze 643: case OUTT_LOCALE:
1.102 schwarze 644: curp->outdata = locale_alloc(curp->mchars,
645: curp->outopts);
1.77 schwarze 646: curp->outfree = ascii_free;
1.51 schwarze 647: break;
1.90 schwarze 648: case OUTT_ASCII:
1.102 schwarze 649: curp->outdata = ascii_alloc(curp->mchars,
650: curp->outopts);
1.51 schwarze 651: curp->outfree = ascii_free;
652: break;
1.90 schwarze 653: case OUTT_PDF:
1.102 schwarze 654: curp->outdata = pdf_alloc(curp->mchars,
655: curp->outopts);
1.51 schwarze 656: curp->outfree = pspdf_free;
657: break;
1.90 schwarze 658: case OUTT_PS:
1.102 schwarze 659: curp->outdata = ps_alloc(curp->mchars,
660: curp->outopts);
1.51 schwarze 661: curp->outfree = pspdf_free;
662: break;
663: default:
664: break;
665: }
666:
667: switch (curp->outtype) {
1.90 schwarze 668: case OUTT_HTML:
1.51 schwarze 669: curp->outman = html_man;
670: curp->outmdoc = html_mdoc;
671: break;
1.90 schwarze 672: case OUTT_TREE:
1.51 schwarze 673: curp->outman = tree_man;
674: curp->outmdoc = tree_mdoc;
675: break;
1.90 schwarze 676: case OUTT_MAN:
1.78 schwarze 677: curp->outmdoc = man_mdoc;
1.80 schwarze 678: curp->outman = man_man;
1.78 schwarze 679: break;
1.90 schwarze 680: case OUTT_PDF:
1.51 schwarze 681: /* FALLTHROUGH */
1.90 schwarze 682: case OUTT_ASCII:
1.51 schwarze 683: /* FALLTHROUGH */
1.90 schwarze 684: case OUTT_UTF8:
1.77 schwarze 685: /* FALLTHROUGH */
1.90 schwarze 686: case OUTT_LOCALE:
1.77 schwarze 687: /* FALLTHROUGH */
1.90 schwarze 688: case OUTT_PS:
1.51 schwarze 689: curp->outman = terminal_man;
690: curp->outmdoc = terminal_mdoc;
691: break;
692: default:
693: break;
694: }
695: }
696:
1.88 schwarze 697: mparse_result(curp->mp, &mdoc, &man, NULL);
1.76 schwarze 698:
1.51 schwarze 699: /* Execute the out device, if it exists. */
700:
1.76 schwarze 701: if (man && curp->outman)
702: (*curp->outman)(curp->outdata, man);
703: if (mdoc && curp->outmdoc)
704: (*curp->outmdoc)(curp->outdata, mdoc);
1.51 schwarze 705:
1.118 schwarze 706: cleanup:
1.76 schwarze 707: if (*level < rc)
708: *level = rc;
1.1 kristaps 709: }
710:
1.95 schwarze 711: static enum mandoclevel
1.105 schwarze 712: passthrough(const char *file, int fd, int synopsis_only)
1.95 schwarze 713: {
1.105 schwarze 714: const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
715: const char synr[] = "SYNOPSIS";
716:
717: FILE *stream;
1.95 schwarze 718: const char *syscall;
1.105 schwarze 719: char *line;
720: size_t len, off;
721: ssize_t nw;
722: int print;
1.116 schwarze 723:
724: fflush(stdout);
1.105 schwarze 725:
726: if ((stream = fdopen(fd, "r")) == NULL) {
727: close(fd);
728: syscall = "fdopen";
729: goto fail;
730: }
1.95 schwarze 731:
1.105 schwarze 732: print = 0;
733: while ((line = fgetln(stream, &len)) != NULL) {
734: if (synopsis_only) {
735: if (print) {
736: if ( ! isspace((unsigned char)*line))
737: goto done;
738: while (len &&
739: isspace((unsigned char)*line)) {
740: line++;
741: len--;
742: }
743: } else {
744: if ((len == sizeof(synb) &&
745: ! strncmp(line, synb, len - 1)) ||
746: (len == sizeof(synr) &&
747: ! strncmp(line, synr, len - 1)))
748: print = 1;
749: continue;
750: }
751: }
752: for (off = 0; off < len; off += nw)
753: if ((nw = write(STDOUT_FILENO, line + off,
754: len - off)) == -1 || nw == 0) {
755: fclose(stream);
1.95 schwarze 756: syscall = "write";
757: goto fail;
758: }
1.105 schwarze 759: }
1.95 schwarze 760:
1.105 schwarze 761: if (ferror(stream)) {
762: fclose(stream);
763: syscall = "fgetln";
764: goto fail;
765: }
1.101 schwarze 766:
1.105 schwarze 767: done:
768: fclose(stream);
769: return(MANDOCLEVEL_OK);
1.95 schwarze 770:
771: fail:
772: fprintf(stderr, "%s: %s: SYSERR: %s: %s",
773: progname, file, syscall, strerror(errno));
774: return(MANDOCLEVEL_SYSERR);
1.103 schwarze 775: }
776:
777: static int
778: koptions(int *options, char *arg)
779: {
780:
781: if ( ! strcmp(arg, "utf-8")) {
782: *options |= MPARSE_UTF8;
783: *options &= ~MPARSE_LATIN1;
784: } else if ( ! strcmp(arg, "iso-8859-1")) {
785: *options |= MPARSE_LATIN1;
786: *options &= ~MPARSE_UTF8;
787: } else if ( ! strcmp(arg, "us-ascii")) {
788: *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
789: } else {
1.115 schwarze 790: fprintf(stderr, "%s: -K %s: Bad argument\n",
1.103 schwarze 791: progname, arg);
792: return(0);
793: }
794: return(1);
1.95 schwarze 795: }
796:
1.1 kristaps 797: static int
1.87 schwarze 798: moptions(int *options, char *arg)
1.1 kristaps 799: {
800:
1.95 schwarze 801: if (arg == NULL)
802: /* nothing to do */;
803: else if (0 == strcmp(arg, "doc"))
1.87 schwarze 804: *options |= MPARSE_MDOC;
1.1 kristaps 805: else if (0 == strcmp(arg, "andoc"))
1.87 schwarze 806: /* nothing to do */;
1.1 kristaps 807: else if (0 == strcmp(arg, "an"))
1.87 schwarze 808: *options |= MPARSE_MAN;
1.1 kristaps 809: else {
1.115 schwarze 810: fprintf(stderr, "%s: -m %s: Bad argument\n",
1.93 schwarze 811: progname, arg);
1.1 kristaps 812: return(0);
813: }
814:
815: return(1);
816: }
817:
818: static int
1.22 schwarze 819: toptions(struct curparse *curp, char *arg)
1.1 kristaps 820: {
821:
822: if (0 == strcmp(arg, "ascii"))
1.22 schwarze 823: curp->outtype = OUTT_ASCII;
824: else if (0 == strcmp(arg, "lint")) {
825: curp->outtype = OUTT_LINT;
1.45 schwarze 826: curp->wlevel = MANDOCLEVEL_WARNING;
1.76 schwarze 827: } else if (0 == strcmp(arg, "tree"))
1.22 schwarze 828: curp->outtype = OUTT_TREE;
1.78 schwarze 829: else if (0 == strcmp(arg, "man"))
830: curp->outtype = OUTT_MAN;
1.17 schwarze 831: else if (0 == strcmp(arg, "html"))
1.22 schwarze 832: curp->outtype = OUTT_HTML;
1.77 schwarze 833: else if (0 == strcmp(arg, "utf8"))
834: curp->outtype = OUTT_UTF8;
835: else if (0 == strcmp(arg, "locale"))
836: curp->outtype = OUTT_LOCALE;
1.21 schwarze 837: else if (0 == strcmp(arg, "xhtml"))
1.102 schwarze 838: curp->outtype = OUTT_HTML;
1.36 schwarze 839: else if (0 == strcmp(arg, "ps"))
840: curp->outtype = OUTT_PS;
1.43 schwarze 841: else if (0 == strcmp(arg, "pdf"))
842: curp->outtype = OUTT_PDF;
1.1 kristaps 843: else {
1.115 schwarze 844: fprintf(stderr, "%s: -T %s: Bad argument\n",
1.93 schwarze 845: progname, arg);
1.1 kristaps 846: return(0);
847: }
848:
849: return(1);
850: }
851:
852: static int
1.45 schwarze 853: woptions(struct curparse *curp, char *arg)
1.1 kristaps 854: {
1.10 schwarze 855: char *v, *o;
1.123 schwarze 856: const char *toks[7];
1.1 kristaps 857:
1.45 schwarze 858: toks[0] = "stop";
859: toks[1] = "all";
860: toks[2] = "warning";
861: toks[3] = "error";
1.123 schwarze 862: toks[4] = "unsupp";
863: toks[5] = "fatal";
864: toks[6] = NULL;
1.1 kristaps 865:
1.10 schwarze 866: while (*arg) {
867: o = arg;
1.17 schwarze 868: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.90 schwarze 869: case 0:
1.45 schwarze 870: curp->wstop = 1;
1.1 kristaps 871: break;
1.90 schwarze 872: case 1:
1.45 schwarze 873: /* FALLTHROUGH */
1.90 schwarze 874: case 2:
1.45 schwarze 875: curp->wlevel = MANDOCLEVEL_WARNING;
1.1 kristaps 876: break;
1.90 schwarze 877: case 3:
1.45 schwarze 878: curp->wlevel = MANDOCLEVEL_ERROR;
1.1 kristaps 879: break;
1.90 schwarze 880: case 4:
1.123 schwarze 881: curp->wlevel = MANDOCLEVEL_UNSUPP;
882: break;
883: case 5:
1.121 schwarze 884: curp->wlevel = MANDOCLEVEL_BADARG;
1.19 schwarze 885: break;
1.1 kristaps 886: default:
1.115 schwarze 887: fprintf(stderr, "%s: -W %s: Bad argument\n",
1.93 schwarze 888: progname, o);
1.1 kristaps 889: return(0);
890: }
1.10 schwarze 891: }
1.1 kristaps 892:
893: return(1);
894: }
895:
1.75 schwarze 896: static void
1.90 schwarze 897: mmsg(enum mandocerr t, enum mandoclevel lvl,
1.76 schwarze 898: const char *file, int line, int col, const char *msg)
1.30 schwarze 899: {
1.94 schwarze 900: const char *mparse_msg;
1.45 schwarze 901:
1.92 schwarze 902: fprintf(stderr, "%s: %s:", progname, file);
903:
904: if (line)
905: fprintf(stderr, "%d:%d:", line, col + 1);
906:
1.94 schwarze 907: fprintf(stderr, " %s", mparse_strlevel(lvl));
908:
909: if (NULL != (mparse_msg = mparse_strerror(t)))
910: fprintf(stderr, ": %s", mparse_msg);
1.30 schwarze 911:
912: if (msg)
913: fprintf(stderr, ": %s", msg);
1.76 schwarze 914:
1.30 schwarze 915: fputc('\n', stderr);
1.95 schwarze 916: }
917:
1.130 ! schwarze 918: static pid_t
1.95 schwarze 919: spawn_pager(void)
920: {
921: #define MAX_PAGER_ARGS 16
922: char *argv[MAX_PAGER_ARGS];
923: const char *pager;
924: char *cp;
925: int fildes[2];
926: int argc;
1.130 ! schwarze 927: pid_t pager_pid;
1.95 schwarze 928:
929: if (pipe(fildes) == -1) {
930: fprintf(stderr, "%s: pipe: %s\n",
931: progname, strerror(errno));
1.130 ! schwarze 932: return(0);
1.95 schwarze 933: }
934:
1.130 ! schwarze 935: switch (pager_pid = fork()) {
1.95 schwarze 936: case -1:
937: fprintf(stderr, "%s: fork: %s\n",
938: progname, strerror(errno));
939: exit((int)MANDOCLEVEL_SYSERR);
940: case 0:
1.129 schwarze 941: break;
942: default:
1.95 schwarze 943: close(fildes[0]);
944: if (dup2(fildes[1], STDOUT_FILENO) == -1) {
945: fprintf(stderr, "%s: dup output: %s\n",
946: progname, strerror(errno));
947: exit((int)MANDOCLEVEL_SYSERR);
948: }
1.129 schwarze 949: close(fildes[1]);
1.130 ! schwarze 950: return(pager_pid);
1.95 schwarze 951: }
952:
1.129 schwarze 953: /* The child process becomes the pager. */
1.95 schwarze 954:
955: close(fildes[1]);
956: if (dup2(fildes[0], STDIN_FILENO) == -1) {
957: fprintf(stderr, "%s: dup input: %s\n",
958: progname, strerror(errno));
959: exit((int)MANDOCLEVEL_SYSERR);
960: }
1.129 schwarze 961: close(fildes[0]);
1.95 schwarze 962:
963: pager = getenv("MANPAGER");
964: if (pager == NULL || *pager == '\0')
965: pager = getenv("PAGER");
966: if (pager == NULL || *pager == '\0')
967: pager = "/usr/bin/more -s";
968: cp = mandoc_strdup(pager);
969:
970: /*
971: * Parse the pager command into words.
972: * Intentionally do not do anything fancy here.
973: */
974:
975: argc = 0;
976: while (argc + 1 < MAX_PAGER_ARGS) {
977: argv[argc++] = cp;
978: cp = strchr(cp, ' ');
979: if (cp == NULL)
980: break;
981: *cp++ = '\0';
982: while (*cp == ' ')
983: cp++;
984: if (*cp == '\0')
985: break;
986: }
987: argv[argc] = NULL;
988:
989: /* Hand over to the pager. */
990:
991: execvp(argv[0], argv);
992: fprintf(stderr, "%s: exec: %s\n",
993: progname, strerror(errno));
994: exit((int)MANDOCLEVEL_SYSERR);
1.30 schwarze 995: }