]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.62 2010/05/09 21:19:42 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... */
83 struct mdoc
*lastmdoc
;
84 enum outt outtype
; /* Output devices... */
92 #define FL_STRICT FL_NIGN_ESCAPE | \
95 static int foptions(int *, char *);
96 static int toptions(struct curparse
*, char *);
97 static int moptions(enum intt
*, char *);
98 static int woptions(int *, char *);
99 static int merr(void *, int, int, const char *);
100 static int mwarn(void *, int, int, const char *);
101 static int ffile(struct buf
*, struct buf
*,
102 const char *, struct curparse
*);
103 static int fdesc(struct buf
*, struct buf
*,
105 static int pset(const char *, int, struct curparse
*,
106 struct man
**, struct mdoc
**);
107 static struct man
*man_init(struct curparse
*);
108 static struct mdoc
*mdoc_init(struct curparse
*);
109 static void version(void) __attribute__((noreturn
));
110 static void usage(void) __attribute__((noreturn
));
112 static const char *progname
;
116 main(int argc
, char *argv
[])
120 struct curparse curp
;
122 progname
= strrchr(argv
[0], '/');
123 if (progname
== NULL
)
128 memset(&curp
, 0, sizeof(struct curparse
));
130 curp
.inttype
= INTT_AUTO
;
131 curp
.outtype
= OUTT_ASCII
;
134 while (-1 != (c
= getopt(argc
, argv
, "f:m:O:T:VW:")))
137 if ( ! foptions(&curp
.fflags
, optarg
))
138 return(EXIT_FAILURE
);
141 if ( ! moptions(&curp
.inttype
, optarg
))
142 return(EXIT_FAILURE
);
145 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
146 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
149 if ( ! toptions(&curp
, optarg
))
150 return(EXIT_FAILURE
);
153 if ( ! woptions(&curp
.wflags
, optarg
))
154 return(EXIT_FAILURE
);
167 memset(&ln
, 0, sizeof(struct buf
));
168 memset(&blk
, 0, sizeof(struct buf
));
173 curp
.file
= "<stdin>";
174 curp
.fd
= STDIN_FILENO
;
176 c
= fdesc(&blk
, &ln
, &curp
);
177 if ( ! (FL_IGN_ERRORS
& curp
.fflags
))
180 rc
= -1 == c
? 0 : 1;
183 while (rc
&& *argv
) {
184 c
= ffile(&blk
, &ln
, *argv
, &curp
);
185 if ( ! (FL_IGN_ERRORS
& curp
.fflags
))
188 rc
= -1 == c
? 0 : 1;
193 man_reset(curp
.lastman
);
195 mdoc_reset(curp
.lastmdoc
);
197 curp
.lastmdoc
= NULL
;
206 (*curp
.outfree
)(curp
.outdata
);
208 mdoc_free(curp
.mdoc
);
212 return(rc
? EXIT_SUCCESS
: EXIT_FAILURE
);
220 (void)printf("%s %s\n", progname
, VERSION
);
229 (void)fprintf(stderr
, "usage: %s [-V] [-foption] "
230 "[-mformat] [-Ooption] [-Toutput] "
231 "[-Werr] [file...]\n", progname
);
237 man_init(struct curparse
*curp
)
242 mancb
.man_err
= merr
;
243 mancb
.man_warn
= mwarn
;
245 /* Defaults from mandoc.1. */
247 pflags
= MAN_IGN_MACRO
| MAN_IGN_ESCAPE
;
249 if (curp
->fflags
& FL_NIGN_MACRO
)
250 pflags
&= ~MAN_IGN_MACRO
;
251 if (curp
->fflags
& FL_NIGN_ESCAPE
)
252 pflags
&= ~MAN_IGN_ESCAPE
;
254 return(man_alloc(curp
, pflags
, &mancb
));
259 mdoc_init(struct curparse
*curp
)
262 struct mdoc_cb mdoccb
;
264 mdoccb
.mdoc_err
= merr
;
265 mdoccb
.mdoc_warn
= mwarn
;
267 /* Defaults from mandoc.1. */
269 pflags
= MDOC_IGN_MACRO
| MDOC_IGN_ESCAPE
;
271 if (curp
->fflags
& FL_IGN_SCOPE
)
272 pflags
|= MDOC_IGN_SCOPE
;
273 if (curp
->fflags
& FL_NIGN_ESCAPE
)
274 pflags
&= ~MDOC_IGN_ESCAPE
;
275 if (curp
->fflags
& FL_NIGN_MACRO
)
276 pflags
&= ~MDOC_IGN_MACRO
;
278 return(mdoc_alloc(curp
, pflags
, &mdoccb
));
283 ffile(struct buf
*blk
, struct buf
*ln
,
284 const char *file
, struct curparse
*curp
)
289 if (-1 == (curp
->fd
= open(curp
->file
, O_RDONLY
, 0))) {
294 c
= fdesc(blk
, ln
, curp
);
296 if (-1 == close(curp
->fd
))
304 fdesc(struct buf
*blk
, struct buf
*ln
, struct curparse
*curp
)
309 int j
, i
, pos
, lnn
, comment
;
318 * Two buffers: ln and buf. buf is the input buffer optimised
319 * here for each file's block size. ln is a line buffer. Both
320 * growable, hence passed in by ptr-ptr.
323 if (-1 == fstat(curp
->fd
, &st
))
325 else if ((size_t)st
.st_blksize
> sz
)
329 blk
->buf
= realloc(blk
->buf
, sz
);
330 if (NULL
== blk
->buf
) {
337 /* Fill buf with file blocksize. */
339 for (lnn
= pos
= comment
= 0; ; ) {
340 if (-1 == (ssz
= read(curp
->fd
, blk
->buf
, sz
))) {
346 /* Parse the read block into partial or full lines. */
348 for (i
= 0; i
< (int)ssz
; i
++) {
349 if (pos
>= (int)ln
->sz
) {
350 ln
->sz
+= 256; /* Step-size. */
351 ln
->buf
= realloc(ln
->buf
, ln
->sz
);
352 if (NULL
== ln
->buf
) {
354 return(EXIT_FAILURE
);
358 if ('\n' != blk
->buf
[i
]) {
361 ln
->buf
[pos
++] = blk
->buf
[i
];
363 /* Handle in-line `\"' comments. */
365 if (1 == pos
|| '\"' != ln
->buf
[pos
- 1])
368 for (j
= pos
- 2; j
>= 0; j
--)
369 if ('\\' != ln
->buf
[j
])
372 if ( ! ((pos
- 2 - j
) % 2))
377 for (; pos
> 0; --pos
) {
378 if (ln
->buf
[pos
] != ' ')
380 if (ln
->buf
[pos
- 1] == '\\')
386 /* Handle escaped `\\n' newlines. */
388 if (pos
> 0 && 0 == comment
&&
389 '\\' == ln
->buf
[pos
- 1]) {
390 for (j
= pos
- 1; j
>= 0; j
--)
391 if ('\\' != ln
->buf
[j
])
393 if ( ! ((pos
- j
) % 2)) {
403 /* If unset, assign parser in pset(). */
405 if ( ! (man
|| mdoc
) && ! pset(ln
->buf
,
406 pos
, curp
, &man
, &mdoc
))
411 /* Pass down into parsers. */
413 if (man
&& ! man_parseln(man
, lnn
, ln
->buf
))
415 if (mdoc
&& ! mdoc_parseln(mdoc
, lnn
, ln
->buf
))
420 /* NOTE a parser may not have been assigned, yet. */
422 if ( ! (man
|| mdoc
)) {
423 fprintf(stderr
, "%s: Not a manual\n", curp
->file
);
427 if (mdoc
&& ! mdoc_endparse(mdoc
))
429 if (man
&& ! man_endparse(man
))
432 /* If unset, allocate output dev now (if applicable). */
434 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
435 switch (curp
->outtype
) {
437 curp
->outdata
= xhtml_alloc(curp
->outopts
);
438 curp
->outman
= html_man
;
439 curp
->outmdoc
= html_mdoc
;
440 curp
->outfree
= html_free
;
443 curp
->outdata
= html_alloc(curp
->outopts
);
444 curp
->outman
= html_man
;
445 curp
->outmdoc
= html_mdoc
;
446 curp
->outfree
= html_free
;
449 curp
->outman
= tree_man
;
450 curp
->outmdoc
= tree_mdoc
;
455 curp
->outdata
= ascii_alloc();
456 curp
->outman
= terminal_man
;
457 curp
->outmdoc
= terminal_mdoc
;
458 curp
->outfree
= terminal_free
;
463 /* Execute the out device, if it exists. */
465 if (man
&& curp
->outman
)
466 (*curp
->outman
)(curp
->outdata
, man
);
467 if (mdoc
&& curp
->outmdoc
)
468 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
475 pset(const char *buf
, int pos
, struct curparse
*curp
,
476 struct man
**man
, struct mdoc
**mdoc
)
481 * Try to intuit which kind of manual parser should be used. If
482 * passed in by command-line (-man, -mdoc), then use that
483 * explicitly. If passed as -mandoc, then try to guess from the
484 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
485 * default to -man, which is more lenient.
489 for (i
= 1; buf
[i
]; i
++)
490 if (' ' != buf
[i
] && '\t' != buf
[i
])
496 switch (curp
->inttype
) {
498 if (NULL
== curp
->mdoc
)
499 curp
->mdoc
= mdoc_init(curp
);
500 if (NULL
== (*mdoc
= curp
->mdoc
))
502 curp
->lastmdoc
= *mdoc
;
505 if (NULL
== curp
->man
)
506 curp
->man
= man_init(curp
);
507 if (NULL
== (*man
= curp
->man
))
509 curp
->lastman
= *man
;
515 if (pos
>= 3 && 0 == memcmp(buf
, ".Dd", 3)) {
516 if (NULL
== curp
->mdoc
)
517 curp
->mdoc
= mdoc_init(curp
);
518 if (NULL
== (*mdoc
= curp
->mdoc
))
520 curp
->lastmdoc
= *mdoc
;
524 if (NULL
== curp
->man
)
525 curp
->man
= man_init(curp
);
526 if (NULL
== (*man
= curp
->man
))
528 curp
->lastman
= *man
;
534 moptions(enum intt
*tflags
, char *arg
)
537 if (0 == strcmp(arg
, "doc"))
539 else if (0 == strcmp(arg
, "andoc"))
541 else if (0 == strcmp(arg
, "an"))
544 fprintf(stderr
, "%s: Bad argument\n", arg
);
553 toptions(struct curparse
*curp
, char *arg
)
556 if (0 == strcmp(arg
, "ascii"))
557 curp
->outtype
= OUTT_ASCII
;
558 else if (0 == strcmp(arg
, "lint")) {
559 curp
->outtype
= OUTT_LINT
;
560 curp
->wflags
|= WARN_WALL
;
561 curp
->fflags
|= FL_STRICT
;
563 else if (0 == strcmp(arg
, "tree"))
564 curp
->outtype
= OUTT_TREE
;
565 else if (0 == strcmp(arg
, "html"))
566 curp
->outtype
= OUTT_HTML
;
567 else if (0 == strcmp(arg
, "xhtml"))
568 curp
->outtype
= OUTT_XHTML
;
570 fprintf(stderr
, "%s: Bad argument\n", arg
);
579 foptions(int *fflags
, char *arg
)
584 toks
[0] = "ign-scope";
585 toks
[1] = "no-ign-escape";
586 toks
[2] = "no-ign-macro";
587 toks
[3] = "ign-errors";
589 toks
[5] = "ign-escape";
594 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
596 *fflags
|= FL_IGN_SCOPE
;
599 *fflags
|= FL_NIGN_ESCAPE
;
602 *fflags
|= FL_NIGN_MACRO
;
605 *fflags
|= FL_IGN_ERRORS
;
608 *fflags
|= FL_STRICT
;
611 *fflags
&= ~FL_NIGN_ESCAPE
;
614 fprintf(stderr
, "%s: Bad argument\n", o
);
624 woptions(int *wflags
, char *arg
)
635 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
637 *wflags
|= WARN_WALL
;
640 *wflags
|= WARN_WERR
;
643 fprintf(stderr
, "%s: Bad argument\n", o
);
654 merr(void *arg
, int line
, int col
, const char *msg
)
656 struct curparse
*curp
;
658 curp
= (struct curparse
*)arg
;
660 (void)fprintf(stderr
, "%s:%d:%d: error: %s\n",
661 curp
->file
, line
, col
+ 1, msg
);
668 mwarn(void *arg
, int line
, int col
, const char *msg
)
670 struct curparse
*curp
;
672 curp
= (struct curparse
*)arg
;
674 if ( ! (curp
->wflags
& WARN_WALL
))
677 (void)fprintf(stderr
, "%s:%d:%d: warning: %s\n",
678 curp
->file
, line
, col
+ 1, msg
);
680 if ( ! (curp
->wflags
& WARN_WERR
))