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