]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.38 2009/07/07 09:52:08 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 mwarn(void *, int, int, const char *);
100 static int ffile(struct buf
*, struct buf
*,
101 const char *, struct curparse
*);
102 static int fdesc(struct buf
*, struct buf
*,
104 static int pset(const char *, int, struct curparse
*,
105 struct man
**, struct mdoc
**);
106 static struct man
*man_init(struct curparse
*);
107 static struct mdoc
*mdoc_init(struct curparse
*);
108 __dead
static void version(void);
109 __dead
static void usage(void);
111 extern char *__progname
;
115 main(int argc
, char *argv
[])
119 struct curparse curp
;
121 bzero(&curp
, sizeof(struct curparse
));
123 curp
.inttype
= INTT_AUTO
;
124 curp
.outtype
= OUTT_ASCII
;
127 while (-1 != (c
= getopt(argc
, argv
, "f:m:VW:T:")))
130 if ( ! foptions(&curp
.fflags
, optarg
))
131 return(EXIT_FAILURE
);
134 if ( ! moptions(&curp
.inttype
, optarg
))
135 return(EXIT_FAILURE
);
138 if ( ! toptions(&curp
.outtype
, optarg
))
139 return(EXIT_FAILURE
);
142 if ( ! woptions(&curp
.wflags
, optarg
))
143 return(EXIT_FAILURE
);
156 bzero(&ln
, sizeof(struct buf
));
157 bzero(&blk
, sizeof(struct buf
));
162 curp
.file
= "<stdin>";
163 curp
.fd
= STDIN_FILENO
;
164 if ( ! fdesc(&blk
, &ln
, &curp
))
168 while (rc
&& *argv
) {
169 if ( ! ffile(&blk
, &ln
, *argv
, &curp
))
174 if ( ! man_reset(curp
.lastman
))
177 if ( ! mdoc_reset(curp
.lastmdoc
))
180 curp
.lastmdoc
= NULL
;
189 (*curp
.outfree
)(curp
.outdata
);
191 mdoc_free(curp
.mdoc
);
195 return(rc
? EXIT_SUCCESS
: EXIT_FAILURE
);
203 (void)printf("%s %s\n", __progname
, VERSION
);
212 (void)fprintf(stderr
, "usage: %s [-V] [-foption...] "
213 "[-mformat] [-Toutput] [-Werr...]\n",
220 man_init(struct curparse
*curp
)
226 mancb
.man_err
= merr
;
227 mancb
.man_warn
= mwarn
;
229 /* Defaults from mandoc.1. */
231 pflags
= MAN_IGN_MACRO
| MAN_IGN_ESCAPE
| MAN_IGN_CHARS
;
233 if (curp
->fflags
& NO_IGN_MACRO
)
234 pflags
&= ~MAN_IGN_MACRO
;
235 if (curp
->fflags
& NO_IGN_CHARS
)
236 pflags
&= ~MAN_IGN_CHARS
;
237 if (curp
->fflags
& NO_IGN_ESCAPE
)
238 pflags
&= ~MAN_IGN_ESCAPE
;
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
= mwarn
;
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 warn("%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";
565 switch (getsubopt(&arg
, toks
, &v
)) {
567 *fflags
|= IGN_SCOPE
;
570 *fflags
|= NO_IGN_ESCAPE
;
573 *fflags
|= NO_IGN_MACRO
;
576 *fflags
|= NO_IGN_CHARS
;
579 *fflags
|= NO_IGN_ESCAPE
|
580 NO_IGN_MACRO
| NO_IGN_CHARS
;
583 warnx("bad argument: -f%s", o
);
593 woptions(int *wflags
, char *arg
)
604 switch (getsubopt(&arg
, toks
, &v
)) {
606 *wflags
|= WARN_WALL
;
609 *wflags
|= WARN_WERR
;
612 warnx("bad argument: -W%s", o
);
623 merr(void *arg
, int line
, int col
, const char *msg
)
625 struct curparse
*curp
;
627 curp
= (struct curparse
*)arg
;
629 warnx("%s:%d: error: %s (column %d)",
630 curp
->file
, line
, msg
, col
);
637 mwarn(void *arg
, int line
, int col
, const char *msg
)
639 struct curparse
*curp
;
641 curp
= (struct curparse
*)arg
;
643 if ( ! (curp
->wflags
& WARN_WALL
))
646 warnx("%s:%d: warning: %s (column %d)",
647 curp
->file
, line
, msg
, col
);
649 if ( ! (curp
->wflags
& WARN_WERR
))
652 warnx("considering warnings as errors");