Annotation of src/usr.bin/mandoc/main.c, Revision 1.116
1.116 ! schwarze 1: /* $OpenBSD: main.c,v 1.115 2014/12/21 14:49:00 schwarze Exp $ */
1.1 kristaps 2: /*
1.95 schwarze 3: * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.87 schwarze 4: * Copyright (c) 2010, 2011, 2012, 2014 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.113 schwarze 287: } else if (((uc = argv[0]) != NULL) &&
288: ((isdigit(uc[0]) && (uc[1] == '\0' ||
289: (isalpha(uc[1]) && uc[2] == '\0'))) ||
290: (uc[0] == 'n' && uc[1] == '\0'))) {
291: search.sec = uc;
1.110 schwarze 292: argv++;
293: argc--;
294: }
1.95 schwarze 295: }
296:
297: rc = MANDOCLEVEL_OK;
298:
299: /* man(1), whatis(1), apropos(1) */
300:
301: if (search.argmode != ARG_FILE) {
302: if (argc == 0)
303: usage(search.argmode);
1.107 schwarze 304:
305: if (search.argmode == ARG_NAME &&
306: outmode == OUTMODE_ONE)
307: search.firstmatch = 1;
1.95 schwarze 308:
309: /* Access the mandoc database. */
310:
311: manpath_parse(&paths, conf_file, defpaths, auxpaths);
312: mansearch_setup(1);
313: if( ! mansearch(&search, &paths, argc, argv, &res, &sz))
314: usage(search.argmode);
315: resp = res;
316:
317: if (sz == 0) {
318: if (search.argmode == ARG_NAME)
319: fprintf(stderr, "%s: No entry for %s "
320: "in the manual.\n", progname, argv[0]);
321: rc = MANDOCLEVEL_BADARG;
322: goto out;
323: }
324:
325: /*
326: * For standard man(1) and -a output mode,
327: * prepare for copying filename pointers
328: * into the program parameter array.
329: */
330:
331: if (outmode == OUTMODE_ONE) {
332: argc = 1;
333: best_prio = 10;
334: } else if (outmode == OUTMODE_ALL)
335: argc = (int)sz;
336:
337: /* Iterate all matching manuals. */
338:
339: for (i = 0; i < sz; i++) {
340: if (outmode == OUTMODE_FLN)
341: puts(res[i].file);
342: else if (outmode == OUTMODE_LST)
343: printf("%s - %s\n", res[i].names,
344: res[i].output == NULL ? "" :
345: res[i].output);
346: else if (outmode == OUTMODE_ONE) {
347: /* Search for the best section. */
348: isec = strcspn(res[i].file, "123456789");
349: sec = res[i].file[isec];
350: if ('\0' == sec)
351: continue;
352: prio = sec_prios[sec - '1'];
353: if (prio >= best_prio)
354: continue;
355: best_prio = prio;
356: resp = res + i;
357: }
1.1 kristaps 358: }
359:
1.95 schwarze 360: /*
361: * For man(1), -a and -i output mode, fall through
362: * to the main mandoc(1) code iterating files
363: * and running the parsers on each of them.
364: */
365:
366: if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
367: goto out;
368: }
369:
370: /* mandoc(1) */
371:
1.115 schwarze 372: if (search.argmode == ARG_FILE && ! moptions(&options, auxpaths))
1.95 schwarze 373: return((int)MANDOCLEVEL_BADARG);
374:
375: if (use_pager && isatty(STDOUT_FILENO))
376: spawn_pager();
377:
1.102 schwarze 378: curp.mchars = mchars_alloc();
379: curp.mp = mparse_alloc(options, curp.wlevel, mmsg,
380: curp.mchars, defos);
1.76 schwarze 381:
1.80 schwarze 382: /*
383: * Conditionally start up the lookaside buffer before parsing.
384: */
385: if (OUTT_MAN == curp.outtype)
386: mparse_keep(curp.mp);
387:
1.95 schwarze 388: if (argc == 0)
1.76 schwarze 389: parse(&curp, STDIN_FILENO, "<stdin>", &rc);
1.27 schwarze 390:
1.95 schwarze 391: while (argc) {
392: if (resp != NULL) {
1.108 schwarze 393: rc = mparse_open(curp.mp, &fd, resp->file);
1.100 schwarze 394: if (fd == -1)
395: /* nothing */;
396: else if (resp->form & FORM_SRC) {
1.97 schwarze 397: /* For .so only; ignore failure. */
398: chdir(paths.paths[resp->ipath]);
1.100 schwarze 399: parse(&curp, fd, resp->file, &rc);
1.97 schwarze 400: } else
1.105 schwarze 401: rc = passthrough(resp->file, fd,
402: synopsis_only);
1.95 schwarze 403: resp++;
1.100 schwarze 404: } else {
1.108 schwarze 405: rc = mparse_open(curp.mp, &fd, *argv++);
1.100 schwarze 406: if (fd != -1)
407: parse(&curp, fd, argv[-1], &rc);
408: }
409:
1.108 schwarze 410: if (mparse_wait(curp.mp) != MANDOCLEVEL_OK)
1.100 schwarze 411: rc = MANDOCLEVEL_SYSERR;
412:
1.76 schwarze 413: if (MANDOCLEVEL_OK != rc && curp.wstop)
1.27 schwarze 414: break;
1.116 ! schwarze 415:
! 416: if (--argc && curp.outtype <= OUTT_UTF8)
! 417: ascii_sepline(curp.outdata);
1.1 kristaps 418: }
419:
420: if (curp.outfree)
421: (*curp.outfree)(curp.outdata);
1.102 schwarze 422: mparse_free(curp.mp);
423: mchars_free(curp.mchars);
1.95 schwarze 424:
425: out:
426: if (search.argmode != ARG_FILE) {
1.97 schwarze 427: manpath_free(&paths);
1.95 schwarze 428: mansearch_free(res, sz);
429: mansearch_setup(0);
430: }
431:
1.83 schwarze 432: free(defos);
1.1 kristaps 433:
1.76 schwarze 434: return((int)rc);
1.1 kristaps 435: }
436:
1.20 schwarze 437: static void
1.3 schwarze 438: version(void)
439: {
440:
1.95 schwarze 441: printf("mandoc %s\n", VERSION);
1.47 schwarze 442: exit((int)MANDOCLEVEL_OK);
1.3 schwarze 443: }
444:
1.20 schwarze 445: static void
1.95 schwarze 446: usage(enum argmode argmode)
1.1 kristaps 447: {
448:
1.95 schwarze 449: switch (argmode) {
450: case ARG_FILE:
1.98 schwarze 451: fputs("usage: mandoc [-acfhklV] [-Ios=name] "
1.104 jmc 452: "[-Kencoding] [-mformat] [-Ooption]\n"
453: "\t [-Toutput] [-Wlevel] [file ...]\n", stderr);
1.95 schwarze 454: break;
455: case ARG_NAME:
1.114 jmc 456: fputs("usage: man [-acfhklVw] [-C file] [-I os=name] "
457: "[-K encoding] [-M path] [-m path]\n"
458: "\t [-O option=value] [-S subsection] [-s section] "
459: "[-T output] [-W level]\n"
1.95 schwarze 460: "\t [section] name ...\n", stderr);
461: break;
462: case ARG_WORD:
1.98 schwarze 463: fputs("usage: whatis [-acfhklVw] [-C file] "
1.96 schwarze 464: "[-M path] [-m path] [-O outkey] [-S arch]\n"
465: "\t [-s section] name ...\n", stderr);
1.95 schwarze 466: break;
467: case ARG_EXPR:
1.98 schwarze 468: fputs("usage: apropos [-acfhklVw] [-C file] "
1.96 schwarze 469: "[-M path] [-m path] [-O outkey] [-S arch]\n"
1.95 schwarze 470: "\t [-s section] expression ...\n", stderr);
471: break;
472: }
1.47 schwarze 473: exit((int)MANDOCLEVEL_BADARG);
1.1 kristaps 474: }
475:
1.27 schwarze 476: static void
1.90 schwarze 477: parse(struct curparse *curp, int fd, const char *file,
478: enum mandoclevel *level)
1.52 schwarze 479: {
1.76 schwarze 480: enum mandoclevel rc;
481: struct mdoc *mdoc;
482: struct man *man;
1.52 schwarze 483:
1.76 schwarze 484: /* Begin by parsing the file itself. */
1.52 schwarze 485:
1.76 schwarze 486: assert(file);
487: assert(fd >= -1);
1.52 schwarze 488:
1.76 schwarze 489: rc = mparse_readfd(curp->mp, fd, file);
1.1 kristaps 490:
1.76 schwarze 491: /* Stop immediately if the parse has failed. */
1.1 kristaps 492:
1.76 schwarze 493: if (MANDOCLEVEL_FATAL <= rc)
1.51 schwarze 494: goto cleanup;
495:
496: /*
1.76 schwarze 497: * With -Wstop and warnings or errors of at least the requested
498: * level, do not produce output.
1.51 schwarze 499: */
500:
1.76 schwarze 501: if (MANDOCLEVEL_OK != rc && curp->wstop)
1.51 schwarze 502: goto cleanup;
503:
504: /* If unset, allocate output dev now (if applicable). */
505:
506: if ( ! (curp->outman && curp->outmdoc)) {
507: switch (curp->outtype) {
1.90 schwarze 508: case OUTT_HTML:
1.102 schwarze 509: curp->outdata = html_alloc(curp->mchars,
510: curp->outopts);
1.77 schwarze 511: curp->outfree = html_free;
512: break;
1.90 schwarze 513: case OUTT_UTF8:
1.102 schwarze 514: curp->outdata = utf8_alloc(curp->mchars,
515: curp->outopts);
1.77 schwarze 516: curp->outfree = ascii_free;
517: break;
1.90 schwarze 518: case OUTT_LOCALE:
1.102 schwarze 519: curp->outdata = locale_alloc(curp->mchars,
520: curp->outopts);
1.77 schwarze 521: curp->outfree = ascii_free;
1.51 schwarze 522: break;
1.90 schwarze 523: case OUTT_ASCII:
1.102 schwarze 524: curp->outdata = ascii_alloc(curp->mchars,
525: curp->outopts);
1.51 schwarze 526: curp->outfree = ascii_free;
527: break;
1.90 schwarze 528: case OUTT_PDF:
1.102 schwarze 529: curp->outdata = pdf_alloc(curp->mchars,
530: curp->outopts);
1.51 schwarze 531: curp->outfree = pspdf_free;
532: break;
1.90 schwarze 533: case OUTT_PS:
1.102 schwarze 534: curp->outdata = ps_alloc(curp->mchars,
535: curp->outopts);
1.51 schwarze 536: curp->outfree = pspdf_free;
537: break;
538: default:
539: break;
540: }
541:
542: switch (curp->outtype) {
1.90 schwarze 543: case OUTT_HTML:
1.51 schwarze 544: curp->outman = html_man;
545: curp->outmdoc = html_mdoc;
546: break;
1.90 schwarze 547: case OUTT_TREE:
1.51 schwarze 548: curp->outman = tree_man;
549: curp->outmdoc = tree_mdoc;
550: break;
1.90 schwarze 551: case OUTT_MAN:
1.78 schwarze 552: curp->outmdoc = man_mdoc;
1.80 schwarze 553: curp->outman = man_man;
1.78 schwarze 554: break;
1.90 schwarze 555: case OUTT_PDF:
1.51 schwarze 556: /* FALLTHROUGH */
1.90 schwarze 557: case OUTT_ASCII:
1.51 schwarze 558: /* FALLTHROUGH */
1.90 schwarze 559: case OUTT_UTF8:
1.77 schwarze 560: /* FALLTHROUGH */
1.90 schwarze 561: case OUTT_LOCALE:
1.77 schwarze 562: /* FALLTHROUGH */
1.90 schwarze 563: case OUTT_PS:
1.51 schwarze 564: curp->outman = terminal_man;
565: curp->outmdoc = terminal_mdoc;
566: break;
567: default:
568: break;
569: }
570: }
571:
1.88 schwarze 572: mparse_result(curp->mp, &mdoc, &man, NULL);
1.76 schwarze 573:
1.51 schwarze 574: /* Execute the out device, if it exists. */
575:
1.76 schwarze 576: if (man && curp->outman)
577: (*curp->outman)(curp->outdata, man);
578: if (mdoc && curp->outmdoc)
579: (*curp->outmdoc)(curp->outdata, mdoc);
1.51 schwarze 580:
581: cleanup:
1.59 schwarze 582:
1.76 schwarze 583: mparse_reset(curp->mp);
1.59 schwarze 584:
1.76 schwarze 585: if (*level < rc)
586: *level = rc;
1.1 kristaps 587: }
588:
1.95 schwarze 589: static enum mandoclevel
1.105 schwarze 590: passthrough(const char *file, int fd, int synopsis_only)
1.95 schwarze 591: {
1.105 schwarze 592: const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
593: const char synr[] = "SYNOPSIS";
594:
595: FILE *stream;
1.95 schwarze 596: const char *syscall;
1.105 schwarze 597: char *line;
598: size_t len, off;
599: ssize_t nw;
600: int print;
1.116 ! schwarze 601:
! 602: fflush(stdout);
1.105 schwarze 603:
604: if ((stream = fdopen(fd, "r")) == NULL) {
605: close(fd);
606: syscall = "fdopen";
607: goto fail;
608: }
1.95 schwarze 609:
1.105 schwarze 610: print = 0;
611: while ((line = fgetln(stream, &len)) != NULL) {
612: if (synopsis_only) {
613: if (print) {
614: if ( ! isspace((unsigned char)*line))
615: goto done;
616: while (len &&
617: isspace((unsigned char)*line)) {
618: line++;
619: len--;
620: }
621: } else {
622: if ((len == sizeof(synb) &&
623: ! strncmp(line, synb, len - 1)) ||
624: (len == sizeof(synr) &&
625: ! strncmp(line, synr, len - 1)))
626: print = 1;
627: continue;
628: }
629: }
630: for (off = 0; off < len; off += nw)
631: if ((nw = write(STDOUT_FILENO, line + off,
632: len - off)) == -1 || nw == 0) {
633: fclose(stream);
1.95 schwarze 634: syscall = "write";
635: goto fail;
636: }
1.105 schwarze 637: }
1.95 schwarze 638:
1.105 schwarze 639: if (ferror(stream)) {
640: fclose(stream);
641: syscall = "fgetln";
642: goto fail;
643: }
1.101 schwarze 644:
1.105 schwarze 645: done:
646: fclose(stream);
647: return(MANDOCLEVEL_OK);
1.95 schwarze 648:
649: fail:
650: fprintf(stderr, "%s: %s: SYSERR: %s: %s",
651: progname, file, syscall, strerror(errno));
652: return(MANDOCLEVEL_SYSERR);
1.103 schwarze 653: }
654:
655: static int
656: koptions(int *options, char *arg)
657: {
658:
659: if ( ! strcmp(arg, "utf-8")) {
660: *options |= MPARSE_UTF8;
661: *options &= ~MPARSE_LATIN1;
662: } else if ( ! strcmp(arg, "iso-8859-1")) {
663: *options |= MPARSE_LATIN1;
664: *options &= ~MPARSE_UTF8;
665: } else if ( ! strcmp(arg, "us-ascii")) {
666: *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
667: } else {
1.115 schwarze 668: fprintf(stderr, "%s: -K %s: Bad argument\n",
1.103 schwarze 669: progname, arg);
670: return(0);
671: }
672: return(1);
1.95 schwarze 673: }
674:
1.1 kristaps 675: static int
1.87 schwarze 676: moptions(int *options, char *arg)
1.1 kristaps 677: {
678:
1.95 schwarze 679: if (arg == NULL)
680: /* nothing to do */;
681: else if (0 == strcmp(arg, "doc"))
1.87 schwarze 682: *options |= MPARSE_MDOC;
1.1 kristaps 683: else if (0 == strcmp(arg, "andoc"))
1.87 schwarze 684: /* nothing to do */;
1.1 kristaps 685: else if (0 == strcmp(arg, "an"))
1.87 schwarze 686: *options |= MPARSE_MAN;
1.1 kristaps 687: else {
1.115 schwarze 688: fprintf(stderr, "%s: -m %s: Bad argument\n",
1.93 schwarze 689: progname, arg);
1.1 kristaps 690: return(0);
691: }
692:
693: return(1);
694: }
695:
696: static int
1.22 schwarze 697: toptions(struct curparse *curp, char *arg)
1.1 kristaps 698: {
699:
700: if (0 == strcmp(arg, "ascii"))
1.22 schwarze 701: curp->outtype = OUTT_ASCII;
702: else if (0 == strcmp(arg, "lint")) {
703: curp->outtype = OUTT_LINT;
1.45 schwarze 704: curp->wlevel = MANDOCLEVEL_WARNING;
1.76 schwarze 705: } else if (0 == strcmp(arg, "tree"))
1.22 schwarze 706: curp->outtype = OUTT_TREE;
1.78 schwarze 707: else if (0 == strcmp(arg, "man"))
708: curp->outtype = OUTT_MAN;
1.17 schwarze 709: else if (0 == strcmp(arg, "html"))
1.22 schwarze 710: curp->outtype = OUTT_HTML;
1.77 schwarze 711: else if (0 == strcmp(arg, "utf8"))
712: curp->outtype = OUTT_UTF8;
713: else if (0 == strcmp(arg, "locale"))
714: curp->outtype = OUTT_LOCALE;
1.21 schwarze 715: else if (0 == strcmp(arg, "xhtml"))
1.102 schwarze 716: curp->outtype = OUTT_HTML;
1.36 schwarze 717: else if (0 == strcmp(arg, "ps"))
718: curp->outtype = OUTT_PS;
1.43 schwarze 719: else if (0 == strcmp(arg, "pdf"))
720: curp->outtype = OUTT_PDF;
1.1 kristaps 721: else {
1.115 schwarze 722: fprintf(stderr, "%s: -T %s: Bad argument\n",
1.93 schwarze 723: progname, arg);
1.1 kristaps 724: return(0);
725: }
726:
727: return(1);
728: }
729:
730: static int
1.45 schwarze 731: woptions(struct curparse *curp, char *arg)
1.1 kristaps 732: {
1.10 schwarze 733: char *v, *o;
1.90 schwarze 734: const char *toks[6];
1.1 kristaps 735:
1.45 schwarze 736: toks[0] = "stop";
737: toks[1] = "all";
738: toks[2] = "warning";
739: toks[3] = "error";
740: toks[4] = "fatal";
741: toks[5] = NULL;
1.1 kristaps 742:
1.10 schwarze 743: while (*arg) {
744: o = arg;
1.17 schwarze 745: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.90 schwarze 746: case 0:
1.45 schwarze 747: curp->wstop = 1;
1.1 kristaps 748: break;
1.90 schwarze 749: case 1:
1.45 schwarze 750: /* FALLTHROUGH */
1.90 schwarze 751: case 2:
1.45 schwarze 752: curp->wlevel = MANDOCLEVEL_WARNING;
1.1 kristaps 753: break;
1.90 schwarze 754: case 3:
1.45 schwarze 755: curp->wlevel = MANDOCLEVEL_ERROR;
1.1 kristaps 756: break;
1.90 schwarze 757: case 4:
1.45 schwarze 758: curp->wlevel = MANDOCLEVEL_FATAL;
1.19 schwarze 759: break;
1.1 kristaps 760: default:
1.115 schwarze 761: fprintf(stderr, "%s: -W %s: Bad argument\n",
1.93 schwarze 762: progname, o);
1.1 kristaps 763: return(0);
764: }
1.10 schwarze 765: }
1.1 kristaps 766:
767: return(1);
768: }
769:
1.75 schwarze 770: static void
1.90 schwarze 771: mmsg(enum mandocerr t, enum mandoclevel lvl,
1.76 schwarze 772: const char *file, int line, int col, const char *msg)
1.30 schwarze 773: {
1.94 schwarze 774: const char *mparse_msg;
1.45 schwarze 775:
1.92 schwarze 776: fprintf(stderr, "%s: %s:", progname, file);
777:
778: if (line)
779: fprintf(stderr, "%d:%d:", line, col + 1);
780:
1.94 schwarze 781: fprintf(stderr, " %s", mparse_strlevel(lvl));
782:
783: if (NULL != (mparse_msg = mparse_strerror(t)))
784: fprintf(stderr, ": %s", mparse_msg);
1.30 schwarze 785:
786: if (msg)
787: fprintf(stderr, ": %s", msg);
1.76 schwarze 788:
1.30 schwarze 789: fputc('\n', stderr);
1.95 schwarze 790: }
791:
792: static void
793: spawn_pager(void)
794: {
795: #define MAX_PAGER_ARGS 16
796: char *argv[MAX_PAGER_ARGS];
797: const char *pager;
798: char *cp;
799: int fildes[2];
800: int argc;
801:
802: if (pipe(fildes) == -1) {
803: fprintf(stderr, "%s: pipe: %s\n",
804: progname, strerror(errno));
805: return;
806: }
807:
808: switch (fork()) {
809: case -1:
810: fprintf(stderr, "%s: fork: %s\n",
811: progname, strerror(errno));
812: exit((int)MANDOCLEVEL_SYSERR);
813: case 0:
814: close(fildes[0]);
815: if (dup2(fildes[1], STDOUT_FILENO) == -1) {
816: fprintf(stderr, "%s: dup output: %s\n",
817: progname, strerror(errno));
818: exit((int)MANDOCLEVEL_SYSERR);
819: }
820: return;
821: default:
822: break;
823: }
824:
825: /* The original process becomes the pager. */
826:
827: close(fildes[1]);
828: if (dup2(fildes[0], STDIN_FILENO) == -1) {
829: fprintf(stderr, "%s: dup input: %s\n",
830: progname, strerror(errno));
831: exit((int)MANDOCLEVEL_SYSERR);
832: }
833:
834: pager = getenv("MANPAGER");
835: if (pager == NULL || *pager == '\0')
836: pager = getenv("PAGER");
837: if (pager == NULL || *pager == '\0')
838: pager = "/usr/bin/more -s";
839: cp = mandoc_strdup(pager);
840:
841: /*
842: * Parse the pager command into words.
843: * Intentionally do not do anything fancy here.
844: */
845:
846: argc = 0;
847: while (argc + 1 < MAX_PAGER_ARGS) {
848: argv[argc++] = cp;
849: cp = strchr(cp, ' ');
850: if (cp == NULL)
851: break;
852: *cp++ = '\0';
853: while (*cp == ' ')
854: cp++;
855: if (*cp == '\0')
856: break;
857: }
858: argv[argc] = NULL;
859:
860: /* Hand over to the pager. */
861:
862: execvp(argv[0], argv);
863: fprintf(stderr, "%s: exec: %s\n",
864: progname, strerror(errno));
865: exit((int)MANDOCLEVEL_SYSERR);
1.30 schwarze 866: }