Annotation of src/usr.bin/mandoc/main.c, Revision 1.129
1.129 ! schwarze 1: /* $OpenBSD: main.c,v 1.128 2015/03/09 21:30:27 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.95 schwarze 96: static void 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;
125: int use_pager;
1.87 schwarze 126: int options;
1.95 schwarze 127: int c;
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.95 schwarze 168: use_pager = 1;
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':
183: use_pager = 0;
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.106 schwarze 191: use_pager = 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;
266: use_pager = 0;
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.118 schwarze 400: if (use_pager && isatty(STDOUT_FILENO))
401: 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) {
412: if (use_pager && isatty(STDOUT_FILENO))
413: spawn_pager();
414: use_pager = 0;
415:
416: if (resp == NULL)
417: parse(&curp, fd, *argv, &rc);
1.100 schwarze 418: else if (resp->form & FORM_SRC) {
1.97 schwarze 419: /* For .so only; ignore failure. */
420: chdir(paths.paths[resp->ipath]);
1.100 schwarze 421: parse(&curp, fd, resp->file, &rc);
1.120 schwarze 422: } else {
423: rctmp = passthrough(resp->file, fd,
1.105 schwarze 424: synopsis_only);
1.120 schwarze 425: if (rc < rctmp)
426: rc = rctmp;
427: }
1.118 schwarze 428:
1.120 schwarze 429: rctmp = mparse_wait(curp.mp);
430: if (rc < rctmp)
431: rc = rctmp;
1.118 schwarze 432:
433: if (argc > 1 && curp.outtype <= OUTT_UTF8)
434: ascii_sepline(curp.outdata);
1.100 schwarze 435: }
436:
1.76 schwarze 437: if (MANDOCLEVEL_OK != rc && curp.wstop)
1.27 schwarze 438: break;
1.116 schwarze 439:
1.118 schwarze 440: if (resp != NULL)
441: resp++;
442: else
443: argv++;
444: if (--argc)
445: mparse_reset(curp.mp);
1.1 kristaps 446: }
447:
448: if (curp.outfree)
449: (*curp.outfree)(curp.outdata);
1.102 schwarze 450: mparse_free(curp.mp);
451: mchars_free(curp.mchars);
1.95 schwarze 452:
453: out:
454: if (search.argmode != ARG_FILE) {
1.97 schwarze 455: manpath_free(&paths);
1.95 schwarze 456: mansearch_free(res, sz);
457: mansearch_setup(0);
458: }
459:
1.83 schwarze 460: free(defos);
1.1 kristaps 461:
1.129 ! schwarze 462: /*
! 463: * Flush the output and signal end of file.
! 464: * If a pager is attached, it allows browsing to the end.
! 465: * Otherwise, it does no harm, we are about to exit anyway.
! 466: */
! 467:
! 468: fclose(stdout);
! 469:
! 470: /*
! 471: * If we spawned a pager, wait for the user to close it.
! 472: * Otherwise, this call fails with no adverse effect.
! 473: */
! 474:
! 475: wait(NULL);
! 476:
1.76 schwarze 477: return((int)rc);
1.1 kristaps 478: }
479:
1.20 schwarze 480: static void
1.95 schwarze 481: usage(enum argmode argmode)
1.1 kristaps 482: {
483:
1.95 schwarze 484: switch (argmode) {
485: case ARG_FILE:
1.126 schwarze 486: fputs("usage: mandoc [-acfhkl] [-Ios=name] "
1.104 jmc 487: "[-Kencoding] [-mformat] [-Ooption]\n"
488: "\t [-Toutput] [-Wlevel] [file ...]\n", stderr);
1.95 schwarze 489: break;
490: case ARG_NAME:
1.126 schwarze 491: fputs("usage: man [-acfhklw] [-C file] [-I os=name] "
1.114 jmc 492: "[-K encoding] [-M path] [-m path]\n"
493: "\t [-O option=value] [-S subsection] [-s section] "
494: "[-T output] [-W level]\n"
1.95 schwarze 495: "\t [section] name ...\n", stderr);
496: break;
497: case ARG_WORD:
1.126 schwarze 498: fputs("usage: whatis [-acfhklw] [-C file] "
1.96 schwarze 499: "[-M path] [-m path] [-O outkey] [-S arch]\n"
500: "\t [-s section] name ...\n", stderr);
1.95 schwarze 501: break;
502: case ARG_EXPR:
1.126 schwarze 503: fputs("usage: apropos [-acfhklw] [-C file] "
1.96 schwarze 504: "[-M path] [-m path] [-O outkey] [-S arch]\n"
1.95 schwarze 505: "\t [-s section] expression ...\n", stderr);
506: break;
507: }
1.47 schwarze 508: exit((int)MANDOCLEVEL_BADARG);
1.119 schwarze 509: }
510:
511: static int
512: fs_lookup(const struct manpaths *paths, size_t ipath,
513: const char *sec, const char *arch, const char *name,
514: struct manpage **res, size_t *ressz)
515: {
1.127 schwarze 516: glob_t globinfo;
1.119 schwarze 517: struct manpage *page;
518: char *file;
1.127 schwarze 519: int form, globres;
1.119 schwarze 520:
1.127 schwarze 521: form = FORM_SRC;
1.119 schwarze 522: mandoc_asprintf(&file, "%s/man%s/%s.%s",
523: paths->paths[ipath], sec, name, sec);
1.127 schwarze 524: if (access(file, R_OK) != -1)
1.119 schwarze 525: goto found;
526: free(file);
527:
528: mandoc_asprintf(&file, "%s/cat%s/%s.0",
529: paths->paths[ipath], sec, name);
530: if (access(file, R_OK) != -1) {
531: form = FORM_CAT;
532: goto found;
533: }
534: free(file);
535:
536: if (arch != NULL) {
537: mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
538: paths->paths[ipath], sec, arch, name, sec);
1.127 schwarze 539: if (access(file, R_OK) != -1)
1.119 schwarze 540: goto found;
541: free(file);
542: }
1.127 schwarze 543:
544: mandoc_asprintf(&file, "%s/man%s/%s.*",
545: paths->paths[ipath], sec, name);
546: globres = glob(file, 0, NULL, &globinfo);
547: if (globres != 0 && globres != GLOB_NOMATCH)
548: fprintf(stderr, "%s: %s: glob: %s\n",
549: progname, file, strerror(errno));
550: free(file);
551: if (globres == 0)
552: file = mandoc_strdup(*globinfo.gl_pathv);
553: globfree(&globinfo);
554: if (globres != 0)
555: return(0);
1.119 schwarze 556:
557: found:
558: fprintf(stderr, "%s: outdated mandoc.db lacks %s(%s) entry,\n"
559: " consider running # makewhatis %s\n",
560: progname, name, sec, paths->paths[ipath]);
1.125 schwarze 561:
1.119 schwarze 562: *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
563: page = *res + (*ressz - 1);
564: page->file = file;
565: page->names = NULL;
566: page->output = NULL;
567: page->ipath = ipath;
568: page->bits = NAME_FILE & NAME_MASK;
569: page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
570: page->form = form;
571: return(1);
572: }
573:
574: static void
575: fs_search(const struct mansearch *cfg, const struct manpaths *paths,
576: int argc, char **argv, struct manpage **res, size_t *ressz)
577: {
578: const char *const sections[] =
579: {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
580: const size_t nsec = sizeof(sections)/sizeof(sections[0]);
581:
582: size_t ipath, isec, lastsz;
583:
584: assert(cfg->argmode == ARG_NAME);
585:
586: *res = NULL;
587: *ressz = lastsz = 0;
588: while (argc) {
589: for (ipath = 0; ipath < paths->sz; ipath++) {
590: if (cfg->sec != NULL) {
591: if (fs_lookup(paths, ipath, cfg->sec,
592: cfg->arch, *argv, res, ressz) &&
593: cfg->firstmatch)
594: return;
595: } else for (isec = 0; isec < nsec; isec++)
596: if (fs_lookup(paths, ipath, sections[isec],
597: cfg->arch, *argv, res, ressz) &&
598: cfg->firstmatch)
599: return;
600: }
601: if (*ressz == lastsz)
602: fprintf(stderr,
603: "%s: No entry for %s in the manual.\n",
604: progname, *argv);
605: lastsz = *ressz;
606: argv++;
607: argc--;
608: }
1.1 kristaps 609: }
610:
1.27 schwarze 611: static void
1.90 schwarze 612: parse(struct curparse *curp, int fd, const char *file,
613: enum mandoclevel *level)
1.52 schwarze 614: {
1.76 schwarze 615: enum mandoclevel rc;
616: struct mdoc *mdoc;
617: struct man *man;
1.52 schwarze 618:
1.76 schwarze 619: /* Begin by parsing the file itself. */
1.52 schwarze 620:
1.76 schwarze 621: assert(file);
622: assert(fd >= -1);
1.52 schwarze 623:
1.76 schwarze 624: rc = mparse_readfd(curp->mp, fd, file);
1.1 kristaps 625:
1.51 schwarze 626: /*
1.76 schwarze 627: * With -Wstop and warnings or errors of at least the requested
628: * level, do not produce output.
1.51 schwarze 629: */
630:
1.76 schwarze 631: if (MANDOCLEVEL_OK != rc && curp->wstop)
1.51 schwarze 632: goto cleanup;
633:
634: /* If unset, allocate output dev now (if applicable). */
635:
636: if ( ! (curp->outman && curp->outmdoc)) {
637: switch (curp->outtype) {
1.90 schwarze 638: case OUTT_HTML:
1.102 schwarze 639: curp->outdata = html_alloc(curp->mchars,
640: curp->outopts);
1.77 schwarze 641: curp->outfree = html_free;
642: break;
1.90 schwarze 643: case OUTT_UTF8:
1.102 schwarze 644: curp->outdata = utf8_alloc(curp->mchars,
645: curp->outopts);
1.77 schwarze 646: curp->outfree = ascii_free;
647: break;
1.90 schwarze 648: case OUTT_LOCALE:
1.102 schwarze 649: curp->outdata = locale_alloc(curp->mchars,
650: curp->outopts);
1.77 schwarze 651: curp->outfree = ascii_free;
1.51 schwarze 652: break;
1.90 schwarze 653: case OUTT_ASCII:
1.102 schwarze 654: curp->outdata = ascii_alloc(curp->mchars,
655: curp->outopts);
1.51 schwarze 656: curp->outfree = ascii_free;
657: break;
1.90 schwarze 658: case OUTT_PDF:
1.102 schwarze 659: curp->outdata = pdf_alloc(curp->mchars,
660: curp->outopts);
1.51 schwarze 661: curp->outfree = pspdf_free;
662: break;
1.90 schwarze 663: case OUTT_PS:
1.102 schwarze 664: curp->outdata = ps_alloc(curp->mchars,
665: curp->outopts);
1.51 schwarze 666: curp->outfree = pspdf_free;
667: break;
668: default:
669: break;
670: }
671:
672: switch (curp->outtype) {
1.90 schwarze 673: case OUTT_HTML:
1.51 schwarze 674: curp->outman = html_man;
675: curp->outmdoc = html_mdoc;
676: break;
1.90 schwarze 677: case OUTT_TREE:
1.51 schwarze 678: curp->outman = tree_man;
679: curp->outmdoc = tree_mdoc;
680: break;
1.90 schwarze 681: case OUTT_MAN:
1.78 schwarze 682: curp->outmdoc = man_mdoc;
1.80 schwarze 683: curp->outman = man_man;
1.78 schwarze 684: break;
1.90 schwarze 685: case OUTT_PDF:
1.51 schwarze 686: /* FALLTHROUGH */
1.90 schwarze 687: case OUTT_ASCII:
1.51 schwarze 688: /* FALLTHROUGH */
1.90 schwarze 689: case OUTT_UTF8:
1.77 schwarze 690: /* FALLTHROUGH */
1.90 schwarze 691: case OUTT_LOCALE:
1.77 schwarze 692: /* FALLTHROUGH */
1.90 schwarze 693: case OUTT_PS:
1.51 schwarze 694: curp->outman = terminal_man;
695: curp->outmdoc = terminal_mdoc;
696: break;
697: default:
698: break;
699: }
700: }
701:
1.88 schwarze 702: mparse_result(curp->mp, &mdoc, &man, NULL);
1.76 schwarze 703:
1.51 schwarze 704: /* Execute the out device, if it exists. */
705:
1.76 schwarze 706: if (man && curp->outman)
707: (*curp->outman)(curp->outdata, man);
708: if (mdoc && curp->outmdoc)
709: (*curp->outmdoc)(curp->outdata, mdoc);
1.51 schwarze 710:
1.118 schwarze 711: cleanup:
1.76 schwarze 712: if (*level < rc)
713: *level = rc;
1.1 kristaps 714: }
715:
1.95 schwarze 716: static enum mandoclevel
1.105 schwarze 717: passthrough(const char *file, int fd, int synopsis_only)
1.95 schwarze 718: {
1.105 schwarze 719: const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
720: const char synr[] = "SYNOPSIS";
721:
722: FILE *stream;
1.95 schwarze 723: const char *syscall;
1.105 schwarze 724: char *line;
725: size_t len, off;
726: ssize_t nw;
727: int print;
1.116 schwarze 728:
729: fflush(stdout);
1.105 schwarze 730:
731: if ((stream = fdopen(fd, "r")) == NULL) {
732: close(fd);
733: syscall = "fdopen";
734: goto fail;
735: }
1.95 schwarze 736:
1.105 schwarze 737: print = 0;
738: while ((line = fgetln(stream, &len)) != NULL) {
739: if (synopsis_only) {
740: if (print) {
741: if ( ! isspace((unsigned char)*line))
742: goto done;
743: while (len &&
744: isspace((unsigned char)*line)) {
745: line++;
746: len--;
747: }
748: } else {
749: if ((len == sizeof(synb) &&
750: ! strncmp(line, synb, len - 1)) ||
751: (len == sizeof(synr) &&
752: ! strncmp(line, synr, len - 1)))
753: print = 1;
754: continue;
755: }
756: }
757: for (off = 0; off < len; off += nw)
758: if ((nw = write(STDOUT_FILENO, line + off,
759: len - off)) == -1 || nw == 0) {
760: fclose(stream);
1.95 schwarze 761: syscall = "write";
762: goto fail;
763: }
1.105 schwarze 764: }
1.95 schwarze 765:
1.105 schwarze 766: if (ferror(stream)) {
767: fclose(stream);
768: syscall = "fgetln";
769: goto fail;
770: }
1.101 schwarze 771:
1.105 schwarze 772: done:
773: fclose(stream);
774: return(MANDOCLEVEL_OK);
1.95 schwarze 775:
776: fail:
777: fprintf(stderr, "%s: %s: SYSERR: %s: %s",
778: progname, file, syscall, strerror(errno));
779: return(MANDOCLEVEL_SYSERR);
1.103 schwarze 780: }
781:
782: static int
783: koptions(int *options, char *arg)
784: {
785:
786: if ( ! strcmp(arg, "utf-8")) {
787: *options |= MPARSE_UTF8;
788: *options &= ~MPARSE_LATIN1;
789: } else if ( ! strcmp(arg, "iso-8859-1")) {
790: *options |= MPARSE_LATIN1;
791: *options &= ~MPARSE_UTF8;
792: } else if ( ! strcmp(arg, "us-ascii")) {
793: *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
794: } else {
1.115 schwarze 795: fprintf(stderr, "%s: -K %s: Bad argument\n",
1.103 schwarze 796: progname, arg);
797: return(0);
798: }
799: return(1);
1.95 schwarze 800: }
801:
1.1 kristaps 802: static int
1.87 schwarze 803: moptions(int *options, char *arg)
1.1 kristaps 804: {
805:
1.95 schwarze 806: if (arg == NULL)
807: /* nothing to do */;
808: else if (0 == strcmp(arg, "doc"))
1.87 schwarze 809: *options |= MPARSE_MDOC;
1.1 kristaps 810: else if (0 == strcmp(arg, "andoc"))
1.87 schwarze 811: /* nothing to do */;
1.1 kristaps 812: else if (0 == strcmp(arg, "an"))
1.87 schwarze 813: *options |= MPARSE_MAN;
1.1 kristaps 814: else {
1.115 schwarze 815: fprintf(stderr, "%s: -m %s: Bad argument\n",
1.93 schwarze 816: progname, arg);
1.1 kristaps 817: return(0);
818: }
819:
820: return(1);
821: }
822:
823: static int
1.22 schwarze 824: toptions(struct curparse *curp, char *arg)
1.1 kristaps 825: {
826:
827: if (0 == strcmp(arg, "ascii"))
1.22 schwarze 828: curp->outtype = OUTT_ASCII;
829: else if (0 == strcmp(arg, "lint")) {
830: curp->outtype = OUTT_LINT;
1.45 schwarze 831: curp->wlevel = MANDOCLEVEL_WARNING;
1.76 schwarze 832: } else if (0 == strcmp(arg, "tree"))
1.22 schwarze 833: curp->outtype = OUTT_TREE;
1.78 schwarze 834: else if (0 == strcmp(arg, "man"))
835: curp->outtype = OUTT_MAN;
1.17 schwarze 836: else if (0 == strcmp(arg, "html"))
1.22 schwarze 837: curp->outtype = OUTT_HTML;
1.77 schwarze 838: else if (0 == strcmp(arg, "utf8"))
839: curp->outtype = OUTT_UTF8;
840: else if (0 == strcmp(arg, "locale"))
841: curp->outtype = OUTT_LOCALE;
1.21 schwarze 842: else if (0 == strcmp(arg, "xhtml"))
1.102 schwarze 843: curp->outtype = OUTT_HTML;
1.36 schwarze 844: else if (0 == strcmp(arg, "ps"))
845: curp->outtype = OUTT_PS;
1.43 schwarze 846: else if (0 == strcmp(arg, "pdf"))
847: curp->outtype = OUTT_PDF;
1.1 kristaps 848: else {
1.115 schwarze 849: fprintf(stderr, "%s: -T %s: Bad argument\n",
1.93 schwarze 850: progname, arg);
1.1 kristaps 851: return(0);
852: }
853:
854: return(1);
855: }
856:
857: static int
1.45 schwarze 858: woptions(struct curparse *curp, char *arg)
1.1 kristaps 859: {
1.10 schwarze 860: char *v, *o;
1.123 schwarze 861: const char *toks[7];
1.1 kristaps 862:
1.45 schwarze 863: toks[0] = "stop";
864: toks[1] = "all";
865: toks[2] = "warning";
866: toks[3] = "error";
1.123 schwarze 867: toks[4] = "unsupp";
868: toks[5] = "fatal";
869: toks[6] = NULL;
1.1 kristaps 870:
1.10 schwarze 871: while (*arg) {
872: o = arg;
1.17 schwarze 873: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.90 schwarze 874: case 0:
1.45 schwarze 875: curp->wstop = 1;
1.1 kristaps 876: break;
1.90 schwarze 877: case 1:
1.45 schwarze 878: /* FALLTHROUGH */
1.90 schwarze 879: case 2:
1.45 schwarze 880: curp->wlevel = MANDOCLEVEL_WARNING;
1.1 kristaps 881: break;
1.90 schwarze 882: case 3:
1.45 schwarze 883: curp->wlevel = MANDOCLEVEL_ERROR;
1.1 kristaps 884: break;
1.90 schwarze 885: case 4:
1.123 schwarze 886: curp->wlevel = MANDOCLEVEL_UNSUPP;
887: break;
888: case 5:
1.121 schwarze 889: curp->wlevel = MANDOCLEVEL_BADARG;
1.19 schwarze 890: break;
1.1 kristaps 891: default:
1.115 schwarze 892: fprintf(stderr, "%s: -W %s: Bad argument\n",
1.93 schwarze 893: progname, o);
1.1 kristaps 894: return(0);
895: }
1.10 schwarze 896: }
1.1 kristaps 897:
898: return(1);
899: }
900:
1.75 schwarze 901: static void
1.90 schwarze 902: mmsg(enum mandocerr t, enum mandoclevel lvl,
1.76 schwarze 903: const char *file, int line, int col, const char *msg)
1.30 schwarze 904: {
1.94 schwarze 905: const char *mparse_msg;
1.45 schwarze 906:
1.92 schwarze 907: fprintf(stderr, "%s: %s:", progname, file);
908:
909: if (line)
910: fprintf(stderr, "%d:%d:", line, col + 1);
911:
1.94 schwarze 912: fprintf(stderr, " %s", mparse_strlevel(lvl));
913:
914: if (NULL != (mparse_msg = mparse_strerror(t)))
915: fprintf(stderr, ": %s", mparse_msg);
1.30 schwarze 916:
917: if (msg)
918: fprintf(stderr, ": %s", msg);
1.76 schwarze 919:
1.30 schwarze 920: fputc('\n', stderr);
1.95 schwarze 921: }
922:
923: static void
924: spawn_pager(void)
925: {
926: #define MAX_PAGER_ARGS 16
927: char *argv[MAX_PAGER_ARGS];
928: const char *pager;
929: char *cp;
930: int fildes[2];
931: int argc;
932:
933: if (pipe(fildes) == -1) {
934: fprintf(stderr, "%s: pipe: %s\n",
935: progname, strerror(errno));
936: return;
937: }
938:
939: switch (fork()) {
940: case -1:
941: fprintf(stderr, "%s: fork: %s\n",
942: progname, strerror(errno));
943: exit((int)MANDOCLEVEL_SYSERR);
944: case 0:
1.129 ! schwarze 945: break;
! 946: default:
1.95 schwarze 947: close(fildes[0]);
948: if (dup2(fildes[1], STDOUT_FILENO) == -1) {
949: fprintf(stderr, "%s: dup output: %s\n",
950: progname, strerror(errno));
951: exit((int)MANDOCLEVEL_SYSERR);
952: }
1.129 ! schwarze 953: close(fildes[1]);
1.95 schwarze 954: return;
955: }
956:
1.129 ! schwarze 957: /* The child process becomes the pager. */
1.95 schwarze 958:
959: close(fildes[1]);
960: if (dup2(fildes[0], STDIN_FILENO) == -1) {
961: fprintf(stderr, "%s: dup input: %s\n",
962: progname, strerror(errno));
963: exit((int)MANDOCLEVEL_SYSERR);
964: }
1.129 ! schwarze 965: close(fildes[0]);
1.95 schwarze 966:
967: pager = getenv("MANPAGER");
968: if (pager == NULL || *pager == '\0')
969: pager = getenv("PAGER");
970: if (pager == NULL || *pager == '\0')
971: pager = "/usr/bin/more -s";
972: cp = mandoc_strdup(pager);
973:
974: /*
975: * Parse the pager command into words.
976: * Intentionally do not do anything fancy here.
977: */
978:
979: argc = 0;
980: while (argc + 1 < MAX_PAGER_ARGS) {
981: argv[argc++] = cp;
982: cp = strchr(cp, ' ');
983: if (cp == NULL)
984: break;
985: *cp++ = '\0';
986: while (*cp == ' ')
987: cp++;
988: if (*cp == '\0')
989: break;
990: }
991: argv[argc] = NULL;
992:
993: /* Hand over to the pager. */
994:
995: execvp(argv[0], argv);
996: fprintf(stderr, "%s: exec: %s\n",
997: progname, strerror(errno));
998: exit((int)MANDOCLEVEL_SYSERR);
1.30 schwarze 999: }