]>
git.cameronkatri.com Git - mandoc.git/blob - mmain.c
1 /* $Id: mmain.c,v 1.5 2009/03/06 14:13:47 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 int getsopts(struct mmain
*, char *);
52 static int parse(struct mmain
*);
53 static void msg_msg(void *, int, int, const char *);
54 static int msg_err(void *, int, int, const char *);
55 static int msg_warn(void *, int, int,
56 enum mdoc_warn
, const char *);
59 extern int getsubopt(char **, char *const *, char **);
60 extern size_t strlcpy(char *, const char *, size_t);
61 extern size_t strlcat(char *, const char *, size_t);
66 * Print our and our caller's usage message.
69 mmain_usage(const char *help
)
72 warnx("usage: %s %s%s[-v] [-Wwarn...] [infile]", __progname
,
73 help
? help
: "", help
? " " : "");
78 * Allocate the convenience library and initialise some values.
85 if (NULL
== (p
= calloc(1, sizeof(struct mmain
))))
89 p
->fdin
= STDIN_FILENO
;
96 * Parse command-line options. Accepts a small (<28 char) opstring "u"
97 * parameter (e.g. "ho:") or NULL, a corresponding "help" string (e.g.
98 * "[-h] [-o output]" or NULL, a callback function for parsed arguments
99 * and an opaque pointer argument for that function.
102 mmain_getopt(struct mmain
*p
, int argc
, char *argv
[],
103 const char *help
, const char *u
, void *arg
,
104 int (*getopt_cb
)(void *, int, const char *))
107 char opts
[32]; /* XXX */
112 sz
= strlcpy(opts
, "VvW:", 32);
116 sz
= strlcat(opts
, u
, 32);
123 while (-1 != (c
= getopt(argc
, argv
, opts
)))
129 (void)printf("%s %s\n", __progname
, VERSION
);
132 if ( ! getsopts(p
, optarg
))
140 if ((*getopt_cb
)(arg
, c
, optarg
))
146 if ((argc
-= optind
) > 0)
154 mmain_exit(struct mmain
*p
, int code
)
165 mmain_mdoc(struct mmain
*p
)
171 cb
.mdoc_err
= msg_err
;
172 cb
.mdoc_warn
= msg_warn
;
173 cb
.mdoc_msg
= msg_msg
;
175 if (0 != strcmp(p
->in
, "-"))
176 if (-1 == (p
->fdin
= open(p
->in
, O_RDONLY
, 0))) {
181 /* Allocate a buffer to be BUFSIZ/block size. */
183 if (-1 == fstat(p
->fdin
, &st
)) {
187 p
->bufsz
= MAX(st
.st_blksize
, BUFSIZ
);
189 p
->buf
= malloc(p
->bufsz
);
193 /* Allocate the parser. */
195 p
->mdoc
= mdoc_alloc(p
, &cb
);
197 /* Parse the input file. */
202 if (STDIN_FILENO
!= p
->fdin
)
203 if (-1 == close(p
->fdin
))
206 return(c
? p
->mdoc
: NULL
);
211 getsopts(struct mmain
*p
, char *arg
)
214 char *toks
[] = { "all", "compat",
215 "syntax", "error", NULL
};
218 switch (getsubopt(&arg
, toks
, &v
)) {
220 p
->warn
|= MD_WARN_ALL
;
223 p
->warn
|= MD_WARN_COMPAT
;
226 p
->warn
|= MD_WARN_SYNTAX
;
229 p
->warn
|= MD_WARN_ERR
;
240 parse(struct mmain
*p
)
244 char line
[MD_LINE_SZ
];
248 * This is a little more complicated than fgets. TODO: have
249 * some benchmarks that show it's faster (note that I want to
250 * check many, many manuals simultaneously, so speed is
251 * important). Fill a buffer (sized to the block size) with a
252 * single read, then parse \n-terminated lines into a line
253 * buffer, which is passed to the parser. Hard-code the line
254 * buffer to a particular size -- a reasonable assumption.
257 for (lnn
= 1, pos
= 0; ; ) {
258 if (-1 == (sz
= read(p
->fdin
, p
->buf
, p
->bufsz
))) {
264 for (i
= 0; i
< sz
; i
++) {
265 if ('\n' != p
->buf
[i
]) {
266 if (pos
< sizeof(line
)) {
267 line
[(int)pos
++] = p
->buf
[(int)i
];
270 warnx("%s: line %d too long", p
->in
, lnn
);
275 if ( ! mdoc_parseln(p
->mdoc
, lnn
, line
))
283 return(mdoc_endparse(p
->mdoc
));
288 msg_err(void *arg
, int line
, int col
, const char *msg
)
292 p
= (struct mmain
*)arg
;
294 warnx("%s:%d: error: %s (column %d)",
295 p
->in
, line
, msg
, col
);
301 msg_msg(void *arg
, int line
, int col
, const char *msg
)
305 p
= (struct mmain
*)arg
;
310 warnx("%s:%d: debug: %s (column %d)",
311 p
->in
, line
, msg
, col
);
316 msg_warn(void *arg
, int line
, int col
,
317 enum mdoc_warn type
, const char *msg
)
321 p
= (struct mmain
*)arg
;
325 if (p
->warn
& MD_WARN_COMPAT
)
329 if (p
->warn
& MD_WARN_SYNTAX
)
334 warnx("%s:%d: warning: %s (column %d)",
335 p
->in
, line
, msg
, col
);
337 if ( ! (p
->warn
& MD_WARN_ERR
))
340 warnx("%s: considering warnings as errors", __progname
);