]> git.cameronkatri.com Git - mandoc.git/blob - main.c
Version.
[mandoc.git] / main.c
1 /* $Id: main.c,v 1.34 2009/07/04 11:10:36 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(EXIT_FAILURE);
136 break;
137 case ('m'):
138 if ( ! moptions(&curp.inttype, optarg))
139 return(EXIT_FAILURE);
140 break;
141 case ('T'):
142 if ( ! toptions(&curp.outtype, optarg))
143 return(EXIT_FAILURE);
144 break;
145 case ('W'):
146 if ( ! woptions(&curp.wflags, optarg))
147 return(EXIT_FAILURE);
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_ESCAPE | 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 if (curp->fflags & NO_IGN_ESCAPE)
242 pflags &= ~MAN_IGN_ESCAPE;
243
244 if (NULL == (man = man_alloc(curp, pflags, &mancb)))
245 warnx("memory exhausted");
246
247 return(man);
248 }
249
250
251 static struct mdoc *
252 mdoc_init(struct curparse *curp)
253 {
254 int pflags;
255 struct mdoc *mdoc;
256 struct mdoc_cb mdoccb;
257
258 mdoccb.mdoc_err = merr;
259 mdoccb.mdoc_warn = mdocwarn;
260
261 /* Defaults from mandoc.1. */
262
263 pflags = MDOC_IGN_MACRO | MDOC_IGN_ESCAPE | MDOC_IGN_CHARS;
264
265 if (curp->fflags & IGN_SCOPE)
266 pflags |= MDOC_IGN_SCOPE;
267 if (curp->fflags & NO_IGN_ESCAPE)
268 pflags &= ~MDOC_IGN_ESCAPE;
269 if (curp->fflags & NO_IGN_MACRO)
270 pflags &= ~MDOC_IGN_MACRO;
271 if (curp->fflags & NO_IGN_CHARS)
272 pflags &= ~MDOC_IGN_CHARS;
273
274 if (NULL == (mdoc = mdoc_alloc(curp, pflags, &mdoccb)))
275 warnx("memory exhausted");
276
277 return(mdoc);
278 }
279
280
281 static int
282 ffile(struct buf *blk, struct buf *ln,
283 const char *file, struct curparse *curp)
284 {
285 int c;
286
287 curp->file = file;
288 if (-1 == (curp->fd = open(curp->file, O_RDONLY, 0))) {
289 warn("%s", curp->file);
290 return(0);
291 }
292
293 c = fdesc(blk, ln, curp);
294
295 if (-1 == close(curp->fd))
296 warn("%s", curp->file);
297
298 return(c);
299 }
300
301
302 static int
303 fdesc(struct buf *blk, struct buf *ln, struct curparse *curp)
304 {
305 size_t sz;
306 ssize_t ssz;
307 struct stat st;
308 int j, i, pos, lnn, comment;
309 struct man *man;
310 struct mdoc *mdoc;
311
312 sz = BUFSIZ;
313 man = NULL;
314 mdoc = NULL;
315
316 /*
317 * Two buffers: ln and buf. buf is the input buffer optimised
318 * here for each file's block size. ln is a line buffer. Both
319 * growable, hence passed in by ptr-ptr.
320 */
321
322 if (-1 == fstat(curp->fd, &st))
323 warn("%s", curp->file);
324 else if ((size_t)st.st_blksize > sz)
325 sz = st.st_blksize;
326
327 if (sz > blk->sz) {
328 blk->buf = realloc(blk->buf, sz);
329 if (NULL == blk->buf) {
330 warn("realloc");
331 return(0);
332 }
333 blk->sz = sz;
334 }
335
336 /* Fill buf with file blocksize. */
337
338 for (lnn = pos = comment = 0; ; ) {
339 if (-1 == (ssz = read(curp->fd, blk->buf, sz))) {
340 warn("%s", curp->file);
341 return(0);
342 } else if (0 == ssz)
343 break;
344
345 /* Parse the read block into partial or full lines. */
346
347 for (i = 0; i < (int)ssz; i++) {
348 if (pos >= (int)ln->sz) {
349 ln->sz += 256; /* Step-size. */
350 ln->buf = realloc(ln->buf, ln->sz);
351 if (NULL == ln->buf) {
352 warn("realloc");
353 return(0);
354 }
355 }
356
357 if ('\n' != blk->buf[i]) {
358 if (comment)
359 continue;
360 ln->buf[pos++] = blk->buf[i];
361
362 /* Handle in-line `\"' comments. */
363
364 if (1 == pos || '\"' != ln->buf[pos - 1])
365 continue;
366
367 for (j = pos - 2; j >= 0; j--)
368 if ('\\' != ln->buf[j])
369 break;
370
371 if ( ! ((pos - 2 - j) % 2))
372 continue;
373
374 comment = 1;
375 pos -= 2;
376 continue;
377 }
378
379 /* Handle escaped `\\n' newlines. */
380
381 if (pos > 0 && 0 == comment &&
382 '\\' == ln->buf[pos - 1]) {
383 for (j = pos - 1; j >= 0; j--)
384 if ('\\' != ln->buf[j])
385 break;
386 if ( ! ((pos - j) % 2)) {
387 pos--;
388 lnn++;
389 continue;
390 }
391 }
392
393 ln->buf[pos] = 0;
394 lnn++;
395
396 /* If unset, assign parser in pset(). */
397
398 if ( ! (man || mdoc) && ! pset(ln->buf,
399 pos, curp, &man, &mdoc))
400 return(0);
401
402 pos = comment = 0;
403
404 /* Pass down into parsers. */
405
406 if (man && ! man_parseln(man, lnn, ln->buf))
407 return(0);
408 if (mdoc && ! mdoc_parseln(mdoc, lnn, ln->buf))
409 return(0);
410 }
411 }
412
413 /* NOTE a parser may not have been assigned, yet. */
414
415 if ( ! (man || mdoc)) {
416 warnx("%s: not a manual", 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_TREE):
430 curp->outman = tree_man;
431 curp->outmdoc = tree_mdoc;
432 break;
433 case (OUTT_LINT):
434 break;
435 default:
436 curp->outdata = ascii_alloc();
437 curp->outman = terminal_man;
438 curp->outmdoc = terminal_mdoc;
439 curp->outfree = terminal_free;
440 break;
441 }
442 }
443
444 /* Execute the out device, if it exists. */
445
446 if (man && curp->outman)
447 if ( ! (*curp->outman)(curp->outdata, man))
448 return(0);
449 if (mdoc && curp->outmdoc)
450 if ( ! (*curp->outmdoc)(curp->outdata, mdoc))
451 return(0);
452
453 return(1);
454 }
455
456
457 static int
458 pset(const char *buf, int pos, struct curparse *curp,
459 struct man **man, struct mdoc **mdoc)
460 {
461 int i;
462
463 /*
464 * Try to intuit which kind of manual parser should be used. If
465 * passed in by command-line (-man, -mdoc), then use that
466 * explicitly. If passed as -mandoc, then try to guess from the
467 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
468 * default to -man, which is more lenient.
469 */
470
471 if (buf[0] == '.') {
472 for (i = 1; buf[i]; i++)
473 if (' ' != buf[i] && '\t' != buf[i])
474 break;
475 if (0 == buf[i])
476 return(1);
477 }
478
479 switch (curp->inttype) {
480 case (INTT_MDOC):
481 if (NULL == curp->mdoc)
482 curp->mdoc = mdoc_init(curp);
483 if (NULL == (*mdoc = curp->mdoc))
484 return(0);
485 curp->lastmdoc = *mdoc;
486 return(1);
487 case (INTT_MAN):
488 if (NULL == curp->man)
489 curp->man = man_init(curp);
490 if (NULL == (*man = curp->man))
491 return(0);
492 curp->lastman = *man;
493 return(1);
494 default:
495 break;
496 }
497
498 if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3)) {
499 if (NULL == curp->mdoc)
500 curp->mdoc = mdoc_init(curp);
501 if (NULL == (*mdoc = curp->mdoc))
502 return(0);
503 curp->lastmdoc = *mdoc;
504 return(1);
505 }
506
507 if (NULL == curp->man)
508 curp->man = man_init(curp);
509 if (NULL == (*man = curp->man))
510 return(0);
511 curp->lastman = *man;
512 return(1);
513 }
514
515
516 static int
517 moptions(enum intt *tflags, char *arg)
518 {
519
520 if (0 == strcmp(arg, "doc"))
521 *tflags = INTT_MDOC;
522 else if (0 == strcmp(arg, "andoc"))
523 *tflags = INTT_AUTO;
524 else if (0 == strcmp(arg, "an"))
525 *tflags = INTT_MAN;
526 else {
527 warnx("bad argument: -m%s", arg);
528 return(0);
529 }
530
531 return(1);
532 }
533
534
535 static int
536 toptions(enum outt *tflags, char *arg)
537 {
538
539 if (0 == strcmp(arg, "ascii"))
540 *tflags = OUTT_ASCII;
541 else if (0 == strcmp(arg, "lint"))
542 *tflags = OUTT_LINT;
543 else if (0 == strcmp(arg, "tree"))
544 *tflags = OUTT_TREE;
545 else {
546 warnx("bad argument: -T%s", arg);
547 return(0);
548 }
549
550 return(1);
551 }
552
553
554 static int
555 foptions(int *fflags, char *arg)
556 {
557 char *v, *o;
558 char *toks[6];
559
560 toks[0] = "ign-scope";
561 toks[1] = "no-ign-escape";
562 toks[2] = "no-ign-macro";
563 toks[3] = "no-ign-chars";
564 toks[4] = "strict";
565 toks[5] = NULL;
566
567 while (*arg) {
568 o = arg;
569 switch (getsubopt(&arg, toks, &v)) {
570 case (0):
571 *fflags |= IGN_SCOPE;
572 break;
573 case (1):
574 *fflags |= NO_IGN_ESCAPE;
575 break;
576 case (2):
577 *fflags |= NO_IGN_MACRO;
578 break;
579 case (3):
580 *fflags |= NO_IGN_CHARS;
581 break;
582 case (4):
583 *fflags |= NO_IGN_ESCAPE |
584 NO_IGN_MACRO | NO_IGN_CHARS;
585 break;
586 default:
587 warnx("bad argument: -f%s", o);
588 return(0);
589 }
590 }
591
592 return(1);
593 }
594
595
596 static int
597 woptions(int *wflags, char *arg)
598 {
599 char *v, *o;
600 char *toks[5];
601
602 toks[0] = "all";
603 toks[1] = "compat";
604 toks[2] = "syntax";
605 toks[3] = "error";
606 toks[4] = NULL;
607
608 while (*arg) {
609 o = arg;
610 switch (getsubopt(&arg, toks, &v)) {
611 case (0):
612 *wflags |= WARN_WALL;
613 break;
614 case (1):
615 *wflags |= WARN_WCOMPAT;
616 break;
617 case (2):
618 *wflags |= WARN_WSYNTAX;
619 break;
620 case (3):
621 *wflags |= WARN_WERR;
622 break;
623 default:
624 warnx("bad argument: -W%s", o);
625 return(0);
626 }
627 }
628
629 return(1);
630 }
631
632
633 /* ARGSUSED */
634 static int
635 merr(void *arg, int line, int col, const char *msg)
636 {
637 struct curparse *curp;
638
639 curp = (struct curparse *)arg;
640 warnx("%s:%d: error: %s (column %d)",
641 curp->file, line, msg, col);
642
643 return(0);
644 }
645
646
647 static int
648 mdocwarn(void *arg, int line, int col,
649 enum mdoc_warn type, const char *msg)
650 {
651 struct curparse *curp;
652 char *wtype;
653
654 curp = (struct curparse *)arg;
655 wtype = NULL;
656
657 switch (type) {
658 case (WARN_COMPAT):
659 wtype = "compat";
660 if (curp->wflags & WARN_WCOMPAT)
661 break;
662 return(1);
663 case (WARN_SYNTAX):
664 wtype = "syntax";
665 if (curp->wflags & WARN_WSYNTAX)
666 break;
667 return(1);
668 }
669
670 assert(wtype);
671 warnx("%s:%d: %s warning: %s (column %d)",
672 curp->file, line, wtype, msg, col);
673
674 if ( ! (curp->wflags & WARN_WERR))
675 return(1);
676
677 warnx("considering warnings as errors");
678 return(0);
679 }
680
681
682 static int
683 manwarn(void *arg, int line, int col, const char *msg)
684 {
685 struct curparse *curp;
686
687 curp = (struct curparse *)arg;
688
689 if ( ! (curp->wflags & WARN_WSYNTAX))
690 return(1);
691
692 warnx("%s:%d: syntax warning: %s (column %d)",
693 curp->file, line, msg, col);
694
695 if ( ! (curp->wflags & WARN_WERR))
696 return(1);
697
698 warnx("considering warnings as errors");
699 return(0);
700 }