Annotation of mandoc/tbl_opts.c, Revision 1.3
1.3 ! kristaps 1: /* $Id: tbl_opts.c,v 1.2 2010/12/28 13:47:38 kristaps Exp $ */
1.1 kristaps 2: /*
1.2 kristaps 3: * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1 kristaps 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 above
7: * copyright notice and this permission notice appear in all copies.
8: *
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.
16: */
1.3 ! kristaps 17: #include <ctype.h>
1.1 kristaps 18: #include <stdio.h>
19: #include <stdlib.h>
20: #include <string.h>
21:
1.3 ! kristaps 22: #include "mandoc.h"
1.1 kristaps 23: #include "libroff.h"
24:
25: enum tbl_ident {
26: KEY_CENTRE = 0,
27: KEY_DELIM,
28: KEY_EXPAND,
29: KEY_BOX,
30: KEY_DBOX,
31: KEY_ALLBOX,
32: KEY_TAB,
33: KEY_LINESIZE,
34: KEY_NOKEEP,
35: KEY_DPOINT,
36: KEY_NOSPACE,
37: KEY_FRAME,
38: KEY_DFRAME,
39: KEY_MAX
40: };
41:
42: struct tbl_phrase {
43: const char *name;
44: int key;
45: enum tbl_ident ident;
46: };
47:
48: /* Handle Commonwealth/American spellings. */
49: #define KEY_MAXKEYS 14
50:
1.3 ! kristaps 51: /* Maximum length of key name string. */
! 52: #define KEY_MAXNAME 13
! 53:
! 54: /* Maximum length of key number size. */
! 55: #define KEY_MAXNUMSZ 10
! 56:
1.1 kristaps 57: static const struct tbl_phrase keys[KEY_MAXKEYS] = {
58: { "center", TBL_OPT_CENTRE, KEY_CENTRE},
59: { "centre", TBL_OPT_CENTRE, KEY_CENTRE},
60: { "delim", 0, KEY_DELIM},
61: { "expand", TBL_OPT_EXPAND, KEY_EXPAND},
62: { "box", TBL_OPT_BOX, KEY_BOX},
63: { "doublebox", TBL_OPT_DBOX, KEY_DBOX},
64: { "allbox", TBL_OPT_ALLBOX, KEY_ALLBOX},
65: { "frame", TBL_OPT_BOX, KEY_FRAME},
66: { "doubleframe", TBL_OPT_DBOX, KEY_DFRAME},
67: { "tab", 0, KEY_TAB},
68: { "linesize", 0, KEY_LINESIZE},
69: { "nokeep", TBL_OPT_NOKEEP, KEY_NOKEEP},
70: { "decimalpoint", 0, KEY_DPOINT},
71: { "nospaces", TBL_OPT_NOSPACE, KEY_NOSPACE},
72: };
73:
74: static int arg(struct tbl *, int, const char *, int *, int);
1.3 ! kristaps 75: static void opt(struct tbl *, int, const char *, int *);
1.1 kristaps 76:
77: static int
78: arg(struct tbl *tbl, int ln, const char *p, int *pos, int key)
79: {
1.3 ! kristaps 80: int i;
! 81: char buf[KEY_MAXNUMSZ];
1.1 kristaps 82:
1.3 ! kristaps 83: while (isspace((unsigned char)p[*pos]))
! 84: (*pos)++;
1.1 kristaps 85:
1.3 ! kristaps 86: /* Arguments always begin with a parenthesis. */
! 87:
! 88: if ('(' != p[*pos]) {
! 89: TBL_MSG(tbl, MANDOCERR_TBL, ln, *pos);
1.1 kristaps 90: return(0);
91: }
92:
1.3 ! kristaps 93: (*pos)++;
1.1 kristaps 94:
1.3 ! kristaps 95: /*
! 96: * The arguments can be ANY value, so we can't just stop at the
! 97: * next close parenthesis (the argument can be a closed
! 98: * parenthesis itself).
! 99: */
1.1 kristaps 100:
101: switch (key) {
102: case (KEY_DELIM):
1.3 ! kristaps 103: if ('\0' == (tbl->delims[0] = p[(*pos)++])) {
! 104: TBL_MSG(tbl, MANDOCERR_TBL, ln, *pos - 1);
! 105: return(0);
! 106: }
! 107:
! 108: if ('\0' == (tbl->delims[1] = p[(*pos)++])) {
! 109: TBL_MSG(tbl, MANDOCERR_TBL, ln, *pos - 1);
1.1 kristaps 110: return(0);
1.3 ! kristaps 111: }
1.1 kristaps 112: break;
113: case (KEY_TAB):
1.3 ! kristaps 114: if ('\0' != (tbl->tab = p[(*pos)++]))
! 115: break;
! 116:
! 117: TBL_MSG(tbl, MANDOCERR_TBL, ln, *pos - 1);
! 118: return(0);
1.1 kristaps 119: case (KEY_LINESIZE):
1.3 ! kristaps 120: for (i = 0; i < KEY_MAXNUMSZ && p[*pos]; i++, (*pos)++) {
! 121: buf[i] = p[*pos];
! 122: if ( ! isdigit((unsigned char)buf[i]))
! 123: break;
! 124: }
! 125:
! 126: if (i < KEY_MAXNUMSZ) {
! 127: buf[i] = '\0';
! 128: tbl->linesize = atoi(buf);
! 129: break;
! 130: }
! 131:
! 132: (*tbl->msg)(MANDOCERR_TBL, tbl->data, ln, *pos, NULL);
! 133: return(0);
1.1 kristaps 134: case (KEY_DPOINT):
1.3 ! kristaps 135: if ('\0' != (tbl->decimal = p[(*pos)++]))
! 136: break;
! 137:
! 138: TBL_MSG(tbl, MANDOCERR_TBL, ln, *pos - 1);
! 139: return(0);
1.1 kristaps 140: default:
141: abort();
1.3 ! kristaps 142: /* NOTREACHED */
1.1 kristaps 143: }
144:
1.3 ! kristaps 145: /* End with a close parenthesis. */
1.1 kristaps 146:
1.3 ! kristaps 147: if (')' == p[(*pos)++])
! 148: return(1);
1.1 kristaps 149:
1.3 ! kristaps 150: TBL_MSG(tbl, MANDOCERR_TBL, ln, *pos - 1);
! 151: return(0);
1.1 kristaps 152: }
153:
1.3 ! kristaps 154: static void
1.1 kristaps 155: opt(struct tbl *tbl, int ln, const char *p, int *pos)
156: {
157: int i, sv;
1.3 ! kristaps 158: char buf[KEY_MAXNAME];
1.1 kristaps 159:
1.3 ! kristaps 160: /*
! 161: * Parse individual options from the stream as surrounded by
! 162: * this goto. Each pass through the routine parses out a single
! 163: * option and registers it. Option arguments are processed in
! 164: * the arg() function.
! 165: */
1.1 kristaps 166:
1.3 ! kristaps 167: again: /*
1.1 kristaps 168: * EBNF describing this section:
169: *
170: * options ::= option_list [:space:]* [;][\n]
171: * option_list ::= option option_tail
172: * option_tail ::= [:space:]+ option_list |
173: * ::= epsilon
174: * option ::= [:alpha:]+ args
175: * args ::= [:space:]* [(] [:alpha:]+ [)]
176: */
177:
1.3 ! kristaps 178: while (isspace((unsigned char)p[*pos]))
! 179: (*pos)++;
! 180:
! 181: /* Safe exit point. */
! 182:
! 183: if (';' == p[*pos])
! 184: return;
! 185:
! 186: /* Copy up to first non-alpha character. */
! 187:
! 188: for (sv = *pos, i = 0; i < KEY_MAXNAME; i++, (*pos)++) {
! 189: buf[i] = tolower(p[*pos]);
! 190: if ( ! isalpha((unsigned char)buf[i]))
! 191: break;
! 192: }
! 193:
! 194: /* Exit if buffer is empty (or overrun). */
! 195:
! 196: if (KEY_MAXNAME == i || 0 == i) {
! 197: TBL_MSG(tbl, MANDOCERR_TBL, ln, *pos);
! 198: return;
1.1 kristaps 199: }
200:
1.3 ! kristaps 201: buf[i] = '\0';
! 202:
! 203: while (isspace((unsigned char)p[*pos]))
! 204: (*pos)++;
! 205:
! 206: /*
! 207: * Look through all of the available keys to find one that
! 208: * matches the input. FIXME: hashtable this.
! 209: */
! 210:
1.1 kristaps 211: for (i = 0; i < KEY_MAXKEYS; i++) {
1.3 ! kristaps 212: if (strcmp(buf, keys[i].name))
1.1 kristaps 213: continue;
1.3 ! kristaps 214:
! 215: /*
! 216: * Note: this is more difficult to recover from, as we
! 217: * can be anywhere in the option sequence and it's
! 218: * harder to jump to the next. Meanwhile, just bail out
! 219: * of the sequence altogether.
! 220: */
! 221:
1.1 kristaps 222: if (keys[i].key)
223: tbl->opts |= keys[i].key;
224: else if ( ! arg(tbl, ln, p, pos, keys[i].ident))
1.3 ! kristaps 225: return;
1.1 kristaps 226:
227: break;
228: }
229:
1.3 ! kristaps 230: /*
! 231: * Allow us to recover from bad options by continuing to another
! 232: * parse sequence.
! 233: */
! 234:
1.1 kristaps 235: if (KEY_MAXKEYS == i)
1.3 ! kristaps 236: TBL_MSG(tbl, MANDOCERR_TBLOPT, ln, sv);
! 237:
! 238: /* Try again... */
1.1 kristaps 239:
1.3 ! kristaps 240: goto again;
1.1 kristaps 241: }
242:
243: int
244: tbl_option(struct tbl *tbl, int ln, const char *p)
245: {
246: int pos;
247:
1.3 ! kristaps 248: /*
! 249: * Table options are always on just one line, so automatically
! 250: * switch into the next input mode here.
! 251: */
! 252: tbl->part = TBL_PART_LAYOUT;
! 253:
1.1 kristaps 254: pos = 0;
1.3 ! kristaps 255: opt(tbl, ln, p, &pos);
! 256:
! 257: /* Always succeed. */
! 258: return(1);
1.1 kristaps 259: }
CVSweb