Annotation of mandoc/mdocml.c, Revision 1.40
1.40 ! kristaps 1: /* $Id: mdocml.c,v 1.39 2009/01/12 10:31:53 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the
7: * above copyright notice and this permission notice appear in all
8: * copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11: * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12: * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13: * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14: * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15: * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16: * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17: * PERFORMANCE OF THIS SOFTWARE.
18: */
1.21 kristaps 19: #include <sys/stat.h>
1.1 kristaps 20: #include <sys/param.h>
21:
22: #include <assert.h>
1.21 kristaps 23: #include <fcntl.h>
1.1 kristaps 24: #include <err.h>
25: #include <getopt.h>
26: #include <stdio.h>
27: #include <stdlib.h>
28: #include <string.h>
29: #include <unistd.h>
30:
1.21 kristaps 31: #include "mdoc.h"
1.1 kristaps 32:
1.21 kristaps 33: #define MD_LINE_SZ (256)
1.2 kristaps 34:
1.21 kristaps 35: struct md_parse {
36: int warn;
37: #define MD_WARN_ALL (1 << 0)
38: #define MD_WARN_ERR (1 << 1)
39: int dbg;
40: struct mdoc *mdoc;
41: char *buf;
42: u_long bufsz;
43: char *name;
44: int fd;
45: int lnn;
46: char *line;
47: };
1.17 kristaps 48:
1.9 kristaps 49: static void usage(void);
50:
1.21 kristaps 51: static int parse_begin(struct md_parse *);
52: static int parse_leave(struct md_parse *, int);
53: static int io_begin(struct md_parse *);
54: static int io_leave(struct md_parse *, int);
55: static int buf_begin(struct md_parse *);
56: static int buf_leave(struct md_parse *, int);
57:
58: static int msg_err(void *, int, int, enum mdoc_err);
59: static int msg_warn(void *, int, int, enum mdoc_warn);
1.37 kristaps 60: static void msg_msg(void *, int, int, const char *);
1.1 kristaps 61:
1.19 kristaps 62: #ifdef __linux__
63: extern int getsubopt(char **, char *const *, char **);
64: #endif
65:
1.1 kristaps 66: int
67: main(int argc, char *argv[])
68: {
69: int c;
1.21 kristaps 70: struct md_parse parser;
71: char *opts, *v;
1.18 kristaps 72: #define ALL 0
73: #define ERROR 1
74: char *toks[] = { "all", "error", NULL };
1.1 kristaps 75:
76: extern char *optarg;
77: extern int optind;
78:
1.21 kristaps 79: (void)memset(&parser, 0, sizeof(struct md_parse));
1.17 kristaps 80:
1.21 kristaps 81: while (-1 != (c = getopt(argc, argv, "vW:")))
1.1 kristaps 82: switch (c) {
1.13 kristaps 83: case ('v'):
1.21 kristaps 84: parser.dbg++;
1.13 kristaps 85: break;
86: case ('W'):
1.18 kristaps 87: opts = optarg;
88: while (*opts)
89: switch (getsubopt(&opts, toks, &v)) {
90: case (ALL):
1.21 kristaps 91: parser.warn |= MD_WARN_ALL;
1.18 kristaps 92: break;
93: case (ERROR):
1.21 kristaps 94: parser.warn |= MD_WARN_ERR;
1.18 kristaps 95: break;
96: default:
97: usage();
98: return(1);
99: }
1.13 kristaps 100: break;
1.1 kristaps 101: default:
102: usage();
103: return(1);
104: }
105:
106: argv += optind;
1.4 kristaps 107: argc -= optind;
1.1 kristaps 108:
1.21 kristaps 109: parser.name = "-";
1.4 kristaps 110: if (1 == argc)
1.21 kristaps 111: parser.name = *argv++;
112:
113: if ( ! io_begin(&parser))
114: return(EXIT_FAILURE);
1.1 kristaps 115:
1.21 kristaps 116: return(EXIT_SUCCESS);
1.1 kristaps 117: }
118:
119:
120: static int
1.21 kristaps 121: io_leave(struct md_parse *p, int code)
1.1 kristaps 122: {
123:
1.21 kristaps 124: if (-1 == p->fd || STDIN_FILENO == p->fd)
125: return(code);
126:
127: if (-1 == close(p->fd)) {
128: warn("%s", p->name);
129: code = 0;
1.4 kristaps 130: }
1.21 kristaps 131: return(code);
132: }
133:
134:
135: static int
136: io_begin(struct md_parse *p)
137: {
138:
139: p->fd = STDIN_FILENO;
140: if (0 != strncmp(p->name, "-", 1))
141: if (-1 == (p->fd = open(p->name, O_RDONLY, 0))) {
142: warn("%s", p->name);
143: return(io_leave(p, 0));
144: }
1.1 kristaps 145:
1.21 kristaps 146: return(io_leave(p, buf_begin(p)));
1.1 kristaps 147: }
148:
149:
150: static int
1.21 kristaps 151: buf_leave(struct md_parse *p, int code)
1.1 kristaps 152: {
1.4 kristaps 153:
1.21 kristaps 154: if (p->buf)
155: free(p->buf);
156: return(code);
157: }
1.1 kristaps 158:
159:
1.21 kristaps 160: static int
161: buf_begin(struct md_parse *p)
162: {
163: struct stat st;
1.1 kristaps 164:
1.21 kristaps 165: if (-1 == fstat(p->fd, &st)) {
166: warn("%s", p->name);
167: return(1);
168: }
169:
170: p->bufsz = MAX(st.st_blksize, BUFSIZ);
171:
172: if (NULL == (p->buf = malloc(p->bufsz))) {
173: warn("malloc");
174: return(buf_leave(p, 0));
175: }
176:
177: return(buf_leave(p, parse_begin(p)));
178: }
179:
180:
181: static void
182: print_node(const struct mdoc_node *n, int indent)
183: {
1.26 kristaps 184: const char *p, *t;
185: int i, j;
186: size_t argc, sz;
187: char **params;
188: struct mdoc_arg *argv;
1.24 kristaps 189:
190: argv = NULL;
191: argc = 0;
1.26 kristaps 192: params = NULL;
193: sz = 0;
1.21 kristaps 194:
1.40 ! kristaps 195: /* FIXME: put parts of this in util.c. */
1.21 kristaps 196: switch (n->type) {
197: case (MDOC_TEXT):
198: assert(NULL == n->child);
1.25 kristaps 199: p = n->data.text.string;
1.21 kristaps 200: t = "text";
201: break;
202: case (MDOC_BODY):
1.40 ! kristaps 203: p = mdoc_macronames[n->tok];
1.21 kristaps 204: t = "block-body";
205: break;
206: case (MDOC_HEAD):
1.40 ! kristaps 207: p = mdoc_macronames[n->tok];
1.21 kristaps 208: t = "block-head";
209: break;
1.34 kristaps 210: case (MDOC_TAIL):
1.40 ! kristaps 211: p = mdoc_macronames[n->tok];
1.34 kristaps 212: t = "block-tail";
213: break;
1.21 kristaps 214: case (MDOC_ELEM):
1.40 ! kristaps 215: p = mdoc_macronames[n->tok];
1.21 kristaps 216: t = "element";
1.24 kristaps 217: argv = n->data.elem.argv;
218: argc = n->data.elem.argc;
1.21 kristaps 219: break;
220: case (MDOC_BLOCK):
1.40 ! kristaps 221: p = mdoc_macronames[n->tok];
1.21 kristaps 222: t = "block";
1.24 kristaps 223: argv = n->data.block.argv;
224: argc = n->data.block.argc;
1.21 kristaps 225: break;
1.38 kristaps 226: case (MDOC_ROOT):
227: p = "root";
228: t = "root";
229: break;
1.22 kristaps 230: default:
231: abort();
232: /* NOTREACHED */
1.21 kristaps 233: }
234:
235: for (i = 0; i < indent; i++)
236: (void)printf(" ");
1.24 kristaps 237: (void)printf("%s (%s)", p, t);
238:
239: for (i = 0; i < (int)argc; i++) {
240: (void)printf(" -%s", mdoc_argnames[argv[i].arg]);
241: for (j = 0; j < (int)argv[i].sz; j++)
242: (void)printf(" \"%s\"", argv[i].value[j]);
243: }
244:
1.26 kristaps 245: for (i = 0; i < (int)sz; i++)
246: (void)printf(" \"%s\"", params[i]);
247:
1.39 kristaps 248: (void)printf(" %d:%d\n", n->line, n->pos);
1.21 kristaps 249:
250: if (n->child)
251: print_node(n->child, indent + 1);
252: if (n->next)
253: print_node(n->next, indent);
254: }
1.1 kristaps 255:
256:
1.21 kristaps 257: static int
258: parse_leave(struct md_parse *p, int code)
259: {
260: const struct mdoc_node *n;
261:
1.36 kristaps 262: if (NULL == p->mdoc)
263: return(code);
264:
265: if ( ! mdoc_endparse(p->mdoc))
266: code = 0;
267: if ((n = mdoc_result(p->mdoc)))
268: print_node(n, 0);
269:
1.38 kristaps 270: mdoc_free(p->mdoc);
271:
1.21 kristaps 272: return(code);
273: }
274:
275:
276: static int
277: parse_begin(struct md_parse *p)
278: {
279: ssize_t sz, i;
280: size_t pos;
281: char line[256], sv[256];
282: struct mdoc_cb cb;
283:
284: cb.mdoc_err = msg_err;
285: cb.mdoc_warn = msg_warn;
286: cb.mdoc_msg = msg_msg;
287:
288: if (NULL == (p->mdoc = mdoc_alloc(p, &cb)))
289: return(parse_leave(p, 0));
290:
291: p->lnn = 1;
292: p->line = sv;
293:
294: for (pos = 0; ; ) {
295: if (-1 == (sz = read(p->fd, p->buf, p->bufsz))) {
296: warn("%s", p->name);
297: return(parse_leave(p, 0));
298: } else if (0 == sz)
299: break;
300:
301: for (i = 0; i < sz; i++) {
302: if ('\n' != p->buf[i]) {
303: if (pos < sizeof(line)) {
1.23 kristaps 304: sv[(int)pos] = p->buf[(int)i];
305: line[(int)pos++] =
306: p->buf[(int)i];
1.21 kristaps 307: continue;
308: }
309: warnx("%s: line %d too long",
310: p->name, p->lnn);
311: return(parse_leave(p, 0));
312: }
313:
314: line[(int)pos] = sv[(int)pos] = 0;
1.36 kristaps 315: if ( ! mdoc_parseln(p->mdoc, p->lnn, line))
1.21 kristaps 316: return(parse_leave(p, 0));
1.1 kristaps 317:
1.21 kristaps 318: p->lnn++;
319: pos = 0;
1.4 kristaps 320: }
1.21 kristaps 321: }
1.1 kristaps 322:
1.21 kristaps 323: return(parse_leave(p, 1));
1.4 kristaps 324: }
1.1 kristaps 325:
326:
1.4 kristaps 327: static int
1.37 kristaps 328: msg_err(void *arg, int line, int col, enum mdoc_err type)
1.21 kristaps 329: {
1.37 kristaps 330: char *lit;
1.21 kristaps 331: struct md_parse *p;
332:
333: p = (struct md_parse *)arg;
334:
1.37 kristaps 335: lit = NULL;
1.21 kristaps 336:
337: switch (type) {
1.35 kristaps 338: case (ERR_SYNTAX_NOTEXT):
339: lit = "syntax: context-free text disallowed";
340: break;
1.21 kristaps 341: case (ERR_SYNTAX_QUOTE):
1.24 kristaps 342: lit = "syntax: disallowed argument quotation";
343: break;
344: case (ERR_SYNTAX_UNQUOTE):
1.21 kristaps 345: lit = "syntax: unterminated quotation";
346: break;
347: case (ERR_SYNTAX_WS):
348: lit = "syntax: whitespace in argument";
349: break;
1.25 kristaps 350: case (ERR_SYNTAX_ARGFORM):
1.37 kristaps 351: lit = "syntax: macro arguments malformed";
1.23 kristaps 352: break;
1.28 kristaps 353: case (ERR_SYNTAX_NOPUNCT):
1.37 kristaps 354: lit = "syntax: macro doesn't understand punctuation";
1.28 kristaps 355: break;
1.25 kristaps 356: case (ERR_SYNTAX_ARG):
1.37 kristaps 357: lit = "syntax: unknown argument for macro";
1.24 kristaps 358: break;
1.21 kristaps 359: case (ERR_SCOPE_BREAK):
360: /* Which scope is broken? */
1.37 kristaps 361: lit = "scope: macro breaks prior explicit scope";
1.24 kristaps 362: break;
363: case (ERR_SCOPE_NOCTX):
1.37 kristaps 364: lit = "scope: closure macro has no context";
1.21 kristaps 365: break;
1.25 kristaps 366: case (ERR_SCOPE_NONEST):
1.37 kristaps 367: lit = "scope: macro may not be nested in the current context";
1.25 kristaps 368: break;
1.21 kristaps 369: case (ERR_MACRO_NOTSUP):
1.35 kristaps 370: lit = "macro not supported";
1.21 kristaps 371: break;
372: case (ERR_MACRO_NOTCALL):
1.37 kristaps 373: lit = "macro not callable";
1.21 kristaps 374: break;
1.23 kristaps 375: case (ERR_SEC_PROLOGUE):
1.37 kristaps 376: lit = "macro cannot be called in the prologue";
1.23 kristaps 377: break;
378: case (ERR_SEC_NPROLOGUE):
1.37 kristaps 379: lit = "macro called outside of prologue";
1.23 kristaps 380: break;
1.28 kristaps 381: case (ERR_ARGS_EQ0):
1.37 kristaps 382: lit = "macro expects zero arguments";
1.28 kristaps 383: break;
1.29 kristaps 384: case (ERR_ARGS_EQ1):
1.37 kristaps 385: lit = "macro expects one argument";
1.29 kristaps 386: break;
1.21 kristaps 387: case (ERR_ARGS_GE1):
1.37 kristaps 388: lit = "macro expects one or more arguments";
1.21 kristaps 389: break;
1.28 kristaps 390: case (ERR_ARGS_LE2):
1.37 kristaps 391: lit = "macro expects two or fewer arguments";
1.28 kristaps 392: break;
1.36 kristaps 393: case (ERR_ARGS_LE8):
1.37 kristaps 394: lit = "macro expects eight or fewer arguments";
1.36 kristaps 395: break;
1.23 kristaps 396: case (ERR_ARGS_MANY):
1.37 kristaps 397: lit = "macro has too many arguments";
1.23 kristaps 398: break;
399: case (ERR_SEC_PROLOGUE_OO):
1.37 kristaps 400: lit = "prologue macro is out-of-order";
1.23 kristaps 401: break;
402: case (ERR_SEC_PROLOGUE_REP):
1.37 kristaps 403: lit = "prologue macro repeated";
1.23 kristaps 404: break;
405: case (ERR_SEC_NAME):
406: lit = "`NAME' section must be first";
407: break;
1.24 kristaps 408: case (ERR_SYNTAX_ARGVAL):
409: lit = "syntax: expected value for macro argument";
410: break;
1.25 kristaps 411: case (ERR_SYNTAX_ARGBAD):
1.39 kristaps 412: lit = "syntax: invalid value(s) for macro argument";
413: break;
414: case (ERR_SYNTAX_ARGMISS):
415: lit = "syntax: missing required argument(s) for macro";
1.25 kristaps 416: break;
1.24 kristaps 417: case (ERR_SYNTAX_ARGMANY):
418: lit = "syntax: too many values for macro argument";
419: break;
1.39 kristaps 420: case (ERR_SYNTAX_CHILDBAD):
421: lit = "syntax: invalid child for parent macro";
422: break;
1.40 ! kristaps 423: case (ERR_SYNTAX_PARENTBAD):
! 424: lit = "syntax: invalid parent for macro";
! 425: break;
1.32 kristaps 426: case (ERR_SYNTAX_CHILDHEAD):
427: lit = "syntax: expected only block-header section";
428: break;
429: case (ERR_SYNTAX_CHILDBODY):
430: lit = "syntax: expected only a block-body section";
431: break;
432: case (ERR_SYNTAX_EMPTYHEAD):
433: lit = "syntax: block-header section may not be empty";
434: break;
435: case (ERR_SYNTAX_EMPTYBODY):
436: lit = "syntax: block-body section may not be empty";
1.31 kristaps 437: break;
1.21 kristaps 438: default:
439: abort();
440: /* NOTREACHED */
441: }
442:
1.39 kristaps 443: (void)fprintf(stderr, "%s:%d: error: %s (column %d)\n",
444: p->name, line, lit, col);
1.21 kristaps 445: return(0);
446: }
447:
448:
449: static void
1.37 kristaps 450: msg_msg(void *arg, int line, int col, const char *msg)
1.4 kristaps 451: {
1.21 kristaps 452: struct md_parse *p;
453:
454: p = (struct md_parse *)arg;
455:
456: if (p->dbg < 2)
457: return;
458:
1.39 kristaps 459: (void)printf("%s:%d: %s (column %d)\n",
460: p->name, line, msg, col);
1.1 kristaps 461: }
462:
463:
464: static int
1.37 kristaps 465: msg_warn(void *arg, int line, int col, enum mdoc_warn type)
1.1 kristaps 466: {
1.37 kristaps 467: char *lit;
1.21 kristaps 468: struct md_parse *p;
469: extern char *__progname;
1.1 kristaps 470:
1.21 kristaps 471: p = (struct md_parse *)arg;
1.1 kristaps 472:
1.21 kristaps 473: if ( ! (p->warn & MD_WARN_ALL))
1.1 kristaps 474: return(1);
1.21 kristaps 475:
1.37 kristaps 476: lit = NULL;
1.21 kristaps 477:
478: switch (type) {
479: case (WARN_SYNTAX_WS_EOLN):
480: lit = "syntax: whitespace at end-of-line";
481: break;
1.25 kristaps 482: case (WARN_SYNTAX_QUOTED):
483: lit = "syntax: quotation mark starting string";
484: break;
1.21 kristaps 485: case (WARN_SYNTAX_MACLIKE):
486: lit = "syntax: macro-like argument";
487: break;
1.24 kristaps 488: case (WARN_SYNTAX_ARGLIKE):
489: lit = "syntax: argument-like value";
490: break;
1.32 kristaps 491: case (WARN_SYNTAX_EMPTYBODY):
1.40 ! kristaps 492: lit = "syntax: macro suggests non-empty block-body section";
! 493: break;
! 494: case (WARN_SYNTAX_EMPTYHEAD):
! 495: lit = "syntax: macro suggests non-empty block-head section";
! 496: break;
! 497: case (WARN_SYNTAX_NOBODY):
! 498: lit = "syntax: macro suggests empty block-body section";
1.32 kristaps 499: break;
1.23 kristaps 500: case (WARN_SEC_OO):
501: lit = "section is out of conventional order";
502: break;
1.38 kristaps 503: case (WARN_SEC_REP):
504: lit = "section repeated";
505: break;
1.21 kristaps 506: case (WARN_ARGS_GE1):
1.37 kristaps 507: lit = "macro suggests one or more arguments";
1.21 kristaps 508: break;
1.25 kristaps 509: case (WARN_ARGS_EQ0):
1.37 kristaps 510: lit = "macro suggests zero arguments";
1.25 kristaps 511: break;
512: case (WARN_IGN_AFTER_BLK):
1.37 kristaps 513: lit = "ignore: macro ignored after block macro";
1.25 kristaps 514: break;
1.30 kristaps 515: case (WARN_IGN_OBSOLETE):
1.37 kristaps 516: lit = "ignore: macro is obsolete";
1.30 kristaps 517: break;
1.25 kristaps 518: case (WARN_IGN_BEFORE_BLK):
1.37 kristaps 519: lit = "ignore: macro before block macro ignored";
1.25 kristaps 520: break;
1.27 kristaps 521: case (WARN_COMPAT_TROFF):
1.37 kristaps 522: lit = "compat: macro behaves differently in troff and nroff";
1.27 kristaps 523: break;
1.21 kristaps 524: default:
525: abort();
526: /* NOTREACHED */
1.1 kristaps 527: }
528:
1.21 kristaps 529:
1.39 kristaps 530: (void)fprintf(stderr, "%s:%d: warning: %s (column %d)\n",
531: p->name, line, lit, col);
1.21 kristaps 532:
533: if (p->warn & MD_WARN_ERR) {
534: (void)fprintf(stderr, "%s: considering warnings as "
535: "errors\n", __progname);
536: return(0);
1.1 kristaps 537: }
538:
1.21 kristaps 539: return(1);
1.1 kristaps 540: }
541:
542:
543: static void
544: usage(void)
545: {
546: extern char *__progname;
547:
1.21 kristaps 548: (void)fprintf(stderr, "usage: %s [-v] [-Wwarn...] [infile]\n",
1.19 kristaps 549: __progname);
1.1 kristaps 550: }
1.18 kristaps 551:
CVSweb