1 /* $Id: tbl_opts.c,v 1.10 2011/03/20 16:02:05 kristaps Exp $ */
3 * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
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.
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.
23 #include "libmandoc.h"
49 /* Handle Commonwealth/American spellings. */
50 #define KEY_MAXKEYS 14
52 /* Maximum length of key name string. */
53 #define KEY_MAXNAME 13
55 /* Maximum length of key number size. */
56 #define KEY_MAXNUMSZ 10
58 static const struct tbl_phrase keys[KEY_MAXKEYS] = {
59 { "center", TBL_OPT_CENTRE, KEY_CENTRE},
60 { "centre", TBL_OPT_CENTRE, KEY_CENTRE},
61 { "delim", 0, KEY_DELIM},
62 { "expand", TBL_OPT_EXPAND, KEY_EXPAND},
63 { "box", TBL_OPT_BOX, KEY_BOX},
64 { "doublebox", TBL_OPT_DBOX, KEY_DBOX},
65 { "allbox", TBL_OPT_ALLBOX, KEY_ALLBOX},
66 { "frame", TBL_OPT_BOX, KEY_FRAME},
67 { "doubleframe", TBL_OPT_DBOX, KEY_DFRAME},
69 { "linesize", 0, KEY_LINESIZE},
70 { "nokeep", TBL_OPT_NOKEEP, KEY_NOKEEP},
71 { "decimalpoint", 0, KEY_DPOINT},
72 { "nospaces", TBL_OPT_NOSPACE, KEY_NOSPACE},
75 static int arg(struct tbl_node *, int,
76 const char *, int *, enum tbl_ident);
77 static void opt(struct tbl_node *, int,
81 arg(struct tbl_node *tbl, int ln, const char *p, int *pos, enum tbl_ident key)
84 char buf[KEY_MAXNUMSZ];
86 while (isspace((unsigned char)p[*pos]))
89 /* Arguments always begin with a parenthesis. */
92 mandoc_msg(MANDOCERR_TBL, tbl->parse,
100 * The arguments can be ANY value, so we can't just stop at the
101 * next close parenthesis (the argument can be a closed
102 * parenthesis itself).
107 if ('\0' == p[(*pos)++]) {
108 mandoc_msg(MANDOCERR_TBL, tbl->parse,
113 if ('\0' == p[(*pos)++]) {
114 mandoc_msg(MANDOCERR_TBL, tbl->parse,
120 if ('\0' != (tbl->opts.tab = p[(*pos)++]))
123 mandoc_msg(MANDOCERR_TBL, tbl->parse,
127 for (i = 0; i < KEY_MAXNUMSZ && p[*pos]; i++, (*pos)++) {
129 if ( ! isdigit((unsigned char)buf[i]))
133 if (i < KEY_MAXNUMSZ) {
135 tbl->opts.linesize = atoi(buf);
139 mandoc_msg(MANDOCERR_TBL, tbl->parse, ln, *pos, NULL);
142 if ('\0' != (tbl->opts.decimal = p[(*pos)++]))
145 mandoc_msg(MANDOCERR_TBL, tbl->parse,
153 /* End with a close parenthesis. */
155 if (')' == p[(*pos)++])
158 mandoc_msg(MANDOCERR_TBL, tbl->parse, ln, *pos - 1, NULL);
163 opt(struct tbl_node *tbl, int ln, const char *p, int *pos)
166 char buf[KEY_MAXNAME];
169 * Parse individual options from the stream as surrounded by
170 * this goto. Each pass through the routine parses out a single
171 * option and registers it. Option arguments are processed in
172 * the arg() function.
176 * EBNF describing this section:
178 * options ::= option_list [:space:]* [;][\n]
179 * option_list ::= option option_tail
180 * option_tail ::= [:space:]+ option_list |
182 * option ::= [:alpha:]+ args
183 * args ::= [:space:]* [(] [:alpha:]+ [)]
186 while (isspace((unsigned char)p[*pos]))
189 /* Safe exit point. */
194 /* Copy up to first non-alpha character. */
196 for (sv = *pos, i = 0; i < KEY_MAXNAME; i++, (*pos)++) {
197 buf[i] = (char)tolower((unsigned char)p[*pos]);
198 if ( ! isalpha((unsigned char)buf[i]))
202 /* Exit if buffer is empty (or overrun). */
204 if (KEY_MAXNAME == i || 0 == i) {
205 mandoc_msg(MANDOCERR_TBL, tbl->parse, ln, *pos, NULL);
211 while (isspace((unsigned char)p[*pos]))
215 * Look through all of the available keys to find one that
216 * matches the input. FIXME: hashtable this.
219 for (i = 0; i < KEY_MAXKEYS; i++) {
220 if (strcmp(buf, keys[i].name))
224 * Note: this is more difficult to recover from, as we
225 * can be anywhere in the option sequence and it's
226 * harder to jump to the next. Meanwhile, just bail out
227 * of the sequence altogether.
231 tbl->opts.opts |= keys[i].key;
232 else if ( ! arg(tbl, ln, p, pos, keys[i].ident))
239 * Allow us to recover from bad options by continuing to another
243 if (KEY_MAXKEYS == i)
244 mandoc_msg(MANDOCERR_TBLOPT, tbl->parse, ln, sv, NULL);
251 tbl_option(struct tbl_node *tbl, int ln, const char *p)
256 * Table options are always on just one line, so automatically
257 * switch into the next input mode here.
259 tbl->part = TBL_PART_LAYOUT;
262 opt(tbl, ln, p, &pos);
264 /* Always succeed. */