]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.54 2009/10/31 06:17:19 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.
31 #define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
33 /* Account for FreeBSD and Linux in our declarations. */
36 extern int getsubopt(char **, char * const *, char **);
37 extern size_t strlcat(char *, const char *, size_t);
39 # define __dead __attribute__((__noreturn__))
41 #elif defined(__dead2)
43 # define __dead __dead2
47 typedef void (*out_mdoc
)(void *, const struct mdoc
*);
48 typedef void (*out_man
)(void *, const struct man
*);
49 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 IGN_SCOPE (1 << 0) /* Ignore scope errors. */
77 #define NO_IGN_ESCAPE (1 << 1) /* Don't ignore bad escapes. */
78 #define NO_IGN_MACRO (1 << 2) /* Don't ignore bad macros. */
79 #define NO_IGN_CHARS (1 << 3) /* Don't ignore bad chars. */
80 #define IGN_ERRORS (1 << 4) /* Ignore failed parse. */
81 enum intt inttype
; /* Input parsers... */
85 struct mdoc
*lastmdoc
;
86 enum outt outtype
; /* Output devices... */
94 static int foptions(int *, char *);
95 static int toptions(enum outt
*, char *);
96 static int moptions(enum intt
*, char *);
97 static int woptions(int *, char *);
98 static int merr(void *, int, int, const char *);
99 static int mwarn(void *, int, int, const char *);
100 static int ffile(struct buf
*, struct buf
*,
101 const char *, struct curparse
*);
102 static int fdesc(struct buf
*, struct buf
*,
104 static int pset(const char *, int, struct curparse
*,
105 struct man
**, struct mdoc
**);
106 static struct man
*man_init(struct curparse
*);
107 static struct mdoc
*mdoc_init(struct curparse
*);
108 __dead
static void version(void);
109 __dead
static void usage(void);
111 static const char *progname
;
115 main(int argc
, char *argv
[])
119 struct curparse curp
;
121 progname
= strrchr(argv
[0], '/');
122 if (progname
== NULL
)
127 memset(&curp
, 0, sizeof(struct curparse
));
129 curp
.inttype
= INTT_AUTO
;
130 curp
.outtype
= OUTT_ASCII
;
133 while (-1 != (c
= getopt(argc
, argv
, "f:m:O:T:VW:")))
136 if ( ! foptions(&curp
.fflags
, optarg
))
137 return(EXIT_FAILURE
);
140 if ( ! moptions(&curp
.inttype
, optarg
))
141 return(EXIT_FAILURE
);
144 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
145 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
148 if ( ! toptions(&curp
.outtype
, optarg
))
149 return(EXIT_FAILURE
);
152 if ( ! woptions(&curp
.wflags
, optarg
))
153 return(EXIT_FAILURE
);
166 memset(&ln
, 0, sizeof(struct buf
));
167 memset(&blk
, 0, sizeof(struct buf
));
172 curp
.file
= "<stdin>";
173 curp
.fd
= STDIN_FILENO
;
175 c
= fdesc(&blk
, &ln
, &curp
);
176 if ( ! (IGN_ERRORS
& curp
.fflags
))
179 rc
= -1 == c
? 0 : 1;
182 while (rc
&& *argv
) {
183 c
= ffile(&blk
, &ln
, *argv
, &curp
);
184 if ( ! (IGN_ERRORS
& curp
.fflags
))
187 rc
= -1 == c
? 0 : 1;
192 man_reset(curp
.lastman
);
194 mdoc_reset(curp
.lastmdoc
);
196 curp
.lastmdoc
= NULL
;
205 (*curp
.outfree
)(curp
.outdata
);
207 mdoc_free(curp
.mdoc
);
211 return(rc
? EXIT_SUCCESS
: EXIT_FAILURE
);
219 (void)printf("%s %s\n", progname
, VERSION
);
228 (void)fprintf(stderr
, "usage: %s [-V] [-foption...] "
229 "[-mformat] [-Ooption] [-Toutput] "
230 "[-Werr...]\n", progname
);
236 man_init(struct curparse
*curp
)
241 mancb
.man_err
= merr
;
242 mancb
.man_warn
= mwarn
;
244 /* Defaults from mandoc.1. */
246 pflags
= MAN_IGN_MACRO
| MAN_IGN_ESCAPE
| MAN_IGN_CHARS
;
248 if (curp
->fflags
& NO_IGN_MACRO
)
249 pflags
&= ~MAN_IGN_MACRO
;
250 if (curp
->fflags
& NO_IGN_CHARS
)
251 pflags
&= ~MAN_IGN_CHARS
;
252 if (curp
->fflags
& NO_IGN_ESCAPE
)
253 pflags
&= ~MAN_IGN_ESCAPE
;
255 return(man_alloc(curp
, pflags
, &mancb
));
260 mdoc_init(struct curparse
*curp
)
263 struct mdoc_cb mdoccb
;
265 mdoccb
.mdoc_err
= merr
;
266 mdoccb
.mdoc_warn
= mwarn
;
268 /* Defaults from mandoc.1. */
270 pflags
= MDOC_IGN_MACRO
| MDOC_IGN_ESCAPE
| MDOC_IGN_CHARS
;
272 if (curp
->fflags
& IGN_SCOPE
)
273 pflags
|= MDOC_IGN_SCOPE
;
274 if (curp
->fflags
& NO_IGN_ESCAPE
)
275 pflags
&= ~MDOC_IGN_ESCAPE
;
276 if (curp
->fflags
& NO_IGN_MACRO
)
277 pflags
&= ~MDOC_IGN_MACRO
;
278 if (curp
->fflags
& NO_IGN_CHARS
)
279 pflags
&= ~MDOC_IGN_CHARS
;
281 return(mdoc_alloc(curp
, pflags
, &mdoccb
));
286 ffile(struct buf
*blk
, struct buf
*ln
,
287 const char *file
, struct curparse
*curp
)
292 if (-1 == (curp
->fd
= open(curp
->file
, O_RDONLY
, 0))) {
297 c
= fdesc(blk
, ln
, curp
);
299 if (-1 == close(curp
->fd
))
307 fdesc(struct buf
*blk
, struct buf
*ln
, struct curparse
*curp
)
312 int j
, i
, pos
, lnn
, comment
;
321 * Two buffers: ln and buf. buf is the input buffer optimised
322 * here for each file's block size. ln is a line buffer. Both
323 * growable, hence passed in by ptr-ptr.
326 if (-1 == fstat(curp
->fd
, &st
))
328 else if ((size_t)st
.st_blksize
> sz
)
332 blk
->buf
= realloc(blk
->buf
, sz
);
333 if (NULL
== blk
->buf
) {
340 /* Fill buf with file blocksize. */
342 for (lnn
= pos
= comment
= 0; ; ) {
343 if (-1 == (ssz
= read(curp
->fd
, blk
->buf
, sz
))) {
349 /* Parse the read block into partial or full lines. */
351 for (i
= 0; i
< (int)ssz
; i
++) {
352 if (pos
>= (int)ln
->sz
) {
353 ln
->sz
+= 256; /* Step-size. */
354 ln
->buf
= realloc(ln
->buf
, ln
->sz
);
355 if (NULL
== ln
->buf
) {
357 return(EXIT_FAILURE
);
361 if ('\n' != blk
->buf
[i
]) {
364 ln
->buf
[pos
++] = blk
->buf
[i
];
366 /* Handle in-line `\"' comments. */
368 if (1 == pos
|| '\"' != ln
->buf
[pos
- 1])
371 for (j
= pos
- 2; j
>= 0; j
--)
372 if ('\\' != ln
->buf
[j
])
375 if ( ! ((pos
- 2 - j
) % 2))
383 /* Handle escaped `\\n' newlines. */
385 if (pos
> 0 && 0 == comment
&&
386 '\\' == ln
->buf
[pos
- 1]) {
387 for (j
= pos
- 1; j
>= 0; j
--)
388 if ('\\' != ln
->buf
[j
])
390 if ( ! ((pos
- j
) % 2)) {
400 /* If unset, assign parser in pset(). */
402 if ( ! (man
|| mdoc
) && ! pset(ln
->buf
,
403 pos
, curp
, &man
, &mdoc
))
408 /* Pass down into parsers. */
410 if (man
&& ! man_parseln(man
, lnn
, ln
->buf
))
412 if (mdoc
&& ! mdoc_parseln(mdoc
, lnn
, ln
->buf
))
417 /* NOTE a parser may not have been assigned, yet. */
419 if ( ! (man
|| mdoc
)) {
420 fprintf(stderr
, "%s: Not a manual\n", curp
->file
);
424 if (mdoc
&& ! mdoc_endparse(mdoc
))
426 if (man
&& ! man_endparse(man
))
429 /* If unset, allocate output dev now (if applicable). */
431 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
432 switch (curp
->outtype
) {
434 curp
->outdata
= html_alloc(curp
->outopts
);
435 curp
->outman
= html_man
;
436 curp
->outmdoc
= html_mdoc
;
437 curp
->outfree
= html_free
;
440 curp
->outman
= tree_man
;
441 curp
->outmdoc
= tree_mdoc
;
446 curp
->outdata
= ascii_alloc();
447 curp
->outman
= terminal_man
;
448 curp
->outmdoc
= terminal_mdoc
;
449 curp
->outfree
= terminal_free
;
454 /* Execute the out device, if it exists. */
456 if (man
&& curp
->outman
)
457 (*curp
->outman
)(curp
->outdata
, man
);
458 if (mdoc
&& curp
->outmdoc
)
459 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
466 pset(const char *buf
, int pos
, struct curparse
*curp
,
467 struct man
**man
, struct mdoc
**mdoc
)
472 * Try to intuit which kind of manual parser should be used. If
473 * passed in by command-line (-man, -mdoc), then use that
474 * explicitly. If passed as -mandoc, then try to guess from the
475 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
476 * default to -man, which is more lenient.
480 for (i
= 1; buf
[i
]; i
++)
481 if (' ' != buf
[i
] && '\t' != buf
[i
])
487 switch (curp
->inttype
) {
489 if (NULL
== curp
->mdoc
)
490 curp
->mdoc
= mdoc_init(curp
);
491 if (NULL
== (*mdoc
= curp
->mdoc
))
493 curp
->lastmdoc
= *mdoc
;
496 if (NULL
== curp
->man
)
497 curp
->man
= man_init(curp
);
498 if (NULL
== (*man
= curp
->man
))
500 curp
->lastman
= *man
;
506 if (pos
>= 3 && 0 == memcmp(buf
, ".Dd", 3)) {
507 if (NULL
== curp
->mdoc
)
508 curp
->mdoc
= mdoc_init(curp
);
509 if (NULL
== (*mdoc
= curp
->mdoc
))
511 curp
->lastmdoc
= *mdoc
;
515 if (NULL
== curp
->man
)
516 curp
->man
= man_init(curp
);
517 if (NULL
== (*man
= curp
->man
))
519 curp
->lastman
= *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", arg
);
544 toptions(enum outt
*tflags
, char *arg
)
547 if (0 == strcmp(arg
, "ascii"))
548 *tflags
= OUTT_ASCII
;
549 else if (0 == strcmp(arg
, "lint"))
551 else if (0 == strcmp(arg
, "tree"))
553 else if (0 == strcmp(arg
, "html"))
556 fprintf(stderr
, "%s: Bad argument", arg
);
565 foptions(int *fflags
, char *arg
)
570 toks
[0] = "ign-scope";
571 toks
[1] = "no-ign-escape";
572 toks
[2] = "no-ign-macro";
573 toks
[3] = "no-ign-chars";
574 toks
[4] = "ign-errors";
576 toks
[6] = "ign-escape";
581 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
583 *fflags
|= IGN_SCOPE
;
586 *fflags
|= NO_IGN_ESCAPE
;
589 *fflags
|= NO_IGN_MACRO
;
592 *fflags
|= NO_IGN_CHARS
;
595 *fflags
|= IGN_ERRORS
;
598 *fflags
|= NO_IGN_ESCAPE
|
599 NO_IGN_MACRO
| NO_IGN_CHARS
;
602 *fflags
&= ~NO_IGN_ESCAPE
;
605 fprintf(stderr
, "%s: Bad argument", 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", 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
);
659 mwarn(void *arg
, int line
, int col
, const char *msg
)
661 struct curparse
*curp
;
663 curp
= (struct curparse
*)arg
;
665 if ( ! (curp
->wflags
& WARN_WALL
))
668 (void)fprintf(stderr
, "%s:%d:%d: warning: %s\n",
669 curp
->file
, line
, col
+ 1, msg
);
671 if ( ! (curp
->wflags
& WARN_WERR
))