]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.31 2009/06/18 10:32:00 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 0x03 /* All-warnings mask. */
69 #define WARN_WCOMPAT (1 << 0) /* Compatibility warnings. */
70 #define WARN_WSYNTAX (1 << 1) /* Syntax warnings. */
71 #define WARN_WERR (1 << 2) /* Warnings->errors. */
73 #define IGN_SCOPE (1 << 0) /* Ignore scope errors. */
74 #define NO_IGN_ESCAPE (1 << 1) /* Don't ignore bad escapes. */
75 #define NO_IGN_MACRO (1 << 2) /* Don't ignore bad macros. */
76 #define NO_IGN_CHARS (1 << 3) /* Don't ignore bad chars. */
77 enum intt inttype
; /* Input parsers... */
81 struct mdoc
*lastmdoc
;
82 enum outt outtype
; /* Output devices... */
89 extern void *ascii_alloc(void);
90 extern int tree_mdoc(void *, const struct mdoc
*);
91 extern int tree_man(void *, const struct man
*);
92 extern int terminal_mdoc(void *, const struct mdoc
*);
93 extern int terminal_man(void *, const struct man
*);
94 extern void terminal_free(void *);
96 static int foptions(int *, char *);
97 static int toptions(enum outt
*, char *);
98 static int moptions(enum intt
*, char *);
99 static int woptions(int *, char *);
100 static int merr(void *, int, int, const char *);
101 static int manwarn(void *, int, int, const char *);
102 static int mdocwarn(void *, int, int,
103 enum mdoc_warn
, const char *);
104 static int ffile(struct buf
*, struct buf
*,
105 const char *, struct curparse
*);
106 static int fdesc(struct buf
*, struct buf
*,
108 static int pset(const char *, int, struct curparse
*,
109 struct man
**, struct mdoc
**);
110 static struct man
*man_init(struct curparse
*);
111 static struct mdoc
*mdoc_init(struct curparse
*);
112 __dead
static void version(void);
113 __dead
static void usage(void);
115 extern char *__progname
;
119 main(int argc
, char *argv
[])
123 struct curparse curp
;
125 bzero(&curp
, sizeof(struct curparse
));
127 curp
.inttype
= INTT_AUTO
;
128 curp
.outtype
= OUTT_ASCII
;
131 while (-1 != (c
= getopt(argc
, argv
, "f:m:VW:T:")))
134 if ( ! foptions(&curp
.fflags
, optarg
))
138 if ( ! moptions(&curp
.inttype
, optarg
))
142 if ( ! toptions(&curp
.outtype
, optarg
))
146 if ( ! woptions(&curp
.wflags
, optarg
))
160 bzero(&ln
, sizeof(struct buf
));
161 bzero(&blk
, sizeof(struct buf
));
166 curp
.file
= "<stdin>";
167 curp
.fd
= STDIN_FILENO
;
168 if ( ! fdesc(&blk
, &ln
, &curp
))
172 while (rc
&& *argv
) {
173 if ( ! ffile(&blk
, &ln
, *argv
, &curp
))
178 if ( ! man_reset(curp
.lastman
))
181 if ( ! mdoc_reset(curp
.lastmdoc
))
184 curp
.lastmdoc
= NULL
;
193 (*curp
.outfree
)(curp
.outdata
);
195 mdoc_free(curp
.mdoc
);
199 return(rc
? EXIT_SUCCESS
: EXIT_FAILURE
);
207 (void)printf("%s %s\n", __progname
, VERSION
);
216 (void)fprintf(stderr
, "usage: %s [-V] [-foption...] "
217 "[-mformat] [-Toutput] [-Werr...]\n",
224 man_init(struct curparse
*curp
)
230 mancb
.man_err
= merr
;
231 mancb
.man_warn
= manwarn
;
233 /* Defaults from mandoc.1. */
235 pflags
= MAN_IGN_MACRO
| MAN_IGN_CHARS
;
237 if (curp
->fflags
& NO_IGN_MACRO
)
238 pflags
&= ~MAN_IGN_MACRO
;
239 if (curp
->fflags
& NO_IGN_CHARS
)
240 pflags
&= ~MAN_IGN_CHARS
;
242 if (NULL
== (man
= man_alloc(curp
, pflags
, &mancb
)))
243 warnx("memory exhausted");
250 mdoc_init(struct curparse
*curp
)
254 struct mdoc_cb mdoccb
;
256 mdoccb
.mdoc_err
= merr
;
257 mdoccb
.mdoc_warn
= mdocwarn
;
259 /* Defaults from mandoc.1. */
261 pflags
= MDOC_IGN_MACRO
| MDOC_IGN_ESCAPE
| MDOC_IGN_CHARS
;
263 if (curp
->fflags
& IGN_SCOPE
)
264 pflags
|= MDOC_IGN_SCOPE
;
265 if (curp
->fflags
& NO_IGN_ESCAPE
)
266 pflags
&= ~MDOC_IGN_ESCAPE
;
267 if (curp
->fflags
& NO_IGN_MACRO
)
268 pflags
&= ~MDOC_IGN_MACRO
;
269 if (curp
->fflags
& NO_IGN_CHARS
)
270 pflags
&= ~MDOC_IGN_CHARS
;
272 if (NULL
== (mdoc
= mdoc_alloc(curp
, pflags
, &mdoccb
)))
273 warnx("memory exhausted");
280 ffile(struct buf
*blk
, struct buf
*ln
,
281 const char *file
, struct curparse
*curp
)
286 if (-1 == (curp
->fd
= open(curp
->file
, O_RDONLY
, 0))) {
287 warn("%s", curp
->file
);
291 c
= fdesc(blk
, ln
, curp
);
293 if (-1 == close(curp
->fd
))
294 warn("%s", curp
->file
);
301 fdesc(struct buf
*blk
, struct buf
*ln
, struct curparse
*curp
)
306 int j
, i
, pos
, lnn
, comment
;
315 * Two buffers: ln and buf. buf is the input buffer optimised
316 * here for each file's block size. ln is a line buffer. Both
317 * growable, hence passed in by ptr-ptr.
320 if (-1 == fstat(curp
->fd
, &st
))
321 warnx("%s", curp
->file
);
322 else if ((size_t)st
.st_blksize
> sz
)
326 blk
->buf
= realloc(blk
->buf
, sz
);
327 if (NULL
== blk
->buf
) {
334 /* Fill buf with file blocksize. */
336 for (lnn
= pos
= comment
= 0; ; ) {
337 if (-1 == (ssz
= read(curp
->fd
, blk
->buf
, sz
))) {
338 warn("%s", curp
->file
);
343 /* Parse the read block into partial or full lines. */
345 for (i
= 0; i
< (int)ssz
; i
++) {
346 if (pos
>= (int)ln
->sz
) {
347 ln
->sz
+= 256; /* Step-size. */
348 ln
->buf
= realloc(ln
->buf
, ln
->sz
);
349 if (NULL
== ln
->buf
) {
355 if ('\n' != blk
->buf
[i
]) {
358 ln
->buf
[pos
++] = blk
->buf
[i
];
360 /* Handle in-line `\"' comments. */
362 if (1 == pos
|| '\"' != ln
->buf
[pos
- 1])
365 for (j
= pos
- 2; j
>= 0; j
--)
366 if ('\\' != ln
->buf
[j
])
369 if ( ! ((pos
- 2 - j
) % 2))
377 /* Handle escaped `\\n' newlines. */
379 if (pos
> 0 && 0 == comment
&&
380 '\\' == ln
->buf
[pos
- 1]) {
381 for (j
= pos
- 1; j
>= 0; j
--)
382 if ('\\' != ln
->buf
[j
])
384 if ( ! ((pos
- j
) % 2)) {
394 /* If unset, assign parser in pset(). */
396 if ( ! (man
|| mdoc
) && ! pset(ln
->buf
,
397 pos
, curp
, &man
, &mdoc
))
402 /* Pass down into parsers. */
404 if (man
&& ! man_parseln(man
, lnn
, ln
->buf
))
406 if (mdoc
&& ! mdoc_parseln(mdoc
, lnn
, ln
->buf
))
411 /* NOTE a parser may not have been assigned, yet. */
413 if ( ! (man
|| mdoc
)) {
414 warnx("%s: not a manual", curp
->file
);
418 if (mdoc
&& ! mdoc_endparse(mdoc
))
420 if (man
&& ! man_endparse(man
))
423 /* If unset, allocate output dev now (if applicable). */
425 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
426 switch (curp
->outtype
) {
428 curp
->outman
= tree_man
;
429 curp
->outmdoc
= tree_mdoc
;
434 curp
->outdata
= ascii_alloc();
435 curp
->outman
= terminal_man
;
436 curp
->outmdoc
= terminal_mdoc
;
437 curp
->outfree
= terminal_free
;
442 /* Execute the out device, if it exists. */
444 if (man
&& curp
->outman
)
445 if ( ! (*curp
->outman
)(curp
->outdata
, man
))
447 if (mdoc
&& curp
->outmdoc
)
448 if ( ! (*curp
->outmdoc
)(curp
->outdata
, mdoc
))
456 pset(const char *buf
, int pos
, struct curparse
*curp
,
457 struct man
**man
, struct mdoc
**mdoc
)
462 * Try to intuit which kind of manual parser should be used. If
463 * passed in by command-line (-man, -mdoc), then use that
464 * explicitly. If passed as -mandoc, then try to guess from the
465 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
466 * default to -man, which is more lenient.
470 for (i
= 1; buf
[i
]; i
++)
471 if (' ' != buf
[i
] && '\t' != buf
[i
])
477 switch (curp
->inttype
) {
479 if (NULL
== curp
->mdoc
)
480 curp
->mdoc
= mdoc_init(curp
);
481 if (NULL
== (*mdoc
= curp
->mdoc
))
483 curp
->lastmdoc
= *mdoc
;
486 if (NULL
== curp
->man
)
487 curp
->man
= man_init(curp
);
488 if (NULL
== (*man
= curp
->man
))
490 curp
->lastman
= *man
;
496 if (pos
>= 3 && 0 == memcmp(buf
, ".Dd", 3)) {
497 if (NULL
== curp
->mdoc
)
498 curp
->mdoc
= mdoc_init(curp
);
499 if (NULL
== (*mdoc
= curp
->mdoc
))
501 curp
->lastmdoc
= *mdoc
;
505 if (NULL
== curp
->man
)
506 curp
->man
= man_init(curp
);
507 if (NULL
== (*man
= curp
->man
))
509 curp
->lastman
= *man
;
515 moptions(enum intt
*tflags
, char *arg
)
518 if (0 == strcmp(arg
, "doc"))
520 else if (0 == strcmp(arg
, "andoc"))
522 else if (0 == strcmp(arg
, "an"))
525 warnx("bad argument: -m%s", arg
);
534 toptions(enum outt
*tflags
, char *arg
)
537 if (0 == strcmp(arg
, "ascii"))
538 *tflags
= OUTT_ASCII
;
539 else if (0 == strcmp(arg
, "lint"))
541 else if (0 == strcmp(arg
, "tree"))
544 warnx("bad argument: -T%s", arg
);
553 foptions(int *fflags
, char *arg
)
558 toks
[0] = "ign-scope";
559 toks
[1] = "no-ign-escape";
560 toks
[2] = "no-ign-macro";
561 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", arg
);
593 woptions(int *wflags
, char *arg
)
605 switch (getsubopt(&arg
, toks
, &v
)) {
607 *wflags
|= WARN_WALL
;
610 *wflags
|= WARN_WCOMPAT
;
613 *wflags
|= WARN_WSYNTAX
;
616 *wflags
|= WARN_WERR
;
619 warnx("bad argument: -W%s", arg
);
629 merr(void *arg
, int line
, int col
, const char *msg
)
631 struct curparse
*curp
;
633 curp
= (struct curparse
*)arg
;
634 warnx("%s:%d: error: %s (column %d)",
635 curp
->file
, line
, msg
, col
);
642 mdocwarn(void *arg
, int line
, int col
,
643 enum mdoc_warn type
, const char *msg
)
645 struct curparse
*curp
;
648 curp
= (struct curparse
*)arg
;
654 if (curp
->wflags
& WARN_WCOMPAT
)
659 if (curp
->wflags
& WARN_WSYNTAX
)
665 warnx("%s:%d: %s warning: %s (column %d)",
666 curp
->file
, line
, wtype
, msg
, col
);
668 if ( ! (curp
->wflags
& WARN_WERR
))
671 warnx("considering warnings as errors");
677 manwarn(void *arg
, int line
, int col
, const char *msg
)
679 struct curparse
*curp
;
681 curp
= (struct curparse
*)arg
;
683 if ( ! (curp
->wflags
& WARN_WSYNTAX
))
686 warnx("%s:%d: syntax warning: %s (column %d)",
687 curp
->file
, line
, msg
, col
);
689 if ( ! (curp
->wflags
& WARN_WERR
))
692 warnx("considering warnings as errors");