1 /* $Id: tbl_opts.c,v 1.13 2014/04/20 16:46:05 schwarze Exp $ */
3 * Copyright (c) 2009, 2010, 2011 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.
27 #include "libmandoc.h"
53 /* Handle Commonwealth/American spellings. */
54 #define KEY_MAXKEYS 14
56 /* Maximum length of key name string. */
57 #define KEY_MAXNAME 13
59 /* Maximum length of key number size. */
60 #define KEY_MAXNUMSZ 10
62 static const struct tbl_phrase keys
[KEY_MAXKEYS
] = {
63 { "center", TBL_OPT_CENTRE
, KEY_CENTRE
},
64 { "centre", TBL_OPT_CENTRE
, KEY_CENTRE
},
65 { "delim", 0, KEY_DELIM
},
66 { "expand", TBL_OPT_EXPAND
, KEY_EXPAND
},
67 { "box", TBL_OPT_BOX
, KEY_BOX
},
68 { "doublebox", TBL_OPT_DBOX
, KEY_DBOX
},
69 { "allbox", TBL_OPT_ALLBOX
, KEY_ALLBOX
},
70 { "frame", TBL_OPT_BOX
, KEY_FRAME
},
71 { "doubleframe", TBL_OPT_DBOX
, KEY_DFRAME
},
73 { "linesize", 0, KEY_LINESIZE
},
74 { "nokeep", TBL_OPT_NOKEEP
, KEY_NOKEEP
},
75 { "decimalpoint", 0, KEY_DPOINT
},
76 { "nospaces", TBL_OPT_NOSPACE
, KEY_NOSPACE
},
79 static int arg(struct tbl_node
*, int,
80 const char *, int *, enum tbl_ident
);
81 static void opt(struct tbl_node
*, int,
86 arg(struct tbl_node
*tbl
, int ln
, const char *p
, int *pos
, enum tbl_ident key
)
89 char buf
[KEY_MAXNUMSZ
];
91 while (isspace((unsigned char)p
[*pos
]))
94 /* Arguments always begin with a parenthesis. */
97 mandoc_msg(MANDOCERR_TBL
, tbl
->parse
,
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).
112 if ('\0' == p
[(*pos
)++]) {
113 mandoc_msg(MANDOCERR_TBL
, tbl
->parse
,
118 if ('\0' == p
[(*pos
)++]) {
119 mandoc_msg(MANDOCERR_TBL
, tbl
->parse
,
125 if ('\0' != (tbl
->opts
.tab
= p
[(*pos
)++]))
128 mandoc_msg(MANDOCERR_TBL
, tbl
->parse
,
132 for (i
= 0; i
< KEY_MAXNUMSZ
&& p
[*pos
]; i
++, (*pos
)++) {
134 if ( ! isdigit((unsigned char)buf
[i
]))
138 if (i
< KEY_MAXNUMSZ
) {
140 tbl
->opts
.linesize
= atoi(buf
);
144 mandoc_msg(MANDOCERR_TBL
, tbl
->parse
, ln
, *pos
, NULL
);
147 if ('\0' != (tbl
->opts
.decimal
= p
[(*pos
)++]))
150 mandoc_msg(MANDOCERR_TBL
, tbl
->parse
,
158 /* End with a close parenthesis. */
160 if (')' == p
[(*pos
)++])
163 mandoc_msg(MANDOCERR_TBL
, tbl
->parse
, ln
, *pos
- 1, NULL
);
168 opt(struct tbl_node
*tbl
, int ln
, const char *p
, int *pos
)
171 char buf
[KEY_MAXNAME
];
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.
181 * EBNF describing this section:
183 * options ::= option_list [:space:]* [;][\n]
184 * option_list ::= option option_tail
185 * option_tail ::= [:space:]+ option_list |
187 * option ::= [:alpha:]+ args
188 * args ::= [:space:]* [(] [:alpha:]+ [)]
191 while (isspace((unsigned char)p
[*pos
]))
194 /* Safe exit point. */
199 /* Copy up to first non-alpha character. */
201 for (sv
= *pos
, i
= 0; i
< KEY_MAXNAME
; i
++, (*pos
)++) {
202 buf
[i
] = (char)tolower((unsigned char)p
[*pos
]);
203 if ( ! isalpha((unsigned char)buf
[i
]))
207 /* Exit if buffer is empty (or overrun). */
209 if (KEY_MAXNAME
== i
|| 0 == i
) {
210 mandoc_msg(MANDOCERR_TBL
, tbl
->parse
, ln
, *pos
, NULL
);
216 while (isspace((unsigned char)p
[*pos
]))
220 * Look through all of the available keys to find one that
221 * matches the input. FIXME: hashtable this.
224 for (i
= 0; i
< KEY_MAXKEYS
; i
++) {
225 if (strcmp(buf
, keys
[i
].name
))
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.
236 tbl
->opts
.opts
|= keys
[i
].key
;
237 else if ( ! arg(tbl
, ln
, p
, pos
, keys
[i
].ident
))
244 * Allow us to recover from bad options by continuing to another
248 if (KEY_MAXKEYS
== i
)
249 mandoc_msg(MANDOCERR_TBLOPT
, tbl
->parse
, ln
, sv
, NULL
);
256 tbl_option(struct tbl_node
*tbl
, int ln
, const char *p
)
261 * Table options are always on just one line, so automatically
262 * switch into the next input mode here.
264 tbl
->part
= TBL_PART_LAYOUT
;
267 opt(tbl
, ln
, p
, &pos
);
269 /* Always succeed. */