]> git.cameronkatri.com Git - mandoc.git/blob - mdocml.c
*** empty log message ***
[mandoc.git] / mdocml.c
1 /* $Id: mdocml.c,v 1.30 2008/12/30 19:06:03 kristaps Exp $ */
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 */
19 #include <sys/stat.h>
20 #include <sys/param.h>
21
22 #include <assert.h>
23 #include <fcntl.h>
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
31 #include "mdoc.h"
32
33 #define MD_LINE_SZ (256)
34
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 };
48
49 static void usage(void);
50
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);
60 static void msg_msg(void *, int, const char *);
61
62 #ifdef __linux__
63 extern int getsubopt(char **, char *const *, char **);
64 #endif
65
66 int
67 main(int argc, char *argv[])
68 {
69 int c;
70 struct md_parse parser;
71 char *opts, *v;
72 #define ALL 0
73 #define ERROR 1
74 char *toks[] = { "all", "error", NULL };
75
76 extern char *optarg;
77 extern int optind;
78
79 (void)memset(&parser, 0, sizeof(struct md_parse));
80
81 while (-1 != (c = getopt(argc, argv, "vW:")))
82 switch (c) {
83 case ('v'):
84 parser.dbg++;
85 break;
86 case ('W'):
87 opts = optarg;
88 while (*opts)
89 switch (getsubopt(&opts, toks, &v)) {
90 case (ALL):
91 parser.warn |= MD_WARN_ALL;
92 break;
93 case (ERROR):
94 parser.warn |= MD_WARN_ERR;
95 break;
96 default:
97 usage();
98 return(1);
99 }
100 break;
101 default:
102 usage();
103 return(1);
104 }
105
106 argv += optind;
107 argc -= optind;
108
109 parser.name = "-";
110 if (1 == argc)
111 parser.name = *argv++;
112
113 if ( ! io_begin(&parser))
114 return(EXIT_FAILURE);
115
116 return(EXIT_SUCCESS);
117 }
118
119
120 static int
121 io_leave(struct md_parse *p, int code)
122 {
123
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;
130 }
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 }
145
146 return(io_leave(p, buf_begin(p)));
147 }
148
149
150 static int
151 buf_leave(struct md_parse *p, int code)
152 {
153
154 if (p->buf)
155 free(p->buf);
156 return(code);
157 }
158
159
160 static int
161 buf_begin(struct md_parse *p)
162 {
163 struct stat st;
164
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 {
184 const char *p, *t;
185 int i, j;
186 size_t argc, sz;
187 char **params;
188 struct mdoc_arg *argv;
189
190 argv = NULL;
191 argc = 0;
192 params = NULL;
193 sz = 0;
194
195 switch (n->type) {
196 case (MDOC_TEXT):
197 assert(NULL == n->child);
198 p = n->data.text.string;
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 params = n->data.head.args;
209 sz = n->data.head.sz;
210 break;
211 case (MDOC_ELEM):
212 assert(NULL == n->child);
213 p = mdoc_macronames[n->data.elem.tok];
214 t = "element";
215 argv = n->data.elem.argv;
216 argc = n->data.elem.argc;
217 params = n->data.elem.args;
218 sz = n->data.elem.sz;
219 break;
220 case (MDOC_BLOCK):
221 p = mdoc_macronames[n->data.block.tok];
222 t = "block";
223 argv = n->data.block.argv;
224 argc = n->data.block.argc;
225 break;
226 default:
227 abort();
228 /* NOTREACHED */
229 }
230
231 for (i = 0; i < indent; i++)
232 (void)printf(" ");
233 (void)printf("%s (%s)", p, t);
234
235 for (i = 0; i < (int)argc; i++) {
236 (void)printf(" -%s", mdoc_argnames[argv[i].arg]);
237 for (j = 0; j < (int)argv[i].sz; j++)
238 (void)printf(" \"%s\"", argv[i].value[j]);
239 }
240
241 for (i = 0; i < (int)sz; i++)
242 (void)printf(" \"%s\"", params[i]);
243
244 (void)printf("\n");
245
246 if (n->child)
247 print_node(n->child, indent + 1);
248 if (n->next)
249 print_node(n->next, indent);
250 }
251
252
253 static int
254 parse_leave(struct md_parse *p, int code)
255 {
256 const struct mdoc_node *n;
257
258 if (p->mdoc) {
259 if ((n = mdoc_result(p->mdoc)))
260 print_node(n, 0);
261 mdoc_free(p->mdoc);
262 }
263 return(code);
264 }
265
266
267 static int
268 parse_begin(struct md_parse *p)
269 {
270 ssize_t sz, i;
271 size_t pos;
272 char line[256], sv[256];
273 struct mdoc_cb cb;
274
275 cb.mdoc_err = msg_err;
276 cb.mdoc_warn = msg_warn;
277 cb.mdoc_msg = msg_msg;
278
279 if (NULL == (p->mdoc = mdoc_alloc(p, &cb)))
280 return(parse_leave(p, 0));
281
282 p->lnn = 1;
283 p->line = sv;
284
285 for (pos = 0; ; ) {
286 if (-1 == (sz = read(p->fd, p->buf, p->bufsz))) {
287 warn("%s", p->name);
288 return(parse_leave(p, 0));
289 } else if (0 == sz)
290 break;
291
292 for (i = 0; i < sz; i++) {
293 if ('\n' != p->buf[i]) {
294 if (pos < sizeof(line)) {
295 sv[(int)pos] = p->buf[(int)i];
296 line[(int)pos++] =
297 p->buf[(int)i];
298 continue;
299 }
300 warnx("%s: line %d too long",
301 p->name, p->lnn);
302 return(parse_leave(p, 0));
303 }
304
305 line[(int)pos] = sv[(int)pos] = 0;
306 if ( ! mdoc_parseln(p->mdoc, line))
307 return(parse_leave(p, 0));
308
309 p->lnn++;
310 pos = 0;
311 }
312 }
313
314 return(parse_leave(p, 1));
315 }
316
317
318 static int
319 msg_err(void *arg, int tok, int col, enum mdoc_err type)
320 {
321 char *fmt, *lit;
322 struct md_parse *p;
323 int i;
324
325 p = (struct md_parse *)arg;
326
327 fmt = lit = NULL;
328
329 switch (type) {
330 case (ERR_SYNTAX_QUOTE):
331 lit = "syntax: disallowed argument quotation";
332 break;
333 case (ERR_SYNTAX_UNQUOTE):
334 lit = "syntax: unterminated quotation";
335 break;
336 case (ERR_SYNTAX_WS):
337 lit = "syntax: whitespace in argument";
338 break;
339 case (ERR_SYNTAX_ARGFORM):
340 fmt = "syntax: macro `%s' arguments malformed";
341 break;
342 case (ERR_SYNTAX_NOPUNCT):
343 fmt = "syntax: macro `%s' doesn't understand punctuation";
344 break;
345 case (ERR_SYNTAX_ARG):
346 fmt = "syntax: unknown argument for macro `%s'";
347 break;
348 case (ERR_SCOPE_BREAK):
349 /* Which scope is broken? */
350 fmt = "scope: macro `%s' breaks prior explicit scope";
351 break;
352 case (ERR_SCOPE_NOCTX):
353 fmt = "scope: closure macro `%s' has no context";
354 break;
355 case (ERR_SCOPE_NONEST):
356 fmt = "scope: macro `%s' may not be nested in the current context";
357 break;
358 case (ERR_MACRO_NOTSUP):
359 fmt = "macro `%s' not supported";
360 break;
361 case (ERR_MACRO_NOTCALL):
362 fmt = "macro `%s' not callable";
363 break;
364 case (ERR_SEC_PROLOGUE):
365 fmt = "macro `%s' cannot be called in the prologue";
366 break;
367 case (ERR_SEC_NPROLOGUE):
368 fmt = "macro `%s' called outside of prologue";
369 break;
370 case (ERR_ARGS_EQ0):
371 fmt = "macro `%s' expects zero arguments";
372 break;
373 case (ERR_ARGS_EQ1):
374 fmt = "macro `%s' expects one argument";
375 break;
376 case (ERR_ARGS_GE1):
377 fmt = "macro `%s' expects one or more arguments";
378 break;
379 case (ERR_ARGS_LE2):
380 fmt = "macro `%s' expects two or fewer arguments";
381 break;
382 case (ERR_ARGS_MANY):
383 fmt = "macro `%s' has too many arguments";
384 break;
385 case (ERR_SEC_PROLOGUE_OO):
386 fmt = "prologue macro `%s' is out-of-order";
387 break;
388 case (ERR_SEC_PROLOGUE_REP):
389 fmt = "prologue macro `%s' repeated";
390 break;
391 case (ERR_SEC_NAME):
392 lit = "`NAME' section must be first";
393 break;
394 case (ERR_SYNTAX_ARGVAL):
395 lit = "syntax: expected value for macro argument";
396 break;
397 case (ERR_SYNTAX_ARGBAD):
398 lit = "syntax: invalid value for macro argument";
399 break;
400 case (ERR_SYNTAX_ARGMANY):
401 lit = "syntax: too many values for macro argument";
402 break;
403 default:
404 abort();
405 /* NOTREACHED */
406 }
407
408 if (fmt) {
409 (void)fprintf(stderr, "%s:%d: error: ",
410 p->name, p->lnn);
411 (void)fprintf(stderr, fmt, mdoc_macronames[tok]);
412 } else
413 (void)fprintf(stderr, "%s:%d: error: %s",
414 p->name, p->lnn, lit);
415
416 if (p->dbg < 1) {
417 if (-1 != col)
418 (void)fprintf(stderr, " (column %d)\n", col);
419 return(0);
420 } else if (-1 == col) {
421 (void)fprintf(stderr, "\nFrom: %s", p->line);
422 return(0);
423 }
424
425 (void)fprintf(stderr, "\nFrom: %s\n ", p->line);
426 for (i = 0; i < col; i++)
427 (void)fprintf(stderr, " ");
428 (void)fprintf(stderr, "^\n");
429
430 return(0);
431 }
432
433
434 static void
435 msg_msg(void *arg, int col, const char *msg)
436 {
437 struct md_parse *p;
438 int i;
439
440 p = (struct md_parse *)arg;
441
442 if (p->dbg < 2)
443 return;
444
445 (void)printf("%s:%d: %s", p->name, p->lnn, msg);
446
447 if (p->dbg < 3) {
448 if (-1 != col)
449 (void)printf(" (column %d)\n", col);
450 return;
451 } else if (-1 == col) {
452 (void)printf("\nFrom %s\n", p->line);
453 return;
454 }
455
456 (void)printf("\nFrom: %s\n ", p->line);
457 for (i = 0; i < col; i++)
458 (void)printf(" ");
459 (void)printf("^\n");
460 }
461
462
463 static int
464 msg_warn(void *arg, int tok, int col, enum mdoc_warn type)
465 {
466 char *fmt, *lit;
467 struct md_parse *p;
468 int i;
469 extern char *__progname;
470
471 p = (struct md_parse *)arg;
472
473 if ( ! (p->warn & MD_WARN_ALL))
474 return(1);
475
476 fmt = lit = NULL;
477
478 switch (type) {
479 case (WARN_SYNTAX_WS_EOLN):
480 lit = "syntax: whitespace at end-of-line";
481 break;
482 case (WARN_SYNTAX_QUOTED):
483 lit = "syntax: quotation mark starting string";
484 break;
485 case (WARN_SYNTAX_MACLIKE):
486 lit = "syntax: macro-like argument";
487 break;
488 case (WARN_SYNTAX_ARGLIKE):
489 lit = "syntax: argument-like value";
490 break;
491 case (WARN_SEC_OO):
492 lit = "section is out of conventional order";
493 break;
494 case (WARN_ARGS_GE1):
495 fmt = "macro `%s' suggests one or more arguments";
496 break;
497 case (WARN_ARGS_EQ0):
498 fmt = "macro `%s' suggests zero arguments";
499 break;
500 case (WARN_IGN_AFTER_BLK):
501 fmt = "ignore: macro `%s' ignored after block macro";
502 break;
503 case (WARN_IGN_OBSOLETE):
504 fmt = "ignore: macro `%s' is obsolete";
505 break;
506 case (WARN_IGN_BEFORE_BLK):
507 fmt = "ignore: macro before block macro `%s' ignored";
508 break;
509 case (WARN_COMPAT_TROFF):
510 fmt = "compat: macro `%s' behaves differently in troff and nroff";
511 break;
512 default:
513 abort();
514 /* NOTREACHED */
515 }
516
517 if (fmt) {
518 (void)fprintf(stderr, "%s:%d: warning: ",
519 p->name, p->lnn);
520 (void)fprintf(stderr, fmt, mdoc_macronames[tok]);
521 } else
522 (void)fprintf(stderr, "%s:%d: warning: %s",
523 p->name, p->lnn, lit);
524
525 if (p->dbg >= 1) {
526 (void)fprintf(stderr, "\nFrom: %s\n ", p->line);
527 for (i = 0; i < col; i++)
528 (void)fprintf(stderr, " ");
529 (void)fprintf(stderr, "^\n");
530 } else
531 (void)fprintf(stderr, " (column %d)\n", col);
532
533 if (p->warn & MD_WARN_ERR) {
534 (void)fprintf(stderr, "%s: considering warnings as "
535 "errors\n", __progname);
536 return(0);
537 }
538
539 return(1);
540 }
541
542
543 static void
544 usage(void)
545 {
546 extern char *__progname;
547
548 (void)fprintf(stderr, "usage: %s [-v] [-Wwarn...] [infile]\n",
549 __progname);
550 }
551