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