]> git.cameronkatri.com Git - mandoc.git/blob - tbl_opts.c
Move clean-up of parsed tbl nodes into the tbl_clear() function, called
[mandoc.git] / tbl_opts.c
1 /* $Id: tbl_opts.c,v 1.4 2010/12/29 14:38:14 kristaps Exp $ */
2 /*
3 * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
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 */
17 #include <ctype.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "mandoc.h"
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
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
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);
75 static void opt(struct tbl *, int, const char *, int *);
76
77 static int
78 arg(struct tbl *tbl, int ln, const char *p, int *pos, int key)
79 {
80 int i;
81 char buf[KEY_MAXNUMSZ];
82
83 while (isspace((unsigned char)p[*pos]))
84 (*pos)++;
85
86 /* Arguments always begin with a parenthesis. */
87
88 if ('(' != p[*pos]) {
89 TBL_MSG(tbl, MANDOCERR_TBL, ln, *pos);
90 return(0);
91 }
92
93 (*pos)++;
94
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 */
100
101 switch (key) {
102 case (KEY_DELIM):
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);
110 return(0);
111 }
112 break;
113 case (KEY_TAB):
114 if ('\0' != (tbl->tab = p[(*pos)++]))
115 break;
116
117 TBL_MSG(tbl, MANDOCERR_TBL, ln, *pos - 1);
118 return(0);
119 case (KEY_LINESIZE):
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);
134 case (KEY_DPOINT):
135 if ('\0' != (tbl->decimal = p[(*pos)++]))
136 break;
137
138 TBL_MSG(tbl, MANDOCERR_TBL, ln, *pos - 1);
139 return(0);
140 default:
141 abort();
142 /* NOTREACHED */
143 }
144
145 /* End with a close parenthesis. */
146
147 if (')' == p[(*pos)++])
148 return(1);
149
150 TBL_MSG(tbl, MANDOCERR_TBL, ln, *pos - 1);
151 return(0);
152 }
153
154 static void
155 opt(struct tbl *tbl, int ln, const char *p, int *pos)
156 {
157 int i, sv;
158 char buf[KEY_MAXNAME];
159
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 */
166
167 again: /*
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
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;
199 }
200
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
211 for (i = 0; i < KEY_MAXKEYS; i++) {
212 if (strcmp(buf, keys[i].name))
213 continue;
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
222 if (keys[i].key)
223 tbl->opts |= keys[i].key;
224 else if ( ! arg(tbl, ln, p, pos, keys[i].ident))
225 return;
226
227 break;
228 }
229
230 /*
231 * Allow us to recover from bad options by continuing to another
232 * parse sequence.
233 */
234
235 if (KEY_MAXKEYS == i)
236 TBL_MSG(tbl, MANDOCERR_TBLOPT, ln, sv);
237
238 goto again;
239 /* NOTREACHED */
240 }
241
242 int
243 tbl_option(struct tbl *tbl, int ln, const char *p)
244 {
245 int pos;
246
247 /*
248 * Table options are always on just one line, so automatically
249 * switch into the next input mode here.
250 */
251 tbl->part = TBL_PART_LAYOUT;
252
253 pos = 0;
254 opt(tbl, ln, p, &pos);
255
256 /* Always succeed. */
257 return(1);
258 }