]>
git.cameronkatri.com Git - mandoc.git/blob - mdoctree.c
1 /* $Id: mdoctree.c,v 1.1 2009/02/21 21:00:06 kristaps Exp $ */
3 * Copyright (c) 2008 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
7 * above copyright notice and this permission notice appear in all
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/param.h>
33 #define MD_LINE_SZ (256) /* Max input line size. */
36 int warn
; /* Warning flags. */
37 #define MD_WARN_SYNTAX (1 << 0) /* Show syntax warnings. */
38 #define MD_WARN_COMPAT (1 << 1) /* Show compat warnings. */
39 #define MD_WARN_ALL (0x03) /* Show all warnings. */
40 #define MD_WARN_ERR (1 << 2) /* Make warnings->errors. */
41 int dbg
; /* Debug level. */
42 struct mdoc
*mdoc
; /* Active parser. */
43 char *buf
; /* Input buffer. */
44 u_long bufsz
; /* Input buffer size. */
45 char *in
; /* Input file name. */
46 int fdin
; /* Input file desc. */
49 extern char *__progname
;
51 static void usage(void);
52 static int getsopts(struct md_parse
*, char *);
53 static int parse(struct md_parse
*);
54 static void msg_msg(void *, int, int, const char *);
55 static int msg_err(void *, int, int, const char *);
56 static int msg_warn(void *, int, int,
57 enum mdoc_warn
, const char *);
59 extern void treeprint(const struct mdoc_node
*,
60 const struct mdoc_meta
*);
63 extern int getsubopt(char **, char *const *, char **);
67 main(int argc
, char *argv
[])
76 (void)memset(&p
, 0, sizeof(struct md_parse
));
78 while (-1 != (c
= getopt(argc
, argv
, "vW:")))
84 if ( ! getsopts(&p
, optarg
))
95 /* Initialise the input file. */
98 p
.fdin
= STDIN_FILENO
;
102 p
.fdin
= open(p
.in
, O_RDONLY
, 0);
107 /* Allocate a buffer to be BUFSIZ/block size. */
109 if (-1 == fstat(p
.fdin
, &st
)) {
113 p
.bufsz
= MAX(st
.st_blksize
, BUFSIZ
);
115 p
.buf
= malloc(p
.bufsz
);
119 /* Allocate the parser. */
121 cb
.mdoc_err
= msg_err
;
122 cb
.mdoc_warn
= msg_warn
;
123 cb
.mdoc_msg
= msg_msg
;
125 p
.mdoc
= mdoc_alloc(&p
, &cb
);
127 /* Parse the input file. */
132 if (STDIN_FILENO
!= p
.fdin
&& -1 == close(p
.fdin
))
137 return(EXIT_FAILURE
);
140 /* If the parse succeeded, print it out. */
142 treeprint(mdoc_node(p
.mdoc
), mdoc_meta(p
.mdoc
));
145 return(EXIT_SUCCESS
);
150 getsopts(struct md_parse
*p
, char *arg
)
153 char *toks
[] = { "all", "compat",
154 "syntax", "error", NULL
};
157 switch (getsubopt(&arg
, toks
, &v
)) {
159 p
->warn
|= MD_WARN_ALL
;
162 p
->warn
|= MD_WARN_COMPAT
;
165 p
->warn
|= MD_WARN_SYNTAX
;
168 p
->warn
|= MD_WARN_ERR
;
180 parse(struct md_parse
*p
)
184 char line
[MD_LINE_SZ
];
188 * This is a little more complicated than fgets. TODO: have
189 * some benchmarks that show it's faster (note that I want to
190 * check many, many manuals simultaneously, so speed is
191 * important). Fill a buffer (sized to the block size) with a
192 * single read, then parse \n-terminated lines into a line
193 * buffer, which is passed to the parser. Hard-code the line
194 * buffer to a particular size -- a reasonable assumption.
197 for (lnn
= 1, pos
= 0; ; ) {
198 if (-1 == (sz
= read(p
->fdin
, p
->buf
, p
->bufsz
))) {
204 for (i
= 0; i
< sz
; i
++) {
205 if ('\n' != p
->buf
[i
]) {
206 if (pos
< sizeof(line
)) {
207 line
[(int)pos
++] = p
->buf
[(int)i
];
210 warnx("%s: line %d too long", p
->in
, lnn
);
215 if ( ! mdoc_parseln(p
->mdoc
, lnn
, line
))
223 return(mdoc_endparse(p
->mdoc
));
228 msg_err(void *arg
, int line
, int col
, const char *msg
)
232 p
= (struct md_parse
*)arg
;
234 warnx("%s:%d: error: %s (column %d)",
235 p
->in
, line
, msg
, col
);
241 msg_msg(void *arg
, int line
, int col
, const char *msg
)
245 p
= (struct md_parse
*)arg
;
250 warnx("%s:%d: debug: %s (column %d)",
251 p
->in
, line
, msg
, col
);
256 msg_warn(void *arg
, int line
, int col
,
257 enum mdoc_warn type
, const char *msg
)
261 p
= (struct md_parse
*)arg
;
265 if (p
->warn
& MD_WARN_COMPAT
)
269 if (p
->warn
& MD_WARN_SYNTAX
)
274 warnx("%s:%d: warning: %s (column %d)",
275 p
->in
, line
, msg
, col
);
277 if ( ! (p
->warn
& MD_WARN_ERR
))
280 warnx("%s: considering warnings as errors", __progname
);
289 warnx("usage: %s [-v] [-Wwarn...] [infile]", __progname
);