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