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