]> git.cameronkatri.com Git - mandoc.git/blob - main.c
Make empty sections and parts (SH, SS, RS) only produce a warning if it
[mandoc.git] / main.c
1 /* $Id: main.c,v 1.158 2011/03/22 10:35:26 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include <assert.h>
23 #include <stdio.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include "mandoc.h"
30 #include "main.h"
31 #include "mdoc.h"
32 #include "man.h"
33
34 #if !defined(__GNUC__) || (__GNUC__ < 2)
35 # if !defined(lint)
36 # define __attribute__(x)
37 # endif
38 #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
39
40 typedef void (*out_mdoc)(void *, const struct mdoc *);
41 typedef void (*out_man)(void *, const struct man *);
42 typedef void (*out_free)(void *);
43
44 enum outt {
45 OUTT_ASCII = 0, /* -Tascii */
46 OUTT_TREE, /* -Ttree */
47 OUTT_HTML, /* -Thtml */
48 OUTT_XHTML, /* -Txhtml */
49 OUTT_LINT, /* -Tlint */
50 OUTT_PS, /* -Tps */
51 OUTT_PDF /* -Tpdf */
52 };
53
54 struct curparse {
55 struct mparse *mp;
56 enum mandoclevel wlevel; /* ignore messages below this */
57 int wstop; /* stop after a file with a warning */
58 enum outt outtype; /* which output to use */
59 out_mdoc outmdoc; /* mdoc output ptr */
60 out_man outman; /* man output ptr */
61 out_free outfree; /* free output ptr */
62 void *outdata; /* data for output */
63 char outopts[BUFSIZ]; /* buf of output opts */
64 };
65
66 static const char * const mandoclevels[MANDOCLEVEL_MAX] = {
67 "SUCCESS",
68 "RESERVED",
69 "WARNING",
70 "ERROR",
71 "FATAL",
72 "BADARG",
73 "SYSERR"
74 };
75
76 static const char * const mandocerrs[MANDOCERR_MAX] = {
77 "ok",
78
79 "generic warning",
80
81 /* related to the prologue */
82 "no title in document",
83 "document title should be all caps",
84 "unknown manual section",
85 "date missing, using today's date",
86 "cannot parse date, using it verbatim",
87 "prologue macros out of order",
88 "duplicate prologue macro",
89 "macro not allowed in prologue",
90 "macro not allowed in body",
91
92 /* related to document structure */
93 ".so is fragile, better use ln(1)",
94 "NAME section must come first",
95 "bad NAME section contents",
96 "manual name not yet set",
97 "sections out of conventional order",
98 "duplicate section name",
99 "section not in conventional manual section",
100
101 /* related to macros and nesting */
102 "skipping obsolete macro",
103 "skipping paragraph macro",
104 "skipping no-space macro",
105 "blocks badly nested",
106 "child violates parent syntax",
107 "nested displays are not portable",
108 "already in literal mode",
109
110 /* related to missing macro arguments */
111 "skipping empty macro",
112 "argument count wrong",
113 "missing display type",
114 "list type must come first",
115 "tag lists require a width argument",
116 "missing font type",
117 "skipping end of block that is not open",
118
119 /* related to bad macro arguments */
120 "skipping argument",
121 "duplicate argument",
122 "duplicate display type",
123 "duplicate list type",
124 "unknown AT&T UNIX version",
125 "bad Boolean value",
126 "unknown font",
127 "unknown standard specifier",
128 "bad width argument",
129
130 /* related to plain text */
131 "blank line in non-literal context",
132 "tab in non-literal context",
133 "end of line whitespace",
134 "bad comment style",
135 "unknown escape sequence",
136 "unterminated quoted string",
137
138 "generic error",
139
140 /* related to tables */
141 "bad table syntax",
142 "bad table option",
143 "bad table layout",
144 "no table layout cells specified",
145 "no table data cells specified",
146 "ignore data in cell",
147 "data block still open",
148 "ignoring extra data cells",
149
150 "input stack limit exceeded, infinite loop?",
151 "skipping bad character",
152 "escaped character not allowed in a name",
153 "skipping text before the first section header",
154 "skipping unknown macro",
155 "NOT IMPLEMENTED, please use groff: skipping request",
156 "line scope broken",
157 "argument count wrong",
158 "skipping end of block that is not open",
159 "missing end of block",
160 "scope open on exit",
161 "uname(3) system call failed",
162 "macro requires line argument(s)",
163 "macro requires body argument(s)",
164 "macro requires argument(s)",
165 "missing list type",
166 "line argument(s) will be lost",
167 "body argument(s) will be lost",
168
169 "generic fatal error",
170
171 "not a manual",
172 "column syntax is inconsistent",
173 "NOT IMPLEMENTED: .Bd -file",
174 "line scope broken, syntax violated",
175 "argument count wrong, violates syntax",
176 "child violates parent syntax",
177 "argument count wrong, violates syntax",
178 "NOT IMPLEMENTED: .so with absolute path or \"..\"",
179 "no document body",
180 "no document prologue",
181 "static buffer exhausted",
182 };
183
184 static int moptions(enum mparset *, char *);
185 static void mmsg(enum mandocerr, enum mandoclevel,
186 const char *, int, int, const char *);
187 static void parse(struct curparse *, int,
188 const char *, enum mandoclevel *);
189 static int toptions(struct curparse *, char *);
190 static void usage(void) __attribute__((noreturn));
191 static void version(void) __attribute__((noreturn));
192 static int woptions(struct curparse *, char *);
193
194 static const char *progname;
195
196 int
197 main(int argc, char *argv[])
198 {
199 int c;
200 struct curparse curp;
201 enum mparset type;
202 enum mandoclevel rc;
203
204 progname = strrchr(argv[0], '/');
205 if (progname == NULL)
206 progname = argv[0];
207 else
208 ++progname;
209
210 memset(&curp, 0, sizeof(struct curparse));
211
212 type = MPARSE_AUTO;
213 curp.outtype = OUTT_ASCII;
214 curp.wlevel = MANDOCLEVEL_FATAL;
215
216 /* LINTED */
217 while (-1 != (c = getopt(argc, argv, "m:O:T:VW:")))
218 switch (c) {
219 case ('m'):
220 if ( ! moptions(&type, optarg))
221 return((int)MANDOCLEVEL_BADARG);
222 break;
223 case ('O'):
224 (void)strlcat(curp.outopts, optarg, BUFSIZ);
225 (void)strlcat(curp.outopts, ",", BUFSIZ);
226 break;
227 case ('T'):
228 if ( ! toptions(&curp, optarg))
229 return((int)MANDOCLEVEL_BADARG);
230 break;
231 case ('W'):
232 if ( ! woptions(&curp, optarg))
233 return((int)MANDOCLEVEL_BADARG);
234 break;
235 case ('V'):
236 version();
237 /* NOTREACHED */
238 default:
239 usage();
240 /* NOTREACHED */
241 }
242
243 curp.mp = mparse_alloc(type, curp.wlevel, mmsg, &curp);
244
245 argc -= optind;
246 argv += optind;
247
248 rc = MANDOCLEVEL_OK;
249
250 if (NULL == *argv)
251 parse(&curp, STDIN_FILENO, "<stdin>", &rc);
252
253 while (*argv) {
254 parse(&curp, -1, *argv, &rc);
255 if (MANDOCLEVEL_OK != rc && curp.wstop)
256 break;
257 ++argv;
258 }
259
260 if (curp.outfree)
261 (*curp.outfree)(curp.outdata);
262 if (curp.mp)
263 mparse_free(curp.mp);
264
265 return((int)rc);
266 }
267
268 static void
269 version(void)
270 {
271
272 printf("%s %s\n", progname, VERSION);
273 exit((int)MANDOCLEVEL_OK);
274 }
275
276 static void
277 usage(void)
278 {
279
280 fprintf(stderr, "usage: %s "
281 "[-V] "
282 "[-foption] "
283 "[-mformat] "
284 "[-Ooption] "
285 "[-Toutput] "
286 "[-Werr] "
287 "[file...]\n",
288 progname);
289
290 exit((int)MANDOCLEVEL_BADARG);
291 }
292
293 static void
294 parse(struct curparse *curp, int fd,
295 const char *file, enum mandoclevel *level)
296 {
297 enum mandoclevel rc;
298 struct mdoc *mdoc;
299 struct man *man;
300
301 /* Begin by parsing the file itself. */
302
303 assert(file);
304 assert(fd >= -1);
305
306 rc = mparse_readfd(curp->mp, fd, file);
307
308 /* Stop immediately if the parse has failed. */
309
310 if (MANDOCLEVEL_FATAL <= rc)
311 goto cleanup;
312
313 /*
314 * With -Wstop and warnings or errors of at least the requested
315 * level, do not produce output.
316 */
317
318 if (MANDOCLEVEL_OK != rc && curp->wstop)
319 goto cleanup;
320
321 /* If unset, allocate output dev now (if applicable). */
322
323 if ( ! (curp->outman && curp->outmdoc)) {
324 switch (curp->outtype) {
325 case (OUTT_XHTML):
326 curp->outdata = xhtml_alloc(curp->outopts);
327 break;
328 case (OUTT_HTML):
329 curp->outdata = html_alloc(curp->outopts);
330 break;
331 case (OUTT_ASCII):
332 curp->outdata = ascii_alloc(curp->outopts);
333 curp->outfree = ascii_free;
334 break;
335 case (OUTT_PDF):
336 curp->outdata = pdf_alloc(curp->outopts);
337 curp->outfree = pspdf_free;
338 break;
339 case (OUTT_PS):
340 curp->outdata = ps_alloc(curp->outopts);
341 curp->outfree = pspdf_free;
342 break;
343 default:
344 break;
345 }
346
347 switch (curp->outtype) {
348 case (OUTT_HTML):
349 /* FALLTHROUGH */
350 case (OUTT_XHTML):
351 curp->outman = html_man;
352 curp->outmdoc = html_mdoc;
353 curp->outfree = html_free;
354 break;
355 case (OUTT_TREE):
356 curp->outman = tree_man;
357 curp->outmdoc = tree_mdoc;
358 break;
359 case (OUTT_PDF):
360 /* FALLTHROUGH */
361 case (OUTT_ASCII):
362 /* FALLTHROUGH */
363 case (OUTT_PS):
364 curp->outman = terminal_man;
365 curp->outmdoc = terminal_mdoc;
366 break;
367 default:
368 break;
369 }
370 }
371
372 mparse_result(curp->mp, &mdoc, &man);
373
374 /* Execute the out device, if it exists. */
375
376 if (man && curp->outman)
377 (*curp->outman)(curp->outdata, man);
378 if (mdoc && curp->outmdoc)
379 (*curp->outmdoc)(curp->outdata, mdoc);
380
381 cleanup:
382
383 mparse_reset(curp->mp);
384
385 if (*level < rc)
386 *level = rc;
387 }
388
389 static int
390 moptions(enum mparset *tflags, char *arg)
391 {
392
393 if (0 == strcmp(arg, "doc"))
394 *tflags = MPARSE_MDOC;
395 else if (0 == strcmp(arg, "andoc"))
396 *tflags = MPARSE_AUTO;
397 else if (0 == strcmp(arg, "an"))
398 *tflags = MPARSE_MAN;
399 else {
400 fprintf(stderr, "%s: Bad argument\n", arg);
401 return(0);
402 }
403
404 return(1);
405 }
406
407 static int
408 toptions(struct curparse *curp, char *arg)
409 {
410
411 if (0 == strcmp(arg, "ascii"))
412 curp->outtype = OUTT_ASCII;
413 else if (0 == strcmp(arg, "lint")) {
414 curp->outtype = OUTT_LINT;
415 curp->wlevel = MANDOCLEVEL_WARNING;
416 } else if (0 == strcmp(arg, "tree"))
417 curp->outtype = OUTT_TREE;
418 else if (0 == strcmp(arg, "html"))
419 curp->outtype = OUTT_HTML;
420 else if (0 == strcmp(arg, "xhtml"))
421 curp->outtype = OUTT_XHTML;
422 else if (0 == strcmp(arg, "ps"))
423 curp->outtype = OUTT_PS;
424 else if (0 == strcmp(arg, "pdf"))
425 curp->outtype = OUTT_PDF;
426 else {
427 fprintf(stderr, "%s: Bad argument\n", arg);
428 return(0);
429 }
430
431 return(1);
432 }
433
434 static int
435 woptions(struct curparse *curp, char *arg)
436 {
437 char *v, *o;
438 const char *toks[6];
439
440 toks[0] = "stop";
441 toks[1] = "all";
442 toks[2] = "warning";
443 toks[3] = "error";
444 toks[4] = "fatal";
445 toks[5] = NULL;
446
447 while (*arg) {
448 o = arg;
449 switch (getsubopt(&arg, UNCONST(toks), &v)) {
450 case (0):
451 curp->wstop = 1;
452 break;
453 case (1):
454 /* FALLTHROUGH */
455 case (2):
456 curp->wlevel = MANDOCLEVEL_WARNING;
457 break;
458 case (3):
459 curp->wlevel = MANDOCLEVEL_ERROR;
460 break;
461 case (4):
462 curp->wlevel = MANDOCLEVEL_FATAL;
463 break;
464 default:
465 fprintf(stderr, "-W%s: Bad argument\n", o);
466 return(0);
467 }
468 }
469
470 return(1);
471 }
472
473 static void
474 mmsg(enum mandocerr t, enum mandoclevel lvl,
475 const char *file, int line, int col, const char *msg)
476 {
477
478 fprintf(stderr, "%s:%d:%d: %s: %s",
479 file, line, col + 1,
480 mandoclevels[lvl], mandocerrs[t]);
481
482 if (msg)
483 fprintf(stderr, ": %s", msg);
484
485 fputc('\n', stderr);
486 }