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