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