]> git.cameronkatri.com Git - mandoc.git/blob - main.c
Patches and results of LLVM static analysis (thanks uqs@sporlein.net).
[mandoc.git] / main.c
1 /* $Id: main.c,v 1.39 2009/07/24 14:00:59 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 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 above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17 #include <sys/stat.h>
18
19 #include <assert.h>
20 #include <err.h>
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "mdoc.h"
28 #include "man.h"
29
30 /* Account for FreeBSD and Linux in our declarations. */
31
32 #ifdef __linux__
33 extern int getsubopt(char **, char * const *, char **);
34 # ifndef __dead
35 # define __dead __attribute__((__noreturn__))
36 # endif
37 #elif defined(__dead2)
38 # ifndef __dead
39 # define __dead __dead2
40 # endif
41 #endif
42
43 typedef int (*out_mdoc)(void *, const struct mdoc *);
44 typedef int (*out_man)(void *, const struct man *);
45 typedef void (*out_free)(void *);
46
47 struct buf {
48 char *buf;
49 size_t sz;
50 };
51
52 enum intt {
53 INTT_AUTO,
54 INTT_MDOC,
55 INTT_MAN
56 };
57
58 enum outt {
59 OUTT_ASCII = 0,
60 OUTT_TREE,
61 OUTT_LINT
62 };
63
64 struct curparse {
65 const char *file; /* Current parse. */
66 int fd; /* Current parse. */
67 int wflags;
68 #define WARN_WALL (1 << 0) /* All-warnings mask. */
69 #define WARN_WERR (1 << 2) /* Warnings->errors. */
70 int fflags;
71 #define IGN_SCOPE (1 << 0) /* Ignore scope errors. */
72 #define NO_IGN_ESCAPE (1 << 1) /* Don't ignore bad escapes. */
73 #define NO_IGN_MACRO (1 << 2) /* Don't ignore bad macros. */
74 #define NO_IGN_CHARS (1 << 3) /* Don't ignore bad chars. */
75 #define IGN_ERRORS (1 << 4) /* Ignore failed parse. */
76 enum intt inttype; /* Input parsers... */
77 struct man *man;
78 struct man *lastman;
79 struct mdoc *mdoc;
80 struct mdoc *lastmdoc;
81 enum outt outtype; /* Output devices... */
82 out_mdoc outmdoc;
83 out_man outman;
84 out_free outfree;
85 void *outdata;
86 };
87
88 extern void *ascii_alloc(void);
89 extern int tree_mdoc(void *, const struct mdoc *);
90 extern int tree_man(void *, const struct man *);
91 extern int terminal_mdoc(void *, const struct mdoc *);
92 extern int terminal_man(void *, const struct man *);
93 extern void terminal_free(void *);
94
95 static int foptions(int *, char *);
96 static int toptions(enum outt *, char *);
97 static int moptions(enum intt *, char *);
98 static int woptions(int *, char *);
99 static int merr(void *, int, int, const char *);
100 static int mwarn(void *, int, int, const char *);
101 static int ffile(struct buf *, struct buf *,
102 const char *, struct curparse *);
103 static int fdesc(struct buf *, struct buf *,
104 struct curparse *);
105 static int pset(const char *, int, struct curparse *,
106 struct man **, struct mdoc **);
107 static struct man *man_init(struct curparse *);
108 static struct mdoc *mdoc_init(struct curparse *);
109 __dead static void version(void);
110 __dead static void usage(void);
111
112 extern char *__progname;
113
114
115 int
116 main(int argc, char *argv[])
117 {
118 int c, rc;
119 struct buf ln, blk;
120 struct curparse curp;
121
122 bzero(&curp, sizeof(struct curparse));
123
124 curp.inttype = INTT_AUTO;
125 curp.outtype = OUTT_ASCII;
126
127 /* LINTED */
128 while (-1 != (c = getopt(argc, argv, "f:m:VW:T:")))
129 switch (c) {
130 case ('f'):
131 if ( ! foptions(&curp.fflags, optarg))
132 return(EXIT_FAILURE);
133 break;
134 case ('m'):
135 if ( ! moptions(&curp.inttype, optarg))
136 return(EXIT_FAILURE);
137 break;
138 case ('T'):
139 if ( ! toptions(&curp.outtype, optarg))
140 return(EXIT_FAILURE);
141 break;
142 case ('W'):
143 if ( ! woptions(&curp.wflags, optarg))
144 return(EXIT_FAILURE);
145 break;
146 case ('V'):
147 version();
148 /* NOTREACHED */
149 default:
150 usage();
151 /* NOTREACHED */
152 }
153
154 argc -= optind;
155 argv += optind;
156
157 bzero(&ln, sizeof(struct buf));
158 bzero(&blk, sizeof(struct buf));
159
160 rc = 1;
161
162 if (NULL == *argv) {
163 curp.file = "<stdin>";
164 curp.fd = STDIN_FILENO;
165
166 c = fdesc(&blk, &ln, &curp);
167 if ( ! (IGN_ERRORS & curp.fflags))
168 rc = 1 == c ? 1 : 0;
169 else
170 rc = -1 == c ? 0 : 1;
171 }
172
173 while (rc && *argv) {
174 c = ffile(&blk, &ln, *argv, &curp);
175 if ( ! (IGN_ERRORS & curp.fflags))
176 rc = 1 == c ? 1 : 0;
177 else
178 rc = -1 == c ? 0 : 1;
179
180 argv++;
181 if (*argv && rc) {
182 if (curp.lastman)
183 if ( ! man_reset(curp.lastman))
184 rc = 0;
185 if (curp.lastmdoc)
186 if ( ! mdoc_reset(curp.lastmdoc))
187 rc = 0;
188 curp.lastman = NULL;
189 curp.lastmdoc = NULL;
190 }
191 }
192
193 if (blk.buf)
194 free(blk.buf);
195 if (ln.buf)
196 free(ln.buf);
197 if (curp.outfree)
198 (*curp.outfree)(curp.outdata);
199 if (curp.mdoc)
200 mdoc_free(curp.mdoc);
201 if (curp.man)
202 man_free(curp.man);
203
204 return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
205 }
206
207
208 __dead static void
209 version(void)
210 {
211
212 (void)printf("%s %s\n", __progname, VERSION);
213 exit(EXIT_SUCCESS);
214 }
215
216
217 __dead static void
218 usage(void)
219 {
220
221 (void)fprintf(stderr, "usage: %s [-V] [-foption...] "
222 "[-mformat] [-Toutput] [-Werr...]\n",
223 __progname);
224 exit(EXIT_FAILURE);
225 }
226
227
228 static struct man *
229 man_init(struct curparse *curp)
230 {
231 int pflags;
232 struct man *man;
233 struct man_cb mancb;
234
235 mancb.man_err = merr;
236 mancb.man_warn = mwarn;
237
238 /* Defaults from mandoc.1. */
239
240 pflags = MAN_IGN_MACRO | MAN_IGN_ESCAPE | MAN_IGN_CHARS;
241
242 if (curp->fflags & NO_IGN_MACRO)
243 pflags &= ~MAN_IGN_MACRO;
244 if (curp->fflags & NO_IGN_CHARS)
245 pflags &= ~MAN_IGN_CHARS;
246 if (curp->fflags & NO_IGN_ESCAPE)
247 pflags &= ~MAN_IGN_ESCAPE;
248
249 if (NULL == (man = man_alloc(curp, pflags, &mancb)))
250 warnx("memory exhausted");
251
252 return(man);
253 }
254
255
256 static struct mdoc *
257 mdoc_init(struct curparse *curp)
258 {
259 int pflags;
260 struct mdoc *mdoc;
261 struct mdoc_cb mdoccb;
262
263 mdoccb.mdoc_err = merr;
264 mdoccb.mdoc_warn = mwarn;
265
266 /* Defaults from mandoc.1. */
267
268 pflags = MDOC_IGN_MACRO | MDOC_IGN_ESCAPE | MDOC_IGN_CHARS;
269
270 if (curp->fflags & IGN_SCOPE)
271 pflags |= MDOC_IGN_SCOPE;
272 if (curp->fflags & NO_IGN_ESCAPE)
273 pflags &= ~MDOC_IGN_ESCAPE;
274 if (curp->fflags & NO_IGN_MACRO)
275 pflags &= ~MDOC_IGN_MACRO;
276 if (curp->fflags & NO_IGN_CHARS)
277 pflags &= ~MDOC_IGN_CHARS;
278
279 if (NULL == (mdoc = mdoc_alloc(curp, pflags, &mdoccb)))
280 warnx("memory exhausted");
281
282 return(mdoc);
283 }
284
285
286 static int
287 ffile(struct buf *blk, struct buf *ln,
288 const char *file, struct curparse *curp)
289 {
290 int c;
291
292 curp->file = file;
293 if (-1 == (curp->fd = open(curp->file, O_RDONLY, 0))) {
294 warn("%s", curp->file);
295 return(-1);
296 }
297
298 c = fdesc(blk, ln, curp);
299
300 if (-1 == close(curp->fd))
301 warn("%s", curp->file);
302
303 return(c);
304 }
305
306
307 static int
308 fdesc(struct buf *blk, struct buf *ln, struct curparse *curp)
309 {
310 size_t sz;
311 ssize_t ssz;
312 struct stat st;
313 int j, i, pos, lnn, comment;
314 struct man *man;
315 struct mdoc *mdoc;
316
317 sz = BUFSIZ;
318 man = NULL;
319 mdoc = NULL;
320
321 /*
322 * Two buffers: ln and buf. buf is the input buffer optimised
323 * here for each file's block size. ln is a line buffer. Both
324 * growable, hence passed in by ptr-ptr.
325 */
326
327 if (-1 == fstat(curp->fd, &st))
328 warn("%s", curp->file);
329 else if ((size_t)st.st_blksize > sz)
330 sz = st.st_blksize;
331
332 if (sz > blk->sz) {
333 blk->buf = realloc(blk->buf, sz);
334 if (NULL == blk->buf) {
335 warn("realloc");
336 return(-1);
337 }
338 blk->sz = sz;
339 }
340
341 /* Fill buf with file blocksize. */
342
343 for (lnn = pos = comment = 0; ; ) {
344 if (-1 == (ssz = read(curp->fd, blk->buf, sz))) {
345 warn("%s", curp->file);
346 return(-1);
347 } else if (0 == ssz)
348 break;
349
350 /* Parse the read block into partial or full lines. */
351
352 for (i = 0; i < (int)ssz; i++) {
353 if (pos >= (int)ln->sz) {
354 ln->sz += 256; /* Step-size. */
355 ln->buf = realloc(ln->buf, ln->sz);
356 if (NULL == ln->buf) {
357 warn("realloc");
358 return(-1);
359 }
360 }
361
362 if ('\n' != blk->buf[i]) {
363 if (comment)
364 continue;
365 ln->buf[pos++] = blk->buf[i];
366
367 /* Handle in-line `\"' comments. */
368
369 if (1 == pos || '\"' != ln->buf[pos - 1])
370 continue;
371
372 for (j = pos - 2; j >= 0; j--)
373 if ('\\' != ln->buf[j])
374 break;
375
376 if ( ! ((pos - 2 - j) % 2))
377 continue;
378
379 comment = 1;
380 pos -= 2;
381 continue;
382 }
383
384 /* Handle escaped `\\n' newlines. */
385
386 if (pos > 0 && 0 == comment &&
387 '\\' == ln->buf[pos - 1]) {
388 for (j = pos - 1; j >= 0; j--)
389 if ('\\' != ln->buf[j])
390 break;
391 if ( ! ((pos - j) % 2)) {
392 pos--;
393 lnn++;
394 continue;
395 }
396 }
397
398 ln->buf[pos] = 0;
399 lnn++;
400
401 /* If unset, assign parser in pset(). */
402
403 if ( ! (man || mdoc) && ! pset(ln->buf,
404 pos, curp, &man, &mdoc))
405 return(-1);
406
407 pos = comment = 0;
408
409 /* Pass down into parsers. */
410
411 if (man && ! man_parseln(man, lnn, ln->buf))
412 return(0);
413 if (mdoc && ! mdoc_parseln(mdoc, lnn, ln->buf))
414 return(0);
415 }
416 }
417
418 /* NOTE a parser may not have been assigned, yet. */
419
420 if ( ! (man || mdoc)) {
421 warnx("%s: not a manual", curp->file);
422 return(0);
423 }
424
425 if (mdoc && ! mdoc_endparse(mdoc))
426 return(0);
427 if (man && ! man_endparse(man))
428 return(0);
429
430 /* If unset, allocate output dev now (if applicable). */
431
432 if ( ! (curp->outman && curp->outmdoc)) {
433 switch (curp->outtype) {
434 case (OUTT_TREE):
435 curp->outman = tree_man;
436 curp->outmdoc = tree_mdoc;
437 break;
438 case (OUTT_LINT):
439 break;
440 default:
441 curp->outdata = ascii_alloc();
442 curp->outman = terminal_man;
443 curp->outmdoc = terminal_mdoc;
444 curp->outfree = terminal_free;
445 break;
446 }
447 }
448
449 /* Execute the out device, if it exists. */
450
451 if (man && curp->outman)
452 if ( ! (*curp->outman)(curp->outdata, man))
453 return(-1);
454 if (mdoc && curp->outmdoc)
455 if ( ! (*curp->outmdoc)(curp->outdata, mdoc))
456 return(-1);
457
458 return(1);
459 }
460
461
462 static int
463 pset(const char *buf, int pos, struct curparse *curp,
464 struct man **man, struct mdoc **mdoc)
465 {
466 int i;
467
468 /*
469 * Try to intuit which kind of manual parser should be used. If
470 * passed in by command-line (-man, -mdoc), then use that
471 * explicitly. If passed as -mandoc, then try to guess from the
472 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
473 * default to -man, which is more lenient.
474 */
475
476 if (buf[0] == '.') {
477 for (i = 1; buf[i]; i++)
478 if (' ' != buf[i] && '\t' != buf[i])
479 break;
480 if (0 == buf[i])
481 return(1);
482 }
483
484 switch (curp->inttype) {
485 case (INTT_MDOC):
486 if (NULL == curp->mdoc)
487 curp->mdoc = mdoc_init(curp);
488 if (NULL == (*mdoc = curp->mdoc))
489 return(0);
490 curp->lastmdoc = *mdoc;
491 return(1);
492 case (INTT_MAN):
493 if (NULL == curp->man)
494 curp->man = man_init(curp);
495 if (NULL == (*man = curp->man))
496 return(0);
497 curp->lastman = *man;
498 return(1);
499 default:
500 break;
501 }
502
503 if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3)) {
504 if (NULL == curp->mdoc)
505 curp->mdoc = mdoc_init(curp);
506 if (NULL == (*mdoc = curp->mdoc))
507 return(0);
508 curp->lastmdoc = *mdoc;
509 return(1);
510 }
511
512 if (NULL == curp->man)
513 curp->man = man_init(curp);
514 if (NULL == (*man = curp->man))
515 return(0);
516 curp->lastman = *man;
517 return(1);
518 }
519
520
521 static int
522 moptions(enum intt *tflags, char *arg)
523 {
524
525 if (0 == strcmp(arg, "doc"))
526 *tflags = INTT_MDOC;
527 else if (0 == strcmp(arg, "andoc"))
528 *tflags = INTT_AUTO;
529 else if (0 == strcmp(arg, "an"))
530 *tflags = INTT_MAN;
531 else {
532 warnx("bad argument: -m%s", arg);
533 return(0);
534 }
535
536 return(1);
537 }
538
539
540 static int
541 toptions(enum outt *tflags, char *arg)
542 {
543
544 if (0 == strcmp(arg, "ascii"))
545 *tflags = OUTT_ASCII;
546 else if (0 == strcmp(arg, "lint"))
547 *tflags = OUTT_LINT;
548 else if (0 == strcmp(arg, "tree"))
549 *tflags = OUTT_TREE;
550 else {
551 warnx("bad argument: -T%s", arg);
552 return(0);
553 }
554
555 return(1);
556 }
557
558
559 static int
560 foptions(int *fflags, char *arg)
561 {
562 char *v, *o;
563 char *toks[7];
564
565 toks[0] = "ign-scope";
566 toks[1] = "no-ign-escape";
567 toks[2] = "no-ign-macro";
568 toks[3] = "no-ign-chars";
569 toks[4] = "ign-errors";
570 toks[5] = "strict";
571 toks[6] = NULL;
572
573 while (*arg) {
574 o = arg;
575 switch (getsubopt(&arg, toks, &v)) {
576 case (0):
577 *fflags |= IGN_SCOPE;
578 break;
579 case (1):
580 *fflags |= NO_IGN_ESCAPE;
581 break;
582 case (2):
583 *fflags |= NO_IGN_MACRO;
584 break;
585 case (3):
586 *fflags |= NO_IGN_CHARS;
587 break;
588 case (4):
589 *fflags |= IGN_ERRORS;
590 break;
591 case (5):
592 *fflags |= NO_IGN_ESCAPE |
593 NO_IGN_MACRO | NO_IGN_CHARS;
594 break;
595 default:
596 warnx("bad argument: -f%s", o);
597 return(0);
598 }
599 }
600
601 return(1);
602 }
603
604
605 static int
606 woptions(int *wflags, char *arg)
607 {
608 char *v, *o;
609 char *toks[3];
610
611 toks[0] = "all";
612 toks[1] = "error";
613 toks[2] = NULL;
614
615 while (*arg) {
616 o = arg;
617 switch (getsubopt(&arg, toks, &v)) {
618 case (0):
619 *wflags |= WARN_WALL;
620 break;
621 case (1):
622 *wflags |= WARN_WERR;
623 break;
624 default:
625 warnx("bad argument: -W%s", o);
626 return(0);
627 }
628 }
629
630 return(1);
631 }
632
633
634 /* ARGSUSED */
635 static int
636 merr(void *arg, int line, int col, const char *msg)
637 {
638 struct curparse *curp;
639
640 curp = (struct curparse *)arg;
641
642 warnx("%s:%d: error: %s (column %d)",
643 curp->file, line, msg, col);
644
645 return(0);
646 }
647
648
649 static int
650 mwarn(void *arg, int line, int col, const char *msg)
651 {
652 struct curparse *curp;
653
654 curp = (struct curparse *)arg;
655
656 if ( ! (curp->wflags & WARN_WALL))
657 return(1);
658
659 warnx("%s:%d: warning: %s (column %d)",
660 curp->file, line, msg, col);
661
662 if ( ! (curp->wflags & WARN_WERR))
663 return(1);
664
665 warnx("considering warnings as errors");
666 return(0);
667 }
668