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