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