]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.28 2009/06/15 10:36:01 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 fstdin(struct buf
*, struct buf
*,
106 static int ffile(struct buf
*, struct buf
*,
107 const char *, struct curparse
*);
108 static int fdesc(struct buf
*, struct buf
*,
110 static int pset(const char *, int, struct curparse
*,
111 struct man
**, struct mdoc
**);
112 static struct man
*man_init(struct curparse
*);
113 static struct mdoc
*mdoc_init(struct curparse
*);
114 __dead
static void version(void);
115 __dead
static void usage(void);
117 extern char *__progname
;
121 main(int argc
, char *argv
[])
125 struct curparse curp
;
127 bzero(&curp
, sizeof(struct curparse
));
129 curp
.inttype
= INTT_AUTO
;
130 curp
.outtype
= OUTT_ASCII
;
133 while (-1 != (c
= getopt(argc
, argv
, "f:m:VW:T:")))
136 if ( ! foptions(&curp
.fflags
, optarg
))
140 if ( ! moptions(&curp
.inttype
, optarg
))
144 if ( ! toptions(&curp
.outtype
, optarg
))
148 if ( ! woptions(&curp
.wflags
, optarg
))
162 /* Configure buffers. */
164 bzero(&ln
, sizeof(struct buf
));
165 bzero(&blk
, sizeof(struct buf
));
170 if ( ! fstdin(&blk
, &ln
, &curp
))
173 while (rc
&& *argv
) {
174 if ( ! ffile(&blk
, &ln
, *argv
, &curp
))
179 if ( ! man_reset(curp
.lastman
))
182 if ( ! mdoc_reset(curp
.lastmdoc
))
185 curp
.lastmdoc
= NULL
;
194 /* TODO: have a curp_free routine. */
196 (*curp
.outfree
)(curp
.outdata
);
198 mdoc_free(curp
.mdoc
);
202 return(rc
? EXIT_SUCCESS
: EXIT_FAILURE
);
210 (void)printf("%s %s\n", __progname
, VERSION
);
219 (void)fprintf(stderr
, "usage: %s [-V] [-foption...] "
220 "[-mformat] [-Toutput] [-Werr...]\n",
227 man_init(struct curparse
*curp
)
233 mancb
.man_err
= merr
;
234 mancb
.man_warn
= manwarn
;
237 * Default behaviour is to ignore unknown macros. This is
238 * specified in mandoc.1.
241 pflags
= MAN_IGN_MACRO
;
243 /* Override default behaviour... */
245 if (curp
->fflags
& NO_IGN_MACRO
)
246 pflags
&= ~MAN_IGN_MACRO
;
248 if (NULL
== (man
= man_alloc(curp
, pflags
, &mancb
)))
249 warnx("memory exhausted");
256 mdoc_init(struct curparse
*curp
)
260 struct mdoc_cb mdoccb
;
262 mdoccb
.mdoc_err
= merr
;
263 mdoccb
.mdoc_warn
= mdocwarn
;
266 * Default behaviour is to ignore unknown macros, escape
267 * sequences and characters (very liberal). This is specified
271 pflags
= MDOC_IGN_MACRO
| MDOC_IGN_ESCAPE
| MDOC_IGN_CHARS
;
273 /* Override default behaviour... */
275 if (curp
->fflags
& IGN_SCOPE
)
276 pflags
|= MDOC_IGN_SCOPE
;
277 if (curp
->fflags
& NO_IGN_ESCAPE
)
278 pflags
&= ~MDOC_IGN_ESCAPE
;
279 if (curp
->fflags
& NO_IGN_MACRO
)
280 pflags
&= ~MDOC_IGN_MACRO
;
281 if (curp
->fflags
& NO_IGN_CHARS
)
282 pflags
&= ~MDOC_IGN_CHARS
;
284 if (NULL
== (mdoc
= mdoc_alloc(curp
, pflags
, &mdoccb
)))
285 warnx("memory exhausted");
292 fstdin(struct buf
*blk
, struct buf
*ln
, struct curparse
*curp
)
295 curp
->file
= "<stdin>";
296 curp
->fd
= STDIN_FILENO
;
297 return(fdesc(blk
, ln
, curp
));
302 ffile(struct buf
*blk
, struct buf
*ln
,
303 const char *file
, struct curparse
*curp
)
308 if (-1 == (curp
->fd
= open(curp
->file
, O_RDONLY
, 0))) {
309 warn("%s", curp
->file
);
313 c
= fdesc(blk
, ln
, curp
);
315 if (-1 == close(curp
->fd
))
316 warn("%s", curp
->file
);
323 fdesc(struct buf
*blk
, struct buf
*ln
, struct curparse
*curp
)
337 * Two buffers: ln and buf. buf is the input buffer optimised
338 * here for each file's block size. ln is a line buffer. Both
339 * growable, hence passed in by ptr-ptr.
342 if (-1 == fstat(curp
->fd
, &st
))
343 warnx("%s", curp
->file
);
344 else if ((size_t)st
.st_blksize
> sz
)
348 blk
->buf
= realloc(blk
->buf
, sz
);
349 if (NULL
== blk
->buf
) {
356 /* Fill buf with file blocksize. */
358 for (lnn
= 0, pos
= 0; ; ) {
359 if (-1 == (ssz
= read(curp
->fd
, blk
->buf
, sz
))) {
360 warn("%s", curp
->file
);
365 /* Parse the read block into partial or full lines. */
367 for (i
= 0; i
< (int)ssz
; i
++) {
368 if (pos
>= (int)ln
->sz
) {
369 ln
->sz
+= 256; /* Step-size. */
370 ln
->buf
= realloc(ln
->buf
, ln
->sz
);
371 if (NULL
== ln
->buf
) {
377 if ('\n' != blk
->buf
[i
]) {
378 ln
->buf
[pos
++] = blk
->buf
[i
];
382 /* Check for CPP-escaped newline. */
384 if (pos
> 0 && '\\' == ln
->buf
[pos
- 1]) {
385 for (j
= pos
- 1; j
>= 0; j
--)
386 if ('\\' != ln
->buf
[j
])
389 if ( ! ((pos
- j
) % 2)) {
400 * If no manual parser has been assigned, then
401 * try to assign one in pset(), which may do
402 * nothing at all. After this, parse the manual
406 if ( ! (man
|| mdoc
) && ! pset(ln
->buf
,
407 pos
, curp
, &man
, &mdoc
))
412 if (man
&& ! man_parseln(man
, lnn
, ln
->buf
))
414 if (mdoc
&& ! mdoc_parseln(mdoc
, lnn
, ln
->buf
))
419 /* Note that a parser may not have been assigned, yet. */
421 if ( ! (man
|| mdoc
)) {
422 warnx("%s: not a manual", curp
->file
);
426 if (mdoc
&& ! mdoc_endparse(mdoc
))
428 if (man
&& ! man_endparse(man
))
432 * If an output device hasn't been allocated, see if we should
433 * do so now. Note that not all outtypes have functions, so
434 * this switch statement may be superfluous, but it's
435 * low-overhead enough not to matter very much.
438 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
439 switch (curp
->outtype
) {
441 curp
->outman
= tree_man
;
442 curp
->outmdoc
= tree_mdoc
;
447 curp
->outdata
= ascii_alloc();
448 curp
->outman
= terminal_man
;
449 curp
->outmdoc
= terminal_mdoc
;
450 curp
->outfree
= terminal_free
;
455 /* Execute the out device, if it exists. */
457 if (man
&& curp
->outman
)
458 if ( ! (*curp
->outman
)(curp
->outdata
, man
))
460 if (mdoc
&& curp
->outmdoc
)
461 if ( ! (*curp
->outmdoc
)(curp
->outdata
, mdoc
))
469 pset(const char *buf
, int pos
, struct curparse
*curp
,
470 struct man
**man
, struct mdoc
**mdoc
)
474 * Try to intuit which kind of manual parser should be used. If
475 * passed in by command-line (-man, -mdoc), then use that
476 * explicitly. If passed as -mandoc, then try to guess from the
477 * line: either skip comments, use -mdoc when finding `.Dt', or
478 * default to -man, which is more lenient.
481 if (pos
>= 3 && 0 == memcmp(buf
, ".\\\"", 3))
484 switch (curp
->inttype
) {
486 if (NULL
== curp
->mdoc
)
487 curp
->mdoc
= mdoc_init(curp
);
488 if (NULL
== (*mdoc
= curp
->mdoc
))
490 curp
->lastmdoc
= *mdoc
;
493 if (NULL
== curp
->man
)
494 curp
->man
= man_init(curp
);
495 if (NULL
== (*man
= curp
->man
))
497 curp
->lastman
= *man
;
503 if (pos
>= 3 && 0 == memcmp(buf
, ".Dd", 3)) {
504 if (NULL
== curp
->mdoc
)
505 curp
->mdoc
= mdoc_init(curp
);
506 if (NULL
== (*mdoc
= curp
->mdoc
))
508 curp
->lastmdoc
= *mdoc
;
512 if (NULL
== curp
->man
)
513 curp
->man
= man_init(curp
);
514 if (NULL
== (*man
= curp
->man
))
516 curp
->lastman
= *man
;
522 moptions(enum intt
*tflags
, char *arg
)
525 if (0 == strcmp(arg
, "doc"))
527 else if (0 == strcmp(arg
, "andoc"))
529 else if (0 == strcmp(arg
, "an"))
532 warnx("bad argument: -m%s", arg
);
541 toptions(enum outt
*tflags
, char *arg
)
544 if (0 == strcmp(arg
, "ascii"))
545 *tflags
= OUTT_ASCII
;
546 else if (0 == strcmp(arg
, "lint"))
548 else if (0 == strcmp(arg
, "tree"))
551 warnx("bad argument: -T%s", arg
);
560 * Parse out the options for [-fopt...] setting compiler options. These
561 * can be comma-delimited or called again.
564 foptions(int *fflags
, char *arg
)
569 toks
[0] = "ign-scope";
570 toks
[1] = "no-ign-escape";
571 toks
[2] = "no-ign-macro";
572 toks
[3] = "no-ign-chars";
577 switch (getsubopt(&arg
, toks
, &v
)) {
579 *fflags
|= IGN_SCOPE
;
582 *fflags
|= NO_IGN_ESCAPE
;
585 *fflags
|= NO_IGN_MACRO
;
588 *fflags
|= NO_IGN_CHARS
;
591 *fflags
|= NO_IGN_ESCAPE
|
592 NO_IGN_MACRO
| NO_IGN_CHARS
;
595 warnx("bad argument: -f%s", arg
);
604 * Parse out the options for [-Werr...], which sets warning modes.
605 * These can be comma-delimited or called again.
608 woptions(int *wflags
, char *arg
)
620 switch (getsubopt(&arg
, toks
, &v
)) {
622 *wflags
|= WARN_WALL
;
625 *wflags
|= WARN_WCOMPAT
;
628 *wflags
|= WARN_WSYNTAX
;
631 *wflags
|= WARN_WERR
;
634 warnx("bad argument: -W%s", arg
);
644 merr(void *arg
, int line
, int col
, const char *msg
)
646 struct curparse
*curp
;
648 curp
= (struct curparse
*)arg
;
649 warnx("%s:%d: error: %s (column %d)",
650 curp
->file
, line
, msg
, col
);
652 /* Always exit on errors... */
658 mdocwarn(void *arg
, int line
, int col
,
659 enum mdoc_warn type
, const char *msg
)
661 struct curparse
*curp
;
664 curp
= (struct curparse
*)arg
;
670 if (curp
->wflags
& WARN_WCOMPAT
)
675 if (curp
->wflags
& WARN_WSYNTAX
)
681 warnx("%s:%d: %s warning: %s (column %d)",
682 curp
->file
, line
, wtype
, msg
, col
);
684 if ( ! (curp
->wflags
& WARN_WERR
))
688 * If the -Werror flag is passed in, as in gcc, then all
689 * warnings are considered as errors.
692 warnx("%s: considering warnings as errors",
699 manwarn(void *arg
, int line
, int col
, const char *msg
)
701 struct curparse
*curp
;
703 curp
= (struct curparse
*)arg
;
705 if ( ! (curp
->wflags
& WARN_WSYNTAX
))
708 warnx("%s:%d: syntax warning: %s (column %d)",
709 curp
->file
, line
, msg
, col
);
711 if ( ! (curp
->wflags
& WARN_WERR
))
715 * If the -Werror flag is passed in, as in gcc, then all
716 * warnings are considered as errors.
719 warnx("%s: considering warnings as errors",