Annotation of mandoc/main.c, Revision 1.3
1.3 ! kristaps 1: /* $Id: main.c,v 1.2 2009/03/19 16:18:36 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
4: *
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
8: * copies.
9: *
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.
18: */
19: #include <sys/stat.h>
20:
21: #include <assert.h>
22: #include <err.h>
23: #include <fcntl.h>
24: #include <stdio.h>
25: #include <stdlib.h>
26: #include <string.h>
27: #include <unistd.h>
28:
29: #include "mdoc.h"
30:
1.3 ! kristaps 31: #ifdef __linux__
! 32: extern int getsubopt(char **, char * const *, char **);
! 33: # ifndef __dead
! 34: # define __dead __attribute__((__noreturn__))
! 35: # endif
! 36: #endif
! 37:
1.1 kristaps 38: #define WARN_WALL 0x03 /* All-warnings mask. */
39: #define WARN_WCOMPAT (1 << 0) /* Compatibility warnings. */
40: #define WARN_WSYNTAX (1 << 1) /* Syntax warnings. */
41: #define WARN_WERR (1 << 2) /* Warnings->errors. */
42:
43: enum outt {
44: OUTT_ASCII,
45: OUTT_LATIN1,
46: OUTT_UTF8,
47: OUTT_TREE,
48: OUTT_LINT
49: };
50:
51: typedef int (*out_run)(void *, const struct mdoc *);
52: typedef void (*out_free)(void *);
53:
54: extern char *__progname;
55:
56: extern void *ascii_alloc(void);
57: extern void *latin1_alloc(void);
58: extern void *utf8_alloc(void);
59: extern int terminal_run(void *, const struct mdoc *);
60: extern int tree_run(void *, const struct mdoc *);
61: extern void terminal_free(void *);
62:
63: __dead static void version(void);
64: __dead static void usage(void);
65: static int foptions(int *, char *);
66: static int toptions(enum outt *, char *);
67: static int woptions(int *, char *);
68: static int merr(void *, int, int, const char *);
69: static int mwarn(void *, int, int,
70: enum mdoc_warn, const char *);
71: static int file(char **, size_t *, char **, size_t *,
72: const char *, struct mdoc *);
73: static int fdesc(char **, size_t *, char **, size_t *,
74: const char *, int, struct mdoc *);
75:
76:
77: int
78: main(int argc, char *argv[])
79: {
80: int c, rc, fflags, wflags;
81: struct mdoc_cb cb;
82: struct mdoc *mdoc;
83: char *buf, *line;
84: size_t bufsz, linesz;
85: void *outdata;
86: enum outt outtype;
87: out_run outrun;
88: out_free outfree;
89:
90: fflags = wflags = 0;
91: outtype = OUTT_ASCII;
92:
93: /* LINTED */
94: while (-1 != (c = getopt(argc, argv, "f:VW:T:")))
95: switch (c) {
96: case ('f'):
97: if ( ! foptions(&fflags, optarg))
98: return(0);
99: break;
100: case ('T'):
101: if ( ! toptions(&outtype, optarg))
102: return(0);
103: break;
104: case ('W'):
105: if ( ! woptions(&wflags, optarg))
106: return(0);
107: break;
108: case ('V'):
109: version();
110: /* NOTREACHED */
111: default:
112: usage();
113: /* NOTREACHED */
114: }
115:
116: argc -= optind;
117: argv += optind;
118:
119: /*
120: * Allocate the appropriate front-end. Note that utf8, ascii
121: * and latin1 all resolve to the terminal front-end with
122: * different encodings (see terminal.c). Not all frontends have
123: * cleanup or alloc routines.
124: */
125:
126: switch (outtype) {
127: case (OUTT_LATIN1):
128: outdata = latin1_alloc();
129: outrun = terminal_run;
130: outfree = terminal_free;
131: break;
132: case (OUTT_UTF8):
133: outdata = utf8_alloc();
134: outrun = terminal_run;
135: outfree = terminal_free;
136: break;
137: case (OUTT_TREE):
138: outdata = NULL;
139: outrun = tree_run;
140: outfree = NULL;
141: break;
142: case (OUTT_LINT):
143: outdata = NULL;
144: outrun = NULL;
145: outfree = NULL;
146: break;
147: default:
148: outdata = ascii_alloc();
149: outrun = terminal_run;
150: outfree = terminal_free;
151: break;
152: }
153:
154: /*
155: * All callbacks route into here, where we print them onto the
156: * screen. XXX - for now, no path for debugging messages.
157: */
158:
159: cb.mdoc_msg = NULL;
160: cb.mdoc_err = merr;
161: cb.mdoc_warn = mwarn;
162:
163: buf = line = NULL;
164: bufsz = linesz = 0;
165:
166: mdoc = mdoc_alloc(&wflags, fflags, &cb);
167:
168: while (*argv) {
169: if ( ! file(&line, &linesz, &buf, &bufsz, *argv, mdoc))
170: break;
171: if (outrun && ! (*outrun)(outdata, mdoc))
172: break;
173:
174: /* Reset the parser for another file. */
175: mdoc_reset(mdoc);
176: argv++;
177: }
178:
179: rc = NULL == *argv;
180:
181: if (buf)
182: free(buf);
183: if (line)
184: free(line);
185: if (outfree)
186: (*outfree)(outdata);
187:
188: mdoc_free(mdoc);
189:
190: return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
191: }
192:
193:
194: __dead static void
195: version(void)
196: {
197:
198: (void)printf("%s %s\n", __progname, VERSION);
199: exit(0);
200: /* NOTREACHED */
201: }
202:
203:
204: __dead static void
205: usage(void)
206: {
207:
208: (void)fprintf(stderr, "usage: %s\n", __progname);
209: exit(1);
210: /* NOTREACHED */
211: }
212:
213:
214: static int
215: file(char **ln, size_t *lnsz, char **buf, size_t *bufsz,
216: const char *file, struct mdoc *mdoc)
217: {
218: int fd, c;
219:
220: if (-1 == (fd = open(file, O_RDONLY, 0))) {
221: warn("%s", file);
222: return(0);
223: }
224:
225: c = fdesc(ln, lnsz, buf, bufsz, file, fd, mdoc);
226:
227: if (-1 == close(fd))
228: warn("%s", file);
229:
230: return(c);
231: }
232:
233:
234: static int
235: fdesc(char **lnp, size_t *lnsz, char **bufp, size_t *bufsz,
236: const char *f, int fd, struct mdoc *mdoc)
237: {
238: size_t sz;
239: ssize_t ssz;
240: struct stat st;
241: int j, i, pos, lnn;
242: char *ln, *buf;
243:
244: buf = *bufp;
245: ln = *lnp;
246:
247: /*
248: * Two buffers: ln and buf. buf is the input buffer, optimised
249: * for each file's block size. ln is a line buffer. Both
250: * growable, hence passed in by ptr-ptr.
251: */
252:
253: if (-1 == fstat(fd, &st)) {
254: warnx("%s", f);
255: sz = BUFSIZ;
256: } else
257: sz = (unsigned)BUFSIZ > st.st_blksize ?
258: (size_t)BUFSIZ : st.st_blksize;
259:
260: if (sz > *bufsz) {
261: if (NULL == (buf = realloc(buf, sz)))
262: err(1, "realloc");
263: *bufp = buf;
264: *bufsz = sz;
265: }
266:
267: /*
268: * Fill buf with file blocksize and parse newlines into ln.
269: */
270:
271: for (lnn = 1, pos = 0; ; ) {
272: if (-1 == (ssz = read(fd, buf, sz))) {
273: warn("%s", f);
274: return(0);
275: } else if (0 == ssz)
276: break;
277:
278: for (i = 0; i < (int)ssz; i++) {
279: if (pos >= (int)*lnsz) {
280: *lnsz += 256; /* Step-size. */
281: ln = realloc(ln, *lnsz);
282: if (NULL == ln)
283: err(1, "realloc");
284: *lnp = ln;
285: }
286:
287: if ('\n' != buf[i]) {
288: ln[pos++] = buf[i];
289: continue;
290: }
291:
292: /* Check for CPP-escaped newline. */
293:
294: if (pos > 0 && '\\' == ln[pos - 1]) {
295: for (j = pos - 1; j >= 0; j--)
296: if ('\\' != ln[j])
297: break;
298:
299: if ( ! ((pos - j) % 2)) {
300: pos--;
301: lnn++;
302: continue;
303: }
304: }
305:
306: ln[pos] = 0;
307: if ( ! mdoc_parseln(mdoc, lnn, ln))
308: return(0);
309: lnn++;
310: pos = 0;
311: }
312: }
313:
314: return(mdoc_endparse(mdoc));
315: }
316:
317:
318: static int
319: toptions(enum outt *tflags, char *arg)
320: {
321:
322: if (0 == strcmp(arg, "ascii"))
323: *tflags = OUTT_ASCII;
324: else if (0 == strcmp(arg, "latin1"))
325: *tflags = OUTT_LATIN1;
326: else if (0 == strcmp(arg, "utf8"))
327: *tflags = OUTT_UTF8;
328: else if (0 == strcmp(arg, "lint"))
329: *tflags = OUTT_LINT;
330: else if (0 == strcmp(arg, "tree"))
331: *tflags = OUTT_TREE;
332: else {
333: warnx("bad argument: -T%s", arg);
334: return(0);
335: }
336:
337: return(1);
338: }
339:
340:
341: /*
342: * Parse out the options for [-fopt...] setting compiler options. These
343: * can be comma-delimited or called again.
344: */
345: static int
346: foptions(int *fflags, char *arg)
347: {
348: char *v;
349: char *toks[4];
350:
351: toks[0] = "ign-scope";
352: toks[1] = "ign-escape";
353: toks[2] = "ign-macro";
354: toks[3] = NULL;
355:
356: while (*arg)
357: switch (getsubopt(&arg, toks, &v)) {
358: case (0):
359: *fflags |= MDOC_IGN_SCOPE;
360: break;
361: case (1):
362: *fflags |= MDOC_IGN_ESCAPE;
363: break;
364: case (2):
365: *fflags |= MDOC_IGN_MACRO;
366: break;
367: default:
368: warnx("bad argument: -f%s", arg);
369: return(0);
370: }
371:
372: return(1);
373: }
374:
375:
376: /*
377: * Parse out the options for [-Werr...], which sets warning modes.
378: * These can be comma-delimited or called again.
379: */
380: static int
381: woptions(int *wflags, char *arg)
382: {
383: char *v;
384: char *toks[5];
385:
386: toks[0] = "all";
387: toks[1] = "compat";
388: toks[2] = "syntax";
389: toks[3] = "error";
390: toks[4] = NULL;
391:
392: while (*arg)
393: switch (getsubopt(&arg, toks, &v)) {
394: case (0):
395: *wflags |= WARN_WALL;
396: break;
397: case (1):
398: *wflags |= WARN_WCOMPAT;
399: break;
400: case (2):
401: *wflags |= WARN_WSYNTAX;
402: break;
403: case (3):
404: *wflags |= WARN_WERR;
405: break;
406: default:
407: warnx("bad argument: -W%s", arg);
408: return(0);
409: }
410:
411: return(1);
412: }
413:
414:
1.2 kristaps 415: /* ARGSUSED */
1.1 kristaps 416: static int
417: merr(void *arg, int line, int col, const char *msg)
418: {
419:
420: warnx("error: %s (line %d, column %d)", msg, line, col);
421: return(0);
422: }
423:
424:
425: static int
426: mwarn(void *arg, int line, int col,
427: enum mdoc_warn type, const char *msg)
428: {
429: int flags;
430: char *wtype;
431:
432: flags = *(int *)arg;
433: wtype = NULL;
434:
435: switch (type) {
436: case (WARN_COMPAT):
437: wtype = "compat";
438: if (flags & WARN_WCOMPAT)
439: break;
440: return(1);
441: case (WARN_SYNTAX):
442: wtype = "syntax";
443: if (flags & WARN_WSYNTAX)
444: break;
445: return(1);
446: }
447:
448: assert(wtype);
449: warnx("%s warning: %s (line %d, column %d)",
450: wtype, msg, line, col);
451:
452: if ( ! (flags & WARN_WERR))
453: return(1);
454:
455: warnx("%s: considering warnings as errors",
456: __progname);
457: return(0);
458: }
459:
460:
CVSweb