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