]> git.cameronkatri.com Git - mandoc.git/blob - mdocml.c
1f29809d381582027acbfdef583c51427568d8fa
[mandoc.git] / mdocml.c
1 /* $Id: mdocml.c,v 1.39 2009/01/12 10:31:53 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, 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 break;
209 case (MDOC_TAIL):
210 p = mdoc_macronames[n->data.tail.tok];
211 t = "block-tail";
212 break;
213 case (MDOC_ELEM):
214 p = mdoc_macronames[n->data.elem.tok];
215 t = "element";
216 argv = n->data.elem.argv;
217 argc = n->data.elem.argc;
218 break;
219 case (MDOC_BLOCK):
220 p = mdoc_macronames[n->data.block.tok];
221 t = "block";
222 argv = n->data.block.argv;
223 argc = n->data.block.argc;
224 break;
225 case (MDOC_ROOT):
226 p = "root";
227 t = "root";
228 break;
229 default:
230 abort();
231 /* NOTREACHED */
232 }
233
234 for (i = 0; i < indent; i++)
235 (void)printf(" ");
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
244 for (i = 0; i < (int)sz; i++)
245 (void)printf(" \"%s\"", params[i]);
246
247 (void)printf(" %d:%d\n", n->line, n->pos);
248
249 if (n->child)
250 print_node(n->child, indent + 1);
251 if (n->next)
252 print_node(n->next, indent);
253 }
254
255
256 static int
257 parse_leave(struct md_parse *p, int code)
258 {
259 const struct mdoc_node *n;
260
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
269 mdoc_free(p->mdoc);
270
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)) {
303 sv[(int)pos] = p->buf[(int)i];
304 line[(int)pos++] =
305 p->buf[(int)i];
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;
314 if ( ! mdoc_parseln(p->mdoc, p->lnn, line))
315 return(parse_leave(p, 0));
316
317 p->lnn++;
318 pos = 0;
319 }
320 }
321
322 return(parse_leave(p, 1));
323 }
324
325
326 static int
327 msg_err(void *arg, int line, int col, enum mdoc_err type)
328 {
329 char *lit;
330 struct md_parse *p;
331
332 p = (struct md_parse *)arg;
333
334 lit = NULL;
335
336 switch (type) {
337 case (ERR_SYNTAX_NOTEXT):
338 lit = "syntax: context-free text disallowed";
339 break;
340 case (ERR_SYNTAX_QUOTE):
341 lit = "syntax: disallowed argument quotation";
342 break;
343 case (ERR_SYNTAX_UNQUOTE):
344 lit = "syntax: unterminated quotation";
345 break;
346 case (ERR_SYNTAX_WS):
347 lit = "syntax: whitespace in argument";
348 break;
349 case (ERR_SYNTAX_ARGFORM):
350 lit = "syntax: macro arguments malformed";
351 break;
352 case (ERR_SYNTAX_NOPUNCT):
353 lit = "syntax: macro doesn't understand punctuation";
354 break;
355 case (ERR_SYNTAX_ARG):
356 lit = "syntax: unknown argument for macro";
357 break;
358 case (ERR_SCOPE_BREAK):
359 /* Which scope is broken? */
360 lit = "scope: macro breaks prior explicit scope";
361 break;
362 case (ERR_SCOPE_NOCTX):
363 lit = "scope: closure macro has no context";
364 break;
365 case (ERR_SCOPE_NONEST):
366 lit = "scope: macro may not be nested in the current context";
367 break;
368 case (ERR_MACRO_NOTSUP):
369 lit = "macro not supported";
370 break;
371 case (ERR_MACRO_NOTCALL):
372 lit = "macro not callable";
373 break;
374 case (ERR_SEC_PROLOGUE):
375 lit = "macro cannot be called in the prologue";
376 break;
377 case (ERR_SEC_NPROLOGUE):
378 lit = "macro called outside of prologue";
379 break;
380 case (ERR_ARGS_EQ0):
381 lit = "macro expects zero arguments";
382 break;
383 case (ERR_ARGS_EQ1):
384 lit = "macro expects one argument";
385 break;
386 case (ERR_ARGS_GE1):
387 lit = "macro expects one or more arguments";
388 break;
389 case (ERR_ARGS_LE2):
390 lit = "macro expects two or fewer arguments";
391 break;
392 case (ERR_ARGS_LE8):
393 lit = "macro expects eight or fewer arguments";
394 break;
395 case (ERR_ARGS_MANY):
396 lit = "macro has too many arguments";
397 break;
398 case (ERR_SEC_PROLOGUE_OO):
399 lit = "prologue macro is out-of-order";
400 break;
401 case (ERR_SEC_PROLOGUE_REP):
402 lit = "prologue macro repeated";
403 break;
404 case (ERR_SEC_NAME):
405 lit = "`NAME' section must be first";
406 break;
407 case (ERR_SYNTAX_ARGVAL):
408 lit = "syntax: expected value for macro argument";
409 break;
410 case (ERR_SYNTAX_ARGBAD):
411 lit = "syntax: invalid value(s) for macro argument";
412 break;
413 case (ERR_SYNTAX_ARGMISS):
414 lit = "syntax: missing required argument(s) for macro";
415 break;
416 case (ERR_SYNTAX_ARGMANY):
417 lit = "syntax: too many values for macro argument";
418 break;
419 case (ERR_SYNTAX_CHILDBAD):
420 lit = "syntax: invalid child for parent macro";
421 break;
422 case (ERR_SYNTAX_CHILDHEAD):
423 lit = "syntax: expected only block-header section";
424 break;
425 case (ERR_SYNTAX_CHILDBODY):
426 lit = "syntax: expected only a block-body section";
427 break;
428 case (ERR_SYNTAX_EMPTYHEAD):
429 lit = "syntax: block-header section may not be empty";
430 break;
431 case (ERR_SYNTAX_EMPTYBODY):
432 lit = "syntax: block-body section may not be empty";
433 break;
434 default:
435 abort();
436 /* NOTREACHED */
437 }
438
439 (void)fprintf(stderr, "%s:%d: error: %s (column %d)\n",
440 p->name, line, lit, col);
441 return(0);
442 }
443
444
445 static void
446 msg_msg(void *arg, int line, int col, const char *msg)
447 {
448 struct md_parse *p;
449
450 p = (struct md_parse *)arg;
451
452 if (p->dbg < 2)
453 return;
454
455 (void)printf("%s:%d: %s (column %d)\n",
456 p->name, line, msg, col);
457 }
458
459
460 static int
461 msg_warn(void *arg, int line, int col, enum mdoc_warn type)
462 {
463 char *lit;
464 struct md_parse *p;
465 extern char *__progname;
466
467 p = (struct md_parse *)arg;
468
469 if ( ! (p->warn & MD_WARN_ALL))
470 return(1);
471
472 lit = NULL;
473
474 switch (type) {
475 case (WARN_SYNTAX_WS_EOLN):
476 lit = "syntax: whitespace at end-of-line";
477 break;
478 case (WARN_SYNTAX_QUOTED):
479 lit = "syntax: quotation mark starting string";
480 break;
481 case (WARN_SYNTAX_MACLIKE):
482 lit = "syntax: macro-like argument";
483 break;
484 case (WARN_SYNTAX_ARGLIKE):
485 lit = "syntax: argument-like value";
486 break;
487 case (WARN_SYNTAX_EMPTYBODY):
488 lit = "syntax: empty block-body section";
489 break;
490 case (WARN_SEC_OO):
491 lit = "section is out of conventional order";
492 break;
493 case (WARN_SEC_REP):
494 lit = "section repeated";
495 break;
496 case (WARN_ARGS_GE1):
497 lit = "macro suggests one or more arguments";
498 break;
499 case (WARN_ARGS_EQ0):
500 lit = "macro suggests zero arguments";
501 break;
502 case (WARN_IGN_AFTER_BLK):
503 lit = "ignore: macro ignored after block macro";
504 break;
505 case (WARN_IGN_OBSOLETE):
506 lit = "ignore: macro is obsolete";
507 break;
508 case (WARN_IGN_BEFORE_BLK):
509 lit = "ignore: macro before block macro ignored";
510 break;
511 case (WARN_COMPAT_TROFF):
512 lit = "compat: macro behaves differently in troff and nroff";
513 break;
514 default:
515 abort();
516 /* NOTREACHED */
517 }
518
519
520 (void)fprintf(stderr, "%s:%d: warning: %s (column %d)\n",
521 p->name, line, lit, col);
522
523 if (p->warn & MD_WARN_ERR) {
524 (void)fprintf(stderr, "%s: considering warnings as "
525 "errors\n", __progname);
526 return(0);
527 }
528
529 return(1);
530 }
531
532
533 static void
534 usage(void)
535 {
536 extern char *__progname;
537
538 (void)fprintf(stderr, "usage: %s [-v] [-Wwarn...] [infile]\n",
539 __progname);
540 }
541