]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.41 2009/07/28 10:15:12 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.
30 /* Account for FreeBSD and Linux in our declarations. */
33 extern int getsubopt(char **, char * const *, char **);
35 # define __dead __attribute__((__noreturn__))
37 #elif defined(__dead2)
39 # define __dead __dead2
43 typedef int (*out_mdoc
)(void *, const struct mdoc
*);
44 typedef int (*out_man
)(void *, const struct man
*);
45 typedef void (*out_free
)(void *);
65 const char *file
; /* Current parse. */
66 int fd
; /* Current parse. */
68 #define WARN_WALL (1 << 0) /* All-warnings mask. */
69 #define WARN_WERR (1 << 2) /* Warnings->errors. */
71 #define IGN_SCOPE (1 << 0) /* Ignore scope errors. */
72 #define NO_IGN_ESCAPE (1 << 1) /* Don't ignore bad escapes. */
73 #define NO_IGN_MACRO (1 << 2) /* Don't ignore bad macros. */
74 #define NO_IGN_CHARS (1 << 3) /* Don't ignore bad chars. */
75 #define IGN_ERRORS (1 << 4) /* Ignore failed parse. */
76 enum intt inttype
; /* Input parsers... */
80 struct mdoc
*lastmdoc
;
81 enum outt outtype
; /* Output devices... */
88 extern void *ascii_alloc(void);
89 extern int tree_mdoc(void *, const struct mdoc
*);
90 extern int tree_man(void *, const struct man
*);
91 extern int terminal_mdoc(void *, const struct mdoc
*);
92 extern int terminal_man(void *, const struct man
*);
93 extern void terminal_free(void *);
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 bzero(&curp
, sizeof(struct curparse
));
124 curp
.inttype
= INTT_AUTO
;
125 curp
.outtype
= OUTT_ASCII
;
128 while (-1 != (c
= getopt(argc
, argv
, "f:m:VW:T:")))
131 if ( ! foptions(&curp
.fflags
, optarg
))
132 return(EXIT_FAILURE
);
135 if ( ! moptions(&curp
.inttype
, optarg
))
136 return(EXIT_FAILURE
);
139 if ( ! toptions(&curp
.outtype
, optarg
))
140 return(EXIT_FAILURE
);
143 if ( ! woptions(&curp
.wflags
, optarg
))
144 return(EXIT_FAILURE
);
157 bzero(&ln
, sizeof(struct buf
));
158 bzero(&blk
, sizeof(struct buf
));
163 curp
.file
= "<stdin>";
164 curp
.fd
= STDIN_FILENO
;
166 c
= fdesc(&blk
, &ln
, &curp
);
167 if ( ! (IGN_ERRORS
& curp
.fflags
))
170 rc
= -1 == c
? 0 : 1;
173 while (rc
&& *argv
) {
174 c
= ffile(&blk
, &ln
, *argv
, &curp
);
175 if ( ! (IGN_ERRORS
& curp
.fflags
))
178 rc
= -1 == c
? 0 : 1;
183 if ( ! man_reset(curp
.lastman
))
186 if ( ! mdoc_reset(curp
.lastmdoc
))
189 curp
.lastmdoc
= NULL
;
198 (*curp
.outfree
)(curp
.outdata
);
200 mdoc_free(curp
.mdoc
);
204 return(rc
? EXIT_SUCCESS
: EXIT_FAILURE
);
212 (void)printf("%s %s\n", __progname
, VERSION
);
221 (void)fprintf(stderr
, "usage: %s [-V] [-foption...] "
222 "[-mformat] [-Toutput] [-Werr...]\n",
229 man_init(struct curparse
*curp
)
235 mancb
.man_err
= merr
;
236 mancb
.man_warn
= mwarn
;
238 /* Defaults from mandoc.1. */
240 pflags
= MAN_IGN_MACRO
| MAN_IGN_ESCAPE
| MAN_IGN_CHARS
;
242 if (curp
->fflags
& NO_IGN_MACRO
)
243 pflags
&= ~MAN_IGN_MACRO
;
244 if (curp
->fflags
& NO_IGN_CHARS
)
245 pflags
&= ~MAN_IGN_CHARS
;
246 if (curp
->fflags
& NO_IGN_ESCAPE
)
247 pflags
&= ~MAN_IGN_ESCAPE
;
249 if (NULL
== (man
= man_alloc(curp
, pflags
, &mancb
)))
250 warnx("memory exhausted");
257 mdoc_init(struct curparse
*curp
)
261 struct mdoc_cb mdoccb
;
263 mdoccb
.mdoc_err
= merr
;
264 mdoccb
.mdoc_warn
= mwarn
;
266 /* Defaults from mandoc.1. */
268 pflags
= MDOC_IGN_MACRO
| MDOC_IGN_ESCAPE
| MDOC_IGN_CHARS
;
270 if (curp
->fflags
& IGN_SCOPE
)
271 pflags
|= MDOC_IGN_SCOPE
;
272 if (curp
->fflags
& NO_IGN_ESCAPE
)
273 pflags
&= ~MDOC_IGN_ESCAPE
;
274 if (curp
->fflags
& NO_IGN_MACRO
)
275 pflags
&= ~MDOC_IGN_MACRO
;
276 if (curp
->fflags
& NO_IGN_CHARS
)
277 pflags
&= ~MDOC_IGN_CHARS
;
279 if (NULL
== (mdoc
= mdoc_alloc(curp
, pflags
, &mdoccb
)))
280 warnx("memory exhausted");
287 ffile(struct buf
*blk
, struct buf
*ln
,
288 const char *file
, struct curparse
*curp
)
293 if (-1 == (curp
->fd
= open(curp
->file
, O_RDONLY
, 0))) {
294 warn("%s", curp
->file
);
298 c
= fdesc(blk
, ln
, curp
);
300 if (-1 == close(curp
->fd
))
301 warn("%s", curp
->file
);
308 fdesc(struct buf
*blk
, struct buf
*ln
, struct curparse
*curp
)
313 int j
, i
, pos
, lnn
, comment
;
322 * Two buffers: ln and buf. buf is the input buffer optimised
323 * here for each file's block size. ln is a line buffer. Both
324 * growable, hence passed in by ptr-ptr.
327 if (-1 == fstat(curp
->fd
, &st
))
328 warn("%s", curp
->file
);
329 else if ((size_t)st
.st_blksize
> sz
)
333 blk
->buf
= realloc(blk
->buf
, sz
);
334 if (NULL
== blk
->buf
) {
341 /* Fill buf with file blocksize. */
343 for (lnn
= pos
= comment
= 0; ; ) {
344 if (-1 == (ssz
= read(curp
->fd
, blk
->buf
, sz
))) {
345 warn("%s", curp
->file
);
350 /* Parse the read block into partial or full lines. */
352 for (i
= 0; i
< (int)ssz
; i
++) {
353 if (pos
>= (int)ln
->sz
) {
354 ln
->sz
+= 256; /* Step-size. */
355 ln
->buf
= realloc(ln
->buf
, ln
->sz
);
356 if (NULL
== ln
->buf
) {
362 if ('\n' != blk
->buf
[i
]) {
365 ln
->buf
[pos
++] = blk
->buf
[i
];
367 /* Handle in-line `\"' comments. */
369 if (1 == pos
|| '\"' != ln
->buf
[pos
- 1])
372 for (j
= pos
- 2; j
>= 0; j
--)
373 if ('\\' != ln
->buf
[j
])
376 if ( ! ((pos
- 2 - j
) % 2))
384 /* Handle escaped `\\n' newlines. */
386 if (pos
> 0 && 0 == comment
&&
387 '\\' == ln
->buf
[pos
- 1]) {
388 for (j
= pos
- 1; j
>= 0; j
--)
389 if ('\\' != ln
->buf
[j
])
391 if ( ! ((pos
- j
) % 2)) {
401 /* If unset, assign parser in pset(). */
403 if ( ! (man
|| mdoc
) && ! pset(ln
->buf
,
404 pos
, curp
, &man
, &mdoc
))
409 /* Pass down into parsers. */
411 if (man
&& ! man_parseln(man
, lnn
, ln
->buf
))
413 if (mdoc
&& ! mdoc_parseln(mdoc
, lnn
, ln
->buf
))
418 /* NOTE a parser may not have been assigned, yet. */
420 if ( ! (man
|| mdoc
)) {
421 (void)fprintf(stderr
, "%s: not a manual\n",
426 if (mdoc
&& ! mdoc_endparse(mdoc
))
428 if (man
&& ! man_endparse(man
))
431 /* If unset, allocate output dev now (if applicable). */
433 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
434 switch (curp
->outtype
) {
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 if ( ! (*curp
->outman
)(curp
->outdata
, man
))
455 if (mdoc
&& curp
->outmdoc
)
456 if ( ! (*curp
->outmdoc
)(curp
->outdata
, mdoc
))
464 pset(const char *buf
, int pos
, struct curparse
*curp
,
465 struct man
**man
, struct mdoc
**mdoc
)
470 * Try to intuit which kind of manual parser should be used. If
471 * passed in by command-line (-man, -mdoc), then use that
472 * explicitly. If passed as -mandoc, then try to guess from the
473 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
474 * default to -man, which is more lenient.
478 for (i
= 1; buf
[i
]; i
++)
479 if (' ' != buf
[i
] && '\t' != buf
[i
])
485 switch (curp
->inttype
) {
487 if (NULL
== curp
->mdoc
)
488 curp
->mdoc
= mdoc_init(curp
);
489 if (NULL
== (*mdoc
= curp
->mdoc
))
491 curp
->lastmdoc
= *mdoc
;
494 if (NULL
== curp
->man
)
495 curp
->man
= man_init(curp
);
496 if (NULL
== (*man
= curp
->man
))
498 curp
->lastman
= *man
;
504 if (pos
>= 3 && 0 == memcmp(buf
, ".Dd", 3)) {
505 if (NULL
== curp
->mdoc
)
506 curp
->mdoc
= mdoc_init(curp
);
507 if (NULL
== (*mdoc
= curp
->mdoc
))
509 curp
->lastmdoc
= *mdoc
;
513 if (NULL
== curp
->man
)
514 curp
->man
= man_init(curp
);
515 if (NULL
== (*man
= curp
->man
))
517 curp
->lastman
= *man
;
523 moptions(enum intt
*tflags
, char *arg
)
526 if (0 == strcmp(arg
, "doc"))
528 else if (0 == strcmp(arg
, "andoc"))
530 else if (0 == strcmp(arg
, "an"))
533 warnx("bad argument: -m%s", arg
);
542 toptions(enum outt
*tflags
, char *arg
)
545 if (0 == strcmp(arg
, "ascii"))
546 *tflags
= OUTT_ASCII
;
547 else if (0 == strcmp(arg
, "lint"))
549 else if (0 == strcmp(arg
, "tree"))
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";
576 switch (getsubopt(&arg
, toks
, &v
)) {
578 *fflags
|= IGN_SCOPE
;
581 *fflags
|= NO_IGN_ESCAPE
;
584 *fflags
|= NO_IGN_MACRO
;
587 *fflags
|= NO_IGN_CHARS
;
590 *fflags
|= IGN_ERRORS
;
593 *fflags
|= NO_IGN_ESCAPE
|
594 NO_IGN_MACRO
| NO_IGN_CHARS
;
597 warnx("bad argument: -f%s", o
);
607 woptions(int *wflags
, char *arg
)
618 switch (getsubopt(&arg
, toks
, &v
)) {
620 *wflags
|= WARN_WALL
;
623 *wflags
|= WARN_WERR
;
626 warnx("bad argument: -W%s", o
);
637 merr(void *arg
, int line
, int col
, const char *msg
)
639 struct curparse
*curp
;
641 curp
= (struct curparse
*)arg
;
643 (void)fprintf(stderr
, "%s:%d:%d: error: %s\n",
644 curp
->file
, line
, col
+ 1, msg
);
651 mwarn(void *arg
, int line
, int col
, const char *msg
)
653 struct curparse
*curp
;
655 curp
= (struct curparse
*)arg
;
657 if ( ! (curp
->wflags
& WARN_WALL
))
660 (void)fprintf(stderr
, "%s:%d:%d: warning: %s\n",
661 curp
->file
, line
, col
+ 1, msg
);
663 if ( ! (curp
->wflags
& WARN_WERR
))