Annotation of mandoc/main.c, Revision 1.157
1.157 ! kristaps 1: /* $Id: main.c,v 1.156 2011/03/20 16:05:21 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.80 kristaps 171: "column syntax is inconsistent",
1.121 kristaps 172: "NOT IMPLEMENTED: .Bd -file",
1.79 kristaps 173: "line scope broken, syntax violated",
174: "argument count wrong, violates syntax",
175: "child violates parent syntax",
176: "argument count wrong, violates syntax",
1.113 kristaps 177: "NOT IMPLEMENTED: .so with absolute path or \"..\"",
1.79 kristaps 178: "no document body",
179: "no document prologue",
1.103 schwarze 180: "static buffer exhausted",
1.5 kristaps 181: };
1.1 kristaps 182:
1.154 kristaps 183: static int moptions(enum mparset *, char *);
1.155 kristaps 184: static void mmsg(enum mandocerr, enum mandoclevel,
185: const char *, int, int, const char *);
1.154 kristaps 186: static void parse(struct curparse *, int,
187: const char *, enum mandoclevel *);
1.66 kristaps 188: static int toptions(struct curparse *, char *);
189: static void usage(void) __attribute__((noreturn));
1.55 kristaps 190: static void version(void) __attribute__((noreturn));
1.103 schwarze 191: static int woptions(struct curparse *, char *);
1.1 kristaps 192:
1.54 kristaps 193: static const char *progname;
1.1 kristaps 194:
195: int
196: main(int argc, char *argv[])
197: {
1.64 kristaps 198: int c;
1.5 kristaps 199: struct curparse curp;
1.154 kristaps 200: enum mparset type;
201: enum mandoclevel rc;
1.1 kristaps 202:
1.54 kristaps 203: progname = strrchr(argv[0], '/');
204: if (progname == NULL)
205: progname = argv[0];
206: else
207: ++progname;
208:
1.51 kristaps 209: memset(&curp, 0, sizeof(struct curparse));
1.5 kristaps 210:
1.154 kristaps 211: type = MPARSE_AUTO;
1.22 kristaps 212: curp.outtype = OUTT_ASCII;
1.103 schwarze 213: curp.wlevel = MANDOCLEVEL_FATAL;
1.19 kristaps 214:
1.1 kristaps 215: /* LINTED */
1.103 schwarze 216: while (-1 != (c = getopt(argc, argv, "m:O:T:VW:")))
1.1 kristaps 217: switch (c) {
1.10 kristaps 218: case ('m'):
1.154 kristaps 219: if ( ! moptions(&type, optarg))
1.105 kristaps 220: return((int)MANDOCLEVEL_BADARG);
1.10 kristaps 221: break;
1.48 kristaps 222: case ('O'):
1.49 kristaps 223: (void)strlcat(curp.outopts, optarg, BUFSIZ);
224: (void)strlcat(curp.outopts, ",", BUFSIZ);
1.44 kristaps 225: break;
1.1 kristaps 226: case ('T'):
1.60 kristaps 227: if ( ! toptions(&curp, optarg))
1.105 kristaps 228: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 229: break;
230: case ('W'):
1.103 schwarze 231: if ( ! woptions(&curp, optarg))
1.105 kristaps 232: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 233: break;
234: case ('V'):
235: version();
236: /* NOTREACHED */
237: default:
238: usage();
239: /* NOTREACHED */
240: }
241:
1.156 kristaps 242: curp.mp = mparse_alloc(type, curp.wlevel, mmsg, &curp);
1.154 kristaps 243:
1.1 kristaps 244: argc -= optind;
245: argv += optind;
246:
1.154 kristaps 247: rc = MANDOCLEVEL_OK;
1.39 kristaps 248:
1.154 kristaps 249: if (NULL == *argv)
250: parse(&curp, STDIN_FILENO, "<stdin>", &rc);
1.64 kristaps 251:
252: while (*argv) {
1.154 kristaps 253: parse(&curp, -1, *argv, &rc);
254: if (MANDOCLEVEL_OK != rc && curp.wstop)
1.64 kristaps 255: break;
256: ++argv;
1.1 kristaps 257: }
258:
1.22 kristaps 259: if (curp.outfree)
260: (*curp.outfree)(curp.outdata);
1.154 kristaps 261: if (curp.mp)
262: mparse_free(curp.mp);
1.1 kristaps 263:
1.154 kristaps 264: return((int)rc);
1.1 kristaps 265: }
266:
1.55 kristaps 267: static void
1.1 kristaps 268: version(void)
269: {
270:
1.154 kristaps 271: printf("%s %s\n", progname, VERSION);
1.105 kristaps 272: exit((int)MANDOCLEVEL_OK);
1.1 kristaps 273: }
274:
1.55 kristaps 275: static void
1.1 kristaps 276: usage(void)
277: {
278:
1.154 kristaps 279: fprintf(stderr, "usage: %s "
1.112 kristaps 280: "[-V] "
281: "[-foption] "
282: "[-mformat] "
283: "[-Ooption] "
284: "[-Toutput] "
285: "[-Werr] "
286: "[file...]\n",
287: progname);
288:
1.105 kristaps 289: exit((int)MANDOCLEVEL_BADARG);
1.68 joerg 290: }
291:
1.64 kristaps 292: static void
1.154 kristaps 293: parse(struct curparse *curp, int fd,
294: const char *file, enum mandoclevel *level)
1.1 kristaps 295: {
1.154 kristaps 296: enum mandoclevel rc;
297: struct mdoc *mdoc;
298: struct man *man;
1.112 kristaps 299:
1.154 kristaps 300: /* Begin by parsing the file itself. */
1.112 kristaps 301:
1.154 kristaps 302: assert(file);
303: assert(fd >= -1);
1.112 kristaps 304:
1.154 kristaps 305: rc = mparse_readfd(curp->mp, fd, file);
1.112 kristaps 306:
1.154 kristaps 307: /* Stop immediately if the parse has failed. */
1.92 kristaps 308:
1.154 kristaps 309: if (MANDOCLEVEL_FATAL <= rc)
1.111 kristaps 310: goto cleanup;
311:
1.1 kristaps 312: /*
1.154 kristaps 313: * With -Wstop and warnings or errors of at least the requested
314: * level, do not produce output.
1.1 kristaps 315: */
316:
1.154 kristaps 317: if (MANDOCLEVEL_OK != rc && curp->wstop)
1.111 kristaps 318: goto cleanup;
319:
320: /* If unset, allocate output dev now (if applicable). */
321:
322: if ( ! (curp->outman && curp->outmdoc)) {
323: switch (curp->outtype) {
324: case (OUTT_XHTML):
325: curp->outdata = xhtml_alloc(curp->outopts);
326: break;
327: case (OUTT_HTML):
328: curp->outdata = html_alloc(curp->outopts);
329: break;
330: case (OUTT_ASCII):
331: curp->outdata = ascii_alloc(curp->outopts);
332: curp->outfree = ascii_free;
333: break;
334: case (OUTT_PDF):
335: curp->outdata = pdf_alloc(curp->outopts);
336: curp->outfree = pspdf_free;
337: break;
338: case (OUTT_PS):
339: curp->outdata = ps_alloc(curp->outopts);
340: curp->outfree = pspdf_free;
341: break;
342: default:
343: break;
344: }
345:
346: switch (curp->outtype) {
347: case (OUTT_HTML):
348: /* FALLTHROUGH */
349: case (OUTT_XHTML):
350: curp->outman = html_man;
351: curp->outmdoc = html_mdoc;
352: curp->outfree = html_free;
353: break;
354: case (OUTT_TREE):
355: curp->outman = tree_man;
356: curp->outmdoc = tree_mdoc;
357: break;
358: case (OUTT_PDF):
359: /* FALLTHROUGH */
360: case (OUTT_ASCII):
361: /* FALLTHROUGH */
362: case (OUTT_PS):
363: curp->outman = terminal_man;
364: curp->outmdoc = terminal_mdoc;
365: break;
366: default:
367: break;
368: }
369: }
370:
1.154 kristaps 371: mparse_result(curp->mp, &mdoc, &man);
372:
1.111 kristaps 373: /* Execute the out device, if it exists. */
374:
1.154 kristaps 375: if (man && curp->outman)
376: (*curp->outman)(curp->outdata, man);
377: if (mdoc && curp->outmdoc)
378: (*curp->outmdoc)(curp->outdata, mdoc);
1.111 kristaps 379:
380: cleanup:
1.112 kristaps 381:
1.154 kristaps 382: mparse_reset(curp->mp);
1.111 kristaps 383:
1.154 kristaps 384: if (*level < rc)
385: *level = rc;
1.10 kristaps 386: }
387:
388: static int
1.154 kristaps 389: moptions(enum mparset *tflags, char *arg)
1.10 kristaps 390: {
391:
1.17 kristaps 392: if (0 == strcmp(arg, "doc"))
1.154 kristaps 393: *tflags = MPARSE_MDOC;
1.19 kristaps 394: else if (0 == strcmp(arg, "andoc"))
1.154 kristaps 395: *tflags = MPARSE_AUTO;
1.17 kristaps 396: else if (0 == strcmp(arg, "an"))
1.154 kristaps 397: *tflags = MPARSE_MAN;
1.10 kristaps 398: else {
1.57 kristaps 399: fprintf(stderr, "%s: Bad argument\n", arg);
1.10 kristaps 400: return(0);
401: }
402:
403: return(1);
1.1 kristaps 404: }
405:
406: static int
1.60 kristaps 407: toptions(struct curparse *curp, char *arg)
1.1 kristaps 408: {
409:
410: if (0 == strcmp(arg, "ascii"))
1.60 kristaps 411: curp->outtype = OUTT_ASCII;
412: else if (0 == strcmp(arg, "lint")) {
413: curp->outtype = OUTT_LINT;
1.103 schwarze 414: curp->wlevel = MANDOCLEVEL_WARNING;
1.154 kristaps 415: } else if (0 == strcmp(arg, "tree"))
1.60 kristaps 416: curp->outtype = OUTT_TREE;
1.43 kristaps 417: else if (0 == strcmp(arg, "html"))
1.60 kristaps 418: curp->outtype = OUTT_HTML;
1.59 kristaps 419: else if (0 == strcmp(arg, "xhtml"))
1.60 kristaps 420: curp->outtype = OUTT_XHTML;
1.85 kristaps 421: else if (0 == strcmp(arg, "ps"))
422: curp->outtype = OUTT_PS;
1.100 kristaps 423: else if (0 == strcmp(arg, "pdf"))
424: curp->outtype = OUTT_PDF;
1.1 kristaps 425: else {
1.57 kristaps 426: fprintf(stderr, "%s: Bad argument\n", arg);
1.1 kristaps 427: return(0);
428: }
429:
430: return(1);
431: }
432:
433: static int
1.103 schwarze 434: woptions(struct curparse *curp, char *arg)
1.1 kristaps 435: {
1.34 kristaps 436: char *v, *o;
1.103 schwarze 437: const char *toks[6];
1.1 kristaps 438:
1.103 schwarze 439: toks[0] = "stop";
440: toks[1] = "all";
441: toks[2] = "warning";
442: toks[3] = "error";
443: toks[4] = "fatal";
444: toks[5] = NULL;
1.1 kristaps 445:
1.34 kristaps 446: while (*arg) {
447: o = arg;
1.45 kristaps 448: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.1 kristaps 449: case (0):
1.103 schwarze 450: curp->wstop = 1;
1.1 kristaps 451: break;
452: case (1):
1.103 schwarze 453: /* FALLTHROUGH */
1.1 kristaps 454: case (2):
1.103 schwarze 455: curp->wlevel = MANDOCLEVEL_WARNING;
1.1 kristaps 456: break;
1.20 kristaps 457: case (3):
1.103 schwarze 458: curp->wlevel = MANDOCLEVEL_ERROR;
1.24 kristaps 459: break;
460: case (4):
1.103 schwarze 461: curp->wlevel = MANDOCLEVEL_FATAL;
1.50 kristaps 462: break;
1.1 kristaps 463: default:
1.103 schwarze 464: fprintf(stderr, "-W%s: Bad argument\n", o);
1.1 kristaps 465: return(0);
466: }
1.34 kristaps 467: }
1.1 kristaps 468:
469: return(1);
470: }
471:
1.153 kristaps 472: static void
1.155 kristaps 473: mmsg(enum mandocerr t, enum mandoclevel lvl,
474: const char *file, int line, int col, const char *msg)
1.71 kristaps 475: {
1.103 schwarze 476:
1.155 kristaps 477: fprintf(stderr, "%s:%d:%d: %s: %s",
478: file, line, col + 1,
479: mandoclevels[lvl], mandocerrs[t]);
1.154 kristaps 480:
1.73 kristaps 481: if (msg)
482: fprintf(stderr, ": %s", msg);
1.154 kristaps 483:
1.73 kristaps 484: fputc('\n', stderr);
1.71 kristaps 485: }
CVSweb