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