]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.76 2010/05/16 10:59:36 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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.
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.
38 #define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
40 /* FIXME: Intel's compiler? LLVM? pcc? */
42 #if !defined(__GNUC__) || (__GNUC__ < 2)
44 # define __attribute__(x)
46 #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
48 typedef void (*out_mdoc
)(void *, const struct mdoc
*);
49 typedef void (*out_man
)(void *, const struct man
*);
50 typedef void (*out_free
)(void *);
72 const char *file
; /* Current parse. */
73 int fd
; /* Current parse. */
75 /* FIXME: set by max error */
76 #define WARN_WALL (1 << 0) /* All-warnings mask. */
77 #define WARN_WERR (1 << 2) /* Warnings->errors. */
79 #define FL_IGN_SCOPE (1 << 0) /* Ignore scope errors. */
80 #define FL_NIGN_ESCAPE (1 << 1) /* Don't ignore bad escapes. */
81 #define FL_NIGN_MACRO (1 << 2) /* Don't ignore bad macros. */
82 #define FL_IGN_ERRORS (1 << 4) /* Ignore failed parse. */
83 #define FL_STRICT FL_NIGN_ESCAPE | \
85 enum intt inttype
; /* Input parsers... */
89 enum outt outtype
; /* Output devices... */
97 static void fdesc(struct curparse
*);
98 static void ffile(const char *, struct curparse
*);
99 static int foptions(int *, char *);
100 static struct man
*man_init(struct curparse
*);
101 static struct mdoc
*mdoc_init(struct curparse
*);
102 static struct roff
*roff_init(struct curparse
*);
103 static int merr(void *, int, int, const char *); /* DEPRECATED */
104 static int moptions(enum intt
*, char *);
105 static int mwarn(void *, int, int, const char *); /* DEPRECATED */
106 static int mmsg(enum mandocerr
, void *,
107 int, int, const char *);
108 static int pset(const char *, int, struct curparse
*,
109 struct man
**, struct mdoc
**);
110 static int toptions(struct curparse
*, char *);
111 static void usage(void) __attribute__((noreturn
));
112 static void version(void) __attribute__((noreturn
));
113 static int woptions(int *, char *);
115 static const char *progname
;
116 static int with_error
;
117 static int with_warning
;
120 main(int argc
, char *argv
[])
123 struct curparse curp
;
125 progname
= strrchr(argv
[0], '/');
126 if (progname
== NULL
)
131 memset(&curp
, 0, sizeof(struct curparse
));
133 curp
.inttype
= INTT_AUTO
;
134 curp
.outtype
= OUTT_ASCII
;
137 while (-1 != (c
= getopt(argc
, argv
, "f:m:O:T:VW:")))
140 if ( ! foptions(&curp
.fflags
, optarg
))
141 return(EXIT_FAILURE
);
144 if ( ! moptions(&curp
.inttype
, optarg
))
145 return(EXIT_FAILURE
);
148 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
149 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
152 if ( ! toptions(&curp
, optarg
))
153 return(EXIT_FAILURE
);
156 if ( ! woptions(&curp
.wflags
, optarg
))
157 return(EXIT_FAILURE
);
171 curp
.file
= "<stdin>";
172 curp
.fd
= STDIN_FILENO
;
180 if (with_error
&& !(curp
.fflags
& FL_IGN_ERRORS
))
186 (*curp
.outfree
)(curp
.outdata
);
188 mdoc_free(curp
.mdoc
);
192 roff_free(curp
.roff
);
194 return((with_warning
|| with_error
) ?
195 EXIT_FAILURE
: EXIT_SUCCESS
);
203 (void)printf("%s %s\n", progname
, VERSION
);
212 (void)fprintf(stderr
, "usage: %s [-V] [-foption] "
213 "[-mformat] [-Ooption] [-Toutput] "
214 "[-Werr] [file...]\n", progname
);
220 man_init(struct curparse
*curp
)
225 mancb
.man_err
= merr
;
226 mancb
.man_warn
= mwarn
;
228 /* Defaults from mandoc.1. */
230 pflags
= MAN_IGN_MACRO
| MAN_IGN_ESCAPE
;
232 if (curp
->fflags
& FL_NIGN_MACRO
)
233 pflags
&= ~MAN_IGN_MACRO
;
234 if (curp
->fflags
& FL_NIGN_ESCAPE
)
235 pflags
&= ~MAN_IGN_ESCAPE
;
237 return(man_alloc(curp
, pflags
, &mancb
));
242 roff_init(struct curparse
*curp
)
245 return(roff_alloc(mmsg
, curp
));
250 mdoc_init(struct curparse
*curp
)
253 struct mdoc_cb mdoccb
;
255 mdoccb
.mdoc_err
= merr
;
256 mdoccb
.mdoc_warn
= mwarn
;
258 /* Defaults from mandoc.1. */
260 pflags
= MDOC_IGN_MACRO
| MDOC_IGN_ESCAPE
;
262 if (curp
->fflags
& FL_IGN_SCOPE
)
263 pflags
|= MDOC_IGN_SCOPE
;
264 if (curp
->fflags
& FL_NIGN_ESCAPE
)
265 pflags
&= ~MDOC_IGN_ESCAPE
;
266 if (curp
->fflags
& FL_NIGN_MACRO
)
267 pflags
&= ~MDOC_IGN_MACRO
;
269 return(mdoc_alloc(curp
, pflags
, &mdoccb
));
274 ffile(const char *file
, struct curparse
*curp
)
278 if (-1 == (curp
->fd
= open(curp
->file
, O_RDONLY
, 0))) {
286 if (-1 == close(curp
->fd
))
292 resize_buf(struct buf
*buf
, size_t initial
)
301 tmp
= realloc(buf
->buf
, sz
);
313 read_whole_file(struct curparse
*curp
, struct buf
*fb
, int *with_mmap
)
319 if (-1 == fstat(curp
->fd
, &st
)) {
326 * If we're a regular file, try just reading in the whole entry
327 * via mmap(). This is faster than reading it into blocks, and
328 * since each file is only a few bytes to begin with, I'm not
329 * concerned that this is going to tank any machines.
332 if (S_ISREG(st
.st_mode
)) {
333 if (st
.st_size
>= (1U << 31)) {
334 fprintf(stderr
, "%s: input too large\n",
340 fb
->sz
= (size_t)st
.st_size
;
341 fb
->buf
= mmap(NULL
, fb
->sz
, PROT_READ
,
342 MAP_FILE
, curp
->fd
, 0);
343 if (fb
->buf
!= MAP_FAILED
)
348 * If this isn't a regular file (like, say, stdin), then we must
349 * go the old way and just read things in bit by bit.
358 if (fb
->sz
== (1U << 31)) {
359 fprintf(stderr
, "%s: input too large\n",
363 if (! resize_buf(fb
, 65536))
366 ssz
= read(curp
->fd
, fb
->buf
+ (int)off
, fb
->sz
- off
);
386 fdesc(struct curparse
*curp
)
389 int i
, pos
, lnn
, lnn_start
, with_mmap
, of
;
398 memset(&ln
, 0, sizeof(struct buf
));
401 * Two buffers: ln and buf. buf is the input file and may be
402 * memory mapped. ln is a line buffer and grows on-demand.
405 if ( ! read_whole_file(curp
, &blk
, &with_mmap
))
408 if (NULL
== curp
->roff
)
409 curp
->roff
= roff_init(curp
);
410 if (NULL
== (roff
= curp
->roff
))
413 for (i
= 0, lnn
= 1; i
< (int)blk
.sz
;) {
416 while (i
< (int)blk
.sz
) {
417 if ('\n' == blk
.buf
[i
]) {
422 /* Trailing backslash is like a plain character. */
423 if ('\\' != blk
.buf
[i
] || i
+ 1 == (int)blk
.sz
) {
424 if (pos
>= (int)ln
.sz
)
425 if (! resize_buf(&ln
, 256))
427 ln
.buf
[pos
++] = blk
.buf
[i
++];
430 /* Found an escape and at least one other character. */
431 if ('\n' == blk
.buf
[i
+ 1]) {
432 /* Escaped newlines are skipped over */
437 if ('"' == blk
.buf
[i
+ 1]) {
439 /* Comment, skip to end of line */
440 for (; i
< (int)blk
.sz
; ++i
) {
441 if ('\n' == blk
.buf
[i
]) {
447 /* Backout trailing whitespaces */
448 for (; pos
> 0; --pos
) {
449 if (ln
.buf
[pos
- 1] != ' ')
451 if (pos
> 2 && ln
.buf
[pos
- 2] == '\\')
456 /* Some other escape sequence, copy and continue. */
457 if (pos
+ 1 >= (int)ln
.sz
)
458 if (! resize_buf(&ln
, 256))
461 ln
.buf
[pos
++] = blk
.buf
[i
++];
462 ln
.buf
[pos
++] = blk
.buf
[i
++];
465 if (pos
>= (int)ln
.sz
)
466 if (! resize_buf(&ln
, 256))
471 * A significant amount of complexity is contained by
472 * the roff preprocessor. It's line-oriented but can be
473 * expressed on one line, so we need at times to
474 * readjust our starting point and re-run it. The roff
475 * preprocessor can also readjust the buffers with new
476 * data, so we pass them in wholesale.
481 re
= roff_parseln(roff
, lnn_start
,
482 &ln
.buf
, &ln
.sz
, of
, &of
);
483 } while (ROFF_RERUN
== re
);
487 else if (ROFF_ERR
== re
)
491 * If input parsers have not been allocated, do so now.
492 * We keep these instanced betwen parsers, but set them
493 * locally per parse routine since we can use different
494 * parsers with each one.
497 if ( ! (man
|| mdoc
))
498 if ( ! pset(ln
.buf
+ of
, pos
- of
, curp
, &man
, &mdoc
))
501 /* Lastly, push down into the parsers themselves. */
503 if (man
&& ! man_parseln(man
, lnn_start
, ln
.buf
, of
))
505 if (mdoc
&& ! mdoc_parseln(mdoc
, lnn_start
, ln
.buf
, of
))
509 /* NOTE a parser may not have been assigned, yet. */
511 if ( ! (man
|| mdoc
)) {
512 fprintf(stderr
, "%s: Not a manual\n", curp
->file
);
516 /* Clean up the parse routine ASTs. */
518 if (mdoc
&& ! mdoc_endparse(mdoc
))
520 if (man
&& ! man_endparse(man
))
522 if (roff
&& ! roff_endparse(roff
))
525 /* If unset, allocate output dev now (if applicable). */
527 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
528 switch (curp
->outtype
) {
530 curp
->outdata
= xhtml_alloc(curp
->outopts
);
531 curp
->outman
= html_man
;
532 curp
->outmdoc
= html_mdoc
;
533 curp
->outfree
= html_free
;
536 curp
->outdata
= html_alloc(curp
->outopts
);
537 curp
->outman
= html_man
;
538 curp
->outmdoc
= html_mdoc
;
539 curp
->outfree
= html_free
;
542 curp
->outman
= tree_man
;
543 curp
->outmdoc
= tree_mdoc
;
548 curp
->outdata
= ascii_alloc(80);
549 curp
->outman
= terminal_man
;
550 curp
->outmdoc
= terminal_mdoc
;
551 curp
->outfree
= terminal_free
;
556 /* Execute the out device, if it exists. */
558 if (man
&& curp
->outman
)
559 (*curp
->outman
)(curp
->outdata
, man
);
560 if (mdoc
&& curp
->outmdoc
)
561 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
573 munmap(blk
.buf
, blk
.sz
);
586 pset(const char *buf
, int pos
, struct curparse
*curp
,
587 struct man
**man
, struct mdoc
**mdoc
)
592 * Try to intuit which kind of manual parser should be used. If
593 * passed in by command-line (-man, -mdoc), then use that
594 * explicitly. If passed as -mandoc, then try to guess from the
595 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
596 * default to -man, which is more lenient.
599 if ('.' == buf
[0] || '\'' == buf
[0]) {
600 for (i
= 1; buf
[i
]; i
++)
601 if (' ' != buf
[i
] && '\t' != buf
[i
])
607 switch (curp
->inttype
) {
609 if (NULL
== curp
->mdoc
)
610 curp
->mdoc
= mdoc_init(curp
);
611 if (NULL
== (*mdoc
= curp
->mdoc
))
615 if (NULL
== curp
->man
)
616 curp
->man
= man_init(curp
);
617 if (NULL
== (*man
= curp
->man
))
624 if (pos
>= 3 && 0 == memcmp(buf
, ".Dd", 3)) {
625 if (NULL
== curp
->mdoc
)
626 curp
->mdoc
= mdoc_init(curp
);
627 if (NULL
== (*mdoc
= curp
->mdoc
))
632 if (NULL
== curp
->man
)
633 curp
->man
= man_init(curp
);
634 if (NULL
== (*man
= curp
->man
))
641 moptions(enum intt
*tflags
, char *arg
)
644 if (0 == strcmp(arg
, "doc"))
646 else if (0 == strcmp(arg
, "andoc"))
648 else if (0 == strcmp(arg
, "an"))
651 fprintf(stderr
, "%s: Bad argument\n", arg
);
660 toptions(struct curparse
*curp
, char *arg
)
663 if (0 == strcmp(arg
, "ascii"))
664 curp
->outtype
= OUTT_ASCII
;
665 else if (0 == strcmp(arg
, "lint")) {
666 curp
->outtype
= OUTT_LINT
;
667 curp
->wflags
|= WARN_WALL
;
668 curp
->fflags
|= FL_STRICT
;
670 else if (0 == strcmp(arg
, "tree"))
671 curp
->outtype
= OUTT_TREE
;
672 else if (0 == strcmp(arg
, "html"))
673 curp
->outtype
= OUTT_HTML
;
674 else if (0 == strcmp(arg
, "xhtml"))
675 curp
->outtype
= OUTT_XHTML
;
677 fprintf(stderr
, "%s: Bad argument\n", arg
);
686 foptions(int *fflags
, char *arg
)
691 toks
[0] = "ign-scope";
692 toks
[1] = "no-ign-escape";
693 toks
[2] = "no-ign-macro";
694 toks
[3] = "ign-errors";
696 toks
[5] = "ign-escape";
701 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
703 *fflags
|= FL_IGN_SCOPE
;
706 *fflags
|= FL_NIGN_ESCAPE
;
709 *fflags
|= FL_NIGN_MACRO
;
712 *fflags
|= FL_IGN_ERRORS
;
715 *fflags
|= FL_STRICT
;
718 *fflags
&= ~FL_NIGN_ESCAPE
;
721 fprintf(stderr
, "%s: Bad argument\n", o
);
731 woptions(int *wflags
, char *arg
)
742 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
744 *wflags
|= WARN_WALL
;
747 *wflags
|= WARN_WERR
;
750 fprintf(stderr
, "%s: Bad argument\n", o
);
761 merr(void *arg
, int line
, int col
, const char *msg
)
763 struct curparse
*curp
;
765 curp
= (struct curparse
*)arg
;
767 (void)fprintf(stderr
, "%s:%d:%d: error: %s\n",
768 curp
->file
, line
, col
+ 1, msg
);
777 mwarn(void *arg
, int line
, int col
, const char *msg
)
779 struct curparse
*curp
;
781 curp
= (struct curparse
*)arg
;
783 if ( ! (curp
->wflags
& WARN_WALL
))
786 (void)fprintf(stderr
, "%s:%d:%d: warning: %s\n",
787 curp
->file
, line
, col
+ 1, msg
);
790 if (curp
->wflags
& WARN_WERR
) {
798 static const char * const mandocerrs
[MANDOCERR_MAX
] = {
800 "multi-line scope open on exit",
801 "request for scope closure when no matching scope is open",
802 "line arguments will be lost",
807 * XXX: this is experimental code that will eventually become the
808 * generic means of covering all warnings and errors!
812 mmsg(enum mandocerr t
, void *arg
, int ln
, int col
, const char *msg
)
817 cp
= (struct curparse
*)arg
;
819 fprintf(stderr
, "%s:%d:%d: %s", cp
->file
,
820 ln
, col
+ 1, mandocerrs
[t
]);
823 fprintf(stderr
, ": %s", msg
);