Annotation of mandoc/mdocml.c, Revision 1.41
1.41 ! kristaps 1: /* $Id: mdocml.c,v 1.40 2009/01/12 12:52:21 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]);
1.41 ! kristaps 241: if (j > 0)
! 242: (void)printf(" [");
1.24 kristaps 243: for (j = 0; j < (int)argv[i].sz; j++)
1.41 ! kristaps 244: (void)printf(" [%s]", argv[i].value[j]);
! 245: if (j > 0)
! 246: (void)printf(" ]");
1.24 kristaps 247: }
248:
1.26 kristaps 249: for (i = 0; i < (int)sz; i++)
1.41 ! kristaps 250: (void)printf(" [%s]", params[i]);
1.26 kristaps 251:
1.39 kristaps 252: (void)printf(" %d:%d\n", n->line, n->pos);
1.21 kristaps 253:
254: if (n->child)
255: print_node(n->child, indent + 1);
256: if (n->next)
257: print_node(n->next, indent);
258: }
1.1 kristaps 259:
260:
1.21 kristaps 261: static int
262: parse_leave(struct md_parse *p, int code)
263: {
264: const struct mdoc_node *n;
265:
1.36 kristaps 266: if (NULL == p->mdoc)
267: return(code);
268:
269: if ( ! mdoc_endparse(p->mdoc))
270: code = 0;
271: if ((n = mdoc_result(p->mdoc)))
272: print_node(n, 0);
273:
1.38 kristaps 274: mdoc_free(p->mdoc);
275:
1.21 kristaps 276: return(code);
277: }
278:
279:
280: static int
281: parse_begin(struct md_parse *p)
282: {
283: ssize_t sz, i;
284: size_t pos;
285: char line[256], sv[256];
286: struct mdoc_cb cb;
287:
288: cb.mdoc_err = msg_err;
289: cb.mdoc_warn = msg_warn;
290: cb.mdoc_msg = msg_msg;
291:
292: if (NULL == (p->mdoc = mdoc_alloc(p, &cb)))
293: return(parse_leave(p, 0));
294:
295: p->lnn = 1;
296: p->line = sv;
297:
298: for (pos = 0; ; ) {
299: if (-1 == (sz = read(p->fd, p->buf, p->bufsz))) {
300: warn("%s", p->name);
301: return(parse_leave(p, 0));
302: } else if (0 == sz)
303: break;
304:
305: for (i = 0; i < sz; i++) {
306: if ('\n' != p->buf[i]) {
307: if (pos < sizeof(line)) {
1.23 kristaps 308: sv[(int)pos] = p->buf[(int)i];
309: line[(int)pos++] =
310: p->buf[(int)i];
1.21 kristaps 311: continue;
312: }
313: warnx("%s: line %d too long",
314: p->name, p->lnn);
315: return(parse_leave(p, 0));
316: }
317:
318: line[(int)pos] = sv[(int)pos] = 0;
1.36 kristaps 319: if ( ! mdoc_parseln(p->mdoc, p->lnn, line))
1.21 kristaps 320: return(parse_leave(p, 0));
1.1 kristaps 321:
1.21 kristaps 322: p->lnn++;
323: pos = 0;
1.4 kristaps 324: }
1.21 kristaps 325: }
1.1 kristaps 326:
1.21 kristaps 327: return(parse_leave(p, 1));
1.4 kristaps 328: }
1.1 kristaps 329:
330:
1.4 kristaps 331: static int
1.37 kristaps 332: msg_err(void *arg, int line, int col, enum mdoc_err type)
1.21 kristaps 333: {
1.37 kristaps 334: char *lit;
1.21 kristaps 335: struct md_parse *p;
336:
337: p = (struct md_parse *)arg;
338:
1.37 kristaps 339: lit = NULL;
1.21 kristaps 340:
341: switch (type) {
1.35 kristaps 342: case (ERR_SYNTAX_NOTEXT):
343: lit = "syntax: context-free text disallowed";
344: break;
1.21 kristaps 345: case (ERR_SYNTAX_QUOTE):
1.24 kristaps 346: lit = "syntax: disallowed argument quotation";
347: break;
348: case (ERR_SYNTAX_UNQUOTE):
1.21 kristaps 349: lit = "syntax: unterminated quotation";
350: break;
351: case (ERR_SYNTAX_WS):
352: lit = "syntax: whitespace in argument";
353: break;
1.25 kristaps 354: case (ERR_SYNTAX_ARGFORM):
1.37 kristaps 355: lit = "syntax: macro arguments malformed";
1.23 kristaps 356: break;
1.28 kristaps 357: case (ERR_SYNTAX_NOPUNCT):
1.37 kristaps 358: lit = "syntax: macro doesn't understand punctuation";
1.28 kristaps 359: break;
1.25 kristaps 360: case (ERR_SYNTAX_ARG):
1.37 kristaps 361: lit = "syntax: unknown argument for macro";
1.24 kristaps 362: break;
1.21 kristaps 363: case (ERR_SCOPE_BREAK):
364: /* Which scope is broken? */
1.37 kristaps 365: lit = "scope: macro breaks prior explicit scope";
1.24 kristaps 366: break;
367: case (ERR_SCOPE_NOCTX):
1.37 kristaps 368: lit = "scope: closure macro has no context";
1.21 kristaps 369: break;
1.25 kristaps 370: case (ERR_SCOPE_NONEST):
1.37 kristaps 371: lit = "scope: macro may not be nested in the current context";
1.25 kristaps 372: break;
1.21 kristaps 373: case (ERR_MACRO_NOTSUP):
1.35 kristaps 374: lit = "macro not supported";
1.21 kristaps 375: break;
376: case (ERR_MACRO_NOTCALL):
1.37 kristaps 377: lit = "macro not callable";
1.21 kristaps 378: break;
1.23 kristaps 379: case (ERR_SEC_PROLOGUE):
1.37 kristaps 380: lit = "macro cannot be called in the prologue";
1.23 kristaps 381: break;
382: case (ERR_SEC_NPROLOGUE):
1.37 kristaps 383: lit = "macro called outside of prologue";
1.23 kristaps 384: break;
1.28 kristaps 385: case (ERR_ARGS_EQ0):
1.37 kristaps 386: lit = "macro expects zero arguments";
1.28 kristaps 387: break;
1.29 kristaps 388: case (ERR_ARGS_EQ1):
1.37 kristaps 389: lit = "macro expects one argument";
1.29 kristaps 390: break;
1.21 kristaps 391: case (ERR_ARGS_GE1):
1.37 kristaps 392: lit = "macro expects one or more arguments";
1.21 kristaps 393: break;
1.28 kristaps 394: case (ERR_ARGS_LE2):
1.37 kristaps 395: lit = "macro expects two or fewer arguments";
1.28 kristaps 396: break;
1.36 kristaps 397: case (ERR_ARGS_LE8):
1.37 kristaps 398: lit = "macro expects eight or fewer arguments";
1.36 kristaps 399: break;
1.23 kristaps 400: case (ERR_ARGS_MANY):
1.37 kristaps 401: lit = "macro has too many arguments";
1.23 kristaps 402: break;
403: case (ERR_SEC_PROLOGUE_OO):
1.37 kristaps 404: lit = "prologue macro is out-of-order";
1.23 kristaps 405: break;
406: case (ERR_SEC_PROLOGUE_REP):
1.37 kristaps 407: lit = "prologue macro repeated";
1.23 kristaps 408: break;
409: case (ERR_SEC_NAME):
410: lit = "`NAME' section must be first";
411: break;
1.24 kristaps 412: case (ERR_SYNTAX_ARGVAL):
413: lit = "syntax: expected value for macro argument";
414: break;
1.25 kristaps 415: case (ERR_SYNTAX_ARGBAD):
1.39 kristaps 416: lit = "syntax: invalid value(s) for macro argument";
417: break;
418: case (ERR_SYNTAX_ARGMISS):
419: lit = "syntax: missing required argument(s) for macro";
1.25 kristaps 420: break;
1.24 kristaps 421: case (ERR_SYNTAX_ARGMANY):
422: lit = "syntax: too many values for macro argument";
423: break;
1.39 kristaps 424: case (ERR_SYNTAX_CHILDBAD):
425: lit = "syntax: invalid child for parent macro";
426: break;
1.40 kristaps 427: case (ERR_SYNTAX_PARENTBAD):
428: lit = "syntax: invalid parent for macro";
429: break;
1.32 kristaps 430: case (ERR_SYNTAX_CHILDHEAD):
431: lit = "syntax: expected only block-header section";
432: break;
433: case (ERR_SYNTAX_CHILDBODY):
434: lit = "syntax: expected only a block-body section";
435: break;
436: case (ERR_SYNTAX_EMPTYHEAD):
437: lit = "syntax: block-header section may not be empty";
438: break;
439: case (ERR_SYNTAX_EMPTYBODY):
440: lit = "syntax: block-body section may not be empty";
1.31 kristaps 441: break;
1.21 kristaps 442: default:
443: abort();
444: /* NOTREACHED */
445: }
446:
1.39 kristaps 447: (void)fprintf(stderr, "%s:%d: error: %s (column %d)\n",
448: p->name, line, lit, col);
1.21 kristaps 449: return(0);
450: }
451:
452:
453: static void
1.37 kristaps 454: msg_msg(void *arg, int line, int col, const char *msg)
1.4 kristaps 455: {
1.21 kristaps 456: struct md_parse *p;
457:
458: p = (struct md_parse *)arg;
459:
460: if (p->dbg < 2)
461: return;
462:
1.39 kristaps 463: (void)printf("%s:%d: %s (column %d)\n",
464: p->name, line, msg, col);
1.1 kristaps 465: }
466:
467:
468: static int
1.37 kristaps 469: msg_warn(void *arg, int line, int col, enum mdoc_warn type)
1.1 kristaps 470: {
1.37 kristaps 471: char *lit;
1.21 kristaps 472: struct md_parse *p;
473: extern char *__progname;
1.1 kristaps 474:
1.21 kristaps 475: p = (struct md_parse *)arg;
1.1 kristaps 476:
1.21 kristaps 477: if ( ! (p->warn & MD_WARN_ALL))
1.1 kristaps 478: return(1);
1.21 kristaps 479:
1.37 kristaps 480: lit = NULL;
1.21 kristaps 481:
482: switch (type) {
483: case (WARN_SYNTAX_WS_EOLN):
484: lit = "syntax: whitespace at end-of-line";
485: break;
1.25 kristaps 486: case (WARN_SYNTAX_QUOTED):
487: lit = "syntax: quotation mark starting string";
488: break;
1.21 kristaps 489: case (WARN_SYNTAX_MACLIKE):
490: lit = "syntax: macro-like argument";
491: break;
1.24 kristaps 492: case (WARN_SYNTAX_ARGLIKE):
493: lit = "syntax: argument-like value";
494: break;
1.32 kristaps 495: case (WARN_SYNTAX_EMPTYBODY):
1.40 kristaps 496: lit = "syntax: macro suggests non-empty block-body section";
497: break;
498: case (WARN_SYNTAX_EMPTYHEAD):
499: lit = "syntax: macro suggests non-empty block-head section";
500: break;
501: case (WARN_SYNTAX_NOBODY):
502: lit = "syntax: macro suggests empty block-body section";
1.32 kristaps 503: break;
1.23 kristaps 504: case (WARN_SEC_OO):
505: lit = "section is out of conventional order";
506: break;
1.38 kristaps 507: case (WARN_SEC_REP):
508: lit = "section repeated";
509: break;
1.21 kristaps 510: case (WARN_ARGS_GE1):
1.37 kristaps 511: lit = "macro suggests one or more arguments";
1.21 kristaps 512: break;
1.25 kristaps 513: case (WARN_ARGS_EQ0):
1.37 kristaps 514: lit = "macro suggests zero arguments";
1.25 kristaps 515: break;
516: case (WARN_IGN_AFTER_BLK):
1.37 kristaps 517: lit = "ignore: macro ignored after block macro";
1.25 kristaps 518: break;
1.30 kristaps 519: case (WARN_IGN_OBSOLETE):
1.37 kristaps 520: lit = "ignore: macro is obsolete";
1.30 kristaps 521: break;
1.25 kristaps 522: case (WARN_IGN_BEFORE_BLK):
1.37 kristaps 523: lit = "ignore: macro before block macro ignored";
1.25 kristaps 524: break;
1.27 kristaps 525: case (WARN_COMPAT_TROFF):
1.37 kristaps 526: lit = "compat: macro behaves differently in troff and nroff";
1.27 kristaps 527: break;
1.21 kristaps 528: default:
529: abort();
530: /* NOTREACHED */
1.1 kristaps 531: }
532:
1.21 kristaps 533:
1.39 kristaps 534: (void)fprintf(stderr, "%s:%d: warning: %s (column %d)\n",
535: p->name, line, lit, col);
1.21 kristaps 536:
537: if (p->warn & MD_WARN_ERR) {
538: (void)fprintf(stderr, "%s: considering warnings as "
539: "errors\n", __progname);
540: return(0);
1.1 kristaps 541: }
542:
1.21 kristaps 543: return(1);
1.1 kristaps 544: }
545:
546:
547: static void
548: usage(void)
549: {
550: extern char *__progname;
551:
1.21 kristaps 552: (void)fprintf(stderr, "usage: %s [-v] [-Wwarn...] [infile]\n",
1.19 kristaps 553: __progname);
1.1 kristaps 554: }
1.18 kristaps 555:
CVSweb