1 /* $Id: main.c,v 1.18 2009/10/27 21:40:07 schwarze 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.
32 #define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
34 typedef void (*out_mdoc
)(void *, const struct mdoc
*);
35 typedef void (*out_man
)(void *, const struct man
*);
36 typedef void (*out_free
)(void *);
57 const char *file
; /* Current parse. */
58 int fd
; /* Current parse. */
60 #define WARN_WALL (1 << 0) /* All-warnings mask. */
61 #define WARN_WERR (1 << 2) /* Warnings->errors. */
63 #define IGN_SCOPE (1 << 0) /* Ignore scope errors. */
64 #define NO_IGN_ESCAPE (1 << 1) /* Don't ignore bad escapes. */
65 #define NO_IGN_MACRO (1 << 2) /* Don't ignore bad macros. */
66 #define NO_IGN_CHARS (1 << 3) /* Don't ignore bad chars. */
67 #define IGN_ERRORS (1 << 4) /* Ignore failed parse. */
68 enum intt inttype
; /* Input parsers... */
72 struct mdoc
*lastmdoc
;
73 enum outt outtype
; /* Output devices... */
81 static int foptions(int *, char *);
82 static int toptions(enum outt
*, char *);
83 static int moptions(enum intt
*, char *);
84 static int woptions(int *, char *);
85 static int merr(void *, int, int, const char *);
86 static int mwarn(void *, int, int, const char *);
87 static int ffile(struct buf
*, struct buf
*,
88 const char *, struct curparse
*);
89 static int fdesc(struct buf
*, struct buf
*,
91 static int pset(const char *, int, struct curparse
*,
92 struct man
**, struct mdoc
**);
93 static struct man
*man_init(struct curparse
*);
94 static struct mdoc
*mdoc_init(struct curparse
*);
95 __dead2
static void version(void);
96 __dead2
static void usage(void);
98 extern char *__progname
;
102 main(int argc
, char *argv
[])
106 struct curparse curp
;
108 bzero(&curp
, sizeof(struct curparse
));
110 curp
.inttype
= INTT_AUTO
;
111 curp
.outtype
= OUTT_ASCII
;
114 while (-1 != (c
= getopt(argc
, argv
, "f:m:O:T:VW:")))
117 if ( ! foptions(&curp
.fflags
, optarg
))
118 return(EXIT_FAILURE
);
121 if ( ! moptions(&curp
.inttype
, optarg
))
122 return(EXIT_FAILURE
);
125 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
126 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
129 if ( ! toptions(&curp
.outtype
, optarg
))
130 return(EXIT_FAILURE
);
133 if ( ! woptions(&curp
.wflags
, optarg
))
134 return(EXIT_FAILURE
);
147 bzero(&ln
, sizeof(struct buf
));
148 bzero(&blk
, sizeof(struct buf
));
153 curp
.file
= "<stdin>";
154 curp
.fd
= STDIN_FILENO
;
156 c
= fdesc(&blk
, &ln
, &curp
);
157 if ( ! (IGN_ERRORS
& curp
.fflags
))
160 rc
= -1 == c
? 0 : 1;
163 while (rc
&& *argv
) {
164 c
= ffile(&blk
, &ln
, *argv
, &curp
);
165 if ( ! (IGN_ERRORS
& curp
.fflags
))
168 rc
= -1 == c
? 0 : 1;
173 if ( ! man_reset(curp
.lastman
))
176 if ( ! mdoc_reset(curp
.lastmdoc
))
179 curp
.lastmdoc
= NULL
;
188 (*curp
.outfree
)(curp
.outdata
);
190 mdoc_free(curp
.mdoc
);
194 return(rc
? EXIT_SUCCESS
: EXIT_FAILURE
);
202 (void)printf("%s %s\n", __progname
, VERSION
);
211 (void)fprintf(stderr
, "usage: %s [-V] [-foption...] "
212 "[-mformat] [-Ooption] [-Toutput] "
213 "[-Werr...]\n", __progname
);
219 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
| MAN_IGN_CHARS
;
232 if (curp
->fflags
& NO_IGN_MACRO
)
233 pflags
&= ~MAN_IGN_MACRO
;
234 if (curp
->fflags
& NO_IGN_CHARS
)
235 pflags
&= ~MAN_IGN_CHARS
;
236 if (curp
->fflags
& NO_IGN_ESCAPE
)
237 pflags
&= ~MAN_IGN_ESCAPE
;
239 if (NULL
== (man
= man_alloc(curp
, pflags
, &mancb
)))
240 warnx("memory exhausted");
247 mdoc_init(struct curparse
*curp
)
251 struct mdoc_cb mdoccb
;
253 mdoccb
.mdoc_err
= merr
;
254 mdoccb
.mdoc_warn
= mwarn
;
256 /* Defaults from mandoc.1. */
258 pflags
= MDOC_IGN_MACRO
| MDOC_IGN_ESCAPE
| MDOC_IGN_CHARS
;
260 if (curp
->fflags
& IGN_SCOPE
)
261 pflags
|= MDOC_IGN_SCOPE
;
262 if (curp
->fflags
& NO_IGN_ESCAPE
)
263 pflags
&= ~MDOC_IGN_ESCAPE
;
264 if (curp
->fflags
& NO_IGN_MACRO
)
265 pflags
&= ~MDOC_IGN_MACRO
;
266 if (curp
->fflags
& NO_IGN_CHARS
)
267 pflags
&= ~MDOC_IGN_CHARS
;
269 if (NULL
== (mdoc
= mdoc_alloc(curp
, pflags
, &mdoccb
)))
270 warnx("memory exhausted");
277 ffile(struct buf
*blk
, struct buf
*ln
,
278 const char *file
, struct curparse
*curp
)
283 if (-1 == (curp
->fd
= open(curp
->file
, O_RDONLY
, 0))) {
284 warn("%s", curp
->file
);
288 c
= fdesc(blk
, ln
, curp
);
290 if (-1 == close(curp
->fd
))
291 warn("%s", curp
->file
);
298 fdesc(struct buf
*blk
, struct buf
*ln
, struct curparse
*curp
)
303 int j
, i
, pos
, lnn
, comment
;
312 * Two buffers: ln and buf. buf is the input buffer optimised
313 * here for each file's block size. ln is a line buffer. Both
314 * growable, hence passed in by ptr-ptr.
317 if (-1 == fstat(curp
->fd
, &st
))
318 warn("%s", curp
->file
);
319 else if ((size_t)st
.st_blksize
> sz
)
323 blk
->buf
= realloc(blk
->buf
, sz
);
324 if (NULL
== blk
->buf
) {
331 /* Fill buf with file blocksize. */
333 for (lnn
= pos
= comment
= 0; ; ) {
334 if (-1 == (ssz
= read(curp
->fd
, blk
->buf
, sz
))) {
335 warn("%s", curp
->file
);
340 /* Parse the read block into partial or full lines. */
342 for (i
= 0; i
< (int)ssz
; i
++) {
343 if (pos
>= (int)ln
->sz
) {
344 ln
->sz
+= 256; /* Step-size. */
345 ln
->buf
= realloc(ln
->buf
, ln
->sz
);
346 if (NULL
== ln
->buf
) {
352 if ('\n' != blk
->buf
[i
]) {
355 ln
->buf
[pos
++] = blk
->buf
[i
];
357 /* Handle in-line `\"' comments. */
359 if (1 == pos
|| '\"' != ln
->buf
[pos
- 1])
362 for (j
= pos
- 2; j
>= 0; j
--)
363 if ('\\' != ln
->buf
[j
])
366 if ( ! ((pos
- 2 - j
) % 2))
374 /* Handle escaped `\\n' newlines. */
376 if (pos
> 0 && 0 == comment
&&
377 '\\' == ln
->buf
[pos
- 1]) {
378 for (j
= pos
- 1; j
>= 0; j
--)
379 if ('\\' != ln
->buf
[j
])
381 if ( ! ((pos
- j
) % 2)) {
391 /* If unset, assign parser in pset(). */
393 if ( ! (man
|| mdoc
) && ! pset(ln
->buf
,
394 pos
, curp
, &man
, &mdoc
))
399 /* Pass down into parsers. */
401 if (man
&& ! man_parseln(man
, lnn
, ln
->buf
))
403 if (mdoc
&& ! mdoc_parseln(mdoc
, lnn
, ln
->buf
))
408 /* NOTE a parser may not have been assigned, yet. */
410 if ( ! (man
|| mdoc
)) {
411 (void)fprintf(stderr
, "%s: not a manual\n",
416 if (mdoc
&& ! mdoc_endparse(mdoc
))
418 if (man
&& ! man_endparse(man
))
421 /* If unset, allocate output dev now (if applicable). */
423 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
424 switch (curp
->outtype
) {
426 curp
->outdata
= html_alloc(curp
->outopts
);
427 curp
->outman
= html_man
;
428 curp
->outmdoc
= html_mdoc
;
429 curp
->outfree
= html_free
;
432 curp
->outman
= tree_man
;
433 curp
->outmdoc
= tree_mdoc
;
438 curp
->outdata
= ascii_alloc();
439 curp
->outman
= terminal_man
;
440 curp
->outmdoc
= terminal_mdoc
;
441 curp
->outfree
= terminal_free
;
446 /* Execute the out device, if it exists. */
448 if (man
&& curp
->outman
)
449 (*curp
->outman
)(curp
->outdata
, man
);
450 if (mdoc
&& curp
->outmdoc
)
451 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
458 pset(const char *buf
, int pos
, struct curparse
*curp
,
459 struct man
**man
, struct mdoc
**mdoc
)
464 * Try to intuit which kind of manual parser should be used. If
465 * passed in by command-line (-man, -mdoc), then use that
466 * explicitly. If passed as -mandoc, then try to guess from the
467 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
468 * default to -man, which is more lenient.
472 for (i
= 1; buf
[i
]; i
++)
473 if (' ' != buf
[i
] && '\t' != buf
[i
])
479 switch (curp
->inttype
) {
481 if (NULL
== curp
->mdoc
)
482 curp
->mdoc
= mdoc_init(curp
);
483 if (NULL
== (*mdoc
= curp
->mdoc
))
485 curp
->lastmdoc
= *mdoc
;
488 if (NULL
== curp
->man
)
489 curp
->man
= man_init(curp
);
490 if (NULL
== (*man
= curp
->man
))
492 curp
->lastman
= *man
;
498 if (pos
>= 3 && 0 == memcmp(buf
, ".Dd", 3)) {
499 if (NULL
== curp
->mdoc
)
500 curp
->mdoc
= mdoc_init(curp
);
501 if (NULL
== (*mdoc
= curp
->mdoc
))
503 curp
->lastmdoc
= *mdoc
;
507 if (NULL
== curp
->man
)
508 curp
->man
= man_init(curp
);
509 if (NULL
== (*man
= curp
->man
))
511 curp
->lastman
= *man
;
517 moptions(enum intt
*tflags
, char *arg
)
520 if (0 == strcmp(arg
, "doc"))
522 else if (0 == strcmp(arg
, "andoc"))
524 else if (0 == strcmp(arg
, "an"))
527 warnx("bad argument: -m%s", arg
);
536 toptions(enum outt
*tflags
, char *arg
)
539 if (0 == strcmp(arg
, "ascii"))
540 *tflags
= OUTT_ASCII
;
541 else if (0 == strcmp(arg
, "lint"))
543 else if (0 == strcmp(arg
, "tree"))
545 else if (0 == strcmp(arg
, "html"))
548 warnx("bad argument: -T%s", arg
);
557 foptions(int *fflags
, char *arg
)
562 toks
[0] = "ign-scope";
563 toks
[1] = "no-ign-escape";
564 toks
[2] = "no-ign-macro";
565 toks
[3] = "no-ign-chars";
566 toks
[4] = "ign-errors";
572 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
574 *fflags
|= IGN_SCOPE
;
577 *fflags
|= NO_IGN_ESCAPE
;
580 *fflags
|= NO_IGN_MACRO
;
583 *fflags
|= NO_IGN_CHARS
;
586 *fflags
|= IGN_ERRORS
;
589 *fflags
|= NO_IGN_ESCAPE
|
590 NO_IGN_MACRO
| NO_IGN_CHARS
;
593 warnx("bad argument: -f%s", o
);
603 woptions(int *wflags
, char *arg
)
614 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
616 *wflags
|= WARN_WALL
;
619 *wflags
|= WARN_WERR
;
622 warnx("bad argument: -W%s", o
);
633 merr(void *arg
, int line
, int col
, const char *msg
)
635 struct curparse
*curp
;
637 curp
= (struct curparse
*)arg
;
639 (void)fprintf(stderr
, "%s:%d:%d: error: %s\n",
640 curp
->file
, line
, col
+ 1, msg
);
647 mwarn(void *arg
, int line
, int col
, const char *msg
)
649 struct curparse
*curp
;
651 curp
= (struct curparse
*)arg
;
653 if ( ! (curp
->wflags
& WARN_WALL
))
656 (void)fprintf(stderr
, "%s:%d:%d: warning: %s\n",
657 curp
->file
, line
, col
+ 1, msg
);
659 if ( ! (curp
->wflags
& WARN_WERR
))