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