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