]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.45 2009/10/13 10:21:24 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 **);
38 # define __dead __attribute__((__noreturn__))
40 #elif defined(__dead2)
42 # define __dead __dead2
46 typedef void (*out_mdoc
)(void *, const struct mdoc
*);
47 typedef void (*out_man
)(void *, const struct man
*);
48 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 IGN_SCOPE (1 << 0) /* Ignore scope errors. */
76 #define NO_IGN_ESCAPE (1 << 1) /* Don't ignore bad escapes. */
77 #define NO_IGN_MACRO (1 << 2) /* Don't ignore bad macros. */
78 #define NO_IGN_CHARS (1 << 3) /* Don't ignore bad chars. */
79 #define IGN_ERRORS (1 << 4) /* Ignore failed parse. */
80 enum intt inttype
; /* Input parsers... */
84 struct mdoc
*lastmdoc
;
85 enum outt outtype
; /* Output devices... */
93 extern void *html_alloc(char *);
94 extern void html_mdoc(void *, const struct mdoc
*);
95 extern void html_man(void *, const struct man
*);
96 extern void html_free(void *);
97 extern void *ascii_alloc(void);
98 extern void tree_mdoc(void *, const struct mdoc
*);
99 extern void tree_man(void *, const struct man
*);
100 extern void terminal_mdoc(void *, const struct mdoc
*);
101 extern void terminal_man(void *, const struct man
*);
102 extern void terminal_free(void *);
104 static int foptions(int *, char *);
105 static int toptions(enum outt
*, char *);
106 static int moptions(enum intt
*, char *);
107 static int woptions(int *, char *);
108 static int merr(void *, int, int, const char *);
109 static int mwarn(void *, int, int, const char *);
110 static int ffile(struct buf
*, struct buf
*,
111 const char *, struct curparse
*);
112 static int fdesc(struct buf
*, struct buf
*,
114 static int pset(const char *, int, struct curparse
*,
115 struct man
**, struct mdoc
**);
116 static struct man
*man_init(struct curparse
*);
117 static struct mdoc
*mdoc_init(struct curparse
*);
118 __dead
static void version(void);
119 __dead
static void usage(void);
121 extern char *__progname
;
125 main(int argc
, char *argv
[])
129 struct curparse curp
;
131 bzero(&curp
, sizeof(struct curparse
));
133 curp
.inttype
= INTT_AUTO
;
134 curp
.outtype
= OUTT_ASCII
;
137 while (-1 != (c
= getopt(argc
, argv
, "f:m:o:T:VW:")))
140 if ( ! foptions(&curp
.fflags
, optarg
))
141 return(EXIT_FAILURE
);
144 if ( ! moptions(&curp
.inttype
, optarg
))
145 return(EXIT_FAILURE
);
148 curp
.outopts
= optarg
;
151 if ( ! toptions(&curp
.outtype
, optarg
))
152 return(EXIT_FAILURE
);
155 if ( ! woptions(&curp
.wflags
, optarg
))
156 return(EXIT_FAILURE
);
169 bzero(&ln
, sizeof(struct buf
));
170 bzero(&blk
, sizeof(struct buf
));
175 curp
.file
= "<stdin>";
176 curp
.fd
= STDIN_FILENO
;
178 c
= fdesc(&blk
, &ln
, &curp
);
179 if ( ! (IGN_ERRORS
& curp
.fflags
))
182 rc
= -1 == c
? 0 : 1;
185 while (rc
&& *argv
) {
186 c
= ffile(&blk
, &ln
, *argv
, &curp
);
187 if ( ! (IGN_ERRORS
& curp
.fflags
))
190 rc
= -1 == c
? 0 : 1;
195 if ( ! man_reset(curp
.lastman
))
198 if ( ! mdoc_reset(curp
.lastmdoc
))
201 curp
.lastmdoc
= NULL
;
210 (*curp
.outfree
)(curp
.outdata
);
212 mdoc_free(curp
.mdoc
);
216 return(rc
? EXIT_SUCCESS
: EXIT_FAILURE
);
224 (void)printf("%s %s\n", __progname
, VERSION
);
233 (void)fprintf(stderr
, "usage: %s [-V] [-foption...] "
234 "[-mformat] [-Toutput] [-Werr...]\n",
241 man_init(struct curparse
*curp
)
247 mancb
.man_err
= merr
;
248 mancb
.man_warn
= mwarn
;
250 /* Defaults from mandoc.1. */
252 pflags
= MAN_IGN_MACRO
| MAN_IGN_ESCAPE
| MAN_IGN_CHARS
;
254 if (curp
->fflags
& NO_IGN_MACRO
)
255 pflags
&= ~MAN_IGN_MACRO
;
256 if (curp
->fflags
& NO_IGN_CHARS
)
257 pflags
&= ~MAN_IGN_CHARS
;
258 if (curp
->fflags
& NO_IGN_ESCAPE
)
259 pflags
&= ~MAN_IGN_ESCAPE
;
261 if (NULL
== (man
= man_alloc(curp
, pflags
, &mancb
)))
262 warnx("memory exhausted");
269 mdoc_init(struct curparse
*curp
)
273 struct mdoc_cb mdoccb
;
275 mdoccb
.mdoc_err
= merr
;
276 mdoccb
.mdoc_warn
= mwarn
;
278 /* Defaults from mandoc.1. */
280 pflags
= MDOC_IGN_MACRO
| MDOC_IGN_ESCAPE
| MDOC_IGN_CHARS
;
282 if (curp
->fflags
& IGN_SCOPE
)
283 pflags
|= MDOC_IGN_SCOPE
;
284 if (curp
->fflags
& NO_IGN_ESCAPE
)
285 pflags
&= ~MDOC_IGN_ESCAPE
;
286 if (curp
->fflags
& NO_IGN_MACRO
)
287 pflags
&= ~MDOC_IGN_MACRO
;
288 if (curp
->fflags
& NO_IGN_CHARS
)
289 pflags
&= ~MDOC_IGN_CHARS
;
291 if (NULL
== (mdoc
= mdoc_alloc(curp
, pflags
, &mdoccb
)))
292 warnx("memory exhausted");
299 ffile(struct buf
*blk
, struct buf
*ln
,
300 const char *file
, struct curparse
*curp
)
305 if (-1 == (curp
->fd
= open(curp
->file
, O_RDONLY
, 0))) {
306 warn("%s", curp
->file
);
310 c
= fdesc(blk
, ln
, curp
);
312 if (-1 == close(curp
->fd
))
313 warn("%s", curp
->file
);
320 fdesc(struct buf
*blk
, struct buf
*ln
, struct curparse
*curp
)
325 int j
, i
, pos
, lnn
, comment
;
334 * Two buffers: ln and buf. buf is the input buffer optimised
335 * here for each file's block size. ln is a line buffer. Both
336 * growable, hence passed in by ptr-ptr.
339 if (-1 == fstat(curp
->fd
, &st
))
340 warn("%s", curp
->file
);
341 else if ((size_t)st
.st_blksize
> sz
)
345 blk
->buf
= realloc(blk
->buf
, sz
);
346 if (NULL
== blk
->buf
) {
353 /* Fill buf with file blocksize. */
355 for (lnn
= pos
= comment
= 0; ; ) {
356 if (-1 == (ssz
= read(curp
->fd
, blk
->buf
, sz
))) {
357 warn("%s", curp
->file
);
362 /* Parse the read block into partial or full lines. */
364 for (i
= 0; i
< (int)ssz
; i
++) {
365 if (pos
>= (int)ln
->sz
) {
366 ln
->sz
+= 256; /* Step-size. */
367 ln
->buf
= realloc(ln
->buf
, ln
->sz
);
368 if (NULL
== ln
->buf
) {
374 if ('\n' != blk
->buf
[i
]) {
377 ln
->buf
[pos
++] = blk
->buf
[i
];
379 /* Handle in-line `\"' comments. */
381 if (1 == pos
|| '\"' != ln
->buf
[pos
- 1])
384 for (j
= pos
- 2; j
>= 0; j
--)
385 if ('\\' != ln
->buf
[j
])
388 if ( ! ((pos
- 2 - j
) % 2))
396 /* Handle escaped `\\n' newlines. */
398 if (pos
> 0 && 0 == comment
&&
399 '\\' == ln
->buf
[pos
- 1]) {
400 for (j
= pos
- 1; j
>= 0; j
--)
401 if ('\\' != ln
->buf
[j
])
403 if ( ! ((pos
- j
) % 2)) {
413 /* If unset, assign parser in pset(). */
415 if ( ! (man
|| mdoc
) && ! pset(ln
->buf
,
416 pos
, curp
, &man
, &mdoc
))
421 /* Pass down into parsers. */
423 if (man
&& ! man_parseln(man
, lnn
, ln
->buf
))
425 if (mdoc
&& ! mdoc_parseln(mdoc
, lnn
, ln
->buf
))
430 /* NOTE a parser may not have been assigned, yet. */
432 if ( ! (man
|| mdoc
)) {
433 (void)fprintf(stderr
, "%s: not a manual\n",
438 if (mdoc
&& ! mdoc_endparse(mdoc
))
440 if (man
&& ! man_endparse(man
))
443 /* If unset, allocate output dev now (if applicable). */
445 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
446 switch (curp
->outtype
) {
448 curp
->outdata
= html_alloc(curp
->outopts
);
449 curp
->outman
= html_man
;
450 curp
->outmdoc
= html_mdoc
;
451 curp
->outfree
= html_free
;
454 curp
->outman
= tree_man
;
455 curp
->outmdoc
= tree_mdoc
;
460 curp
->outdata
= ascii_alloc();
461 curp
->outman
= terminal_man
;
462 curp
->outmdoc
= terminal_mdoc
;
463 curp
->outfree
= terminal_free
;
468 /* Execute the out device, if it exists. */
470 if (man
&& curp
->outman
)
471 (*curp
->outman
)(curp
->outdata
, man
);
472 if (mdoc
&& curp
->outmdoc
)
473 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
480 pset(const char *buf
, int pos
, struct curparse
*curp
,
481 struct man
**man
, struct mdoc
**mdoc
)
486 * Try to intuit which kind of manual parser should be used. If
487 * passed in by command-line (-man, -mdoc), then use that
488 * explicitly. If passed as -mandoc, then try to guess from the
489 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
490 * default to -man, which is more lenient.
494 for (i
= 1; buf
[i
]; i
++)
495 if (' ' != buf
[i
] && '\t' != buf
[i
])
501 switch (curp
->inttype
) {
503 if (NULL
== curp
->mdoc
)
504 curp
->mdoc
= mdoc_init(curp
);
505 if (NULL
== (*mdoc
= curp
->mdoc
))
507 curp
->lastmdoc
= *mdoc
;
510 if (NULL
== curp
->man
)
511 curp
->man
= man_init(curp
);
512 if (NULL
== (*man
= curp
->man
))
514 curp
->lastman
= *man
;
520 if (pos
>= 3 && 0 == memcmp(buf
, ".Dd", 3)) {
521 if (NULL
== curp
->mdoc
)
522 curp
->mdoc
= mdoc_init(curp
);
523 if (NULL
== (*mdoc
= curp
->mdoc
))
525 curp
->lastmdoc
= *mdoc
;
529 if (NULL
== curp
->man
)
530 curp
->man
= man_init(curp
);
531 if (NULL
== (*man
= curp
->man
))
533 curp
->lastman
= *man
;
539 moptions(enum intt
*tflags
, char *arg
)
542 if (0 == strcmp(arg
, "doc"))
544 else if (0 == strcmp(arg
, "andoc"))
546 else if (0 == strcmp(arg
, "an"))
549 warnx("bad argument: -m%s", arg
);
558 toptions(enum outt
*tflags
, char *arg
)
561 if (0 == strcmp(arg
, "ascii"))
562 *tflags
= OUTT_ASCII
;
563 else if (0 == strcmp(arg
, "lint"))
565 else if (0 == strcmp(arg
, "tree"))
567 else if (0 == strcmp(arg
, "html"))
570 warnx("bad argument: -T%s", 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] = "no-ign-chars";
588 toks
[4] = "ign-errors";
594 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
596 *fflags
|= IGN_SCOPE
;
599 *fflags
|= NO_IGN_ESCAPE
;
602 *fflags
|= NO_IGN_MACRO
;
605 *fflags
|= NO_IGN_CHARS
;
608 *fflags
|= IGN_ERRORS
;
611 *fflags
|= NO_IGN_ESCAPE
|
612 NO_IGN_MACRO
| NO_IGN_CHARS
;
615 warnx("bad argument: -f%s", o
);
625 woptions(int *wflags
, char *arg
)
636 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
638 *wflags
|= WARN_WALL
;
641 *wflags
|= WARN_WERR
;
644 warnx("bad argument: -W%s", o
);
655 merr(void *arg
, int line
, int col
, const char *msg
)
657 struct curparse
*curp
;
659 curp
= (struct curparse
*)arg
;
661 (void)fprintf(stderr
, "%s:%d:%d: error: %s\n",
662 curp
->file
, line
, col
+ 1, msg
);
669 mwarn(void *arg
, int line
, int col
, const char *msg
)
671 struct curparse
*curp
;
673 curp
= (struct curparse
*)arg
;
675 if ( ! (curp
->wflags
& WARN_WALL
))
678 (void)fprintf(stderr
, "%s:%d:%d: warning: %s\n",
679 curp
->file
, line
, col
+ 1, msg
);
681 if ( ! (curp
->wflags
& WARN_WERR
))