summaryrefslogtreecommitdiffstats
path: root/tbl_opts.c
blob: 227deb1f6e6f3f22566eb6d787ae58ac3f4b087c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*	$Id$ */
/*
 * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "mandoc.h"
#include "libroff.h"

enum	tbl_ident {
	KEY_CENTRE = 0,
	KEY_DELIM,
	KEY_EXPAND,
	KEY_BOX,
	KEY_DBOX,
	KEY_ALLBOX,
	KEY_TAB,
	KEY_LINESIZE,
	KEY_NOKEEP,
	KEY_DPOINT,
	KEY_NOSPACE,
	KEY_FRAME,
	KEY_DFRAME,
	KEY_MAX
};

struct	tbl_phrase {
	const char	*name;
	int		 key;
	enum tbl_ident	 ident;
};

/* Handle Commonwealth/American spellings. */
#define	KEY_MAXKEYS	 14

/* Maximum length of key name string. */
#define	KEY_MAXNAME	 13

/* Maximum length of key number size. */
#define	KEY_MAXNUMSZ	 10

static	const struct tbl_phrase keys[KEY_MAXKEYS] = {
	{ "center",	 TBL_OPT_CENTRE,	KEY_CENTRE},
	{ "centre",	 TBL_OPT_CENTRE,	KEY_CENTRE},
	{ "delim",	 0,	       		KEY_DELIM},
	{ "expand",	 TBL_OPT_EXPAND,	KEY_EXPAND},
	{ "box",	 TBL_OPT_BOX,   	KEY_BOX},
	{ "doublebox",	 TBL_OPT_DBOX,  	KEY_DBOX},
	{ "allbox",	 TBL_OPT_ALLBOX,	KEY_ALLBOX},
	{ "frame",	 TBL_OPT_BOX,		KEY_FRAME},
	{ "doubleframe", TBL_OPT_DBOX,		KEY_DFRAME},
	{ "tab",	 0,			KEY_TAB},
	{ "linesize",	 0,			KEY_LINESIZE},
	{ "nokeep",	 TBL_OPT_NOKEEP,	KEY_NOKEEP},
	{ "decimalpoint", 0,			KEY_DPOINT},
	{ "nospaces",	 TBL_OPT_NOSPACE,	KEY_NOSPACE},
};

static	int		 arg(struct tbl_node *, int, 
				const char *, int *, enum tbl_ident);
static	void		 opt(struct tbl_node *, int, 
				const char *, int *);

static int
arg(struct tbl_node *tbl, int ln, const char *p, int *pos, enum tbl_ident key)
{
	int		 i;
	char		 buf[KEY_MAXNUMSZ];

	while (isspace((unsigned char)p[*pos]))
		(*pos)++;

	/* Arguments always begin with a parenthesis. */

	if ('(' != p[*pos]) {
		TBL_MSG(tbl, MANDOCERR_TBL, ln, *pos);
		return(0);
	}

	(*pos)++;

	/*
	 * The arguments can be ANY value, so we can't just stop at the
	 * next close parenthesis (the argument can be a closed
	 * parenthesis itself).
	 */

	switch (key) {
	case (KEY_DELIM):
		if ('\0' == p[(*pos)++]) {
			TBL_MSG(tbl, MANDOCERR_TBL, ln, *pos - 1);
			return(0);
		} 

		if ('\0' == p[(*pos)++]) {
			TBL_MSG(tbl, MANDOCERR_TBL, ln, *pos - 1);
			return(0);
		} 
		break;
	case (KEY_TAB):
		if ('\0' != (tbl->opts.tab = p[(*pos)++]))
			break;

		TBL_MSG(tbl, MANDOCERR_TBL, ln, *pos - 1);
		return(0);
	case (KEY_LINESIZE):
		for (i = 0; i < KEY_MAXNUMSZ && p[*pos]; i++, (*pos)++) {
			buf[i] = p[*pos];
			if ( ! isdigit((unsigned char)buf[i]))
				break;
		}

		if (i < KEY_MAXNUMSZ) {
			buf[i] = '\0';
			tbl->opts.linesize = atoi(buf);
			break;
		}

		(*tbl->msg)(MANDOCERR_TBL, tbl->data, ln, *pos, NULL);
		return(0);
	case (KEY_DPOINT):
		if ('\0' != (tbl->opts.decimal = p[(*pos)++]))
			break;

		TBL_MSG(tbl, MANDOCERR_TBL, ln, *pos - 1);
		return(0);
	default:
		abort();
		/* NOTREACHED */
	}

	/* End with a close parenthesis. */

	if (')' == p[(*pos)++])
		return(1);

	TBL_MSG(tbl, MANDOCERR_TBL, ln, *pos - 1);
	return(0);
}

static void
opt(struct tbl_node *tbl, int ln, const char *p, int *pos)
{
	int		 i, sv;
	char		 buf[KEY_MAXNAME];

	/*
	 * Parse individual options from the stream as surrounded by
	 * this goto.  Each pass through the routine parses out a single
	 * option and registers it.  Option arguments are processed in
	 * the arg() function.
	 */

again:	/*
	 * EBNF describing this section:
	 *
	 * options	::= option_list [:space:]* [;][\n]
	 * option_list	::= option option_tail
	 * option_tail	::= [:space:]+ option_list |
	 * 		::= epsilon
	 * option	::= [:alpha:]+ args
	 * args		::= [:space:]* [(] [:alpha:]+ [)]
	 */

	while (isspace((unsigned char)p[*pos]))
		(*pos)++;

	/* Safe exit point. */

	if (';' == p[*pos])
		return;

	/* Copy up to first non-alpha character. */

	for (sv = *pos, i = 0; i < KEY_MAXNAME; i++, (*pos)++) {
		buf[i] = tolower(p[*pos]);
		if ( ! isalpha((unsigned char)buf[i]))
			break;
	}

	/* Exit if buffer is empty (or overrun). */

	if (KEY_MAXNAME == i || 0 == i) {
		TBL_MSG(tbl, MANDOCERR_TBL, ln, *pos);
		return;
	}

	buf[i] = '\0';

	while (isspace((unsigned char)p[*pos]))
		(*pos)++;

	/* 
	 * Look through all of the available keys to find one that
	 * matches the input.  FIXME: hashtable this.
	 */

	for (i = 0; i < KEY_MAXKEYS; i++) {
		if (strcmp(buf, keys[i].name))
			continue;

		/*
		 * Note: this is more difficult to recover from, as we
		 * can be anywhere in the option sequence and it's
		 * harder to jump to the next.  Meanwhile, just bail out
		 * of the sequence altogether.
		 */

		if (keys[i].key) 
			tbl->opts.opts |= keys[i].key;
		else if ( ! arg(tbl, ln, p, pos, keys[i].ident))
			return;

		break;
	}

	/* 
	 * Allow us to recover from bad options by continuing to another
	 * parse sequence.
	 */

	if (KEY_MAXKEYS == i)
		TBL_MSG(tbl, MANDOCERR_TBLOPT, ln, sv);

	goto again;
	/* NOTREACHED */
}

int
tbl_option(struct tbl_node *tbl, int ln, const char *p)
{
	int		 pos;

	/*
	 * Table options are always on just one line, so automatically
	 * switch into the next input mode here.
	 */
	tbl->part = TBL_PART_LAYOUT;

	pos = 0;
	opt(tbl, ln, p, &pos);

	/* Always succeed. */
	return(1);
}