]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_opts.c
1 /* $Id: tbl_opts.c,v 1.4 2010/12/29 14:38:14 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.
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
*, int, const char *, int *, int);
75 static void opt(struct tbl
*, int, const char *, int *);
78 arg(struct tbl
*tbl
, int ln
, const char *p
, int *pos
, int key
)
81 char buf
[KEY_MAXNUMSZ
];
83 while (isspace((unsigned char)p
[*pos
]))
86 /* Arguments always begin with a parenthesis. */
89 TBL_MSG(tbl
, MANDOCERR_TBL
, ln
, *pos
);
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).
103 if ('\0' == (tbl
->delims
[0] = p
[(*pos
)++])) {
104 TBL_MSG(tbl
, MANDOCERR_TBL
, ln
, *pos
- 1);
108 if ('\0' == (tbl
->delims
[1] = p
[(*pos
)++])) {
109 TBL_MSG(tbl
, MANDOCERR_TBL
, ln
, *pos
- 1);
114 if ('\0' != (tbl
->tab
= p
[(*pos
)++]))
117 TBL_MSG(tbl
, MANDOCERR_TBL
, ln
, *pos
- 1);
120 for (i
= 0; i
< KEY_MAXNUMSZ
&& p
[*pos
]; i
++, (*pos
)++) {
122 if ( ! isdigit((unsigned char)buf
[i
]))
126 if (i
< KEY_MAXNUMSZ
) {
128 tbl
->linesize
= atoi(buf
);
132 (*tbl
->msg
)(MANDOCERR_TBL
, tbl
->data
, ln
, *pos
, NULL
);
135 if ('\0' != (tbl
->decimal
= p
[(*pos
)++]))
138 TBL_MSG(tbl
, MANDOCERR_TBL
, ln
, *pos
- 1);
145 /* End with a close parenthesis. */
147 if (')' == p
[(*pos
)++])
150 TBL_MSG(tbl
, MANDOCERR_TBL
, ln
, *pos
- 1);
155 opt(struct tbl
*tbl
, int ln
, const char *p
, int *pos
)
158 char buf
[KEY_MAXNAME
];
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.
168 * EBNF describing this section:
170 * options ::= option_list [:space:]* [;][\n]
171 * option_list ::= option option_tail
172 * option_tail ::= [:space:]+ option_list |
174 * option ::= [:alpha:]+ args
175 * args ::= [:space:]* [(] [:alpha:]+ [)]
178 while (isspace((unsigned char)p
[*pos
]))
181 /* Safe exit point. */
186 /* Copy up to first non-alpha character. */
188 for (sv
= *pos
, i
= 0; i
< KEY_MAXNAME
; i
++, (*pos
)++) {
189 buf
[i
] = tolower(p
[*pos
]);
190 if ( ! isalpha((unsigned char)buf
[i
]))
194 /* Exit if buffer is empty (or overrun). */
196 if (KEY_MAXNAME
== i
|| 0 == i
) {
197 TBL_MSG(tbl
, MANDOCERR_TBL
, ln
, *pos
);
203 while (isspace((unsigned char)p
[*pos
]))
207 * Look through all of the available keys to find one that
208 * matches the input. FIXME: hashtable this.
211 for (i
= 0; i
< KEY_MAXKEYS
; i
++) {
212 if (strcmp(buf
, keys
[i
].name
))
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.
223 tbl
->opts
|= keys
[i
].key
;
224 else if ( ! arg(tbl
, ln
, p
, pos
, keys
[i
].ident
))
231 * Allow us to recover from bad options by continuing to another
235 if (KEY_MAXKEYS
== i
)
236 TBL_MSG(tbl
, MANDOCERR_TBLOPT
, ln
, sv
);
243 tbl_option(struct tbl
*tbl
, int ln
, const char *p
)
248 * Table options are always on just one line, so automatically
249 * switch into the next input mode here.
251 tbl
->part
= TBL_PART_LAYOUT
;
254 opt(tbl
, ln
, p
, &pos
);
256 /* Always succeed. */