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