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