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