1 /* $Vendor-Id: tbl_opts.c,v 1.8 2011/01/09 05:38:23 joerg 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.
48 /* Handle Commonwealth/American spellings. */
49 #define KEY_MAXKEYS 14
51 /* Maximum length of key name string. */
52 #define KEY_MAXNAME 13
54 /* Maximum length of key number size. */
55 #define KEY_MAXNUMSZ 10
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
},
68 { "linesize", 0, KEY_LINESIZE
},
69 { "nokeep", TBL_OPT_NOKEEP
, KEY_NOKEEP
},
70 { "decimalpoint", 0, KEY_DPOINT
},
71 { "nospaces", TBL_OPT_NOSPACE
, KEY_NOSPACE
},
74 static int arg(struct tbl_node
*, int,
75 const char *, int *, enum tbl_ident
);
76 static void opt(struct tbl_node
*, int,
80 arg(struct tbl_node
*tbl
, int ln
, const char *p
, int *pos
, enum tbl_ident key
)
83 char buf
[KEY_MAXNUMSZ
];
85 while (isspace((unsigned char)p
[*pos
]))
88 /* Arguments always begin with a parenthesis. */
91 TBL_MSG(tbl
, MANDOCERR_TBL
, ln
, *pos
);
98 * The arguments can be ANY value, so we can't just stop at the
99 * next close parenthesis (the argument can be a closed
100 * parenthesis itself).
105 if ('\0' == p
[(*pos
)++]) {
106 TBL_MSG(tbl
, MANDOCERR_TBL
, ln
, *pos
- 1);
110 if ('\0' == p
[(*pos
)++]) {
111 TBL_MSG(tbl
, MANDOCERR_TBL
, ln
, *pos
- 1);
116 if ('\0' != (tbl
->opts
.tab
= p
[(*pos
)++]))
119 TBL_MSG(tbl
, MANDOCERR_TBL
, ln
, *pos
- 1);
122 for (i
= 0; i
< KEY_MAXNUMSZ
&& p
[*pos
]; i
++, (*pos
)++) {
124 if ( ! isdigit((unsigned char)buf
[i
]))
128 if (i
< KEY_MAXNUMSZ
) {
130 tbl
->opts
.linesize
= atoi(buf
);
134 (*tbl
->msg
)(MANDOCERR_TBL
, tbl
->data
, ln
, *pos
, NULL
);
137 if ('\0' != (tbl
->opts
.decimal
= p
[(*pos
)++]))
140 TBL_MSG(tbl
, MANDOCERR_TBL
, ln
, *pos
- 1);
147 /* End with a close parenthesis. */
149 if (')' == p
[(*pos
)++])
152 TBL_MSG(tbl
, MANDOCERR_TBL
, ln
, *pos
- 1);
157 opt(struct tbl_node
*tbl
, int ln
, const char *p
, int *pos
)
160 char buf
[KEY_MAXNAME
];
163 * Parse individual options from the stream as surrounded by
164 * this goto. Each pass through the routine parses out a single
165 * option and registers it. Option arguments are processed in
166 * the arg() function.
170 * EBNF describing this section:
172 * options ::= option_list [:space:]* [;][\n]
173 * option_list ::= option option_tail
174 * option_tail ::= [:space:]+ option_list |
176 * option ::= [:alpha:]+ args
177 * args ::= [:space:]* [(] [:alpha:]+ [)]
180 while (isspace((unsigned char)p
[*pos
]))
183 /* Safe exit point. */
188 /* Copy up to first non-alpha character. */
190 for (sv
= *pos
, i
= 0; i
< KEY_MAXNAME
; i
++, (*pos
)++) {
191 buf
[i
] = tolower((unsigned char)p
[*pos
]);
192 if ( ! isalpha((unsigned char)buf
[i
]))
196 /* Exit if buffer is empty (or overrun). */
198 if (KEY_MAXNAME
== i
|| 0 == i
) {
199 TBL_MSG(tbl
, MANDOCERR_TBL
, ln
, *pos
);
205 while (isspace((unsigned char)p
[*pos
]))
209 * Look through all of the available keys to find one that
210 * matches the input. FIXME: hashtable this.
213 for (i
= 0; i
< KEY_MAXKEYS
; i
++) {
214 if (strcmp(buf
, keys
[i
].name
))
218 * Note: this is more difficult to recover from, as we
219 * can be anywhere in the option sequence and it's
220 * harder to jump to the next. Meanwhile, just bail out
221 * of the sequence altogether.
225 tbl
->opts
.opts
|= keys
[i
].key
;
226 else if ( ! arg(tbl
, ln
, p
, pos
, keys
[i
].ident
))
233 * Allow us to recover from bad options by continuing to another
237 if (KEY_MAXKEYS
== i
)
238 TBL_MSG(tbl
, MANDOCERR_TBLOPT
, ln
, sv
);
245 tbl_option(struct tbl_node
*tbl
, int ln
, const char *p
)
250 * Table options are always on just one line, so automatically
251 * switch into the next input mode here.
253 tbl
->part
= TBL_PART_LAYOUT
;
256 opt(tbl
, ln
, p
, &pos
);
258 /* Always succeed. */