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