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