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