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