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