]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.36 2009/07/06 09:34:29 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 enum intt inttype
; /* Input parsers... */
79 struct mdoc
*lastmdoc
;
80 enum outt outtype
; /* Output devices... */
87 extern void *ascii_alloc(void);
88 extern int tree_mdoc(void *, const struct mdoc
*);
89 extern int tree_man(void *, const struct man
*);
90 extern int terminal_mdoc(void *, const struct mdoc
*);
91 extern int terminal_man(void *, const struct man
*);
92 extern void terminal_free(void *);
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 manwarn(void *, int, int, const char *);
100 static int mdocwarn(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
;
165 if ( ! fdesc(&blk
, &ln
, &curp
))
169 while (rc
&& *argv
) {
170 if ( ! ffile(&blk
, &ln
, *argv
, &curp
))
175 if ( ! man_reset(curp
.lastman
))
178 if ( ! mdoc_reset(curp
.lastmdoc
))
181 curp
.lastmdoc
= NULL
;
190 (*curp
.outfree
)(curp
.outdata
);
192 mdoc_free(curp
.mdoc
);
196 return(rc
? EXIT_SUCCESS
: EXIT_FAILURE
);
204 (void)printf("%s %s\n", __progname
, VERSION
);
213 (void)fprintf(stderr
, "usage: %s [-V] [-foption...] "
214 "[-mformat] [-Toutput] [-Werr...]\n",
221 man_init(struct curparse
*curp
)
227 mancb
.man_err
= merr
;
228 mancb
.man_warn
= manwarn
;
230 /* Defaults from mandoc.1. */
232 pflags
= MAN_IGN_MACRO
| MAN_IGN_ESCAPE
| MAN_IGN_CHARS
;
234 if (curp
->fflags
& NO_IGN_MACRO
)
235 pflags
&= ~MAN_IGN_MACRO
;
236 if (curp
->fflags
& NO_IGN_CHARS
)
237 pflags
&= ~MAN_IGN_CHARS
;
238 if (curp
->fflags
& NO_IGN_ESCAPE
)
239 pflags
&= ~MAN_IGN_ESCAPE
;
241 if (NULL
== (man
= man_alloc(curp
, pflags
, &mancb
)))
242 warnx("memory exhausted");
249 mdoc_init(struct curparse
*curp
)
253 struct mdoc_cb mdoccb
;
255 mdoccb
.mdoc_err
= merr
;
256 mdoccb
.mdoc_warn
= mdocwarn
;
258 /* Defaults from mandoc.1. */
260 pflags
= MDOC_IGN_MACRO
| MDOC_IGN_ESCAPE
| MDOC_IGN_CHARS
;
262 if (curp
->fflags
& IGN_SCOPE
)
263 pflags
|= MDOC_IGN_SCOPE
;
264 if (curp
->fflags
& NO_IGN_ESCAPE
)
265 pflags
&= ~MDOC_IGN_ESCAPE
;
266 if (curp
->fflags
& NO_IGN_MACRO
)
267 pflags
&= ~MDOC_IGN_MACRO
;
268 if (curp
->fflags
& NO_IGN_CHARS
)
269 pflags
&= ~MDOC_IGN_CHARS
;
271 if (NULL
== (mdoc
= mdoc_alloc(curp
, pflags
, &mdoccb
)))
272 warnx("memory exhausted");
279 ffile(struct buf
*blk
, struct buf
*ln
,
280 const char *file
, struct curparse
*curp
)
285 if (-1 == (curp
->fd
= open(curp
->file
, O_RDONLY
, 0))) {
286 warn("%s", curp
->file
);
290 c
= fdesc(blk
, ln
, curp
);
292 if (-1 == close(curp
->fd
))
293 warn("%s", curp
->file
);
300 fdesc(struct buf
*blk
, struct buf
*ln
, struct curparse
*curp
)
305 int j
, i
, pos
, lnn
, comment
;
314 * Two buffers: ln and buf. buf is the input buffer optimised
315 * here for each file's block size. ln is a line buffer. Both
316 * growable, hence passed in by ptr-ptr.
319 if (-1 == fstat(curp
->fd
, &st
))
320 warn("%s", curp
->file
);
321 else if ((size_t)st
.st_blksize
> sz
)
325 blk
->buf
= realloc(blk
->buf
, sz
);
326 if (NULL
== blk
->buf
) {
333 /* Fill buf with file blocksize. */
335 for (lnn
= pos
= comment
= 0; ; ) {
336 if (-1 == (ssz
= read(curp
->fd
, blk
->buf
, sz
))) {
337 warn("%s", curp
->file
);
342 /* Parse the read block into partial or full lines. */
344 for (i
= 0; i
< (int)ssz
; i
++) {
345 if (pos
>= (int)ln
->sz
) {
346 ln
->sz
+= 256; /* Step-size. */
347 ln
->buf
= realloc(ln
->buf
, ln
->sz
);
348 if (NULL
== ln
->buf
) {
354 if ('\n' != blk
->buf
[i
]) {
357 ln
->buf
[pos
++] = blk
->buf
[i
];
359 /* Handle in-line `\"' comments. */
361 if (1 == pos
|| '\"' != ln
->buf
[pos
- 1])
364 for (j
= pos
- 2; j
>= 0; j
--)
365 if ('\\' != ln
->buf
[j
])
368 if ( ! ((pos
- 2 - j
) % 2))
376 /* Handle escaped `\\n' newlines. */
378 if (pos
> 0 && 0 == comment
&&
379 '\\' == ln
->buf
[pos
- 1]) {
380 for (j
= pos
- 1; j
>= 0; j
--)
381 if ('\\' != ln
->buf
[j
])
383 if ( ! ((pos
- j
) % 2)) {
393 /* If unset, assign parser in pset(). */
395 if ( ! (man
|| mdoc
) && ! pset(ln
->buf
,
396 pos
, curp
, &man
, &mdoc
))
401 /* Pass down into parsers. */
403 if (man
&& ! man_parseln(man
, lnn
, ln
->buf
))
405 if (mdoc
&& ! mdoc_parseln(mdoc
, lnn
, ln
->buf
))
410 /* NOTE a parser may not have been assigned, yet. */
412 if ( ! (man
|| mdoc
)) {
413 warnx("%s: not a manual", curp
->file
);
417 if (mdoc
&& ! mdoc_endparse(mdoc
))
419 if (man
&& ! man_endparse(man
))
422 /* If unset, allocate output dev now (if applicable). */
424 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
425 switch (curp
->outtype
) {
427 curp
->outman
= tree_man
;
428 curp
->outmdoc
= tree_mdoc
;
433 curp
->outdata
= ascii_alloc();
434 curp
->outman
= terminal_man
;
435 curp
->outmdoc
= terminal_mdoc
;
436 curp
->outfree
= terminal_free
;
441 /* Execute the out device, if it exists. */
443 if (man
&& curp
->outman
)
444 if ( ! (*curp
->outman
)(curp
->outdata
, man
))
446 if (mdoc
&& curp
->outmdoc
)
447 if ( ! (*curp
->outmdoc
)(curp
->outdata
, mdoc
))
455 pset(const char *buf
, int pos
, struct curparse
*curp
,
456 struct man
**man
, struct mdoc
**mdoc
)
461 * Try to intuit which kind of manual parser should be used. If
462 * passed in by command-line (-man, -mdoc), then use that
463 * explicitly. If passed as -mandoc, then try to guess from the
464 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
465 * default to -man, which is more lenient.
469 for (i
= 1; buf
[i
]; i
++)
470 if (' ' != buf
[i
] && '\t' != buf
[i
])
476 switch (curp
->inttype
) {
478 if (NULL
== curp
->mdoc
)
479 curp
->mdoc
= mdoc_init(curp
);
480 if (NULL
== (*mdoc
= curp
->mdoc
))
482 curp
->lastmdoc
= *mdoc
;
485 if (NULL
== curp
->man
)
486 curp
->man
= man_init(curp
);
487 if (NULL
== (*man
= curp
->man
))
489 curp
->lastman
= *man
;
495 if (pos
>= 3 && 0 == memcmp(buf
, ".Dd", 3)) {
496 if (NULL
== curp
->mdoc
)
497 curp
->mdoc
= mdoc_init(curp
);
498 if (NULL
== (*mdoc
= curp
->mdoc
))
500 curp
->lastmdoc
= *mdoc
;
504 if (NULL
== curp
->man
)
505 curp
->man
= man_init(curp
);
506 if (NULL
== (*man
= curp
->man
))
508 curp
->lastman
= *man
;
514 moptions(enum intt
*tflags
, char *arg
)
517 if (0 == strcmp(arg
, "doc"))
519 else if (0 == strcmp(arg
, "andoc"))
521 else if (0 == strcmp(arg
, "an"))
524 warnx("bad argument: -m%s", arg
);
533 toptions(enum outt
*tflags
, char *arg
)
536 if (0 == strcmp(arg
, "ascii"))
537 *tflags
= OUTT_ASCII
;
538 else if (0 == strcmp(arg
, "lint"))
540 else if (0 == strcmp(arg
, "tree"))
543 warnx("bad argument: -T%s", arg
);
552 foptions(int *fflags
, char *arg
)
557 toks
[0] = "ign-scope";
558 toks
[1] = "no-ign-escape";
559 toks
[2] = "no-ign-macro";
560 toks
[3] = "no-ign-chars";
566 switch (getsubopt(&arg
, toks
, &v
)) {
568 *fflags
|= IGN_SCOPE
;
571 *fflags
|= NO_IGN_ESCAPE
;
574 *fflags
|= NO_IGN_MACRO
;
577 *fflags
|= NO_IGN_CHARS
;
580 *fflags
|= NO_IGN_ESCAPE
|
581 NO_IGN_MACRO
| NO_IGN_CHARS
;
584 warnx("bad argument: -f%s", o
);
594 woptions(int *wflags
, char *arg
)
605 switch (getsubopt(&arg
, toks
, &v
)) {
607 *wflags
|= WARN_WALL
;
610 *wflags
|= WARN_WERR
;
613 warnx("bad argument: -W%s", o
);
624 merr(void *arg
, int line
, int col
, const char *msg
)
626 struct curparse
*curp
;
628 curp
= (struct curparse
*)arg
;
630 warnx("%s:%d: error: %s (column %d)",
631 curp
->file
, line
, msg
, col
);
638 mdocwarn(void *arg
, int line
, int col
, const char *msg
)
640 struct curparse
*curp
;
642 curp
= (struct curparse
*)arg
;
644 if ( ! (curp
->wflags
& WARN_WALL
))
647 warnx("%s:%d: syntax warning: %s (column %d)",
648 curp
->file
, line
, msg
, col
);
650 if ( ! (curp
->wflags
& WARN_WERR
))
653 warnx("considering warnings as errors");
659 manwarn(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 warnx("%s:%d: syntax warning: %s (column %d)",
669 curp
->file
, line
, msg
, col
);
671 if ( ! (curp
->wflags
& WARN_WERR
))
674 warnx("considering warnings as errors");