Annotation of src/usr.bin/mandoc/main.c, Revision 1.123
1.123 ! schwarze 1: /* $OpenBSD: main.c,v 1.122 2015/01/16 21:12:01 schwarze Exp $ */
1.1 kristaps 2: /*
1.95 schwarze 3: * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.118 schwarze 4: * Copyright (c) 2010-2012, 2014, 2015 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>
1.122 schwarze 21: #include <sys/param.h> /* MACHINE */
1.95 schwarze 22:
1.1 kristaps 23: #include <assert.h>
1.95 schwarze 24: #include <ctype.h>
25: #include <errno.h>
26: #include <fcntl.h>
1.1 kristaps 27: #include <stdio.h>
1.17 schwarze 28: #include <stdint.h>
1.1 kristaps 29: #include <stdlib.h>
30: #include <string.h>
31: #include <unistd.h>
32:
1.30 schwarze 33: #include "mandoc.h"
1.89 schwarze 34: #include "mandoc_aux.h"
1.38 schwarze 35: #include "main.h"
1.1 kristaps 36: #include "mdoc.h"
37: #include "man.h"
1.95 schwarze 38: #include "manpath.h"
39: #include "mansearch.h"
40:
41: enum outmode {
42: OUTMODE_DEF = 0,
43: OUTMODE_FLN,
44: OUTMODE_LST,
45: OUTMODE_ALL,
46: OUTMODE_INT,
47: OUTMODE_ONE
48: };
1.1 kristaps 49:
1.16 schwarze 50: typedef void (*out_mdoc)(void *, const struct mdoc *);
51: typedef void (*out_man)(void *, const struct man *);
1.1 kristaps 52: typedef void (*out_free)(void *);
53:
54: enum outt {
1.76 schwarze 55: OUTT_ASCII = 0, /* -Tascii */
1.77 schwarze 56: OUTT_LOCALE, /* -Tlocale */
57: OUTT_UTF8, /* -Tutf8 */
1.76 schwarze 58: OUTT_TREE, /* -Ttree */
1.78 schwarze 59: OUTT_MAN, /* -Tman */
1.76 schwarze 60: OUTT_HTML, /* -Thtml */
61: OUTT_LINT, /* -Tlint */
62: OUTT_PS, /* -Tps */
63: OUTT_PDF /* -Tpdf */
1.1 kristaps 64: };
65:
66: struct curparse {
1.76 schwarze 67: struct mparse *mp;
1.102 schwarze 68: struct mchars *mchars; /* character table */
1.75 schwarze 69: enum mandoclevel wlevel; /* ignore messages below this */
70: int wstop; /* stop after a file with a warning */
1.90 schwarze 71: enum outt outtype; /* which output to use */
1.33 schwarze 72: out_mdoc outmdoc; /* mdoc output ptr */
1.90 schwarze 73: out_man outman; /* man output ptr */
1.33 schwarze 74: out_free outfree; /* free output ptr */
75: void *outdata; /* data for output */
76: char outopts[BUFSIZ]; /* buf of output opts */
77: };
78:
1.119 schwarze 79: static int fs_lookup(const struct manpaths *,
80: size_t ipath, const char *,
81: const char *, const char *,
82: struct manpage **, size_t *);
83: static void fs_search(const struct mansearch *,
84: const struct manpaths *, int, char**,
85: struct manpage **, size_t *);
1.103 schwarze 86: static int koptions(int *, char *);
1.79 schwarze 87: int mandocdb(int, char**);
1.87 schwarze 88: static int moptions(int *, char *);
1.76 schwarze 89: static void mmsg(enum mandocerr, enum mandoclevel,
90: const char *, int, int, const char *);
1.90 schwarze 91: static void parse(struct curparse *, int,
1.76 schwarze 92: const char *, enum mandoclevel *);
1.105 schwarze 93: static enum mandoclevel passthrough(const char *, int, int);
1.95 schwarze 94: static void spawn_pager(void);
1.27 schwarze 95: static int toptions(struct curparse *, char *);
1.95 schwarze 96: static void usage(enum argmode) __attribute__((noreturn));
1.20 schwarze 97: static void version(void) __attribute__((noreturn));
1.45 schwarze 98: static int woptions(struct curparse *, char *);
1.1 kristaps 99:
1.95 schwarze 100: static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
1.110 schwarze 101: static char help_arg[] = "help";
102: static char *help_argv[] = {help_arg, NULL};
1.19 schwarze 103: static const char *progname;
1.1 kristaps 104:
1.90 schwarze 105:
1.1 kristaps 106: int
107: main(int argc, char *argv[])
108: {
109: struct curparse curp;
1.95 schwarze 110: struct mansearch search;
111: struct manpaths paths;
1.111 schwarze 112: char *auxpaths;
1.95 schwarze 113: char *defos;
1.113 schwarze 114: unsigned char *uc;
1.95 schwarze 115: struct manpage *res, *resp;
1.111 schwarze 116: char *conf_file, *defpaths;
1.95 schwarze 117: size_t isec, i, sz;
1.111 schwarze 118: int prio, best_prio, synopsis_only;
1.95 schwarze 119: char sec;
1.120 schwarze 120: enum mandoclevel rc, rctmp;
1.95 schwarze 121: enum outmode outmode;
1.100 schwarze 122: int fd;
1.95 schwarze 123: int show_usage;
124: int use_pager;
1.87 schwarze 125: int options;
1.95 schwarze 126: int c;
1.1 kristaps 127:
1.19 schwarze 128: progname = strrchr(argv[0], '/');
129: if (progname == NULL)
130: progname = argv[0];
131: else
132: ++progname;
1.79 schwarze 133:
1.82 schwarze 134: if (0 == strncmp(progname, "mandocdb", 8) ||
135: 0 == strncmp(progname, "makewhatis", 10))
1.79 schwarze 136: return(mandocdb(argc, argv));
1.19 schwarze 137:
1.95 schwarze 138: /* Search options. */
139:
140: memset(&paths, 0, sizeof(struct manpaths));
1.111 schwarze 141: conf_file = defpaths = NULL;
142: auxpaths = NULL;
1.95 schwarze 143:
144: memset(&search, 0, sizeof(struct mansearch));
145: search.outkey = "Nd";
146:
147: if (strcmp(progname, "man") == 0)
148: search.argmode = ARG_NAME;
149: else if (strncmp(progname, "apropos", 7) == 0)
150: search.argmode = ARG_EXPR;
151: else if (strncmp(progname, "whatis", 6) == 0)
152: search.argmode = ARG_WORD;
1.110 schwarze 153: else if (strncmp(progname, "help", 4) == 0)
154: search.argmode = ARG_NAME;
1.95 schwarze 155: else
156: search.argmode = ARG_FILE;
157:
158: /* Parser and formatter options. */
159:
1.19 schwarze 160: memset(&curp, 0, sizeof(struct curparse));
1.109 schwarze 161: curp.outtype = OUTT_LOCALE;
1.121 schwarze 162: curp.wlevel = MANDOCLEVEL_BADARG;
1.103 schwarze 163: options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
1.83 schwarze 164: defos = NULL;
1.1 kristaps 165:
1.95 schwarze 166: use_pager = 1;
167: show_usage = 0;
1.105 schwarze 168: synopsis_only = 0;
1.95 schwarze 169: outmode = OUTMODE_DEF;
170:
1.103 schwarze 171: while (-1 != (c = getopt(argc, argv,
172: "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
1.1 kristaps 173: switch (c) {
1.95 schwarze 174: case 'a':
175: outmode = OUTMODE_ALL;
176: break;
177: case 'C':
178: conf_file = optarg;
179: break;
180: case 'c':
181: use_pager = 0;
182: break;
183: case 'f':
184: search.argmode = ARG_WORD;
185: break;
1.98 schwarze 186: case 'h':
187: (void)strlcat(curp.outopts, "synopsis,", BUFSIZ);
1.105 schwarze 188: synopsis_only = 1;
1.106 schwarze 189: use_pager = 0;
1.98 schwarze 190: outmode = OUTMODE_ALL;
191: break;
1.90 schwarze 192: case 'I':
1.83 schwarze 193: if (strncmp(optarg, "os=", 3)) {
1.90 schwarze 194: fprintf(stderr,
1.115 schwarze 195: "%s: -I %s: Bad argument\n",
1.93 schwarze 196: progname, optarg);
1.83 schwarze 197: return((int)MANDOCLEVEL_BADARG);
198: }
199: if (defos) {
1.90 schwarze 200: fprintf(stderr,
1.115 schwarze 201: "%s: -I %s: Duplicate argument\n",
1.93 schwarze 202: progname, optarg);
1.83 schwarze 203: return((int)MANDOCLEVEL_BADARG);
204: }
205: defos = mandoc_strdup(optarg + 3);
206: break;
1.95 schwarze 207: case 'i':
208: outmode = OUTMODE_INT;
209: break;
1.103 schwarze 210: case 'K':
211: if ( ! koptions(&options, optarg))
212: return((int)MANDOCLEVEL_BADARG);
213: break;
1.95 schwarze 214: case 'k':
215: search.argmode = ARG_EXPR;
216: break;
1.96 schwarze 217: case 'l':
218: search.argmode = ARG_FILE;
219: outmode = OUTMODE_ALL;
220: break;
1.95 schwarze 221: case 'M':
222: defpaths = optarg;
223: break;
1.90 schwarze 224: case 'm':
1.95 schwarze 225: auxpaths = optarg;
1.1 kristaps 226: break;
1.90 schwarze 227: case 'O':
1.95 schwarze 228: search.outkey = optarg;
1.18 schwarze 229: (void)strlcat(curp.outopts, optarg, BUFSIZ);
230: (void)strlcat(curp.outopts, ",", BUFSIZ);
1.17 schwarze 231: break;
1.95 schwarze 232: case 'S':
233: search.arch = optarg;
234: break;
235: case 's':
236: search.sec = optarg;
237: break;
1.90 schwarze 238: case 'T':
1.22 schwarze 239: if ( ! toptions(&curp, optarg))
1.47 schwarze 240: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 241: break;
1.90 schwarze 242: case 'W':
1.45 schwarze 243: if ( ! woptions(&curp, optarg))
1.47 schwarze 244: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 245: break;
1.95 schwarze 246: case 'w':
247: outmode = OUTMODE_FLN;
248: break;
1.90 schwarze 249: case 'V':
1.3 schwarze 250: version();
251: /* NOTREACHED */
1.1 kristaps 252: default:
1.95 schwarze 253: show_usage = 1;
254: break;
255: }
256: }
257:
258: if (show_usage)
259: usage(search.argmode);
260:
261: /* Postprocess options. */
262:
263: if (outmode == OUTMODE_DEF) {
264: switch (search.argmode) {
265: case ARG_FILE:
266: outmode = OUTMODE_ALL;
267: use_pager = 0;
268: break;
269: case ARG_NAME:
270: outmode = OUTMODE_ONE;
271: break;
272: default:
273: outmode = OUTMODE_LST;
274: break;
275: }
276: }
277:
278: /* Parse arguments. */
279:
280: argc -= optind;
281: argv += optind;
282: resp = NULL;
283:
1.110 schwarze 284: /*
285: * Quirks for help(1)
286: * and for a man(1) section argument without -s.
287: */
288:
289: if (search.argmode == ARG_NAME) {
290: if (*progname == 'h') {
291: if (argc == 0) {
292: argv = help_argv;
293: argc = 1;
294: }
1.117 schwarze 295: } else if (argc > 1 &&
296: ((uc = argv[0]) != NULL) &&
1.113 schwarze 297: ((isdigit(uc[0]) && (uc[1] == '\0' ||
298: (isalpha(uc[1]) && uc[2] == '\0'))) ||
299: (uc[0] == 'n' && uc[1] == '\0'))) {
300: search.sec = uc;
1.110 schwarze 301: argv++;
302: argc--;
303: }
1.122 schwarze 304: if (search.arch == NULL)
305: search.arch = getenv("MACHINE");
306: if (search.arch == NULL)
307: search.arch = MACHINE;
1.95 schwarze 308: }
309:
310: rc = MANDOCLEVEL_OK;
311:
312: /* man(1), whatis(1), apropos(1) */
313:
314: if (search.argmode != ARG_FILE) {
315: if (argc == 0)
316: usage(search.argmode);
1.107 schwarze 317:
318: if (search.argmode == ARG_NAME &&
319: outmode == OUTMODE_ONE)
320: search.firstmatch = 1;
1.95 schwarze 321:
322: /* Access the mandoc database. */
323:
324: manpath_parse(&paths, conf_file, defpaths, auxpaths);
325: mansearch_setup(1);
326: if( ! mansearch(&search, &paths, argc, argv, &res, &sz))
327: usage(search.argmode);
1.119 schwarze 328:
329: if (sz == 0 && search.argmode == ARG_NAME)
330: fs_search(&search, &paths, argc, argv, &res, &sz);
1.95 schwarze 331:
332: if (sz == 0) {
333: rc = MANDOCLEVEL_BADARG;
334: goto out;
335: }
336:
337: /*
338: * For standard man(1) and -a output mode,
339: * prepare for copying filename pointers
340: * into the program parameter array.
341: */
342:
343: if (outmode == OUTMODE_ONE) {
344: argc = 1;
345: best_prio = 10;
346: } else if (outmode == OUTMODE_ALL)
347: argc = (int)sz;
348:
349: /* Iterate all matching manuals. */
350:
1.119 schwarze 351: resp = res;
1.95 schwarze 352: for (i = 0; i < sz; i++) {
353: if (outmode == OUTMODE_FLN)
354: puts(res[i].file);
355: else if (outmode == OUTMODE_LST)
356: printf("%s - %s\n", res[i].names,
357: res[i].output == NULL ? "" :
358: res[i].output);
359: else if (outmode == OUTMODE_ONE) {
360: /* Search for the best section. */
361: isec = strcspn(res[i].file, "123456789");
362: sec = res[i].file[isec];
363: if ('\0' == sec)
364: continue;
365: prio = sec_prios[sec - '1'];
366: if (prio >= best_prio)
367: continue;
368: best_prio = prio;
369: resp = res + i;
370: }
1.1 kristaps 371: }
372:
1.95 schwarze 373: /*
374: * For man(1), -a and -i output mode, fall through
375: * to the main mandoc(1) code iterating files
376: * and running the parsers on each of them.
377: */
378:
379: if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
380: goto out;
381: }
382:
383: /* mandoc(1) */
384:
1.115 schwarze 385: if (search.argmode == ARG_FILE && ! moptions(&options, auxpaths))
1.95 schwarze 386: return((int)MANDOCLEVEL_BADARG);
387:
1.102 schwarze 388: curp.mchars = mchars_alloc();
389: curp.mp = mparse_alloc(options, curp.wlevel, mmsg,
390: curp.mchars, defos);
1.76 schwarze 391:
1.80 schwarze 392: /*
393: * Conditionally start up the lookaside buffer before parsing.
394: */
395: if (OUTT_MAN == curp.outtype)
396: mparse_keep(curp.mp);
397:
1.118 schwarze 398: if (argc == 0) {
399: if (use_pager && isatty(STDOUT_FILENO))
400: spawn_pager();
1.76 schwarze 401: parse(&curp, STDIN_FILENO, "<stdin>", &rc);
1.118 schwarze 402: }
1.27 schwarze 403:
1.95 schwarze 404: while (argc) {
1.120 schwarze 405: rctmp = mparse_open(curp.mp, &fd,
1.118 schwarze 406: resp != NULL ? resp->file : *argv);
1.120 schwarze 407: if (rc < rctmp)
408: rc = rctmp;
1.118 schwarze 409:
410: if (fd != -1) {
411: if (use_pager && isatty(STDOUT_FILENO))
412: spawn_pager();
413: use_pager = 0;
414:
415: if (resp == NULL)
416: parse(&curp, fd, *argv, &rc);
1.100 schwarze 417: else if (resp->form & FORM_SRC) {
1.97 schwarze 418: /* For .so only; ignore failure. */
419: chdir(paths.paths[resp->ipath]);
1.100 schwarze 420: parse(&curp, fd, resp->file, &rc);
1.120 schwarze 421: } else {
422: rctmp = passthrough(resp->file, fd,
1.105 schwarze 423: synopsis_only);
1.120 schwarze 424: if (rc < rctmp)
425: rc = rctmp;
426: }
1.118 schwarze 427:
1.120 schwarze 428: rctmp = mparse_wait(curp.mp);
429: if (rc < rctmp)
430: rc = rctmp;
1.118 schwarze 431:
432: if (argc > 1 && curp.outtype <= OUTT_UTF8)
433: ascii_sepline(curp.outdata);
1.100 schwarze 434: }
435:
1.76 schwarze 436: if (MANDOCLEVEL_OK != rc && curp.wstop)
1.27 schwarze 437: break;
1.116 schwarze 438:
1.118 schwarze 439: if (resp != NULL)
440: resp++;
441: else
442: argv++;
443: if (--argc)
444: mparse_reset(curp.mp);
1.1 kristaps 445: }
446:
447: if (curp.outfree)
448: (*curp.outfree)(curp.outdata);
1.102 schwarze 449: mparse_free(curp.mp);
450: mchars_free(curp.mchars);
1.95 schwarze 451:
452: out:
453: if (search.argmode != ARG_FILE) {
1.97 schwarze 454: manpath_free(&paths);
1.95 schwarze 455: mansearch_free(res, sz);
456: mansearch_setup(0);
457: }
458:
1.83 schwarze 459: free(defos);
1.1 kristaps 460:
1.76 schwarze 461: return((int)rc);
1.1 kristaps 462: }
463:
1.20 schwarze 464: static void
1.3 schwarze 465: version(void)
466: {
467:
1.95 schwarze 468: printf("mandoc %s\n", VERSION);
1.47 schwarze 469: exit((int)MANDOCLEVEL_OK);
1.3 schwarze 470: }
471:
1.20 schwarze 472: static void
1.95 schwarze 473: usage(enum argmode argmode)
1.1 kristaps 474: {
475:
1.95 schwarze 476: switch (argmode) {
477: case ARG_FILE:
1.98 schwarze 478: fputs("usage: mandoc [-acfhklV] [-Ios=name] "
1.104 jmc 479: "[-Kencoding] [-mformat] [-Ooption]\n"
480: "\t [-Toutput] [-Wlevel] [file ...]\n", stderr);
1.95 schwarze 481: break;
482: case ARG_NAME:
1.114 jmc 483: fputs("usage: man [-acfhklVw] [-C file] [-I os=name] "
484: "[-K encoding] [-M path] [-m path]\n"
485: "\t [-O option=value] [-S subsection] [-s section] "
486: "[-T output] [-W level]\n"
1.95 schwarze 487: "\t [section] name ...\n", stderr);
488: break;
489: case ARG_WORD:
1.98 schwarze 490: fputs("usage: whatis [-acfhklVw] [-C file] "
1.96 schwarze 491: "[-M path] [-m path] [-O outkey] [-S arch]\n"
492: "\t [-s section] name ...\n", stderr);
1.95 schwarze 493: break;
494: case ARG_EXPR:
1.98 schwarze 495: fputs("usage: apropos [-acfhklVw] [-C file] "
1.96 schwarze 496: "[-M path] [-m path] [-O outkey] [-S arch]\n"
1.95 schwarze 497: "\t [-s section] expression ...\n", stderr);
498: break;
499: }
1.47 schwarze 500: exit((int)MANDOCLEVEL_BADARG);
1.119 schwarze 501: }
502:
503: static int
504: fs_lookup(const struct manpaths *paths, size_t ipath,
505: const char *sec, const char *arch, const char *name,
506: struct manpage **res, size_t *ressz)
507: {
508: struct manpage *page;
509: char *file;
510: int form;
511:
512: mandoc_asprintf(&file, "%s/man%s/%s.%s",
513: paths->paths[ipath], sec, name, sec);
514: if (access(file, R_OK) != -1) {
515: form = FORM_SRC;
516: goto found;
517: }
518: free(file);
519:
520: mandoc_asprintf(&file, "%s/cat%s/%s.0",
521: paths->paths[ipath], sec, name);
522: if (access(file, R_OK) != -1) {
523: form = FORM_CAT;
524: goto found;
525: }
526: free(file);
527:
528: if (arch != NULL) {
529: mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
530: paths->paths[ipath], sec, arch, name, sec);
531: if (access(file, R_OK) != -1) {
532: form = FORM_SRC;
533: goto found;
534: }
535: free(file);
536: }
537: return(0);
538:
539: found:
540: fprintf(stderr, "%s: outdated mandoc.db lacks %s(%s) entry,\n"
541: " consider running # makewhatis %s\n",
542: progname, name, sec, paths->paths[ipath]);
543:
544: *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
545: page = *res + (*ressz - 1);
546: page->file = file;
547: page->names = NULL;
548: page->output = NULL;
549: page->ipath = ipath;
550: page->bits = NAME_FILE & NAME_MASK;
551: page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
552: page->form = form;
553: return(1);
554: }
555:
556: static void
557: fs_search(const struct mansearch *cfg, const struct manpaths *paths,
558: int argc, char **argv, struct manpage **res, size_t *ressz)
559: {
560: const char *const sections[] =
561: {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
562: const size_t nsec = sizeof(sections)/sizeof(sections[0]);
563:
564: size_t ipath, isec, lastsz;
565:
566: assert(cfg->argmode == ARG_NAME);
567:
568: *res = NULL;
569: *ressz = lastsz = 0;
570: while (argc) {
571: for (ipath = 0; ipath < paths->sz; ipath++) {
572: if (cfg->sec != NULL) {
573: if (fs_lookup(paths, ipath, cfg->sec,
574: cfg->arch, *argv, res, ressz) &&
575: cfg->firstmatch)
576: return;
577: } else for (isec = 0; isec < nsec; isec++)
578: if (fs_lookup(paths, ipath, sections[isec],
579: cfg->arch, *argv, res, ressz) &&
580: cfg->firstmatch)
581: return;
582: }
583: if (*ressz == lastsz)
584: fprintf(stderr,
585: "%s: No entry for %s in the manual.\n",
586: progname, *argv);
587: lastsz = *ressz;
588: argv++;
589: argc--;
590: }
1.1 kristaps 591: }
592:
1.27 schwarze 593: static void
1.90 schwarze 594: parse(struct curparse *curp, int fd, const char *file,
595: enum mandoclevel *level)
1.52 schwarze 596: {
1.76 schwarze 597: enum mandoclevel rc;
598: struct mdoc *mdoc;
599: struct man *man;
1.52 schwarze 600:
1.76 schwarze 601: /* Begin by parsing the file itself. */
1.52 schwarze 602:
1.76 schwarze 603: assert(file);
604: assert(fd >= -1);
1.52 schwarze 605:
1.76 schwarze 606: rc = mparse_readfd(curp->mp, fd, file);
1.1 kristaps 607:
1.51 schwarze 608: /*
1.76 schwarze 609: * With -Wstop and warnings or errors of at least the requested
610: * level, do not produce output.
1.51 schwarze 611: */
612:
1.76 schwarze 613: if (MANDOCLEVEL_OK != rc && curp->wstop)
1.51 schwarze 614: goto cleanup;
615:
616: /* If unset, allocate output dev now (if applicable). */
617:
618: if ( ! (curp->outman && curp->outmdoc)) {
619: switch (curp->outtype) {
1.90 schwarze 620: case OUTT_HTML:
1.102 schwarze 621: curp->outdata = html_alloc(curp->mchars,
622: curp->outopts);
1.77 schwarze 623: curp->outfree = html_free;
624: break;
1.90 schwarze 625: case OUTT_UTF8:
1.102 schwarze 626: curp->outdata = utf8_alloc(curp->mchars,
627: curp->outopts);
1.77 schwarze 628: curp->outfree = ascii_free;
629: break;
1.90 schwarze 630: case OUTT_LOCALE:
1.102 schwarze 631: curp->outdata = locale_alloc(curp->mchars,
632: curp->outopts);
1.77 schwarze 633: curp->outfree = ascii_free;
1.51 schwarze 634: break;
1.90 schwarze 635: case OUTT_ASCII:
1.102 schwarze 636: curp->outdata = ascii_alloc(curp->mchars,
637: curp->outopts);
1.51 schwarze 638: curp->outfree = ascii_free;
639: break;
1.90 schwarze 640: case OUTT_PDF:
1.102 schwarze 641: curp->outdata = pdf_alloc(curp->mchars,
642: curp->outopts);
1.51 schwarze 643: curp->outfree = pspdf_free;
644: break;
1.90 schwarze 645: case OUTT_PS:
1.102 schwarze 646: curp->outdata = ps_alloc(curp->mchars,
647: curp->outopts);
1.51 schwarze 648: curp->outfree = pspdf_free;
649: break;
650: default:
651: break;
652: }
653:
654: switch (curp->outtype) {
1.90 schwarze 655: case OUTT_HTML:
1.51 schwarze 656: curp->outman = html_man;
657: curp->outmdoc = html_mdoc;
658: break;
1.90 schwarze 659: case OUTT_TREE:
1.51 schwarze 660: curp->outman = tree_man;
661: curp->outmdoc = tree_mdoc;
662: break;
1.90 schwarze 663: case OUTT_MAN:
1.78 schwarze 664: curp->outmdoc = man_mdoc;
1.80 schwarze 665: curp->outman = man_man;
1.78 schwarze 666: break;
1.90 schwarze 667: case OUTT_PDF:
1.51 schwarze 668: /* FALLTHROUGH */
1.90 schwarze 669: case OUTT_ASCII:
1.51 schwarze 670: /* FALLTHROUGH */
1.90 schwarze 671: case OUTT_UTF8:
1.77 schwarze 672: /* FALLTHROUGH */
1.90 schwarze 673: case OUTT_LOCALE:
1.77 schwarze 674: /* FALLTHROUGH */
1.90 schwarze 675: case OUTT_PS:
1.51 schwarze 676: curp->outman = terminal_man;
677: curp->outmdoc = terminal_mdoc;
678: break;
679: default:
680: break;
681: }
682: }
683:
1.88 schwarze 684: mparse_result(curp->mp, &mdoc, &man, NULL);
1.76 schwarze 685:
1.51 schwarze 686: /* Execute the out device, if it exists. */
687:
1.76 schwarze 688: if (man && curp->outman)
689: (*curp->outman)(curp->outdata, man);
690: if (mdoc && curp->outmdoc)
691: (*curp->outmdoc)(curp->outdata, mdoc);
1.51 schwarze 692:
1.118 schwarze 693: cleanup:
1.76 schwarze 694: if (*level < rc)
695: *level = rc;
1.1 kristaps 696: }
697:
1.95 schwarze 698: static enum mandoclevel
1.105 schwarze 699: passthrough(const char *file, int fd, int synopsis_only)
1.95 schwarze 700: {
1.105 schwarze 701: const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
702: const char synr[] = "SYNOPSIS";
703:
704: FILE *stream;
1.95 schwarze 705: const char *syscall;
1.105 schwarze 706: char *line;
707: size_t len, off;
708: ssize_t nw;
709: int print;
1.116 schwarze 710:
711: fflush(stdout);
1.105 schwarze 712:
713: if ((stream = fdopen(fd, "r")) == NULL) {
714: close(fd);
715: syscall = "fdopen";
716: goto fail;
717: }
1.95 schwarze 718:
1.105 schwarze 719: print = 0;
720: while ((line = fgetln(stream, &len)) != NULL) {
721: if (synopsis_only) {
722: if (print) {
723: if ( ! isspace((unsigned char)*line))
724: goto done;
725: while (len &&
726: isspace((unsigned char)*line)) {
727: line++;
728: len--;
729: }
730: } else {
731: if ((len == sizeof(synb) &&
732: ! strncmp(line, synb, len - 1)) ||
733: (len == sizeof(synr) &&
734: ! strncmp(line, synr, len - 1)))
735: print = 1;
736: continue;
737: }
738: }
739: for (off = 0; off < len; off += nw)
740: if ((nw = write(STDOUT_FILENO, line + off,
741: len - off)) == -1 || nw == 0) {
742: fclose(stream);
1.95 schwarze 743: syscall = "write";
744: goto fail;
745: }
1.105 schwarze 746: }
1.95 schwarze 747:
1.105 schwarze 748: if (ferror(stream)) {
749: fclose(stream);
750: syscall = "fgetln";
751: goto fail;
752: }
1.101 schwarze 753:
1.105 schwarze 754: done:
755: fclose(stream);
756: return(MANDOCLEVEL_OK);
1.95 schwarze 757:
758: fail:
759: fprintf(stderr, "%s: %s: SYSERR: %s: %s",
760: progname, file, syscall, strerror(errno));
761: return(MANDOCLEVEL_SYSERR);
1.103 schwarze 762: }
763:
764: static int
765: koptions(int *options, char *arg)
766: {
767:
768: if ( ! strcmp(arg, "utf-8")) {
769: *options |= MPARSE_UTF8;
770: *options &= ~MPARSE_LATIN1;
771: } else if ( ! strcmp(arg, "iso-8859-1")) {
772: *options |= MPARSE_LATIN1;
773: *options &= ~MPARSE_UTF8;
774: } else if ( ! strcmp(arg, "us-ascii")) {
775: *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
776: } else {
1.115 schwarze 777: fprintf(stderr, "%s: -K %s: Bad argument\n",
1.103 schwarze 778: progname, arg);
779: return(0);
780: }
781: return(1);
1.95 schwarze 782: }
783:
1.1 kristaps 784: static int
1.87 schwarze 785: moptions(int *options, char *arg)
1.1 kristaps 786: {
787:
1.95 schwarze 788: if (arg == NULL)
789: /* nothing to do */;
790: else if (0 == strcmp(arg, "doc"))
1.87 schwarze 791: *options |= MPARSE_MDOC;
1.1 kristaps 792: else if (0 == strcmp(arg, "andoc"))
1.87 schwarze 793: /* nothing to do */;
1.1 kristaps 794: else if (0 == strcmp(arg, "an"))
1.87 schwarze 795: *options |= MPARSE_MAN;
1.1 kristaps 796: else {
1.115 schwarze 797: fprintf(stderr, "%s: -m %s: Bad argument\n",
1.93 schwarze 798: progname, arg);
1.1 kristaps 799: return(0);
800: }
801:
802: return(1);
803: }
804:
805: static int
1.22 schwarze 806: toptions(struct curparse *curp, char *arg)
1.1 kristaps 807: {
808:
809: if (0 == strcmp(arg, "ascii"))
1.22 schwarze 810: curp->outtype = OUTT_ASCII;
811: else if (0 == strcmp(arg, "lint")) {
812: curp->outtype = OUTT_LINT;
1.45 schwarze 813: curp->wlevel = MANDOCLEVEL_WARNING;
1.76 schwarze 814: } else if (0 == strcmp(arg, "tree"))
1.22 schwarze 815: curp->outtype = OUTT_TREE;
1.78 schwarze 816: else if (0 == strcmp(arg, "man"))
817: curp->outtype = OUTT_MAN;
1.17 schwarze 818: else if (0 == strcmp(arg, "html"))
1.22 schwarze 819: curp->outtype = OUTT_HTML;
1.77 schwarze 820: else if (0 == strcmp(arg, "utf8"))
821: curp->outtype = OUTT_UTF8;
822: else if (0 == strcmp(arg, "locale"))
823: curp->outtype = OUTT_LOCALE;
1.21 schwarze 824: else if (0 == strcmp(arg, "xhtml"))
1.102 schwarze 825: curp->outtype = OUTT_HTML;
1.36 schwarze 826: else if (0 == strcmp(arg, "ps"))
827: curp->outtype = OUTT_PS;
1.43 schwarze 828: else if (0 == strcmp(arg, "pdf"))
829: curp->outtype = OUTT_PDF;
1.1 kristaps 830: else {
1.115 schwarze 831: fprintf(stderr, "%s: -T %s: Bad argument\n",
1.93 schwarze 832: progname, arg);
1.1 kristaps 833: return(0);
834: }
835:
836: return(1);
837: }
838:
839: static int
1.45 schwarze 840: woptions(struct curparse *curp, char *arg)
1.1 kristaps 841: {
1.10 schwarze 842: char *v, *o;
1.123 ! schwarze 843: const char *toks[7];
1.1 kristaps 844:
1.45 schwarze 845: toks[0] = "stop";
846: toks[1] = "all";
847: toks[2] = "warning";
848: toks[3] = "error";
1.123 ! schwarze 849: toks[4] = "unsupp";
! 850: toks[5] = "fatal";
! 851: toks[6] = NULL;
1.1 kristaps 852:
1.10 schwarze 853: while (*arg) {
854: o = arg;
1.17 schwarze 855: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.90 schwarze 856: case 0:
1.45 schwarze 857: curp->wstop = 1;
1.1 kristaps 858: break;
1.90 schwarze 859: case 1:
1.45 schwarze 860: /* FALLTHROUGH */
1.90 schwarze 861: case 2:
1.45 schwarze 862: curp->wlevel = MANDOCLEVEL_WARNING;
1.1 kristaps 863: break;
1.90 schwarze 864: case 3:
1.45 schwarze 865: curp->wlevel = MANDOCLEVEL_ERROR;
1.1 kristaps 866: break;
1.90 schwarze 867: case 4:
1.123 ! schwarze 868: curp->wlevel = MANDOCLEVEL_UNSUPP;
! 869: break;
! 870: case 5:
1.121 schwarze 871: curp->wlevel = MANDOCLEVEL_BADARG;
1.19 schwarze 872: break;
1.1 kristaps 873: default:
1.115 schwarze 874: fprintf(stderr, "%s: -W %s: Bad argument\n",
1.93 schwarze 875: progname, o);
1.1 kristaps 876: return(0);
877: }
1.10 schwarze 878: }
1.1 kristaps 879:
880: return(1);
881: }
882:
1.75 schwarze 883: static void
1.90 schwarze 884: mmsg(enum mandocerr t, enum mandoclevel lvl,
1.76 schwarze 885: const char *file, int line, int col, const char *msg)
1.30 schwarze 886: {
1.94 schwarze 887: const char *mparse_msg;
1.45 schwarze 888:
1.92 schwarze 889: fprintf(stderr, "%s: %s:", progname, file);
890:
891: if (line)
892: fprintf(stderr, "%d:%d:", line, col + 1);
893:
1.94 schwarze 894: fprintf(stderr, " %s", mparse_strlevel(lvl));
895:
896: if (NULL != (mparse_msg = mparse_strerror(t)))
897: fprintf(stderr, ": %s", mparse_msg);
1.30 schwarze 898:
899: if (msg)
900: fprintf(stderr, ": %s", msg);
1.76 schwarze 901:
1.30 schwarze 902: fputc('\n', stderr);
1.95 schwarze 903: }
904:
905: static void
906: spawn_pager(void)
907: {
908: #define MAX_PAGER_ARGS 16
909: char *argv[MAX_PAGER_ARGS];
910: const char *pager;
911: char *cp;
912: int fildes[2];
913: int argc;
914:
915: if (pipe(fildes) == -1) {
916: fprintf(stderr, "%s: pipe: %s\n",
917: progname, strerror(errno));
918: return;
919: }
920:
921: switch (fork()) {
922: case -1:
923: fprintf(stderr, "%s: fork: %s\n",
924: progname, strerror(errno));
925: exit((int)MANDOCLEVEL_SYSERR);
926: case 0:
927: close(fildes[0]);
928: if (dup2(fildes[1], STDOUT_FILENO) == -1) {
929: fprintf(stderr, "%s: dup output: %s\n",
930: progname, strerror(errno));
931: exit((int)MANDOCLEVEL_SYSERR);
932: }
933: return;
934: default:
935: break;
936: }
937:
938: /* The original process becomes the pager. */
939:
940: close(fildes[1]);
941: if (dup2(fildes[0], STDIN_FILENO) == -1) {
942: fprintf(stderr, "%s: dup input: %s\n",
943: progname, strerror(errno));
944: exit((int)MANDOCLEVEL_SYSERR);
945: }
946:
947: pager = getenv("MANPAGER");
948: if (pager == NULL || *pager == '\0')
949: pager = getenv("PAGER");
950: if (pager == NULL || *pager == '\0')
951: pager = "/usr/bin/more -s";
952: cp = mandoc_strdup(pager);
953:
954: /*
955: * Parse the pager command into words.
956: * Intentionally do not do anything fancy here.
957: */
958:
959: argc = 0;
960: while (argc + 1 < MAX_PAGER_ARGS) {
961: argv[argc++] = cp;
962: cp = strchr(cp, ' ');
963: if (cp == NULL)
964: break;
965: *cp++ = '\0';
966: while (*cp == ' ')
967: cp++;
968: if (*cp == '\0')
969: break;
970: }
971: argv[argc] = NULL;
972:
973: /* Hand over to the pager. */
974:
975: execvp(argv[0], argv);
976: fprintf(stderr, "%s: exec: %s\n",
977: progname, strerror(errno));
978: exit((int)MANDOCLEVEL_SYSERR);
1.30 schwarze 979: }