]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.52 2009/10/30 05:58:37 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.
32 #define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
34 /* Account for FreeBSD and Linux in our declarations. */
37 extern int getsubopt(char **, char * const *, char **);
38 extern size_t strlcat(char *, const char *, size_t);
40 # define __dead __attribute__((__noreturn__))
42 #elif defined(__dead2)
44 # define __dead __dead2
48 typedef void (*out_mdoc
)(void *, const struct mdoc
*);
49 typedef void (*out_man
)(void *, const struct man
*);
50 typedef void (*out_free
)(void *);
71 const char *file
; /* Current parse. */
72 int fd
; /* Current parse. */
74 #define WARN_WALL (1 << 0) /* All-warnings mask. */
75 #define WARN_WERR (1 << 2) /* Warnings->errors. */
77 #define IGN_SCOPE (1 << 0) /* Ignore scope errors. */
78 #define NO_IGN_ESCAPE (1 << 1) /* Don't ignore bad escapes. */
79 #define NO_IGN_MACRO (1 << 2) /* Don't ignore bad macros. */
80 #define NO_IGN_CHARS (1 << 3) /* Don't ignore bad chars. */
81 #define IGN_ERRORS (1 << 4) /* Ignore failed parse. */
82 enum intt inttype
; /* Input parsers... */
86 struct mdoc
*lastmdoc
;
87 enum outt outtype
; /* Output devices... */
95 static int foptions(int *, char *);
96 static int toptions(enum outt
*, 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 __dead
static void version(void);
110 __dead
static void usage(void);
112 extern char *__progname
;
116 main(int argc
, char *argv
[])
120 struct curparse curp
;
122 memset(&curp
, 0, sizeof(struct curparse
));
124 curp
.inttype
= INTT_AUTO
;
125 curp
.outtype
= OUTT_ASCII
;
128 while (-1 != (c
= getopt(argc
, argv
, "f:m:O:T:VW:")))
131 if ( ! foptions(&curp
.fflags
, optarg
))
132 return(EXIT_FAILURE
);
135 if ( ! moptions(&curp
.inttype
, optarg
))
136 return(EXIT_FAILURE
);
139 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
140 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
143 if ( ! toptions(&curp
.outtype
, optarg
))
144 return(EXIT_FAILURE
);
147 if ( ! woptions(&curp
.wflags
, optarg
))
148 return(EXIT_FAILURE
);
161 memset(&ln
, 0, sizeof(struct buf
));
162 memset(&blk
, 0, sizeof(struct buf
));
167 curp
.file
= "<stdin>";
168 curp
.fd
= STDIN_FILENO
;
170 c
= fdesc(&blk
, &ln
, &curp
);
171 if ( ! (IGN_ERRORS
& curp
.fflags
))
174 rc
= -1 == c
? 0 : 1;
177 while (rc
&& *argv
) {
178 c
= ffile(&blk
, &ln
, *argv
, &curp
);
179 if ( ! (IGN_ERRORS
& curp
.fflags
))
182 rc
= -1 == c
? 0 : 1;
187 man_reset(curp
.lastman
);
189 mdoc_reset(curp
.lastmdoc
);
191 curp
.lastmdoc
= NULL
;
200 (*curp
.outfree
)(curp
.outdata
);
202 mdoc_free(curp
.mdoc
);
206 return(rc
? EXIT_SUCCESS
: EXIT_FAILURE
);
214 (void)printf("%s %s\n", __progname
, VERSION
);
223 (void)fprintf(stderr
, "usage: %s [-V] [-foption...] "
224 "[-mformat] [-Ooption] [-Toutput] "
225 "[-Werr...]\n", __progname
);
231 man_init(struct curparse
*curp
)
236 mancb
.man_err
= merr
;
237 mancb
.man_warn
= mwarn
;
239 /* Defaults from mandoc.1. */
241 pflags
= MAN_IGN_MACRO
| MAN_IGN_ESCAPE
| MAN_IGN_CHARS
;
243 if (curp
->fflags
& NO_IGN_MACRO
)
244 pflags
&= ~MAN_IGN_MACRO
;
245 if (curp
->fflags
& NO_IGN_CHARS
)
246 pflags
&= ~MAN_IGN_CHARS
;
247 if (curp
->fflags
& NO_IGN_ESCAPE
)
248 pflags
&= ~MAN_IGN_ESCAPE
;
250 return(man_alloc(curp
, pflags
, &mancb
));
255 mdoc_init(struct curparse
*curp
)
258 struct mdoc_cb mdoccb
;
260 mdoccb
.mdoc_err
= merr
;
261 mdoccb
.mdoc_warn
= mwarn
;
263 /* Defaults from mandoc.1. */
265 pflags
= MDOC_IGN_MACRO
| MDOC_IGN_ESCAPE
| MDOC_IGN_CHARS
;
267 if (curp
->fflags
& IGN_SCOPE
)
268 pflags
|= MDOC_IGN_SCOPE
;
269 if (curp
->fflags
& NO_IGN_ESCAPE
)
270 pflags
&= ~MDOC_IGN_ESCAPE
;
271 if (curp
->fflags
& NO_IGN_MACRO
)
272 pflags
&= ~MDOC_IGN_MACRO
;
273 if (curp
->fflags
& NO_IGN_CHARS
)
274 pflags
&= ~MDOC_IGN_CHARS
;
276 return(mdoc_alloc(curp
, pflags
, &mdoccb
));
281 ffile(struct buf
*blk
, struct buf
*ln
,
282 const char *file
, struct curparse
*curp
)
287 if (-1 == (curp
->fd
= open(curp
->file
, O_RDONLY
, 0))) {
288 warn("%s", curp
->file
);
292 c
= fdesc(blk
, ln
, curp
);
294 if (-1 == close(curp
->fd
))
295 warn("%s", curp
->file
);
302 fdesc(struct buf
*blk
, struct buf
*ln
, struct curparse
*curp
)
307 int j
, i
, pos
, lnn
, comment
;
316 * Two buffers: ln and buf. buf is the input buffer optimised
317 * here for each file's block size. ln is a line buffer. Both
318 * growable, hence passed in by ptr-ptr.
321 if (-1 == fstat(curp
->fd
, &st
))
322 warn("%s", curp
->file
);
323 else if ((size_t)st
.st_blksize
> sz
)
327 blk
->buf
= realloc(blk
->buf
, sz
);
328 if (NULL
== blk
->buf
) {
335 /* Fill buf with file blocksize. */
337 for (lnn
= pos
= comment
= 0; ; ) {
338 if (-1 == (ssz
= read(curp
->fd
, blk
->buf
, sz
))) {
339 warn("%s", curp
->file
);
344 /* Parse the read block into partial or full lines. */
346 for (i
= 0; i
< (int)ssz
; i
++) {
347 if (pos
>= (int)ln
->sz
) {
348 ln
->sz
+= 256; /* Step-size. */
349 ln
->buf
= realloc(ln
->buf
, ln
->sz
);
350 if (NULL
== ln
->buf
) {
356 if ('\n' != blk
->buf
[i
]) {
359 ln
->buf
[pos
++] = blk
->buf
[i
];
361 /* Handle in-line `\"' comments. */
363 if (1 == pos
|| '\"' != ln
->buf
[pos
- 1])
366 for (j
= pos
- 2; j
>= 0; j
--)
367 if ('\\' != ln
->buf
[j
])
370 if ( ! ((pos
- 2 - j
) % 2))
378 /* Handle escaped `\\n' newlines. */
380 if (pos
> 0 && 0 == comment
&&
381 '\\' == ln
->buf
[pos
- 1]) {
382 for (j
= pos
- 1; j
>= 0; j
--)
383 if ('\\' != ln
->buf
[j
])
385 if ( ! ((pos
- j
) % 2)) {
395 /* If unset, assign parser in pset(). */
397 if ( ! (man
|| mdoc
) && ! pset(ln
->buf
,
398 pos
, curp
, &man
, &mdoc
))
403 /* Pass down into parsers. */
405 if (man
&& ! man_parseln(man
, lnn
, ln
->buf
))
407 if (mdoc
&& ! mdoc_parseln(mdoc
, lnn
, ln
->buf
))
412 /* NOTE a parser may not have been assigned, yet. */
414 if ( ! (man
|| mdoc
)) {
415 (void)fprintf(stderr
, "%s: not a manual\n",
420 if (mdoc
&& ! mdoc_endparse(mdoc
))
422 if (man
&& ! man_endparse(man
))
425 /* If unset, allocate output dev now (if applicable). */
427 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
428 switch (curp
->outtype
) {
430 curp
->outdata
= html_alloc(curp
->outopts
);
431 curp
->outman
= html_man
;
432 curp
->outmdoc
= html_mdoc
;
433 curp
->outfree
= html_free
;
436 curp
->outman
= tree_man
;
437 curp
->outmdoc
= tree_mdoc
;
442 curp
->outdata
= ascii_alloc();
443 curp
->outman
= terminal_man
;
444 curp
->outmdoc
= terminal_mdoc
;
445 curp
->outfree
= terminal_free
;
450 /* Execute the out device, if it exists. */
452 if (man
&& curp
->outman
)
453 (*curp
->outman
)(curp
->outdata
, man
);
454 if (mdoc
&& curp
->outmdoc
)
455 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
462 pset(const char *buf
, int pos
, struct curparse
*curp
,
463 struct man
**man
, struct mdoc
**mdoc
)
468 * Try to intuit which kind of manual parser should be used. If
469 * passed in by command-line (-man, -mdoc), then use that
470 * explicitly. If passed as -mandoc, then try to guess from the
471 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
472 * default to -man, which is more lenient.
476 for (i
= 1; buf
[i
]; i
++)
477 if (' ' != buf
[i
] && '\t' != buf
[i
])
483 switch (curp
->inttype
) {
485 if (NULL
== curp
->mdoc
)
486 curp
->mdoc
= mdoc_init(curp
);
487 if (NULL
== (*mdoc
= curp
->mdoc
))
489 curp
->lastmdoc
= *mdoc
;
492 if (NULL
== curp
->man
)
493 curp
->man
= man_init(curp
);
494 if (NULL
== (*man
= curp
->man
))
496 curp
->lastman
= *man
;
502 if (pos
>= 3 && 0 == memcmp(buf
, ".Dd", 3)) {
503 if (NULL
== curp
->mdoc
)
504 curp
->mdoc
= mdoc_init(curp
);
505 if (NULL
== (*mdoc
= curp
->mdoc
))
507 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 moptions(enum intt
*tflags
, char *arg
)
524 if (0 == strcmp(arg
, "doc"))
526 else if (0 == strcmp(arg
, "andoc"))
528 else if (0 == strcmp(arg
, "an"))
531 warnx("bad argument: -m%s", arg
);
540 toptions(enum outt
*tflags
, char *arg
)
543 if (0 == strcmp(arg
, "ascii"))
544 *tflags
= OUTT_ASCII
;
545 else if (0 == strcmp(arg
, "lint"))
547 else if (0 == strcmp(arg
, "tree"))
549 else if (0 == strcmp(arg
, "html"))
552 warnx("bad argument: -T%s", arg
);
561 foptions(int *fflags
, char *arg
)
566 toks
[0] = "ign-scope";
567 toks
[1] = "no-ign-escape";
568 toks
[2] = "no-ign-macro";
569 toks
[3] = "no-ign-chars";
570 toks
[4] = "ign-errors";
572 toks
[6] = "ign-escape";
577 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
579 *fflags
|= IGN_SCOPE
;
582 *fflags
|= NO_IGN_ESCAPE
;
585 *fflags
|= NO_IGN_MACRO
;
588 *fflags
|= NO_IGN_CHARS
;
591 *fflags
|= IGN_ERRORS
;
594 *fflags
|= NO_IGN_ESCAPE
|
595 NO_IGN_MACRO
| NO_IGN_CHARS
;
598 *fflags
&= ~NO_IGN_ESCAPE
;
601 warnx("bad argument: -f%s", o
);
611 woptions(int *wflags
, char *arg
)
622 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
624 *wflags
|= WARN_WALL
;
627 *wflags
|= WARN_WERR
;
630 warnx("bad argument: -W%s", o
);
641 merr(void *arg
, int line
, int col
, const char *msg
)
643 struct curparse
*curp
;
645 curp
= (struct curparse
*)arg
;
647 (void)fprintf(stderr
, "%s:%d:%d: error: %s\n",
648 curp
->file
, line
, col
+ 1, msg
);
655 mwarn(void *arg
, int line
, int col
, const char *msg
)
657 struct curparse
*curp
;
659 curp
= (struct curparse
*)arg
;
661 if ( ! (curp
->wflags
& WARN_WALL
))
664 (void)fprintf(stderr
, "%s:%d:%d: warning: %s\n",
665 curp
->file
, line
, col
+ 1, msg
);
667 if ( ! (curp
->wflags
& WARN_WERR
))