]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.65 2010/05/15 04:46:10 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.
35 #define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
37 /* FIXME: Intel's compiler? LLVM? pcc? */
39 #if !defined(__GNUC__) || (__GNUC__ < 2)
41 # define __attribute__(x)
43 #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
45 typedef void (*out_mdoc
)(void *, const struct mdoc
*);
46 typedef void (*out_man
)(void *, const struct man
*);
47 typedef void (*out_free
)(void *);
69 const char *file
; /* Current parse. */
70 int fd
; /* Current parse. */
72 #define WARN_WALL (1 << 0) /* All-warnings mask. */
73 #define WARN_WERR (1 << 2) /* Warnings->errors. */
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... */
82 enum outt outtype
; /* Output devices... */
90 #define FL_STRICT FL_NIGN_ESCAPE | \
93 static int foptions(int *, char *);
94 static int toptions(struct curparse
*, char *);
95 static int moptions(enum intt
*, char *);
96 static int woptions(int *, char *);
97 static int merr(void *, int, int, const char *);
98 static int mwarn(void *, int, int, const char *);
99 static void ffile(struct buf
*, struct buf
*,
100 const char *, struct curparse
*);
101 static void fdesc(struct buf
*, struct buf
*,
103 static int pset(const char *, int, struct curparse
*,
104 struct man
**, struct mdoc
**);
105 static struct man
*man_init(struct curparse
*);
106 static struct mdoc
*mdoc_init(struct curparse
*);
107 static void version(void) __attribute__((noreturn
));
108 static void usage(void) __attribute__((noreturn
));
110 static const char *progname
;
111 static int with_error
, with_warning
;
114 main(int argc
, char *argv
[])
118 struct curparse curp
;
120 progname
= strrchr(argv
[0], '/');
121 if (progname
== NULL
)
126 memset(&curp
, 0, sizeof(struct curparse
));
128 curp
.inttype
= INTT_AUTO
;
129 curp
.outtype
= OUTT_ASCII
;
132 while (-1 != (c
= getopt(argc
, argv
, "f:m:O:T:VW:")))
135 if ( ! foptions(&curp
.fflags
, optarg
))
136 return(EXIT_FAILURE
);
139 if ( ! moptions(&curp
.inttype
, optarg
))
140 return(EXIT_FAILURE
);
143 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
144 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
147 if ( ! toptions(&curp
, optarg
))
148 return(EXIT_FAILURE
);
151 if ( ! woptions(&curp
.wflags
, optarg
))
152 return(EXIT_FAILURE
);
165 memset(&ln
, 0, sizeof(struct buf
));
166 memset(&blk
, 0, sizeof(struct buf
));
169 curp
.file
= "<stdin>";
170 curp
.fd
= STDIN_FILENO
;
172 fdesc(&blk
, &ln
, &curp
);
176 ffile(&blk
, &ln
, *argv
, &curp
);
178 if (with_error
&& !(curp
.fflags
& FL_IGN_ERRORS
))
188 (*curp
.outfree
)(curp
.outdata
);
190 return((with_warning
|| with_error
) ? EXIT_FAILURE
: EXIT_SUCCESS
);
198 (void)printf("%s %s\n", progname
, VERSION
);
207 (void)fprintf(stderr
, "usage: %s [-V] [-foption] "
208 "[-mformat] [-Ooption] [-Toutput] "
209 "[-Werr] [file...]\n", progname
);
215 man_init(struct curparse
*curp
)
220 mancb
.man_err
= merr
;
221 mancb
.man_warn
= mwarn
;
223 /* Defaults from mandoc.1. */
225 pflags
= MAN_IGN_MACRO
| MAN_IGN_ESCAPE
;
227 if (curp
->fflags
& FL_NIGN_MACRO
)
228 pflags
&= ~MAN_IGN_MACRO
;
229 if (curp
->fflags
& FL_NIGN_ESCAPE
)
230 pflags
&= ~MAN_IGN_ESCAPE
;
232 return(man_alloc(curp
, pflags
, &mancb
));
237 mdoc_init(struct curparse
*curp
)
240 struct mdoc_cb mdoccb
;
242 mdoccb
.mdoc_err
= merr
;
243 mdoccb
.mdoc_warn
= mwarn
;
245 /* Defaults from mandoc.1. */
247 pflags
= MDOC_IGN_MACRO
| MDOC_IGN_ESCAPE
;
249 if (curp
->fflags
& FL_IGN_SCOPE
)
250 pflags
|= MDOC_IGN_SCOPE
;
251 if (curp
->fflags
& FL_NIGN_ESCAPE
)
252 pflags
&= ~MDOC_IGN_ESCAPE
;
253 if (curp
->fflags
& FL_NIGN_MACRO
)
254 pflags
&= ~MDOC_IGN_MACRO
;
256 return(mdoc_alloc(curp
, pflags
, &mdoccb
));
261 ffile(struct buf
*blk
, struct buf
*ln
,
262 const char *file
, struct curparse
*curp
)
266 if (-1 == (curp
->fd
= open(curp
->file
, O_RDONLY
, 0))) {
272 fdesc(blk
, ln
, curp
);
274 if (-1 == close(curp
->fd
))
280 fdesc(struct buf
*blk
, struct buf
*ln
, struct curparse
*curp
)
285 int j
, i
, pos
, lnn
, comment
;
294 * Two buffers: ln and buf. buf is the input buffer optimised
295 * here for each file's block size. ln is a line buffer. Both
296 * growable, hence passed in by ptr-ptr.
299 if (-1 == fstat(curp
->fd
, &st
)) {
304 if ((size_t)st
.st_blksize
> sz
)
308 void *buf
= realloc(blk
->buf
, sz
);
319 /* Fill buf with file blocksize. */
321 for (lnn
= pos
= comment
= 0; ; ) {
322 if (-1 == (ssz
= read(curp
->fd
, blk
->buf
, sz
))) {
328 /* Parse the read block into partial or full lines. */
330 for (i
= 0; i
< (int)ssz
; i
++) {
331 if (pos
>= (int)ln
->sz
) {
332 ln
->sz
+= 256; /* Step-size. */
333 ln
->buf
= realloc(ln
->buf
, ln
->sz
);
334 if (NULL
== ln
->buf
) {
340 if ('\n' != blk
->buf
[i
]) {
343 ln
->buf
[pos
++] = blk
->buf
[i
];
345 /* Handle in-line `\"' comments. */
347 if (1 == pos
|| '\"' != ln
->buf
[pos
- 1])
350 for (j
= pos
- 2; j
>= 0; j
--)
351 if ('\\' != ln
->buf
[j
])
354 if ( ! ((pos
- 2 - j
) % 2))
359 for (; pos
> 0; --pos
) {
360 if (ln
->buf
[pos
- 1] != ' ')
362 if (pos
> 2 && ln
->buf
[pos
- 2] == '\\')
368 /* Handle escaped `\\n' newlines. */
370 if (pos
> 0 && 0 == comment
&&
371 '\\' == ln
->buf
[pos
- 1]) {
372 for (j
= pos
- 1; j
>= 0; j
--)
373 if ('\\' != ln
->buf
[j
])
375 if ( ! ((pos
- j
) % 2)) {
385 /* If unset, assign parser in pset(). */
387 if ( ! (man
|| mdoc
) && ! pset(ln
->buf
,
388 pos
, curp
, &man
, &mdoc
))
393 /* Pass down into parsers. */
395 if (man
&& ! man_parseln(man
, lnn
, ln
->buf
))
397 if (mdoc
&& ! mdoc_parseln(mdoc
, lnn
, ln
->buf
))
402 /* NOTE a parser may not have been assigned, yet. */
404 if ( ! (man
|| mdoc
)) {
405 fprintf(stderr
, "%s: Not a manual\n", curp
->file
);
409 if (mdoc
&& ! mdoc_endparse(mdoc
))
411 if (man
&& ! man_endparse(man
))
414 /* If unset, allocate output dev now (if applicable). */
416 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
417 switch (curp
->outtype
) {
419 curp
->outdata
= xhtml_alloc(curp
->outopts
);
420 curp
->outman
= html_man
;
421 curp
->outmdoc
= html_mdoc
;
422 curp
->outfree
= html_free
;
425 curp
->outdata
= html_alloc(curp
->outopts
);
426 curp
->outman
= html_man
;
427 curp
->outmdoc
= html_mdoc
;
428 curp
->outfree
= html_free
;
431 curp
->outman
= tree_man
;
432 curp
->outmdoc
= tree_mdoc
;
437 curp
->outdata
= ascii_alloc();
438 curp
->outman
= terminal_man
;
439 curp
->outmdoc
= terminal_mdoc
;
440 curp
->outfree
= terminal_free
;
445 /* Execute the out device, if it exists. */
447 if (man
&& curp
->outman
)
448 (*curp
->outman
)(curp
->outdata
, man
);
449 if (mdoc
&& curp
->outmdoc
)
450 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
454 mdoc_free(curp
->mdoc
);
470 pset(const char *buf
, int pos
, struct curparse
*curp
,
471 struct man
**man
, struct mdoc
**mdoc
)
476 * Try to intuit which kind of manual parser should be used. If
477 * passed in by command-line (-man, -mdoc), then use that
478 * explicitly. If passed as -mandoc, then try to guess from the
479 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
480 * default to -man, which is more lenient.
484 for (i
= 1; buf
[i
]; i
++)
485 if (' ' != buf
[i
] && '\t' != buf
[i
])
491 switch (curp
->inttype
) {
493 if (NULL
== curp
->mdoc
)
494 curp
->mdoc
= mdoc_init(curp
);
495 if (NULL
== (*mdoc
= curp
->mdoc
))
499 if (NULL
== curp
->man
)
500 curp
->man
= man_init(curp
);
501 if (NULL
== (*man
= curp
->man
))
508 if (pos
>= 3 && 0 == memcmp(buf
, ".Dd", 3)) {
509 if (NULL
== curp
->mdoc
)
510 curp
->mdoc
= mdoc_init(curp
);
511 if (NULL
== (*mdoc
= curp
->mdoc
))
516 if (NULL
== curp
->man
)
517 curp
->man
= man_init(curp
);
518 if (NULL
== (*man
= curp
->man
))
525 moptions(enum intt
*tflags
, char *arg
)
528 if (0 == strcmp(arg
, "doc"))
530 else if (0 == strcmp(arg
, "andoc"))
532 else if (0 == strcmp(arg
, "an"))
535 fprintf(stderr
, "%s: Bad argument\n", arg
);
544 toptions(struct curparse
*curp
, char *arg
)
547 if (0 == strcmp(arg
, "ascii"))
548 curp
->outtype
= OUTT_ASCII
;
549 else if (0 == strcmp(arg
, "lint")) {
550 curp
->outtype
= OUTT_LINT
;
551 curp
->wflags
|= WARN_WALL
;
552 curp
->fflags
|= FL_STRICT
;
554 else if (0 == strcmp(arg
, "tree"))
555 curp
->outtype
= OUTT_TREE
;
556 else if (0 == strcmp(arg
, "html"))
557 curp
->outtype
= OUTT_HTML
;
558 else if (0 == strcmp(arg
, "xhtml"))
559 curp
->outtype
= OUTT_XHTML
;
561 fprintf(stderr
, "%s: Bad argument\n", arg
);
570 foptions(int *fflags
, char *arg
)
575 toks
[0] = "ign-scope";
576 toks
[1] = "no-ign-escape";
577 toks
[2] = "no-ign-macro";
578 toks
[3] = "ign-errors";
580 toks
[5] = "ign-escape";
585 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
587 *fflags
|= FL_IGN_SCOPE
;
590 *fflags
|= FL_NIGN_ESCAPE
;
593 *fflags
|= FL_NIGN_MACRO
;
596 *fflags
|= FL_IGN_ERRORS
;
599 *fflags
|= FL_STRICT
;
602 *fflags
&= ~FL_NIGN_ESCAPE
;
605 fprintf(stderr
, "%s: Bad argument\n", o
);
615 woptions(int *wflags
, char *arg
)
626 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
628 *wflags
|= WARN_WALL
;
631 *wflags
|= WARN_WERR
;
634 fprintf(stderr
, "%s: Bad argument\n", o
);
645 merr(void *arg
, int line
, int col
, const char *msg
)
647 struct curparse
*curp
;
649 curp
= (struct curparse
*)arg
;
651 (void)fprintf(stderr
, "%s:%d:%d: error: %s\n",
652 curp
->file
, line
, col
+ 1, msg
);
661 mwarn(void *arg
, int line
, int col
, const char *msg
)
663 struct curparse
*curp
;
665 curp
= (struct curparse
*)arg
;
667 if ( ! (curp
->wflags
& WARN_WALL
))
670 (void)fprintf(stderr
, "%s:%d:%d: warning: %s\n",
671 curp
->file
, line
, col
+ 1, msg
);
674 if (curp
->wflags
& WARN_WERR
) {