]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.61 2010/04/12 19:27:22 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_NIGN_CHARS (1 << 3) /* Don't ignore bad chars. */
79 #define FL_IGN_ERRORS (1 << 4) /* Ignore failed parse. */
80 enum intt inttype
; /* Input parsers... */
84 struct mdoc
*lastmdoc
;
85 enum outt outtype
; /* Output devices... */
93 #define FL_STRICT FL_NIGN_ESCAPE | \
97 static int foptions(int *, char *);
98 static int toptions(struct curparse
*, char *);
99 static int moptions(enum intt
*, char *);
100 static int woptions(int *, char *);
101 static int merr(void *, int, int, const char *);
102 static int mwarn(void *, int, int, const char *);
103 static int ffile(struct buf
*, struct buf
*,
104 const char *, struct curparse
*);
105 static int fdesc(struct buf
*, struct buf
*,
107 static int pset(const char *, int, struct curparse
*,
108 struct man
**, struct mdoc
**);
109 static struct man
*man_init(struct curparse
*);
110 static struct mdoc
*mdoc_init(struct curparse
*);
111 static void version(void) __attribute__((noreturn
));
112 static void usage(void) __attribute__((noreturn
));
114 static const char *progname
;
118 main(int argc
, char *argv
[])
122 struct curparse curp
;
124 progname
= strrchr(argv
[0], '/');
125 if (progname
== NULL
)
130 memset(&curp
, 0, sizeof(struct curparse
));
132 curp
.inttype
= INTT_AUTO
;
133 curp
.outtype
= OUTT_ASCII
;
136 while (-1 != (c
= getopt(argc
, argv
, "f:m:O:T:VW:")))
139 if ( ! foptions(&curp
.fflags
, optarg
))
140 return(EXIT_FAILURE
);
143 if ( ! moptions(&curp
.inttype
, optarg
))
144 return(EXIT_FAILURE
);
147 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
148 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
151 if ( ! toptions(&curp
, optarg
))
152 return(EXIT_FAILURE
);
155 if ( ! woptions(&curp
.wflags
, optarg
))
156 return(EXIT_FAILURE
);
169 memset(&ln
, 0, sizeof(struct buf
));
170 memset(&blk
, 0, sizeof(struct buf
));
175 curp
.file
= "<stdin>";
176 curp
.fd
= STDIN_FILENO
;
178 c
= fdesc(&blk
, &ln
, &curp
);
179 if ( ! (FL_IGN_ERRORS
& curp
.fflags
))
182 rc
= -1 == c
? 0 : 1;
185 while (rc
&& *argv
) {
186 c
= ffile(&blk
, &ln
, *argv
, &curp
);
187 if ( ! (FL_IGN_ERRORS
& curp
.fflags
))
190 rc
= -1 == c
? 0 : 1;
195 man_reset(curp
.lastman
);
197 mdoc_reset(curp
.lastmdoc
);
199 curp
.lastmdoc
= NULL
;
208 (*curp
.outfree
)(curp
.outdata
);
210 mdoc_free(curp
.mdoc
);
214 return(rc
? EXIT_SUCCESS
: EXIT_FAILURE
);
222 (void)printf("%s %s\n", progname
, VERSION
);
231 (void)fprintf(stderr
, "usage: %s [-V] [-foption] "
232 "[-mformat] [-Ooption] [-Toutput] "
233 "[-Werr] [file...]\n", progname
);
239 man_init(struct curparse
*curp
)
244 mancb
.man_err
= merr
;
245 mancb
.man_warn
= mwarn
;
247 /* Defaults from mandoc.1. */
249 pflags
= MAN_IGN_MACRO
| MAN_IGN_ESCAPE
| MAN_IGN_CHARS
;
251 if (curp
->fflags
& FL_NIGN_MACRO
)
252 pflags
&= ~MAN_IGN_MACRO
;
253 if (curp
->fflags
& FL_NIGN_CHARS
)
254 pflags
&= ~MAN_IGN_CHARS
;
255 if (curp
->fflags
& FL_NIGN_ESCAPE
)
256 pflags
&= ~MAN_IGN_ESCAPE
;
258 return(man_alloc(curp
, pflags
, &mancb
));
263 mdoc_init(struct curparse
*curp
)
266 struct mdoc_cb mdoccb
;
268 mdoccb
.mdoc_err
= merr
;
269 mdoccb
.mdoc_warn
= mwarn
;
271 /* Defaults from mandoc.1. */
273 pflags
= MDOC_IGN_MACRO
| MDOC_IGN_ESCAPE
| MDOC_IGN_CHARS
;
275 if (curp
->fflags
& FL_IGN_SCOPE
)
276 pflags
|= MDOC_IGN_SCOPE
;
277 if (curp
->fflags
& FL_NIGN_ESCAPE
)
278 pflags
&= ~MDOC_IGN_ESCAPE
;
279 if (curp
->fflags
& FL_NIGN_MACRO
)
280 pflags
&= ~MDOC_IGN_MACRO
;
281 if (curp
->fflags
& FL_NIGN_CHARS
)
282 pflags
&= ~MDOC_IGN_CHARS
;
284 return(mdoc_alloc(curp
, pflags
, &mdoccb
));
289 ffile(struct buf
*blk
, struct buf
*ln
,
290 const char *file
, struct curparse
*curp
)
295 if (-1 == (curp
->fd
= open(curp
->file
, O_RDONLY
, 0))) {
300 c
= fdesc(blk
, ln
, curp
);
302 if (-1 == close(curp
->fd
))
310 fdesc(struct buf
*blk
, struct buf
*ln
, struct curparse
*curp
)
315 int j
, i
, pos
, lnn
, comment
;
324 * Two buffers: ln and buf. buf is the input buffer optimised
325 * here for each file's block size. ln is a line buffer. Both
326 * growable, hence passed in by ptr-ptr.
329 if (-1 == fstat(curp
->fd
, &st
))
331 else if ((size_t)st
.st_blksize
> sz
)
335 blk
->buf
= realloc(blk
->buf
, sz
);
336 if (NULL
== blk
->buf
) {
343 /* Fill buf with file blocksize. */
345 for (lnn
= pos
= comment
= 0; ; ) {
346 if (-1 == (ssz
= read(curp
->fd
, blk
->buf
, sz
))) {
352 /* Parse the read block into partial or full lines. */
354 for (i
= 0; i
< (int)ssz
; i
++) {
355 if (pos
>= (int)ln
->sz
) {
356 ln
->sz
+= 256; /* Step-size. */
357 ln
->buf
= realloc(ln
->buf
, ln
->sz
);
358 if (NULL
== ln
->buf
) {
360 return(EXIT_FAILURE
);
364 if ('\n' != blk
->buf
[i
]) {
367 ln
->buf
[pos
++] = blk
->buf
[i
];
369 /* Handle in-line `\"' comments. */
371 if (1 == pos
|| '\"' != ln
->buf
[pos
- 1])
374 for (j
= pos
- 2; j
>= 0; j
--)
375 if ('\\' != ln
->buf
[j
])
378 if ( ! ((pos
- 2 - j
) % 2))
383 for (; pos
> 0; --pos
) {
384 if (ln
->buf
[pos
] != ' ')
386 if (ln
->buf
[pos
- 1] == '\\')
392 /* Handle escaped `\\n' newlines. */
394 if (pos
> 0 && 0 == comment
&&
395 '\\' == ln
->buf
[pos
- 1]) {
396 for (j
= pos
- 1; j
>= 0; j
--)
397 if ('\\' != ln
->buf
[j
])
399 if ( ! ((pos
- j
) % 2)) {
409 /* If unset, assign parser in pset(). */
411 if ( ! (man
|| mdoc
) && ! pset(ln
->buf
,
412 pos
, curp
, &man
, &mdoc
))
417 /* Pass down into parsers. */
419 if (man
&& ! man_parseln(man
, lnn
, ln
->buf
))
421 if (mdoc
&& ! mdoc_parseln(mdoc
, lnn
, ln
->buf
))
426 /* NOTE a parser may not have been assigned, yet. */
428 if ( ! (man
|| mdoc
)) {
429 fprintf(stderr
, "%s: Not a manual\n", curp
->file
);
433 if (mdoc
&& ! mdoc_endparse(mdoc
))
435 if (man
&& ! man_endparse(man
))
438 /* If unset, allocate output dev now (if applicable). */
440 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
441 switch (curp
->outtype
) {
443 curp
->outdata
= xhtml_alloc(curp
->outopts
);
444 curp
->outman
= html_man
;
445 curp
->outmdoc
= html_mdoc
;
446 curp
->outfree
= html_free
;
449 curp
->outdata
= html_alloc(curp
->outopts
);
450 curp
->outman
= html_man
;
451 curp
->outmdoc
= html_mdoc
;
452 curp
->outfree
= html_free
;
455 curp
->outman
= tree_man
;
456 curp
->outmdoc
= tree_mdoc
;
461 curp
->outdata
= ascii_alloc();
462 curp
->outman
= terminal_man
;
463 curp
->outmdoc
= terminal_mdoc
;
464 curp
->outfree
= terminal_free
;
469 /* Execute the out device, if it exists. */
471 if (man
&& curp
->outman
)
472 (*curp
->outman
)(curp
->outdata
, man
);
473 if (mdoc
&& curp
->outmdoc
)
474 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
481 pset(const char *buf
, int pos
, struct curparse
*curp
,
482 struct man
**man
, struct mdoc
**mdoc
)
487 * Try to intuit which kind of manual parser should be used. If
488 * passed in by command-line (-man, -mdoc), then use that
489 * explicitly. If passed as -mandoc, then try to guess from the
490 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
491 * default to -man, which is more lenient.
495 for (i
= 1; buf
[i
]; i
++)
496 if (' ' != buf
[i
] && '\t' != buf
[i
])
502 switch (curp
->inttype
) {
504 if (NULL
== curp
->mdoc
)
505 curp
->mdoc
= mdoc_init(curp
);
506 if (NULL
== (*mdoc
= curp
->mdoc
))
508 curp
->lastmdoc
= *mdoc
;
511 if (NULL
== curp
->man
)
512 curp
->man
= man_init(curp
);
513 if (NULL
== (*man
= curp
->man
))
515 curp
->lastman
= *man
;
521 if (pos
>= 3 && 0 == memcmp(buf
, ".Dd", 3)) {
522 if (NULL
== curp
->mdoc
)
523 curp
->mdoc
= mdoc_init(curp
);
524 if (NULL
== (*mdoc
= curp
->mdoc
))
526 curp
->lastmdoc
= *mdoc
;
530 if (NULL
== curp
->man
)
531 curp
->man
= man_init(curp
);
532 if (NULL
== (*man
= curp
->man
))
534 curp
->lastman
= *man
;
540 moptions(enum intt
*tflags
, char *arg
)
543 if (0 == strcmp(arg
, "doc"))
545 else if (0 == strcmp(arg
, "andoc"))
547 else if (0 == strcmp(arg
, "an"))
550 fprintf(stderr
, "%s: Bad argument\n", arg
);
559 toptions(struct curparse
*curp
, char *arg
)
562 if (0 == strcmp(arg
, "ascii"))
563 curp
->outtype
= OUTT_ASCII
;
564 else if (0 == strcmp(arg
, "lint")) {
565 curp
->outtype
= OUTT_LINT
;
566 curp
->wflags
|= WARN_WALL
;
567 curp
->fflags
|= FL_STRICT
;
569 else if (0 == strcmp(arg
, "tree"))
570 curp
->outtype
= OUTT_TREE
;
571 else if (0 == strcmp(arg
, "html"))
572 curp
->outtype
= OUTT_HTML
;
573 else if (0 == strcmp(arg
, "xhtml"))
574 curp
->outtype
= OUTT_XHTML
;
576 fprintf(stderr
, "%s: Bad argument\n", arg
);
585 foptions(int *fflags
, char *arg
)
590 toks
[0] = "ign-scope";
591 toks
[1] = "no-ign-escape";
592 toks
[2] = "no-ign-macro";
593 toks
[3] = "no-ign-chars";
594 toks
[4] = "ign-errors";
596 toks
[6] = "ign-escape";
601 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
603 *fflags
|= FL_IGN_SCOPE
;
606 *fflags
|= FL_NIGN_ESCAPE
;
609 *fflags
|= FL_NIGN_MACRO
;
612 *fflags
|= FL_NIGN_CHARS
;
615 *fflags
|= FL_IGN_ERRORS
;
618 *fflags
|= FL_STRICT
;
621 *fflags
&= ~FL_NIGN_ESCAPE
;
624 fprintf(stderr
, "%s: Bad argument\n", o
);
634 woptions(int *wflags
, char *arg
)
645 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
647 *wflags
|= WARN_WALL
;
650 *wflags
|= WARN_WERR
;
653 fprintf(stderr
, "%s: Bad argument\n", o
);
664 merr(void *arg
, int line
, int col
, const char *msg
)
666 struct curparse
*curp
;
668 curp
= (struct curparse
*)arg
;
670 (void)fprintf(stderr
, "%s:%d:%d: error: %s\n",
671 curp
->file
, line
, col
+ 1, msg
);
678 mwarn(void *arg
, int line
, int col
, const char *msg
)
680 struct curparse
*curp
;
682 curp
= (struct curparse
*)arg
;
684 if ( ! (curp
->wflags
& WARN_WALL
))
687 (void)fprintf(stderr
, "%s:%d:%d: warning: %s\n",
688 curp
->file
, line
, col
+ 1, msg
);
690 if ( ! (curp
->wflags
& WARN_WERR
))