Annotation of mandoc/main.c, Revision 1.158
1.158 ! kristaps 1: /* $Id: main.c,v 1.157 2011/03/21 12:04:26 kristaps Exp $ */
1.1 kristaps 2: /*
1.133 schwarze 3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.140 schwarze 4: * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
1.25 kristaps 7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 9: *
1.25 kristaps 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 17: */
1.58 kristaps 18: #ifdef HAVE_CONFIG_H
19: #include "config.h"
20: #endif
21:
1.1 kristaps 22: #include <assert.h>
23: #include <stdio.h>
1.45 kristaps 24: #include <stdint.h>
1.1 kristaps 25: #include <stdlib.h>
26: #include <string.h>
27: #include <unistd.h>
28:
1.71 kristaps 29: #include "mandoc.h"
1.90 kristaps 30: #include "main.h"
1.1 kristaps 31: #include "mdoc.h"
1.10 kristaps 32: #include "man.h"
1.101 joerg 33:
1.55 kristaps 34: #if !defined(__GNUC__) || (__GNUC__ < 2)
1.56 kristaps 35: # if !defined(lint)
36: # define __attribute__(x)
37: # endif
1.55 kristaps 38: #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
1.16 kristaps 39:
1.42 kristaps 40: typedef void (*out_mdoc)(void *, const struct mdoc *);
41: typedef void (*out_man)(void *, const struct man *);
1.22 kristaps 42: typedef void (*out_free)(void *);
43:
1.19 kristaps 44: enum outt {
1.154 kristaps 45: OUTT_ASCII = 0, /* -Tascii */
46: OUTT_TREE, /* -Ttree */
47: OUTT_HTML, /* -Thtml */
48: OUTT_XHTML, /* -Txhtml */
49: OUTT_LINT, /* -Tlint */
50: OUTT_PS, /* -Tps */
51: OUTT_PDF /* -Tpdf */
1.19 kristaps 52: };
53:
1.5 kristaps 54: struct curparse {
1.154 kristaps 55: struct mparse *mp;
1.148 kristaps 56: enum mandoclevel wlevel; /* ignore messages below this */
57: int wstop; /* stop after a file with a warning */
1.79 kristaps 58: enum outt outtype; /* which output to use */
59: out_mdoc outmdoc; /* mdoc output ptr */
60: out_man outman; /* man output ptr */
61: out_free outfree; /* free output ptr */
62: void *outdata; /* data for output */
63: char outopts[BUFSIZ]; /* buf of output opts */
64: };
65:
1.103 schwarze 66: static const char * const mandoclevels[MANDOCLEVEL_MAX] = {
67: "SUCCESS",
68: "RESERVED",
69: "WARNING",
70: "ERROR",
71: "FATAL",
72: "BADARG",
73: "SYSERR"
74: };
75:
1.79 kristaps 76: static const char * const mandocerrs[MANDOCERR_MAX] = {
77: "ok",
1.94 schwarze 78:
79: "generic warning",
80:
1.121 kristaps 81: /* related to the prologue */
82: "no title in document",
83: "document title should be all caps",
84: "unknown manual section",
1.147 schwarze 85: "date missing, using today's date",
86: "cannot parse date, using it verbatim",
1.121 kristaps 87: "prologue macros out of order",
88: "duplicate prologue macro",
89: "macro not allowed in prologue",
90: "macro not allowed in body",
91:
92: /* related to document structure */
1.113 kristaps 93: ".so is fragile, better use ln(1)",
1.121 kristaps 94: "NAME section must come first",
95: "bad NAME section contents",
96: "manual name not yet set",
1.81 kristaps 97: "sections out of conventional order",
1.121 kristaps 98: "duplicate section name",
1.79 kristaps 99: "section not in conventional manual section",
1.121 kristaps 100:
101: /* related to macros and nesting */
102: "skipping obsolete macro",
1.125 kristaps 103: "skipping paragraph macro",
1.142 kristaps 104: "skipping no-space macro",
1.95 schwarze 105: "blocks badly nested",
1.121 kristaps 106: "child violates parent syntax",
107: "nested displays are not portable",
108: "already in literal mode",
1.94 schwarze 109:
1.121 kristaps 110: /* related to missing macro arguments */
111: "skipping empty macro",
1.133 schwarze 112: "argument count wrong",
1.121 kristaps 113: "missing display type",
114: "list type must come first",
115: "tag lists require a width argument",
116: "missing font type",
1.138 kristaps 117: "skipping end of block that is not open",
1.94 schwarze 118:
1.121 kristaps 119: /* related to bad macro arguments */
120: "skipping argument",
121: "duplicate argument",
122: "duplicate display type",
123: "duplicate list type",
124: "unknown AT&T UNIX version",
1.79 kristaps 125: "bad Boolean value",
1.120 kristaps 126: "unknown font",
1.121 kristaps 127: "unknown standard specifier",
128: "bad width argument",
129:
130: /* related to plain text */
131: "blank line in non-literal context",
132: "tab in non-literal context",
133: "end of line whitespace",
1.79 kristaps 134: "bad comment style",
1.121 kristaps 135: "unknown escape sequence",
136: "unterminated quoted string",
1.131 kristaps 137:
1.121 kristaps 138: "generic error",
139:
1.131 kristaps 140: /* related to tables */
1.126 kristaps 141: "bad table syntax",
142: "bad table option",
1.127 kristaps 143: "bad table layout",
144: "no table layout cells specified",
1.129 kristaps 145: "no table data cells specified",
1.134 kristaps 146: "ignore data in cell",
1.135 kristaps 147: "data block still open",
1.136 kristaps 148: "ignoring extra data cells",
1.134 kristaps 149:
1.122 schwarze 150: "input stack limit exceeded, infinite loop?",
1.121 kristaps 151: "skipping bad character",
1.137 schwarze 152: "escaped character not allowed in a name",
1.121 kristaps 153: "skipping text before the first section header",
154: "skipping unknown macro",
1.139 schwarze 155: "NOT IMPLEMENTED, please use groff: skipping request",
1.79 kristaps 156: "line scope broken",
157: "argument count wrong",
1.121 kristaps 158: "skipping end of block that is not open",
159: "missing end of block",
1.106 schwarze 160: "scope open on exit",
1.117 kristaps 161: "uname(3) system call failed",
1.79 kristaps 162: "macro requires line argument(s)",
163: "macro requires body argument(s)",
164: "macro requires argument(s)",
1.82 kristaps 165: "missing list type",
1.79 kristaps 166: "line argument(s) will be lost",
167: "body argument(s) will be lost",
1.94 schwarze 168:
169: "generic fatal error",
170:
1.158 ! kristaps 171: "not a manual",
1.80 kristaps 172: "column syntax is inconsistent",
1.121 kristaps 173: "NOT IMPLEMENTED: .Bd -file",
1.79 kristaps 174: "line scope broken, syntax violated",
175: "argument count wrong, violates syntax",
176: "child violates parent syntax",
177: "argument count wrong, violates syntax",
1.113 kristaps 178: "NOT IMPLEMENTED: .so with absolute path or \"..\"",
1.79 kristaps 179: "no document body",
180: "no document prologue",
1.103 schwarze 181: "static buffer exhausted",
1.5 kristaps 182: };
1.1 kristaps 183:
1.154 kristaps 184: static int moptions(enum mparset *, char *);
1.155 kristaps 185: static void mmsg(enum mandocerr, enum mandoclevel,
186: const char *, int, int, const char *);
1.154 kristaps 187: static void parse(struct curparse *, int,
188: const char *, enum mandoclevel *);
1.66 kristaps 189: static int toptions(struct curparse *, char *);
190: static void usage(void) __attribute__((noreturn));
1.55 kristaps 191: static void version(void) __attribute__((noreturn));
1.103 schwarze 192: static int woptions(struct curparse *, char *);
1.1 kristaps 193:
1.54 kristaps 194: static const char *progname;
1.1 kristaps 195:
196: int
197: main(int argc, char *argv[])
198: {
1.64 kristaps 199: int c;
1.5 kristaps 200: struct curparse curp;
1.154 kristaps 201: enum mparset type;
202: enum mandoclevel rc;
1.1 kristaps 203:
1.54 kristaps 204: progname = strrchr(argv[0], '/');
205: if (progname == NULL)
206: progname = argv[0];
207: else
208: ++progname;
209:
1.51 kristaps 210: memset(&curp, 0, sizeof(struct curparse));
1.5 kristaps 211:
1.154 kristaps 212: type = MPARSE_AUTO;
1.22 kristaps 213: curp.outtype = OUTT_ASCII;
1.103 schwarze 214: curp.wlevel = MANDOCLEVEL_FATAL;
1.19 kristaps 215:
1.1 kristaps 216: /* LINTED */
1.103 schwarze 217: while (-1 != (c = getopt(argc, argv, "m:O:T:VW:")))
1.1 kristaps 218: switch (c) {
1.10 kristaps 219: case ('m'):
1.154 kristaps 220: if ( ! moptions(&type, optarg))
1.105 kristaps 221: return((int)MANDOCLEVEL_BADARG);
1.10 kristaps 222: break;
1.48 kristaps 223: case ('O'):
1.49 kristaps 224: (void)strlcat(curp.outopts, optarg, BUFSIZ);
225: (void)strlcat(curp.outopts, ",", BUFSIZ);
1.44 kristaps 226: break;
1.1 kristaps 227: case ('T'):
1.60 kristaps 228: if ( ! toptions(&curp, optarg))
1.105 kristaps 229: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 230: break;
231: case ('W'):
1.103 schwarze 232: if ( ! woptions(&curp, optarg))
1.105 kristaps 233: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 234: break;
235: case ('V'):
236: version();
237: /* NOTREACHED */
238: default:
239: usage();
240: /* NOTREACHED */
241: }
242:
1.156 kristaps 243: curp.mp = mparse_alloc(type, curp.wlevel, mmsg, &curp);
1.154 kristaps 244:
1.1 kristaps 245: argc -= optind;
246: argv += optind;
247:
1.154 kristaps 248: rc = MANDOCLEVEL_OK;
1.39 kristaps 249:
1.154 kristaps 250: if (NULL == *argv)
251: parse(&curp, STDIN_FILENO, "<stdin>", &rc);
1.64 kristaps 252:
253: while (*argv) {
1.154 kristaps 254: parse(&curp, -1, *argv, &rc);
255: if (MANDOCLEVEL_OK != rc && curp.wstop)
1.64 kristaps 256: break;
257: ++argv;
1.1 kristaps 258: }
259:
1.22 kristaps 260: if (curp.outfree)
261: (*curp.outfree)(curp.outdata);
1.154 kristaps 262: if (curp.mp)
263: mparse_free(curp.mp);
1.1 kristaps 264:
1.154 kristaps 265: return((int)rc);
1.1 kristaps 266: }
267:
1.55 kristaps 268: static void
1.1 kristaps 269: version(void)
270: {
271:
1.154 kristaps 272: printf("%s %s\n", progname, VERSION);
1.105 kristaps 273: exit((int)MANDOCLEVEL_OK);
1.1 kristaps 274: }
275:
1.55 kristaps 276: static void
1.1 kristaps 277: usage(void)
278: {
279:
1.154 kristaps 280: fprintf(stderr, "usage: %s "
1.112 kristaps 281: "[-V] "
282: "[-foption] "
283: "[-mformat] "
284: "[-Ooption] "
285: "[-Toutput] "
286: "[-Werr] "
287: "[file...]\n",
288: progname);
289:
1.105 kristaps 290: exit((int)MANDOCLEVEL_BADARG);
1.68 joerg 291: }
292:
1.64 kristaps 293: static void
1.154 kristaps 294: parse(struct curparse *curp, int fd,
295: const char *file, enum mandoclevel *level)
1.1 kristaps 296: {
1.154 kristaps 297: enum mandoclevel rc;
298: struct mdoc *mdoc;
299: struct man *man;
1.112 kristaps 300:
1.154 kristaps 301: /* Begin by parsing the file itself. */
1.112 kristaps 302:
1.154 kristaps 303: assert(file);
304: assert(fd >= -1);
1.112 kristaps 305:
1.154 kristaps 306: rc = mparse_readfd(curp->mp, fd, file);
1.112 kristaps 307:
1.154 kristaps 308: /* Stop immediately if the parse has failed. */
1.92 kristaps 309:
1.154 kristaps 310: if (MANDOCLEVEL_FATAL <= rc)
1.111 kristaps 311: goto cleanup;
312:
1.1 kristaps 313: /*
1.154 kristaps 314: * With -Wstop and warnings or errors of at least the requested
315: * level, do not produce output.
1.1 kristaps 316: */
317:
1.154 kristaps 318: if (MANDOCLEVEL_OK != rc && curp->wstop)
1.111 kristaps 319: goto cleanup;
320:
321: /* If unset, allocate output dev now (if applicable). */
322:
323: if ( ! (curp->outman && curp->outmdoc)) {
324: switch (curp->outtype) {
325: case (OUTT_XHTML):
326: curp->outdata = xhtml_alloc(curp->outopts);
327: break;
328: case (OUTT_HTML):
329: curp->outdata = html_alloc(curp->outopts);
330: break;
331: case (OUTT_ASCII):
332: curp->outdata = ascii_alloc(curp->outopts);
333: curp->outfree = ascii_free;
334: break;
335: case (OUTT_PDF):
336: curp->outdata = pdf_alloc(curp->outopts);
337: curp->outfree = pspdf_free;
338: break;
339: case (OUTT_PS):
340: curp->outdata = ps_alloc(curp->outopts);
341: curp->outfree = pspdf_free;
342: break;
343: default:
344: break;
345: }
346:
347: switch (curp->outtype) {
348: case (OUTT_HTML):
349: /* FALLTHROUGH */
350: case (OUTT_XHTML):
351: curp->outman = html_man;
352: curp->outmdoc = html_mdoc;
353: curp->outfree = html_free;
354: break;
355: case (OUTT_TREE):
356: curp->outman = tree_man;
357: curp->outmdoc = tree_mdoc;
358: break;
359: case (OUTT_PDF):
360: /* FALLTHROUGH */
361: case (OUTT_ASCII):
362: /* FALLTHROUGH */
363: case (OUTT_PS):
364: curp->outman = terminal_man;
365: curp->outmdoc = terminal_mdoc;
366: break;
367: default:
368: break;
369: }
370: }
371:
1.154 kristaps 372: mparse_result(curp->mp, &mdoc, &man);
373:
1.111 kristaps 374: /* Execute the out device, if it exists. */
375:
1.154 kristaps 376: if (man && curp->outman)
377: (*curp->outman)(curp->outdata, man);
378: if (mdoc && curp->outmdoc)
379: (*curp->outmdoc)(curp->outdata, mdoc);
1.111 kristaps 380:
381: cleanup:
1.112 kristaps 382:
1.154 kristaps 383: mparse_reset(curp->mp);
1.111 kristaps 384:
1.154 kristaps 385: if (*level < rc)
386: *level = rc;
1.10 kristaps 387: }
388:
389: static int
1.154 kristaps 390: moptions(enum mparset *tflags, char *arg)
1.10 kristaps 391: {
392:
1.17 kristaps 393: if (0 == strcmp(arg, "doc"))
1.154 kristaps 394: *tflags = MPARSE_MDOC;
1.19 kristaps 395: else if (0 == strcmp(arg, "andoc"))
1.154 kristaps 396: *tflags = MPARSE_AUTO;
1.17 kristaps 397: else if (0 == strcmp(arg, "an"))
1.154 kristaps 398: *tflags = MPARSE_MAN;
1.10 kristaps 399: else {
1.57 kristaps 400: fprintf(stderr, "%s: Bad argument\n", arg);
1.10 kristaps 401: return(0);
402: }
403:
404: return(1);
1.1 kristaps 405: }
406:
407: static int
1.60 kristaps 408: toptions(struct curparse *curp, char *arg)
1.1 kristaps 409: {
410:
411: if (0 == strcmp(arg, "ascii"))
1.60 kristaps 412: curp->outtype = OUTT_ASCII;
413: else if (0 == strcmp(arg, "lint")) {
414: curp->outtype = OUTT_LINT;
1.103 schwarze 415: curp->wlevel = MANDOCLEVEL_WARNING;
1.154 kristaps 416: } else if (0 == strcmp(arg, "tree"))
1.60 kristaps 417: curp->outtype = OUTT_TREE;
1.43 kristaps 418: else if (0 == strcmp(arg, "html"))
1.60 kristaps 419: curp->outtype = OUTT_HTML;
1.59 kristaps 420: else if (0 == strcmp(arg, "xhtml"))
1.60 kristaps 421: curp->outtype = OUTT_XHTML;
1.85 kristaps 422: else if (0 == strcmp(arg, "ps"))
423: curp->outtype = OUTT_PS;
1.100 kristaps 424: else if (0 == strcmp(arg, "pdf"))
425: curp->outtype = OUTT_PDF;
1.1 kristaps 426: else {
1.57 kristaps 427: fprintf(stderr, "%s: Bad argument\n", arg);
1.1 kristaps 428: return(0);
429: }
430:
431: return(1);
432: }
433:
434: static int
1.103 schwarze 435: woptions(struct curparse *curp, char *arg)
1.1 kristaps 436: {
1.34 kristaps 437: char *v, *o;
1.103 schwarze 438: const char *toks[6];
1.1 kristaps 439:
1.103 schwarze 440: toks[0] = "stop";
441: toks[1] = "all";
442: toks[2] = "warning";
443: toks[3] = "error";
444: toks[4] = "fatal";
445: toks[5] = NULL;
1.1 kristaps 446:
1.34 kristaps 447: while (*arg) {
448: o = arg;
1.45 kristaps 449: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.1 kristaps 450: case (0):
1.103 schwarze 451: curp->wstop = 1;
1.1 kristaps 452: break;
453: case (1):
1.103 schwarze 454: /* FALLTHROUGH */
1.1 kristaps 455: case (2):
1.103 schwarze 456: curp->wlevel = MANDOCLEVEL_WARNING;
1.1 kristaps 457: break;
1.20 kristaps 458: case (3):
1.103 schwarze 459: curp->wlevel = MANDOCLEVEL_ERROR;
1.24 kristaps 460: break;
461: case (4):
1.103 schwarze 462: curp->wlevel = MANDOCLEVEL_FATAL;
1.50 kristaps 463: break;
1.1 kristaps 464: default:
1.103 schwarze 465: fprintf(stderr, "-W%s: Bad argument\n", o);
1.1 kristaps 466: return(0);
467: }
1.34 kristaps 468: }
1.1 kristaps 469:
470: return(1);
471: }
472:
1.153 kristaps 473: static void
1.155 kristaps 474: mmsg(enum mandocerr t, enum mandoclevel lvl,
475: const char *file, int line, int col, const char *msg)
1.71 kristaps 476: {
1.103 schwarze 477:
1.155 kristaps 478: fprintf(stderr, "%s:%d:%d: %s: %s",
479: file, line, col + 1,
480: mandoclevels[lvl], mandocerrs[t]);
1.154 kristaps 481:
1.73 kristaps 482: if (msg)
483: fprintf(stderr, ": %s", msg);
1.154 kristaps 484:
1.73 kristaps 485: fputc('\n', stderr);
1.71 kristaps 486: }
CVSweb