]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.44 2009/09/21 13:06:13 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 void (*out_mdoc
)(void *, const struct mdoc
*);
44 typedef void (*out_man
)(void *, const struct man
*);
45 typedef void (*out_free
)(void *);
66 const char *file
; /* Current parse. */
67 int fd
; /* Current parse. */
69 #define WARN_WALL (1 << 0) /* All-warnings mask. */
70 #define WARN_WERR (1 << 2) /* Warnings->errors. */
72 #define IGN_SCOPE (1 << 0) /* Ignore scope errors. */
73 #define NO_IGN_ESCAPE (1 << 1) /* Don't ignore bad escapes. */
74 #define NO_IGN_MACRO (1 << 2) /* Don't ignore bad macros. */
75 #define NO_IGN_CHARS (1 << 3) /* Don't ignore bad chars. */
76 #define IGN_ERRORS (1 << 4) /* Ignore failed parse. */
77 enum intt inttype
; /* Input parsers... */
81 struct mdoc
*lastmdoc
;
82 enum outt outtype
; /* Output devices... */
90 extern void *html_alloc(char *);
91 extern void html_mdoc(void *, const struct mdoc
*);
92 extern void html_man(void *, const struct man
*);
93 extern void html_free(void *);
94 extern void *ascii_alloc(void);
95 extern void tree_mdoc(void *, const struct mdoc
*);
96 extern void tree_man(void *, const struct man
*);
97 extern void terminal_mdoc(void *, const struct mdoc
*);
98 extern void terminal_man(void *, const struct man
*);
99 extern void terminal_free(void *);
101 static int foptions(int *, char *);
102 static int toptions(enum outt
*, char *);
103 static int moptions(enum intt
*, char *);
104 static int woptions(int *, char *);
105 static int merr(void *, int, int, const char *);
106 static int mwarn(void *, int, int, const char *);
107 static int ffile(struct buf
*, struct buf
*,
108 const char *, struct curparse
*);
109 static int fdesc(struct buf
*, struct buf
*,
111 static int pset(const char *, int, struct curparse
*,
112 struct man
**, struct mdoc
**);
113 static struct man
*man_init(struct curparse
*);
114 static struct mdoc
*mdoc_init(struct curparse
*);
115 __dead
static void version(void);
116 __dead
static void usage(void);
118 extern char *__progname
;
122 main(int argc
, char *argv
[])
126 struct curparse curp
;
128 bzero(&curp
, 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 curp
.outopts
= optarg
;
148 if ( ! toptions(&curp
.outtype
, optarg
))
149 return(EXIT_FAILURE
);
152 if ( ! woptions(&curp
.wflags
, optarg
))
153 return(EXIT_FAILURE
);
166 bzero(&ln
, sizeof(struct buf
));
167 bzero(&blk
, 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 if ( ! man_reset(curp
.lastman
))
195 if ( ! mdoc_reset(curp
.lastmdoc
))
198 curp
.lastmdoc
= NULL
;
207 (*curp
.outfree
)(curp
.outdata
);
209 mdoc_free(curp
.mdoc
);
213 return(rc
? EXIT_SUCCESS
: EXIT_FAILURE
);
221 (void)printf("%s %s\n", __progname
, VERSION
);
230 (void)fprintf(stderr
, "usage: %s [-V] [-foption...] "
231 "[-mformat] [-Toutput] [-Werr...]\n",
238 man_init(struct curparse
*curp
)
244 mancb
.man_err
= merr
;
245 mancb
.man_warn
= mwarn
;
247 /* Defaults from mandoc.1. */
249 pflags
= MAN_IGN_MACRO
| MAN_IGN_ESCAPE
| MAN_IGN_CHARS
;
251 if (curp
->fflags
& NO_IGN_MACRO
)
252 pflags
&= ~MAN_IGN_MACRO
;
253 if (curp
->fflags
& NO_IGN_CHARS
)
254 pflags
&= ~MAN_IGN_CHARS
;
255 if (curp
->fflags
& NO_IGN_ESCAPE
)
256 pflags
&= ~MAN_IGN_ESCAPE
;
258 if (NULL
== (man
= man_alloc(curp
, pflags
, &mancb
)))
259 warnx("memory exhausted");
266 mdoc_init(struct curparse
*curp
)
270 struct mdoc_cb mdoccb
;
272 mdoccb
.mdoc_err
= merr
;
273 mdoccb
.mdoc_warn
= mwarn
;
275 /* Defaults from mandoc.1. */
277 pflags
= MDOC_IGN_MACRO
| MDOC_IGN_ESCAPE
| MDOC_IGN_CHARS
;
279 if (curp
->fflags
& IGN_SCOPE
)
280 pflags
|= MDOC_IGN_SCOPE
;
281 if (curp
->fflags
& NO_IGN_ESCAPE
)
282 pflags
&= ~MDOC_IGN_ESCAPE
;
283 if (curp
->fflags
& NO_IGN_MACRO
)
284 pflags
&= ~MDOC_IGN_MACRO
;
285 if (curp
->fflags
& NO_IGN_CHARS
)
286 pflags
&= ~MDOC_IGN_CHARS
;
288 if (NULL
== (mdoc
= mdoc_alloc(curp
, pflags
, &mdoccb
)))
289 warnx("memory exhausted");
296 ffile(struct buf
*blk
, struct buf
*ln
,
297 const char *file
, struct curparse
*curp
)
302 if (-1 == (curp
->fd
= open(curp
->file
, O_RDONLY
, 0))) {
303 warn("%s", curp
->file
);
307 c
= fdesc(blk
, ln
, curp
);
309 if (-1 == close(curp
->fd
))
310 warn("%s", curp
->file
);
317 fdesc(struct buf
*blk
, struct buf
*ln
, struct curparse
*curp
)
322 int j
, i
, pos
, lnn
, comment
;
331 * Two buffers: ln and buf. buf is the input buffer optimised
332 * here for each file's block size. ln is a line buffer. Both
333 * growable, hence passed in by ptr-ptr.
336 if (-1 == fstat(curp
->fd
, &st
))
337 warn("%s", curp
->file
);
338 else if ((size_t)st
.st_blksize
> sz
)
342 blk
->buf
= realloc(blk
->buf
, sz
);
343 if (NULL
== blk
->buf
) {
350 /* Fill buf with file blocksize. */
352 for (lnn
= pos
= comment
= 0; ; ) {
353 if (-1 == (ssz
= read(curp
->fd
, blk
->buf
, sz
))) {
354 warn("%s", curp
->file
);
359 /* Parse the read block into partial or full lines. */
361 for (i
= 0; i
< (int)ssz
; i
++) {
362 if (pos
>= (int)ln
->sz
) {
363 ln
->sz
+= 256; /* Step-size. */
364 ln
->buf
= realloc(ln
->buf
, ln
->sz
);
365 if (NULL
== ln
->buf
) {
371 if ('\n' != blk
->buf
[i
]) {
374 ln
->buf
[pos
++] = blk
->buf
[i
];
376 /* Handle in-line `\"' comments. */
378 if (1 == pos
|| '\"' != ln
->buf
[pos
- 1])
381 for (j
= pos
- 2; j
>= 0; j
--)
382 if ('\\' != ln
->buf
[j
])
385 if ( ! ((pos
- 2 - j
) % 2))
393 /* Handle escaped `\\n' newlines. */
395 if (pos
> 0 && 0 == comment
&&
396 '\\' == ln
->buf
[pos
- 1]) {
397 for (j
= pos
- 1; j
>= 0; j
--)
398 if ('\\' != ln
->buf
[j
])
400 if ( ! ((pos
- j
) % 2)) {
410 /* If unset, assign parser in pset(). */
412 if ( ! (man
|| mdoc
) && ! pset(ln
->buf
,
413 pos
, curp
, &man
, &mdoc
))
418 /* Pass down into parsers. */
420 if (man
&& ! man_parseln(man
, lnn
, ln
->buf
))
422 if (mdoc
&& ! mdoc_parseln(mdoc
, lnn
, ln
->buf
))
427 /* NOTE a parser may not have been assigned, yet. */
429 if ( ! (man
|| mdoc
)) {
430 (void)fprintf(stderr
, "%s: not a manual\n",
435 if (mdoc
&& ! mdoc_endparse(mdoc
))
437 if (man
&& ! man_endparse(man
))
440 /* If unset, allocate output dev now (if applicable). */
442 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
443 switch (curp
->outtype
) {
445 curp
->outdata
= html_alloc(curp
->outopts
);
446 curp
->outman
= html_man
;
447 curp
->outmdoc
= html_mdoc
;
448 curp
->outfree
= html_free
;
451 curp
->outman
= tree_man
;
452 curp
->outmdoc
= tree_mdoc
;
457 curp
->outdata
= ascii_alloc();
458 curp
->outman
= terminal_man
;
459 curp
->outmdoc
= terminal_mdoc
;
460 curp
->outfree
= terminal_free
;
465 /* Execute the out device, if it exists. */
467 if (man
&& curp
->outman
)
468 (*curp
->outman
)(curp
->outdata
, man
);
469 if (mdoc
&& curp
->outmdoc
)
470 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
477 pset(const char *buf
, int pos
, struct curparse
*curp
,
478 struct man
**man
, struct mdoc
**mdoc
)
483 * Try to intuit which kind of manual parser should be used. If
484 * passed in by command-line (-man, -mdoc), then use that
485 * explicitly. If passed as -mandoc, then try to guess from the
486 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
487 * default to -man, which is more lenient.
491 for (i
= 1; buf
[i
]; i
++)
492 if (' ' != buf
[i
] && '\t' != buf
[i
])
498 switch (curp
->inttype
) {
500 if (NULL
== curp
->mdoc
)
501 curp
->mdoc
= mdoc_init(curp
);
502 if (NULL
== (*mdoc
= curp
->mdoc
))
504 curp
->lastmdoc
= *mdoc
;
507 if (NULL
== curp
->man
)
508 curp
->man
= man_init(curp
);
509 if (NULL
== (*man
= curp
->man
))
511 curp
->lastman
= *man
;
517 if (pos
>= 3 && 0 == memcmp(buf
, ".Dd", 3)) {
518 if (NULL
== curp
->mdoc
)
519 curp
->mdoc
= mdoc_init(curp
);
520 if (NULL
== (*mdoc
= curp
->mdoc
))
522 curp
->lastmdoc
= *mdoc
;
526 if (NULL
== curp
->man
)
527 curp
->man
= man_init(curp
);
528 if (NULL
== (*man
= curp
->man
))
530 curp
->lastman
= *man
;
536 moptions(enum intt
*tflags
, char *arg
)
539 if (0 == strcmp(arg
, "doc"))
541 else if (0 == strcmp(arg
, "andoc"))
543 else if (0 == strcmp(arg
, "an"))
546 warnx("bad argument: -m%s", arg
);
555 toptions(enum outt
*tflags
, char *arg
)
558 if (0 == strcmp(arg
, "ascii"))
559 *tflags
= OUTT_ASCII
;
560 else if (0 == strcmp(arg
, "lint"))
562 else if (0 == strcmp(arg
, "tree"))
564 else if (0 == strcmp(arg
, "html"))
567 warnx("bad argument: -T%s", arg
);
576 foptions(int *fflags
, char *arg
)
581 toks
[0] = "ign-scope";
582 toks
[1] = "no-ign-escape";
583 toks
[2] = "no-ign-macro";
584 toks
[3] = "no-ign-chars";
585 toks
[4] = "ign-errors";
591 switch (getsubopt(&arg
, toks
, &v
)) {
593 *fflags
|= IGN_SCOPE
;
596 *fflags
|= NO_IGN_ESCAPE
;
599 *fflags
|= NO_IGN_MACRO
;
602 *fflags
|= NO_IGN_CHARS
;
605 *fflags
|= IGN_ERRORS
;
608 *fflags
|= NO_IGN_ESCAPE
|
609 NO_IGN_MACRO
| NO_IGN_CHARS
;
612 warnx("bad argument: -f%s", o
);
622 woptions(int *wflags
, char *arg
)
633 switch (getsubopt(&arg
, toks
, &v
)) {
635 *wflags
|= WARN_WALL
;
638 *wflags
|= WARN_WERR
;
641 warnx("bad argument: -W%s", o
);
652 merr(void *arg
, int line
, int col
, const char *msg
)
654 struct curparse
*curp
;
656 curp
= (struct curparse
*)arg
;
658 (void)fprintf(stderr
, "%s:%d:%d: error: %s\n",
659 curp
->file
, line
, col
+ 1, msg
);
666 mwarn(void *arg
, int line
, int col
, const char *msg
)
668 struct curparse
*curp
;
670 curp
= (struct curparse
*)arg
;
672 if ( ! (curp
->wflags
& WARN_WALL
))
675 (void)fprintf(stderr
, "%s:%d:%d: warning: %s\n",
676 curp
->file
, line
, col
+ 1, msg
);
678 if ( ! (curp
->wflags
& WARN_WERR
))