]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.30 2009/06/16 20:22:23 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
;
237 if (curp
->fflags
& NO_IGN_MACRO
)
238 pflags
&= ~MAN_IGN_MACRO
;
240 if (NULL
== (man
= man_alloc(curp
, pflags
, &mancb
)))
241 warnx("memory exhausted");
248 mdoc_init(struct curparse
*curp
)
252 struct mdoc_cb mdoccb
;
254 mdoccb
.mdoc_err
= merr
;
255 mdoccb
.mdoc_warn
= mdocwarn
;
257 /* Defaults from mandoc.1. */
259 pflags
= MDOC_IGN_MACRO
| MDOC_IGN_ESCAPE
| MDOC_IGN_CHARS
;
261 if (curp
->fflags
& IGN_SCOPE
)
262 pflags
|= MDOC_IGN_SCOPE
;
263 if (curp
->fflags
& NO_IGN_ESCAPE
)
264 pflags
&= ~MDOC_IGN_ESCAPE
;
265 if (curp
->fflags
& NO_IGN_MACRO
)
266 pflags
&= ~MDOC_IGN_MACRO
;
267 if (curp
->fflags
& NO_IGN_CHARS
)
268 pflags
&= ~MDOC_IGN_CHARS
;
270 if (NULL
== (mdoc
= mdoc_alloc(curp
, pflags
, &mdoccb
)))
271 warnx("memory exhausted");
278 ffile(struct buf
*blk
, struct buf
*ln
,
279 const char *file
, struct curparse
*curp
)
284 if (-1 == (curp
->fd
= open(curp
->file
, O_RDONLY
, 0))) {
285 warn("%s", curp
->file
);
289 c
= fdesc(blk
, ln
, curp
);
291 if (-1 == close(curp
->fd
))
292 warn("%s", curp
->file
);
299 fdesc(struct buf
*blk
, struct buf
*ln
, struct curparse
*curp
)
304 int j
, i
, pos
, lnn
, comment
;
313 * Two buffers: ln and buf. buf is the input buffer optimised
314 * here for each file's block size. ln is a line buffer. Both
315 * growable, hence passed in by ptr-ptr.
318 if (-1 == fstat(curp
->fd
, &st
))
319 warnx("%s", curp
->file
);
320 else if ((size_t)st
.st_blksize
> sz
)
324 blk
->buf
= realloc(blk
->buf
, sz
);
325 if (NULL
== blk
->buf
) {
332 /* Fill buf with file blocksize. */
334 for (lnn
= pos
= comment
= 0; ; ) {
335 if (-1 == (ssz
= read(curp
->fd
, blk
->buf
, sz
))) {
336 warn("%s", curp
->file
);
341 /* Parse the read block into partial or full lines. */
343 for (i
= 0; i
< (int)ssz
; i
++) {
344 if (pos
>= (int)ln
->sz
) {
345 ln
->sz
+= 256; /* Step-size. */
346 ln
->buf
= realloc(ln
->buf
, ln
->sz
);
347 if (NULL
== ln
->buf
) {
353 if ('\n' != blk
->buf
[i
]) {
356 ln
->buf
[pos
++] = blk
->buf
[i
];
358 /* Handle in-line `\"' comments. */
360 if (1 == pos
|| '\"' != ln
->buf
[pos
- 1])
363 for (j
= pos
- 2; j
>= 0; j
--)
364 if ('\\' != ln
->buf
[j
])
367 if ( ! ((pos
- 2 - j
) % 2))
375 /* Handle escaped `\\n' newlines. */
377 if (pos
> 0 && 0 == comment
&&
378 '\\' == ln
->buf
[pos
- 1]) {
379 for (j
= pos
- 1; j
>= 0; j
--)
380 if ('\\' != ln
->buf
[j
])
382 if ( ! ((pos
- j
) % 2)) {
392 /* If unset, assign parser in pset(). */
394 if ( ! (man
|| mdoc
) && ! pset(ln
->buf
,
395 pos
, curp
, &man
, &mdoc
))
400 /* Pass down into parsers. */
402 if (man
&& ! man_parseln(man
, lnn
, ln
->buf
))
404 if (mdoc
&& ! mdoc_parseln(mdoc
, lnn
, ln
->buf
))
409 /* NOTE a parser may not have been assigned, yet. */
411 if ( ! (man
|| mdoc
)) {
412 warnx("%s: not a manual", curp
->file
);
416 if (mdoc
&& ! mdoc_endparse(mdoc
))
418 if (man
&& ! man_endparse(man
))
421 /* If unset, allocate output dev now (if applicable). */
423 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
424 switch (curp
->outtype
) {
426 curp
->outman
= tree_man
;
427 curp
->outmdoc
= tree_mdoc
;
432 curp
->outdata
= ascii_alloc();
433 curp
->outman
= terminal_man
;
434 curp
->outmdoc
= terminal_mdoc
;
435 curp
->outfree
= terminal_free
;
440 /* Execute the out device, if it exists. */
442 if (man
&& curp
->outman
)
443 if ( ! (*curp
->outman
)(curp
->outdata
, man
))
445 if (mdoc
&& curp
->outmdoc
)
446 if ( ! (*curp
->outmdoc
)(curp
->outdata
, mdoc
))
454 pset(const char *buf
, int pos
, struct curparse
*curp
,
455 struct man
**man
, struct mdoc
**mdoc
)
460 * Try to intuit which kind of manual parser should be used. If
461 * passed in by command-line (-man, -mdoc), then use that
462 * explicitly. If passed as -mandoc, then try to guess from the
463 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
464 * default to -man, which is more lenient.
468 for (i
= 1; buf
[i
]; i
++)
469 if (' ' != buf
[i
] && '\t' != buf
[i
])
475 switch (curp
->inttype
) {
477 if (NULL
== curp
->mdoc
)
478 curp
->mdoc
= mdoc_init(curp
);
479 if (NULL
== (*mdoc
= curp
->mdoc
))
481 curp
->lastmdoc
= *mdoc
;
484 if (NULL
== curp
->man
)
485 curp
->man
= man_init(curp
);
486 if (NULL
== (*man
= curp
->man
))
488 curp
->lastman
= *man
;
494 if (pos
>= 3 && 0 == memcmp(buf
, ".Dd", 3)) {
495 if (NULL
== curp
->mdoc
)
496 curp
->mdoc
= mdoc_init(curp
);
497 if (NULL
== (*mdoc
= curp
->mdoc
))
499 curp
->lastmdoc
= *mdoc
;
503 if (NULL
== curp
->man
)
504 curp
->man
= man_init(curp
);
505 if (NULL
== (*man
= curp
->man
))
507 curp
->lastman
= *man
;
513 moptions(enum intt
*tflags
, char *arg
)
516 if (0 == strcmp(arg
, "doc"))
518 else if (0 == strcmp(arg
, "andoc"))
520 else if (0 == strcmp(arg
, "an"))
523 warnx("bad argument: -m%s", arg
);
532 toptions(enum outt
*tflags
, char *arg
)
535 if (0 == strcmp(arg
, "ascii"))
536 *tflags
= OUTT_ASCII
;
537 else if (0 == strcmp(arg
, "lint"))
539 else if (0 == strcmp(arg
, "tree"))
542 warnx("bad argument: -T%s", arg
);
551 foptions(int *fflags
, char *arg
)
556 toks
[0] = "ign-scope";
557 toks
[1] = "no-ign-escape";
558 toks
[2] = "no-ign-macro";
559 toks
[3] = "no-ign-chars";
564 switch (getsubopt(&arg
, toks
, &v
)) {
566 *fflags
|= IGN_SCOPE
;
569 *fflags
|= NO_IGN_ESCAPE
;
572 *fflags
|= NO_IGN_MACRO
;
575 *fflags
|= NO_IGN_CHARS
;
578 *fflags
|= NO_IGN_ESCAPE
|
579 NO_IGN_MACRO
| NO_IGN_CHARS
;
582 warnx("bad argument: -f%s", arg
);
591 woptions(int *wflags
, char *arg
)
603 switch (getsubopt(&arg
, toks
, &v
)) {
605 *wflags
|= WARN_WALL
;
608 *wflags
|= WARN_WCOMPAT
;
611 *wflags
|= WARN_WSYNTAX
;
614 *wflags
|= WARN_WERR
;
617 warnx("bad argument: -W%s", arg
);
627 merr(void *arg
, int line
, int col
, const char *msg
)
629 struct curparse
*curp
;
631 curp
= (struct curparse
*)arg
;
632 warnx("%s:%d: error: %s (column %d)",
633 curp
->file
, line
, msg
, col
);
640 mdocwarn(void *arg
, int line
, int col
,
641 enum mdoc_warn type
, const char *msg
)
643 struct curparse
*curp
;
646 curp
= (struct curparse
*)arg
;
652 if (curp
->wflags
& WARN_WCOMPAT
)
657 if (curp
->wflags
& WARN_WSYNTAX
)
663 warnx("%s:%d: %s warning: %s (column %d)",
664 curp
->file
, line
, wtype
, msg
, col
);
666 if ( ! (curp
->wflags
& WARN_WERR
))
669 warnx("considering warnings as errors");
675 manwarn(void *arg
, int line
, int col
, const char *msg
)
677 struct curparse
*curp
;
679 curp
= (struct curparse
*)arg
;
681 if ( ! (curp
->wflags
& WARN_WSYNTAX
))
684 warnx("%s:%d: syntax warning: %s (column %d)",
685 curp
->file
, line
, msg
, col
);
687 if ( ! (curp
->wflags
& WARN_WERR
))
690 warnx("considering warnings as errors");