]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.67 2010/05/15 05:50:19 joerg 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.
36 #define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
38 /* FIXME: Intel's compiler? LLVM? pcc? */
40 #if !defined(__GNUC__) || (__GNUC__ < 2)
42 # define __attribute__(x)
44 #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
46 typedef void (*out_mdoc
)(void *, const struct mdoc
*);
47 typedef void (*out_man
)(void *, const struct man
*);
48 typedef void (*out_free
)(void *);
70 const char *file
; /* Current parse. */
71 int fd
; /* Current parse. */
73 #define WARN_WALL (1 << 0) /* All-warnings mask. */
74 #define WARN_WERR (1 << 2) /* Warnings->errors. */
76 #define FL_IGN_SCOPE (1 << 0) /* Ignore scope errors. */
77 #define FL_NIGN_ESCAPE (1 << 1) /* Don't ignore bad escapes. */
78 #define FL_NIGN_MACRO (1 << 2) /* Don't ignore bad macros. */
79 #define FL_IGN_ERRORS (1 << 4) /* Ignore failed parse. */
80 #define FL_STRICT FL_NIGN_ESCAPE | \
82 enum intt inttype
; /* Input parsers... */
85 enum outt outtype
; /* Output devices... */
93 static void fdesc(struct curparse
*);
94 static void ffile(const char *, struct curparse
*);
95 static int foptions(int *, char *);
96 static struct man
*man_init(struct curparse
*);
97 static struct mdoc
*mdoc_init(struct curparse
*);
98 static int merr(void *, int, int, const char *);
99 static int moptions(enum intt
*, char *);
100 static int mwarn(void *, int, int, const char *);
101 static int pset(const char *, int, struct curparse
*,
102 struct man
**, struct mdoc
**);
103 static int toptions(struct curparse
*, char *);
104 static void usage(void) __attribute__((noreturn
));
105 static void version(void) __attribute__((noreturn
));
106 static int woptions(int *, char *);
108 static const char *progname
;
109 static int with_error
;
110 static int with_warning
;
113 main(int argc
, char *argv
[])
116 struct curparse curp
;
118 progname
= strrchr(argv
[0], '/');
119 if (progname
== NULL
)
124 memset(&curp
, 0, sizeof(struct curparse
));
126 curp
.inttype
= INTT_AUTO
;
127 curp
.outtype
= OUTT_ASCII
;
130 while (-1 != (c
= getopt(argc
, argv
, "f:m:O:T:VW:")))
133 if ( ! foptions(&curp
.fflags
, optarg
))
134 return(EXIT_FAILURE
);
137 if ( ! moptions(&curp
.inttype
, optarg
))
138 return(EXIT_FAILURE
);
141 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
142 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
145 if ( ! toptions(&curp
, optarg
))
146 return(EXIT_FAILURE
);
149 if ( ! woptions(&curp
.wflags
, optarg
))
150 return(EXIT_FAILURE
);
164 curp
.file
= "<stdin>";
165 curp
.fd
= STDIN_FILENO
;
173 if (with_error
&& !(curp
.fflags
& FL_IGN_ERRORS
))
179 (*curp
.outfree
)(curp
.outdata
);
181 return((with_warning
|| with_error
) ?
182 EXIT_FAILURE
: EXIT_SUCCESS
);
190 (void)printf("%s %s\n", progname
, VERSION
);
199 (void)fprintf(stderr
, "usage: %s [-V] [-foption] "
200 "[-mformat] [-Ooption] [-Toutput] "
201 "[-Werr] [file...]\n", progname
);
207 man_init(struct curparse
*curp
)
212 mancb
.man_err
= merr
;
213 mancb
.man_warn
= mwarn
;
215 /* Defaults from mandoc.1. */
217 pflags
= MAN_IGN_MACRO
| MAN_IGN_ESCAPE
;
219 if (curp
->fflags
& FL_NIGN_MACRO
)
220 pflags
&= ~MAN_IGN_MACRO
;
221 if (curp
->fflags
& FL_NIGN_ESCAPE
)
222 pflags
&= ~MAN_IGN_ESCAPE
;
224 return(man_alloc(curp
, pflags
, &mancb
));
229 mdoc_init(struct curparse
*curp
)
232 struct mdoc_cb mdoccb
;
234 mdoccb
.mdoc_err
= merr
;
235 mdoccb
.mdoc_warn
= mwarn
;
237 /* Defaults from mandoc.1. */
239 pflags
= MDOC_IGN_MACRO
| MDOC_IGN_ESCAPE
;
241 if (curp
->fflags
& FL_IGN_SCOPE
)
242 pflags
|= MDOC_IGN_SCOPE
;
243 if (curp
->fflags
& FL_NIGN_ESCAPE
)
244 pflags
&= ~MDOC_IGN_ESCAPE
;
245 if (curp
->fflags
& FL_NIGN_MACRO
)
246 pflags
&= ~MDOC_IGN_MACRO
;
248 return(mdoc_alloc(curp
, pflags
, &mdoccb
));
253 ffile(const char *file
, struct curparse
*curp
)
257 if (-1 == (curp
->fd
= open(curp
->file
, O_RDONLY
, 0))) {
265 if (-1 == close(curp
->fd
))
271 read_whole_file(struct curparse
*curp
, struct buf
*fb
, int *with_mmap
)
278 if (-1 == fstat(curp
->fd
, &st
)) {
285 * If we're a regular file, try just reading in the whole entry
286 * via mmap(). This is faster than reading it into blocks, and
287 * since each file is only a few bytes to begin with, I'm not
288 * concerned that this is going to tank any machines.
291 if (S_ISREG(st
.st_mode
)) {
292 if (st
.st_size
>= (1U << 31)) {
293 fprintf(stderr
, "%s: input too large\n",
300 fb
->buf
= mmap(NULL
, fb
->sz
, PROT_READ
,
301 MAP_FILE
, curp
->fd
, 0);
302 if (fb
->buf
!= MAP_FAILED
)
307 * If this isn't a regular file (like, say, stdin), then we must
308 * go the old way and just read things in bit by bit.
317 if (fb
->sz
== (1U << 31)) {
318 fprintf(stderr
, "%s: input too large\n",
326 buf
= realloc(fb
->buf
, sz
);
334 ssz
= read(curp
->fd
, fb
->buf
+ off
, fb
->sz
- off
);
354 fdesc(struct curparse
*curp
)
358 int j
, i
, pos
, lnn
, comment
, with_mmap
;
365 memset(&ln
, 0, sizeof(struct buf
));
368 * Two buffers: ln and buf. buf is the input buffer optimised
369 * here for each file's block size. ln is a line buffer. Both
370 * growable, hence passed in by ptr-ptr.
373 if (!read_whole_file(curp
, &blk
, &with_mmap
))
376 /* Fill buf with file blocksize. */
378 for (i
= lnn
= pos
= comment
= 0; i
< (int)blk
.sz
; ++i
) {
379 if (pos
>= (int)ln
.sz
) {
380 ln
.sz
+= 256; /* Step-size. */
381 ln
.buf
= realloc(ln
.buf
, ln
.sz
);
382 if (NULL
== ln
.buf
) {
388 if ('\n' != blk
.buf
[i
]) {
391 ln
.buf
[pos
++] = blk
.buf
[i
];
393 /* Handle in-line `\"' comments. */
395 if (1 == pos
|| '\"' != ln
.buf
[pos
- 1])
398 for (j
= pos
- 2; j
>= 0; j
--)
399 if ('\\' != ln
.buf
[j
])
402 if ( ! ((pos
- 2 - j
) % 2))
407 for (; pos
> 0; --pos
) {
408 if (ln
.buf
[pos
- 1] != ' ')
410 if (pos
> 2 && ln
.buf
[pos
- 2] == '\\')
416 /* Handle escaped `\\n' newlines. */
418 if (pos
> 0 && 0 == comment
&& '\\' == ln
.buf
[pos
- 1]) {
419 for (j
= pos
- 1; j
>= 0; j
--)
420 if ('\\' != ln
.buf
[j
])
422 if ( ! ((pos
- j
) % 2)) {
432 /* If unset, assign parser in pset(). */
434 if ( ! (man
|| mdoc
) && ! pset(ln
.buf
, pos
, curp
, &man
, &mdoc
))
439 /* Pass down into parsers. */
441 if (man
&& ! man_parseln(man
, lnn
, ln
.buf
))
443 if (mdoc
&& ! mdoc_parseln(mdoc
, lnn
, ln
.buf
))
447 /* NOTE a parser may not have been assigned, yet. */
449 if ( ! (man
|| mdoc
)) {
450 fprintf(stderr
, "%s: Not a manual\n", curp
->file
);
454 if (mdoc
&& ! mdoc_endparse(mdoc
))
456 if (man
&& ! man_endparse(man
))
459 /* If unset, allocate output dev now (if applicable). */
461 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
462 switch (curp
->outtype
) {
464 curp
->outdata
= xhtml_alloc(curp
->outopts
);
465 curp
->outman
= html_man
;
466 curp
->outmdoc
= html_mdoc
;
467 curp
->outfree
= html_free
;
470 curp
->outdata
= html_alloc(curp
->outopts
);
471 curp
->outman
= html_man
;
472 curp
->outmdoc
= html_mdoc
;
473 curp
->outfree
= html_free
;
476 curp
->outman
= tree_man
;
477 curp
->outmdoc
= tree_mdoc
;
482 curp
->outdata
= ascii_alloc();
483 curp
->outman
= terminal_man
;
484 curp
->outmdoc
= terminal_mdoc
;
485 curp
->outfree
= terminal_free
;
490 /* Execute the out device, if it exists. */
492 if (man
&& curp
->outman
)
493 (*curp
->outman
)(curp
->outdata
, man
);
494 if (mdoc
&& curp
->outmdoc
)
495 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
499 mdoc_free(curp
->mdoc
);
509 munmap(blk
.buf
, blk
.sz
);
521 pset(const char *buf
, int pos
, struct curparse
*curp
,
522 struct man
**man
, struct mdoc
**mdoc
)
527 * Try to intuit which kind of manual parser should be used. If
528 * passed in by command-line (-man, -mdoc), then use that
529 * explicitly. If passed as -mandoc, then try to guess from the
530 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
531 * default to -man, which is more lenient.
535 for (i
= 1; buf
[i
]; i
++)
536 if (' ' != buf
[i
] && '\t' != buf
[i
])
542 switch (curp
->inttype
) {
544 if (NULL
== curp
->mdoc
)
545 curp
->mdoc
= mdoc_init(curp
);
546 if (NULL
== (*mdoc
= curp
->mdoc
))
550 if (NULL
== curp
->man
)
551 curp
->man
= man_init(curp
);
552 if (NULL
== (*man
= curp
->man
))
559 if (pos
>= 3 && 0 == memcmp(buf
, ".Dd", 3)) {
560 if (NULL
== curp
->mdoc
)
561 curp
->mdoc
= mdoc_init(curp
);
562 if (NULL
== (*mdoc
= curp
->mdoc
))
567 if (NULL
== curp
->man
)
568 curp
->man
= man_init(curp
);
569 if (NULL
== (*man
= curp
->man
))
576 moptions(enum intt
*tflags
, char *arg
)
579 if (0 == strcmp(arg
, "doc"))
581 else if (0 == strcmp(arg
, "andoc"))
583 else if (0 == strcmp(arg
, "an"))
586 fprintf(stderr
, "%s: Bad argument\n", arg
);
595 toptions(struct curparse
*curp
, char *arg
)
598 if (0 == strcmp(arg
, "ascii"))
599 curp
->outtype
= OUTT_ASCII
;
600 else if (0 == strcmp(arg
, "lint")) {
601 curp
->outtype
= OUTT_LINT
;
602 curp
->wflags
|= WARN_WALL
;
603 curp
->fflags
|= FL_STRICT
;
605 else if (0 == strcmp(arg
, "tree"))
606 curp
->outtype
= OUTT_TREE
;
607 else if (0 == strcmp(arg
, "html"))
608 curp
->outtype
= OUTT_HTML
;
609 else if (0 == strcmp(arg
, "xhtml"))
610 curp
->outtype
= OUTT_XHTML
;
612 fprintf(stderr
, "%s: Bad argument\n", arg
);
621 foptions(int *fflags
, char *arg
)
626 toks
[0] = "ign-scope";
627 toks
[1] = "no-ign-escape";
628 toks
[2] = "no-ign-macro";
629 toks
[3] = "ign-errors";
631 toks
[5] = "ign-escape";
636 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
638 *fflags
|= FL_IGN_SCOPE
;
641 *fflags
|= FL_NIGN_ESCAPE
;
644 *fflags
|= FL_NIGN_MACRO
;
647 *fflags
|= FL_IGN_ERRORS
;
650 *fflags
|= FL_STRICT
;
653 *fflags
&= ~FL_NIGN_ESCAPE
;
656 fprintf(stderr
, "%s: Bad argument\n", o
);
666 woptions(int *wflags
, char *arg
)
677 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
679 *wflags
|= WARN_WALL
;
682 *wflags
|= WARN_WERR
;
685 fprintf(stderr
, "%s: Bad argument\n", o
);
696 merr(void *arg
, int line
, int col
, const char *msg
)
698 struct curparse
*curp
;
700 curp
= (struct curparse
*)arg
;
702 (void)fprintf(stderr
, "%s:%d:%d: error: %s\n",
703 curp
->file
, line
, col
+ 1, msg
);
712 mwarn(void *arg
, int line
, int col
, const char *msg
)
714 struct curparse
*curp
;
716 curp
= (struct curparse
*)arg
;
718 if ( ! (curp
->wflags
& WARN_WALL
))
721 (void)fprintf(stderr
, "%s:%d:%d: warning: %s\n",
722 curp
->file
, line
, col
+ 1, msg
);
725 if (curp
->wflags
& WARN_WERR
) {