summaryrefslogtreecommitdiff
path: root/usr.bin/mandoc/tbl_option.c
blob: 09cd3afd680f22d9e81e5ee7ac2eb8c49472151e (plain)
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
/*	$Id: tbl_option.c,v 1.2 2010/10/15 21:33:47 schwarze Exp $ */
/*
 * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
 *
 * 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 <sys/queue.h>

#include <stdlib.h>
#include <string.h>

#include "out.h"
#include "term.h"
#include "tbl_extern.h"

struct	tbl_phrase {
	char		*name;
	int		 key;
	int		 ident;
#define	KEY_CENTRE	 0
#define	KEY_DELIM	 1
#define	KEY_EXPAND	 2
#define	KEY_BOX		 3
#define	KEY_DBOX	 4
#define	KEY_ALLBOX	 5
#define	KEY_TAB		 6
#define	KEY_LINESIZE	 7
#define	KEY_NOKEEP	 8
#define	KEY_DPOINT	 9
#define	KEY_NOSPACE	 10
#define	KEY_FRAME	 11
#define	KEY_DFRAME	 12
};

#define	KEY_MAXKEYS	 14

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 *, const char *, 
				int, const char *, int *, int);
static	int		 opt(struct tbl *, const char *, 
				int, const char *, int *);

static int
arg(struct tbl *tbl, const char *f, int ln,
		const char *p, int *pos, int key)
{
	const char	*buf;
	int		 sv;

again:
	sv = *pos;

	switch (tbl_next(p, pos)) {
	case (TBL_TOK_OPENPAREN):
		break;
	case (TBL_TOK_SPACE):
		/* FALLTHROUGH */
	case (TBL_TOK_TAB):
		goto again;
	default:
		return(tbl_errx(tbl, ERR_SYNTAX, f, ln, sv));
	}

	sv = *pos;

	switch (tbl_next(p, pos)) {
	case (TBL_TOK_WORD):
		break;
	default:
		return(tbl_errx(tbl, ERR_SYNTAX, f, ln, sv));
	}

	buf = tbl_last();

	switch (key) {
	case (KEY_DELIM):
		if (2 != strlen(buf))
			return(tbl_errx(tbl, ERR_SYNTAX, f, ln, sv));
		tbl->delims[0] = buf[0];
		tbl->delims[1] = buf[1];
		break;
	case (KEY_TAB):
		if (1 != strlen(buf))
			return(tbl_errx(tbl, ERR_SYNTAX, f, ln, sv));
		tbl->tab = buf[0];
		break;
	case (KEY_LINESIZE):
		if (-1 == (tbl->linesize = tbl_last_uint()))
			return(tbl_errx(tbl, ERR_SYNTAX, f, ln, sv));
		break;
	case (KEY_DPOINT):
		if (1 != strlen(buf))
			return(tbl_errx(tbl, ERR_SYNTAX, f, ln, sv));
		tbl->decimal = buf[0];
		break;
	default:
		abort();
	}

	sv = *pos;

	switch (tbl_next(p, pos)) {
	case (TBL_TOK_CLOSEPAREN):
		break;
	default:
		return(tbl_errx(tbl, ERR_SYNTAX, f, ln, sv));
	}

	return(1);
}


static int
opt(struct tbl *tbl, const char *f, int ln, const char *p, int *pos)
{
	int		 i, sv;

again:
	sv = *pos;

	/*
	 * 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:]+ [)]
	 */

	switch (tbl_next(p, pos)) {
	case (TBL_TOK_WORD):
		break;
	case (TBL_TOK_SPACE):
		/* FALLTHROUGH */
	case (TBL_TOK_TAB):
		goto again;
	case (TBL_TOK_SEMICOLON):
		tbl->part = TBL_PART_LAYOUT;
		return(1);
	default:
		return(tbl_errx(tbl, ERR_SYNTAX, f, ln, sv));
	}

	for (i = 0; i < KEY_MAXKEYS; i++) {
		if (strcasecmp(tbl_last(), keys[i].name))
			continue;
		if (keys[i].key) 
			tbl->opts |= keys[i].key;
		else if ( ! arg(tbl, f, ln, p, pos, keys[i].ident))
			return(0);

		break;
	}

	if (KEY_MAXKEYS == i)
		return(tbl_errx(tbl, ERR_OPTION, f, ln, sv));

	return(opt(tbl, f, ln, p, pos));
}


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

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