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