Annotation of mandoc/mdocml.c, Revision 1.38
1.38 ! kristaps 1: /* $Id: mdocml.c,v 1.37 2009/01/08 14:55:59 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:
195: switch (n->type) {
196: case (MDOC_TEXT):
197: assert(NULL == n->child);
1.25 kristaps 198: p = n->data.text.string;
1.21 kristaps 199: t = "text";
200: break;
201: case (MDOC_BODY):
202: p = mdoc_macronames[n->data.body.tok];
203: t = "block-body";
204: break;
205: case (MDOC_HEAD):
206: p = mdoc_macronames[n->data.head.tok];
207: t = "block-head";
208: break;
1.34 kristaps 209: case (MDOC_TAIL):
210: p = mdoc_macronames[n->data.tail.tok];
211: t = "block-tail";
212: break;
1.21 kristaps 213: case (MDOC_ELEM):
214: p = mdoc_macronames[n->data.elem.tok];
215: t = "element";
1.24 kristaps 216: argv = n->data.elem.argv;
217: argc = n->data.elem.argc;
1.21 kristaps 218: break;
219: case (MDOC_BLOCK):
220: p = mdoc_macronames[n->data.block.tok];
221: t = "block";
1.24 kristaps 222: argv = n->data.block.argv;
223: argc = n->data.block.argc;
1.21 kristaps 224: break;
1.38 ! kristaps 225: case (MDOC_ROOT):
! 226: p = "root";
! 227: t = "root";
! 228: break;
1.22 kristaps 229: default:
230: abort();
231: /* NOTREACHED */
1.21 kristaps 232: }
233:
234: for (i = 0; i < indent; i++)
235: (void)printf(" ");
1.24 kristaps 236: (void)printf("%s (%s)", p, t);
237:
238: for (i = 0; i < (int)argc; i++) {
239: (void)printf(" -%s", mdoc_argnames[argv[i].arg]);
240: for (j = 0; j < (int)argv[i].sz; j++)
241: (void)printf(" \"%s\"", argv[i].value[j]);
242: }
243:
1.26 kristaps 244: for (i = 0; i < (int)sz; i++)
245: (void)printf(" \"%s\"", params[i]);
246:
1.24 kristaps 247: (void)printf("\n");
1.21 kristaps 248:
249: if (n->child)
250: print_node(n->child, indent + 1);
251: if (n->next)
252: print_node(n->next, indent);
253: }
1.1 kristaps 254:
255:
1.21 kristaps 256: static int
257: parse_leave(struct md_parse *p, int code)
258: {
259: const struct mdoc_node *n;
260:
1.36 kristaps 261: if (NULL == p->mdoc)
262: return(code);
263:
264: if ( ! mdoc_endparse(p->mdoc))
265: code = 0;
266: if ((n = mdoc_result(p->mdoc)))
267: print_node(n, 0);
268:
1.38 ! kristaps 269: mdoc_free(p->mdoc);
! 270:
1.21 kristaps 271: return(code);
272: }
273:
274:
275: static int
276: parse_begin(struct md_parse *p)
277: {
278: ssize_t sz, i;
279: size_t pos;
280: char line[256], sv[256];
281: struct mdoc_cb cb;
282:
283: cb.mdoc_err = msg_err;
284: cb.mdoc_warn = msg_warn;
285: cb.mdoc_msg = msg_msg;
286:
287: if (NULL == (p->mdoc = mdoc_alloc(p, &cb)))
288: return(parse_leave(p, 0));
289:
290: p->lnn = 1;
291: p->line = sv;
292:
293: for (pos = 0; ; ) {
294: if (-1 == (sz = read(p->fd, p->buf, p->bufsz))) {
295: warn("%s", p->name);
296: return(parse_leave(p, 0));
297: } else if (0 == sz)
298: break;
299:
300: for (i = 0; i < sz; i++) {
301: if ('\n' != p->buf[i]) {
302: if (pos < sizeof(line)) {
1.23 kristaps 303: sv[(int)pos] = p->buf[(int)i];
304: line[(int)pos++] =
305: p->buf[(int)i];
1.21 kristaps 306: continue;
307: }
308: warnx("%s: line %d too long",
309: p->name, p->lnn);
310: return(parse_leave(p, 0));
311: }
312:
313: line[(int)pos] = sv[(int)pos] = 0;
1.36 kristaps 314: if ( ! mdoc_parseln(p->mdoc, p->lnn, line))
1.21 kristaps 315: return(parse_leave(p, 0));
1.1 kristaps 316:
1.21 kristaps 317: p->lnn++;
318: pos = 0;
1.4 kristaps 319: }
1.21 kristaps 320: }
1.1 kristaps 321:
1.21 kristaps 322: return(parse_leave(p, 1));
1.4 kristaps 323: }
1.1 kristaps 324:
325:
1.4 kristaps 326: static int
1.37 kristaps 327: msg_err(void *arg, int line, int col, enum mdoc_err type)
1.21 kristaps 328: {
1.37 kristaps 329: char *lit;
1.21 kristaps 330: struct md_parse *p;
331: int i;
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):
412: lit = "syntax: invalid value for macro argument";
413: break;
1.24 kristaps 414: case (ERR_SYNTAX_ARGMANY):
415: lit = "syntax: too many values for macro argument";
416: break;
1.32 kristaps 417: case (ERR_SYNTAX_CHILDHEAD):
418: lit = "syntax: expected only block-header section";
419: break;
420: case (ERR_SYNTAX_CHILDBODY):
421: lit = "syntax: expected only a block-body section";
422: break;
423: case (ERR_SYNTAX_EMPTYHEAD):
424: lit = "syntax: block-header section may not be empty";
425: break;
426: case (ERR_SYNTAX_EMPTYBODY):
427: lit = "syntax: block-body section may not be empty";
1.31 kristaps 428: break;
1.21 kristaps 429: default:
430: abort();
431: /* NOTREACHED */
432: }
433:
1.37 kristaps 434: (void)fprintf(stderr, "%s:%d: error: %s", p->name, p->lnn, lit);
1.21 kristaps 435:
436: if (p->dbg < 1) {
1.37 kristaps 437: (void)fprintf(stderr, " (column %d)\n", col);
1.23 kristaps 438: return(0);
1.37 kristaps 439: }
1.21 kristaps 440:
441: (void)fprintf(stderr, "\nFrom: %s\n ", p->line);
442: for (i = 0; i < col; i++)
443: (void)fprintf(stderr, " ");
444: (void)fprintf(stderr, "^\n");
445:
446: return(0);
447: }
448:
449:
450: static void
1.37 kristaps 451: msg_msg(void *arg, int line, int col, const char *msg)
1.4 kristaps 452: {
1.21 kristaps 453: struct md_parse *p;
454: int i;
455:
456: p = (struct md_parse *)arg;
457:
458: if (p->dbg < 2)
459: return;
460:
1.37 kristaps 461: (void)printf("%s:%d: %s", p->name, line, msg);
1.21 kristaps 462:
463: if (p->dbg < 3) {
1.37 kristaps 464: (void)printf(" (column %d)\n", col);
1.21 kristaps 465: return;
466: }
467:
468: (void)printf("\nFrom: %s\n ", p->line);
469: for (i = 0; i < col; i++)
470: (void)printf(" ");
471: (void)printf("^\n");
1.1 kristaps 472: }
473:
474:
475: static int
1.37 kristaps 476: msg_warn(void *arg, int line, int col, enum mdoc_warn type)
1.1 kristaps 477: {
1.37 kristaps 478: char *lit;
1.21 kristaps 479: struct md_parse *p;
480: int i;
481: extern char *__progname;
1.1 kristaps 482:
1.21 kristaps 483: p = (struct md_parse *)arg;
1.1 kristaps 484:
1.21 kristaps 485: if ( ! (p->warn & MD_WARN_ALL))
1.1 kristaps 486: return(1);
1.21 kristaps 487:
1.37 kristaps 488: lit = NULL;
1.21 kristaps 489:
490: switch (type) {
491: case (WARN_SYNTAX_WS_EOLN):
492: lit = "syntax: whitespace at end-of-line";
493: break;
1.25 kristaps 494: case (WARN_SYNTAX_QUOTED):
495: lit = "syntax: quotation mark starting string";
496: break;
1.21 kristaps 497: case (WARN_SYNTAX_MACLIKE):
498: lit = "syntax: macro-like argument";
499: break;
1.24 kristaps 500: case (WARN_SYNTAX_ARGLIKE):
501: lit = "syntax: argument-like value";
502: break;
1.32 kristaps 503: case (WARN_SYNTAX_EMPTYBODY):
504: lit = "syntax: empty block-body section";
505: break;
1.23 kristaps 506: case (WARN_SEC_OO):
507: lit = "section is out of conventional order";
508: break;
1.38 ! kristaps 509: case (WARN_SEC_REP):
! 510: lit = "section repeated";
! 511: break;
1.21 kristaps 512: case (WARN_ARGS_GE1):
1.37 kristaps 513: lit = "macro suggests one or more arguments";
1.21 kristaps 514: break;
1.25 kristaps 515: case (WARN_ARGS_EQ0):
1.37 kristaps 516: lit = "macro suggests zero arguments";
1.25 kristaps 517: break;
518: case (WARN_IGN_AFTER_BLK):
1.37 kristaps 519: lit = "ignore: macro ignored after block macro";
1.25 kristaps 520: break;
1.30 kristaps 521: case (WARN_IGN_OBSOLETE):
1.37 kristaps 522: lit = "ignore: macro is obsolete";
1.30 kristaps 523: break;
1.25 kristaps 524: case (WARN_IGN_BEFORE_BLK):
1.37 kristaps 525: lit = "ignore: macro before block macro ignored";
1.25 kristaps 526: break;
1.27 kristaps 527: case (WARN_COMPAT_TROFF):
1.37 kristaps 528: lit = "compat: macro behaves differently in troff and nroff";
1.27 kristaps 529: break;
1.21 kristaps 530: default:
531: abort();
532: /* NOTREACHED */
1.1 kristaps 533: }
534:
1.21 kristaps 535:
1.37 kristaps 536: (void)fprintf(stderr, "%s:%d: warning: %s", p->name, line, lit);
537:
538: if (p->dbg >= 1) {
1.21 kristaps 539: (void)fprintf(stderr, "\nFrom: %s\n ", p->line);
540: for (i = 0; i < col; i++)
541: (void)fprintf(stderr, " ");
542: (void)fprintf(stderr, "^\n");
1.37 kristaps 543: } else
1.21 kristaps 544: (void)fprintf(stderr, " (column %d)\n", col);
545:
546: if (p->warn & MD_WARN_ERR) {
547: (void)fprintf(stderr, "%s: considering warnings as "
548: "errors\n", __progname);
549: return(0);
1.1 kristaps 550: }
551:
1.21 kristaps 552: return(1);
1.1 kristaps 553: }
554:
555:
556: static void
557: usage(void)
558: {
559: extern char *__progname;
560:
1.21 kristaps 561: (void)fprintf(stderr, "usage: %s [-v] [-Wwarn...] [infile]\n",
1.19 kristaps 562: __progname);
1.1 kristaps 563: }
1.18 kristaps 564:
CVSweb