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