Annotation of mandoc/main.c, Revision 1.61
1.61 ! kristaps 1: /* $Id: main.c,v 1.60 2010/03/22 20:43:00 kristaps Exp $ */
1.1 kristaps 2: /*
1.26 kristaps 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.25 kristaps 6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 8: *
1.25 kristaps 9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 16: */
1.58 kristaps 17: #ifdef HAVE_CONFIG_H
18: #include "config.h"
19: #endif
20:
1.1 kristaps 21: #include <sys/stat.h>
22:
23: #include <assert.h>
24: #include <fcntl.h>
25: #include <stdio.h>
1.45 kristaps 26: #include <stdint.h>
1.1 kristaps 27: #include <stdlib.h>
28: #include <string.h>
29: #include <unistd.h>
30:
31: #include "mdoc.h"
1.10 kristaps 32: #include "man.h"
1.46 kristaps 33: #include "main.h"
1.1 kristaps 34:
1.45 kristaps 35: #define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
36:
1.55 kristaps 37: /* FIXME: Intel's compiler? LLVM? pcc? */
38:
39: #if !defined(__GNUC__) || (__GNUC__ < 2)
1.56 kristaps 40: # if !defined(lint)
41: # define __attribute__(x)
42: # endif
1.55 kristaps 43: #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
1.16 kristaps 44:
1.42 kristaps 45: typedef void (*out_mdoc)(void *, const struct mdoc *);
46: typedef void (*out_man)(void *, const struct man *);
1.22 kristaps 47: typedef void (*out_free)(void *);
48:
1.5 kristaps 49: struct buf {
50: char *buf;
51: size_t sz;
52: };
53:
1.19 kristaps 54: enum intt {
55: INTT_AUTO,
56: INTT_MDOC,
57: INTT_MAN
58: };
59:
60: enum outt {
61: OUTT_ASCII = 0,
62: OUTT_TREE,
1.43 kristaps 63: OUTT_HTML,
1.59 kristaps 64: OUTT_XHTML,
1.19 kristaps 65: OUTT_LINT
66: };
67:
1.5 kristaps 68: struct curparse {
1.22 kristaps 69: const char *file; /* Current parse. */
70: int fd; /* Current parse. */
1.5 kristaps 71: int wflags;
1.36 kristaps 72: #define WARN_WALL (1 << 0) /* All-warnings mask. */
1.1 kristaps 73: #define WARN_WERR (1 << 2) /* Warnings->errors. */
1.23 kristaps 74: int fflags;
1.60 kristaps 75: #define FL_IGN_SCOPE (1 << 0) /* Ignore scope errors. */
76: #define FL_NIGN_ESCAPE (1 << 1) /* Don't ignore bad escapes. */
77: #define FL_NIGN_MACRO (1 << 2) /* Don't ignore bad macros. */
78: #define FL_NIGN_CHARS (1 << 3) /* Don't ignore bad chars. */
79: #define FL_IGN_ERRORS (1 << 4) /* Ignore failed parse. */
1.29 kristaps 80: enum intt inttype; /* Input parsers... */
1.19 kristaps 81: struct man *man;
1.22 kristaps 82: struct man *lastman;
1.19 kristaps 83: struct mdoc *mdoc;
1.22 kristaps 84: struct mdoc *lastmdoc;
1.29 kristaps 85: enum outt outtype; /* Output devices... */
1.22 kristaps 86: out_mdoc outmdoc;
87: out_man outman;
88: out_free outfree;
89: void *outdata;
1.49 kristaps 90: char outopts[BUFSIZ];
1.5 kristaps 91: };
1.1 kristaps 92:
1.60 kristaps 93: #define FL_STRICT FL_NIGN_ESCAPE | \
94: FL_NIGN_MACRO | \
95: FL_NIGN_CHARS
96:
1.1 kristaps 97: static int foptions(int *, char *);
1.60 kristaps 98: static int toptions(struct curparse *, char *);
1.10 kristaps 99: static int moptions(enum intt *, char *);
1.1 kristaps 100: static int woptions(int *, char *);
101: static int merr(void *, int, int, const char *);
1.37 kristaps 102: static int mwarn(void *, int, int, const char *);
1.19 kristaps 103: static int ffile(struct buf *, struct buf *,
104: const char *, struct curparse *);
1.5 kristaps 105: static int fdesc(struct buf *, struct buf *,
1.19 kristaps 106: struct curparse *);
1.22 kristaps 107: static int pset(const char *, int, struct curparse *,
1.19 kristaps 108: struct man **, struct mdoc **);
109: static struct man *man_init(struct curparse *);
110: static struct mdoc *mdoc_init(struct curparse *);
1.55 kristaps 111: static void version(void) __attribute__((noreturn));
112: static void usage(void) __attribute__((noreturn));
1.1 kristaps 113:
1.54 kristaps 114: static const char *progname;
1.22 kristaps 115:
1.1 kristaps 116:
117: int
118: main(int argc, char *argv[])
119: {
1.19 kristaps 120: int c, rc;
1.5 kristaps 121: struct buf ln, blk;
122: struct curparse curp;
1.1 kristaps 123:
1.54 kristaps 124: progname = strrchr(argv[0], '/');
125: if (progname == NULL)
126: progname = argv[0];
127: else
128: ++progname;
129:
1.51 kristaps 130: memset(&curp, 0, sizeof(struct curparse));
1.5 kristaps 131:
1.19 kristaps 132: curp.inttype = INTT_AUTO;
1.22 kristaps 133: curp.outtype = OUTT_ASCII;
1.19 kristaps 134:
1.1 kristaps 135: /* LINTED */
1.47 kristaps 136: while (-1 != (c = getopt(argc, argv, "f:m:O:T:VW:")))
1.1 kristaps 137: switch (c) {
138: case ('f'):
1.19 kristaps 139: if ( ! foptions(&curp.fflags, optarg))
1.32 kristaps 140: return(EXIT_FAILURE);
1.1 kristaps 141: break;
1.10 kristaps 142: case ('m'):
1.19 kristaps 143: if ( ! moptions(&curp.inttype, optarg))
1.32 kristaps 144: return(EXIT_FAILURE);
1.10 kristaps 145: break;
1.48 kristaps 146: case ('O'):
1.49 kristaps 147: (void)strlcat(curp.outopts, optarg, BUFSIZ);
148: (void)strlcat(curp.outopts, ",", BUFSIZ);
1.44 kristaps 149: break;
1.1 kristaps 150: case ('T'):
1.60 kristaps 151: if ( ! toptions(&curp, optarg))
1.32 kristaps 152: return(EXIT_FAILURE);
1.1 kristaps 153: break;
154: case ('W'):
1.5 kristaps 155: if ( ! woptions(&curp.wflags, optarg))
1.32 kristaps 156: return(EXIT_FAILURE);
1.1 kristaps 157: break;
158: case ('V'):
159: version();
160: /* NOTREACHED */
161: default:
162: usage();
163: /* NOTREACHED */
164: }
165:
166: argc -= optind;
167: argv += optind;
168:
1.51 kristaps 169: memset(&ln, 0, sizeof(struct buf));
170: memset(&blk, 0, sizeof(struct buf));
1.1 kristaps 171:
1.22 kristaps 172: rc = 1;
1.1 kristaps 173:
1.30 kristaps 174: if (NULL == *argv) {
175: curp.file = "<stdin>";
176: curp.fd = STDIN_FILENO;
1.39 kristaps 177:
178: c = fdesc(&blk, &ln, &curp);
1.60 kristaps 179: if ( ! (FL_IGN_ERRORS & curp.fflags))
1.39 kristaps 180: rc = 1 == c ? 1 : 0;
181: else
182: rc = -1 == c ? 0 : 1;
1.30 kristaps 183: }
1.22 kristaps 184:
185: while (rc && *argv) {
1.39 kristaps 186: c = ffile(&blk, &ln, *argv, &curp);
1.60 kristaps 187: if ( ! (FL_IGN_ERRORS & curp.fflags))
1.39 kristaps 188: rc = 1 == c ? 1 : 0;
189: else
190: rc = -1 == c ? 0 : 1;
191:
1.22 kristaps 192: argv++;
193: if (*argv && rc) {
194: if (curp.lastman)
1.52 kristaps 195: man_reset(curp.lastman);
1.22 kristaps 196: if (curp.lastmdoc)
1.52 kristaps 197: mdoc_reset(curp.lastmdoc);
1.22 kristaps 198: curp.lastman = NULL;
199: curp.lastmdoc = NULL;
1.4 kristaps 200: }
1.1 kristaps 201: }
202:
1.5 kristaps 203: if (blk.buf)
204: free(blk.buf);
205: if (ln.buf)
206: free(ln.buf);
1.22 kristaps 207: if (curp.outfree)
208: (*curp.outfree)(curp.outdata);
1.19 kristaps 209: if (curp.mdoc)
210: mdoc_free(curp.mdoc);
211: if (curp.man)
212: man_free(curp.man);
1.1 kristaps 213:
214: return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
215: }
216:
217:
1.55 kristaps 218: static void
1.1 kristaps 219: version(void)
220: {
221:
1.54 kristaps 222: (void)printf("%s %s\n", progname, VERSION);
1.18 kristaps 223: exit(EXIT_SUCCESS);
1.1 kristaps 224: }
225:
226:
1.55 kristaps 227: static void
1.1 kristaps 228: usage(void)
229: {
230:
1.61 ! kristaps 231: (void)fprintf(stderr, "usage: %s [-V] [-foption] "
1.48 kristaps 232: "[-mformat] [-Ooption] [-Toutput] "
1.61 ! kristaps 233: "[-Werr] [file...]\n", progname);
1.18 kristaps 234: exit(EXIT_FAILURE);
1.1 kristaps 235: }
236:
237:
1.19 kristaps 238: static struct man *
239: man_init(struct curparse *curp)
240: {
241: int pflags;
242: struct man_cb mancb;
243:
244: mancb.man_err = merr;
1.37 kristaps 245: mancb.man_warn = mwarn;
1.19 kristaps 246:
1.30 kristaps 247: /* Defaults from mandoc.1. */
1.27 kristaps 248:
1.33 kristaps 249: pflags = MAN_IGN_MACRO | MAN_IGN_ESCAPE | MAN_IGN_CHARS;
1.27 kristaps 250:
1.60 kristaps 251: if (curp->fflags & FL_NIGN_MACRO)
1.20 kristaps 252: pflags &= ~MAN_IGN_MACRO;
1.60 kristaps 253: if (curp->fflags & FL_NIGN_CHARS)
1.31 kristaps 254: pflags &= ~MAN_IGN_CHARS;
1.60 kristaps 255: if (curp->fflags & FL_NIGN_ESCAPE)
1.33 kristaps 256: pflags &= ~MAN_IGN_ESCAPE;
1.19 kristaps 257:
1.52 kristaps 258: return(man_alloc(curp, pflags, &mancb));
1.19 kristaps 259: }
260:
261:
262: static struct mdoc *
263: mdoc_init(struct curparse *curp)
264: {
265: int pflags;
266: struct mdoc_cb mdoccb;
267:
268: mdoccb.mdoc_err = merr;
1.37 kristaps 269: mdoccb.mdoc_warn = mwarn;
1.19 kristaps 270:
1.30 kristaps 271: /* Defaults from mandoc.1. */
1.27 kristaps 272:
1.24 kristaps 273: pflags = MDOC_IGN_MACRO | MDOC_IGN_ESCAPE | MDOC_IGN_CHARS;
1.19 kristaps 274:
1.60 kristaps 275: if (curp->fflags & FL_IGN_SCOPE)
1.19 kristaps 276: pflags |= MDOC_IGN_SCOPE;
1.60 kristaps 277: if (curp->fflags & FL_NIGN_ESCAPE)
1.24 kristaps 278: pflags &= ~MDOC_IGN_ESCAPE;
1.60 kristaps 279: if (curp->fflags & FL_NIGN_MACRO)
1.24 kristaps 280: pflags &= ~MDOC_IGN_MACRO;
1.60 kristaps 281: if (curp->fflags & FL_NIGN_CHARS)
1.24 kristaps 282: pflags &= ~MDOC_IGN_CHARS;
1.19 kristaps 283:
1.52 kristaps 284: return(mdoc_alloc(curp, pflags, &mdoccb));
1.19 kristaps 285: }
286:
287:
288: static int
289: ffile(struct buf *blk, struct buf *ln,
290: const char *file, struct curparse *curp)
1.1 kristaps 291: {
1.19 kristaps 292: int c;
1.1 kristaps 293:
1.19 kristaps 294: curp->file = file;
295: if (-1 == (curp->fd = open(curp->file, O_RDONLY, 0))) {
1.53 kristaps 296: perror(curp->file);
1.39 kristaps 297: return(-1);
1.1 kristaps 298: }
299:
1.19 kristaps 300: c = fdesc(blk, ln, curp);
1.1 kristaps 301:
1.19 kristaps 302: if (-1 == close(curp->fd))
1.53 kristaps 303: perror(curp->file);
1.1 kristaps 304:
305: return(c);
306: }
307:
308:
309: static int
1.19 kristaps 310: fdesc(struct buf *blk, struct buf *ln, struct curparse *curp)
1.1 kristaps 311: {
312: size_t sz;
313: ssize_t ssz;
314: struct stat st;
1.29 kristaps 315: int j, i, pos, lnn, comment;
1.19 kristaps 316: struct man *man;
317: struct mdoc *mdoc;
1.10 kristaps 318:
1.19 kristaps 319: sz = BUFSIZ;
320: man = NULL;
321: mdoc = NULL;
1.1 kristaps 322:
323: /*
1.19 kristaps 324: * Two buffers: ln and buf. buf is the input buffer optimised
325: * here for each file's block size. ln is a line buffer. Both
1.1 kristaps 326: * growable, hence passed in by ptr-ptr.
327: */
328:
1.19 kristaps 329: if (-1 == fstat(curp->fd, &st))
1.53 kristaps 330: perror(curp->file);
1.6 kristaps 331: else if ((size_t)st.st_blksize > sz)
332: sz = st.st_blksize;
1.1 kristaps 333:
1.5 kristaps 334: if (sz > blk->sz) {
335: blk->buf = realloc(blk->buf, sz);
1.19 kristaps 336: if (NULL == blk->buf) {
1.53 kristaps 337: perror(NULL);
338: exit(EXIT_FAILURE);
1.19 kristaps 339: }
1.5 kristaps 340: blk->sz = sz;
1.1 kristaps 341: }
342:
1.19 kristaps 343: /* Fill buf with file blocksize. */
1.1 kristaps 344:
1.29 kristaps 345: for (lnn = pos = comment = 0; ; ) {
1.19 kristaps 346: if (-1 == (ssz = read(curp->fd, blk->buf, sz))) {
1.53 kristaps 347: perror(curp->file);
1.39 kristaps 348: return(-1);
1.1 kristaps 349: } else if (0 == ssz)
350: break;
351:
1.19 kristaps 352: /* Parse the read block into partial or full lines. */
353:
1.1 kristaps 354: for (i = 0; i < (int)ssz; i++) {
1.5 kristaps 355: if (pos >= (int)ln->sz) {
356: ln->sz += 256; /* Step-size. */
357: ln->buf = realloc(ln->buf, ln->sz);
1.19 kristaps 358: if (NULL == ln->buf) {
1.53 kristaps 359: perror(NULL);
360: return(EXIT_FAILURE);
1.19 kristaps 361: }
1.1 kristaps 362: }
363:
1.5 kristaps 364: if ('\n' != blk->buf[i]) {
1.29 kristaps 365: if (comment)
366: continue;
1.5 kristaps 367: ln->buf[pos++] = blk->buf[i];
1.29 kristaps 368:
369: /* Handle in-line `\"' comments. */
370:
371: if (1 == pos || '\"' != ln->buf[pos - 1])
372: continue;
373:
374: for (j = pos - 2; j >= 0; j--)
375: if ('\\' != ln->buf[j])
376: break;
377:
378: if ( ! ((pos - 2 - j) % 2))
379: continue;
380:
381: comment = 1;
382: pos -= 2;
1.61 ! kristaps 383: for (; pos > 0; --pos) {
! 384: if (ln->buf[pos] != ' ')
! 385: break;
! 386: if (ln->buf[pos - 1] == '\\')
! 387: break;
! 388: }
1.1 kristaps 389: continue;
1.29 kristaps 390: }
1.1 kristaps 391:
1.29 kristaps 392: /* Handle escaped `\\n' newlines. */
1.1 kristaps 393:
1.29 kristaps 394: if (pos > 0 && 0 == comment &&
395: '\\' == ln->buf[pos - 1]) {
1.1 kristaps 396: for (j = pos - 1; j >= 0; j--)
1.5 kristaps 397: if ('\\' != ln->buf[j])
1.1 kristaps 398: break;
399: if ( ! ((pos - j) % 2)) {
400: pos--;
401: lnn++;
402: continue;
403: }
404: }
405:
1.5 kristaps 406: ln->buf[pos] = 0;
1.19 kristaps 407: lnn++;
1.29 kristaps 408:
409: /* If unset, assign parser in pset(). */
1.19 kristaps 410:
411: if ( ! (man || mdoc) && ! pset(ln->buf,
412: pos, curp, &man, &mdoc))
1.39 kristaps 413: return(-1);
1.19 kristaps 414:
1.29 kristaps 415: pos = comment = 0;
1.19 kristaps 416:
1.30 kristaps 417: /* Pass down into parsers. */
418:
1.10 kristaps 419: if (man && ! man_parseln(man, lnn, ln->buf))
1.1 kristaps 420: return(0);
1.19 kristaps 421: if (mdoc && ! mdoc_parseln(mdoc, lnn, ln->buf))
422: return(0);
1.1 kristaps 423: }
424: }
425:
1.29 kristaps 426: /* NOTE a parser may not have been assigned, yet. */
1.19 kristaps 427:
1.22 kristaps 428: if ( ! (man || mdoc)) {
1.53 kristaps 429: fprintf(stderr, "%s: Not a manual\n", curp->file);
1.22 kristaps 430: return(0);
431: }
432:
433: if (mdoc && ! mdoc_endparse(mdoc))
434: return(0);
435: if (man && ! man_endparse(man))
436: return(0);
1.19 kristaps 437:
1.29 kristaps 438: /* If unset, allocate output dev now (if applicable). */
1.22 kristaps 439:
440: if ( ! (curp->outman && curp->outmdoc)) {
441: switch (curp->outtype) {
1.59 kristaps 442: case (OUTT_XHTML):
443: curp->outdata = xhtml_alloc(curp->outopts);
444: curp->outman = html_man;
445: curp->outmdoc = html_mdoc;
446: curp->outfree = html_free;
447: break;
1.43 kristaps 448: case (OUTT_HTML):
1.44 kristaps 449: curp->outdata = html_alloc(curp->outopts);
1.43 kristaps 450: curp->outman = html_man;
451: curp->outmdoc = html_mdoc;
452: curp->outfree = html_free;
453: break;
1.22 kristaps 454: case (OUTT_TREE):
455: curp->outman = tree_man;
456: curp->outmdoc = tree_mdoc;
457: break;
458: case (OUTT_LINT):
459: break;
460: default:
461: curp->outdata = ascii_alloc();
462: curp->outman = terminal_man;
463: curp->outmdoc = terminal_mdoc;
464: curp->outfree = terminal_free;
465: break;
466: }
467: }
468:
469: /* Execute the out device, if it exists. */
470:
471: if (man && curp->outman)
1.42 kristaps 472: (*curp->outman)(curp->outdata, man);
1.22 kristaps 473: if (mdoc && curp->outmdoc)
1.42 kristaps 474: (*curp->outmdoc)(curp->outdata, mdoc);
1.22 kristaps 475:
476: return(1);
1.19 kristaps 477: }
478:
479:
480: static int
1.22 kristaps 481: pset(const char *buf, int pos, struct curparse *curp,
1.19 kristaps 482: struct man **man, struct mdoc **mdoc)
483: {
1.29 kristaps 484: int i;
1.19 kristaps 485:
486: /*
487: * Try to intuit which kind of manual parser should be used. If
488: * passed in by command-line (-man, -mdoc), then use that
489: * explicitly. If passed as -mandoc, then try to guess from the
1.30 kristaps 490: * line: either skip dot-lines, use -mdoc when finding `.Dt', or
1.19 kristaps 491: * default to -man, which is more lenient.
492: */
493:
1.29 kristaps 494: if (buf[0] == '.') {
495: for (i = 1; buf[i]; i++)
496: if (' ' != buf[i] && '\t' != buf[i])
497: break;
498: if (0 == buf[i])
499: return(1);
500: }
1.10 kristaps 501:
1.19 kristaps 502: switch (curp->inttype) {
503: case (INTT_MDOC):
504: if (NULL == curp->mdoc)
505: curp->mdoc = mdoc_init(curp);
506: if (NULL == (*mdoc = curp->mdoc))
507: return(0);
1.22 kristaps 508: curp->lastmdoc = *mdoc;
1.19 kristaps 509: return(1);
510: case (INTT_MAN):
511: if (NULL == curp->man)
512: curp->man = man_init(curp);
513: if (NULL == (*man = curp->man))
514: return(0);
1.22 kristaps 515: curp->lastman = *man;
1.19 kristaps 516: return(1);
517: default:
518: break;
519: }
520:
521: if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3)) {
522: if (NULL == curp->mdoc)
523: curp->mdoc = mdoc_init(curp);
524: if (NULL == (*mdoc = curp->mdoc))
525: return(0);
1.22 kristaps 526: curp->lastmdoc = *mdoc;
1.19 kristaps 527: return(1);
528: }
529:
530: if (NULL == curp->man)
531: curp->man = man_init(curp);
532: if (NULL == (*man = curp->man))
533: return(0);
1.22 kristaps 534: curp->lastman = *man;
1.19 kristaps 535: return(1);
1.10 kristaps 536: }
537:
538:
539: static int
540: moptions(enum intt *tflags, char *arg)
541: {
542:
1.17 kristaps 543: if (0 == strcmp(arg, "doc"))
1.10 kristaps 544: *tflags = INTT_MDOC;
1.19 kristaps 545: else if (0 == strcmp(arg, "andoc"))
546: *tflags = INTT_AUTO;
1.17 kristaps 547: else if (0 == strcmp(arg, "an"))
1.10 kristaps 548: *tflags = INTT_MAN;
549: else {
1.57 kristaps 550: fprintf(stderr, "%s: Bad argument\n", arg);
1.10 kristaps 551: return(0);
552: }
553:
554: return(1);
1.1 kristaps 555: }
556:
557:
558: static int
1.60 kristaps 559: toptions(struct curparse *curp, char *arg)
1.1 kristaps 560: {
561:
562: if (0 == strcmp(arg, "ascii"))
1.60 kristaps 563: curp->outtype = OUTT_ASCII;
564: else if (0 == strcmp(arg, "lint")) {
565: curp->outtype = OUTT_LINT;
566: curp->wflags |= WARN_WALL;
567: curp->fflags |= FL_STRICT;
568: }
1.1 kristaps 569: else if (0 == strcmp(arg, "tree"))
1.60 kristaps 570: curp->outtype = OUTT_TREE;
1.43 kristaps 571: else if (0 == strcmp(arg, "html"))
1.60 kristaps 572: curp->outtype = OUTT_HTML;
1.59 kristaps 573: else if (0 == strcmp(arg, "xhtml"))
1.60 kristaps 574: curp->outtype = OUTT_XHTML;
1.1 kristaps 575: else {
1.57 kristaps 576: fprintf(stderr, "%s: Bad argument\n", arg);
1.1 kristaps 577: return(0);
578: }
579:
580: return(1);
581: }
582:
583:
584: static int
585: foptions(int *fflags, char *arg)
586: {
1.34 kristaps 587: char *v, *o;
1.50 kristaps 588: const char *toks[8];
1.1 kristaps 589:
590: toks[0] = "ign-scope";
1.24 kristaps 591: toks[1] = "no-ign-escape";
592: toks[2] = "no-ign-macro";
593: toks[3] = "no-ign-chars";
1.39 kristaps 594: toks[4] = "ign-errors";
595: toks[5] = "strict";
1.50 kristaps 596: toks[6] = "ign-escape";
597: toks[7] = NULL;
1.1 kristaps 598:
1.34 kristaps 599: while (*arg) {
600: o = arg;
1.45 kristaps 601: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.1 kristaps 602: case (0):
1.60 kristaps 603: *fflags |= FL_IGN_SCOPE;
1.1 kristaps 604: break;
605: case (1):
1.60 kristaps 606: *fflags |= FL_NIGN_ESCAPE;
1.1 kristaps 607: break;
608: case (2):
1.60 kristaps 609: *fflags |= FL_NIGN_MACRO;
1.1 kristaps 610: break;
1.20 kristaps 611: case (3):
1.60 kristaps 612: *fflags |= FL_NIGN_CHARS;
1.24 kristaps 613: break;
614: case (4):
1.60 kristaps 615: *fflags |= FL_IGN_ERRORS;
1.39 kristaps 616: break;
617: case (5):
1.60 kristaps 618: *fflags |= FL_STRICT;
1.20 kristaps 619: break;
1.50 kristaps 620: case (6):
1.60 kristaps 621: *fflags &= ~FL_NIGN_ESCAPE;
1.50 kristaps 622: break;
1.1 kristaps 623: default:
1.57 kristaps 624: fprintf(stderr, "%s: Bad argument\n", o);
1.1 kristaps 625: return(0);
626: }
1.34 kristaps 627: }
1.1 kristaps 628:
629: return(1);
630: }
631:
632:
633: static int
634: woptions(int *wflags, char *arg)
635: {
1.34 kristaps 636: char *v, *o;
1.45 kristaps 637: const char *toks[3];
1.1 kristaps 638:
639: toks[0] = "all";
1.36 kristaps 640: toks[1] = "error";
641: toks[2] = NULL;
1.1 kristaps 642:
1.34 kristaps 643: while (*arg) {
644: o = arg;
1.45 kristaps 645: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.1 kristaps 646: case (0):
647: *wflags |= WARN_WALL;
648: break;
649: case (1):
650: *wflags |= WARN_WERR;
651: break;
652: default:
1.57 kristaps 653: fprintf(stderr, "%s: Bad argument\n", o);
1.1 kristaps 654: return(0);
655: }
1.34 kristaps 656: }
1.1 kristaps 657:
658: return(1);
659: }
660:
661:
1.2 kristaps 662: /* ARGSUSED */
1.1 kristaps 663: static int
664: merr(void *arg, int line, int col, const char *msg)
665: {
1.5 kristaps 666: struct curparse *curp;
667:
668: curp = (struct curparse *)arg;
1.36 kristaps 669:
1.40 kristaps 670: (void)fprintf(stderr, "%s:%d:%d: error: %s\n",
671: curp->file, line, col + 1, msg);
1.27 kristaps 672:
1.1 kristaps 673: return(0);
674: }
675:
676:
677: static int
1.37 kristaps 678: mwarn(void *arg, int line, int col, const char *msg)
1.1 kristaps 679: {
1.5 kristaps 680: struct curparse *curp;
1.1 kristaps 681:
1.5 kristaps 682: curp = (struct curparse *)arg;
1.1 kristaps 683:
1.36 kristaps 684: if ( ! (curp->wflags & WARN_WALL))
685: return(1);
686:
1.40 kristaps 687: (void)fprintf(stderr, "%s:%d:%d: warning: %s\n",
688: curp->file, line, col + 1, msg);
1.1 kristaps 689:
1.5 kristaps 690: if ( ! (curp->wflags & WARN_WERR))
1.1 kristaps 691: return(1);
1.27 kristaps 692:
1.1 kristaps 693: return(0);
694: }
695:
CVSweb