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
261
262
263
264
265
266
|
/* $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 <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "mandoc.h"
#include "libmandoc.h"
#include "libroff.h"
struct tbl_phrase {
char name;
enum tbl_cellt key;
};
#define KEYS_MAX 11
static const struct tbl_phrase keys[KEYS_MAX] = {
{ 'c', TBL_CELL_CENTRE },
{ 'r', TBL_CELL_RIGHT },
{ 'l', TBL_CELL_LEFT },
{ 'n', TBL_CELL_NUMBER },
{ 's', TBL_CELL_SPAN },
{ 'a', TBL_CELL_LONG },
{ '^', TBL_CELL_DOWN },
{ '-', TBL_CELL_HORIZ },
{ '_', TBL_CELL_HORIZ },
{ '=', TBL_CELL_DHORIZ },
{ '|', TBL_CELL_VERT }
};
static int mods(struct tbl *, struct tbl_cell *,
int, const char *, int *);
static int cell(struct tbl *, struct tbl_row *,
int, const char *, int *);
static void row(struct tbl *, int, const char *, int *);
static int
mods(struct tbl *tbl, struct tbl_cell *cp,
int ln, const char *p, int *pos)
{
char buf[5];
int i;
mod:
/*
* XXX: since, at least for now, modifiers are non-conflicting
* (are separable by value, regardless of position), we let
* modifiers come in any order. The existing tbl doesn't let
* this happen.
*/
switch (p[*pos]) {
case ('\0'):
/* FALLTHROUGH */
case (' '):
/* FALLTHROUGH */
case ('\t'):
/* FALLTHROUGH */
case (','):
/* FALLTHROUGH */
case ('.'):
return(1);
default:
break;
}
/* Parse numerical spacing from modifier string. */
if (isdigit((unsigned char)p[*pos])) {
for (i = 0; i < 4; i++) {
if ( ! isdigit((unsigned char)p[*pos + i]))
break;
buf[i] = p[*pos + i];
}
buf[i] = '\0';
/* No greater than 4 digits. */
if (4 == i) {
TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos);
return(0);
}
*pos += i;
cp->spacing = atoi(buf);
goto mod;
/* NOTREACHED */
}
/* TODO: GNU has many more extensions. */
switch (tolower(p[(*pos)++])) {
case ('z'):
cp->flags |= TBL_CELL_WIGN;
goto mod;
case ('u'):
cp->flags |= TBL_CELL_UP;
goto mod;
case ('e'):
cp->flags |= TBL_CELL_EQUAL;
goto mod;
case ('t'):
cp->flags |= TBL_CELL_TALIGN;
goto mod;
case ('d'):
cp->flags |= TBL_CELL_BALIGN;
goto mod;
case ('f'):
break;
case ('b'):
/* FALLTHROUGH */
case ('i'):
(*pos)--;
break;
default:
TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos - 1);
return(0);
}
switch (tolower(p[(*pos)++])) {
case ('b'):
cp->flags |= TBL_CELL_BOLD;
goto mod;
case ('i'):
cp->flags |= TBL_CELL_ITALIC;
goto mod;
default:
break;
}
TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos - 1);
return(0);
}
static int
cell(struct tbl *tbl, struct tbl_row *rp,
int ln, const char *p, int *pos)
{
struct tbl_cell *cp;
int i;
enum tbl_cellt c;
/* Parse the column position (`r', `R', `|', ...). */
for (i = 0; i < KEYS_MAX; i++)
if (tolower(p[*pos]) == keys[i].name)
break;
if (KEYS_MAX == i) {
TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos);
return(0);
}
(*pos)++;
c = keys[i].key;
/* Extra check for the double-vertical. */
if (TBL_CELL_VERT == c && '|' == p[*pos]) {
(*pos)++;
c = TBL_CELL_DVERT;
}
/* Disallow adjacent spacers. */
if (rp->last && (TBL_CELL_VERT == c || TBL_CELL_DVERT == c) &&
(TBL_CELL_VERT == rp->last->pos ||
TBL_CELL_DVERT == rp->last->pos)) {
TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos - 1);
return(0);
}
/* Allocate cell then parse its modifiers. */
cp = mandoc_calloc(1, sizeof(struct tbl_cell));
cp->pos = c;
if (rp->last) {
rp->last->next = cp;
rp->last = cp;
} else
rp->last = rp->first = cp;
return(mods(tbl, cp, ln, p, pos));
}
static void
row(struct tbl *tbl, int ln, const char *p, int *pos)
{
struct tbl_row *rp;
row: /*
* EBNF describing this section:
*
* row ::= row_list [:space:]* [.]?[\n]
* row_list ::= [:space:]* row_elem row_tail
* row_tail ::= [:space:]*[,] row_list |
* epsilon
* row_elem ::= [\t\ ]*[:alpha:]+
*/
rp = mandoc_calloc(1, sizeof(struct tbl_row));
if (tbl->last) {
tbl->last->next = rp;
tbl->last = rp;
} else
tbl->last = tbl->first = rp;
cell:
while (isspace((unsigned char)p[*pos]))
(*pos)++;
/* Safely exit layout context. */
if ('.' == p[*pos]) {
tbl->part = TBL_PART_DATA;
if (NULL == tbl->first)
TBL_MSG(tbl, MANDOCERR_TBLNOLAYOUT, ln, *pos);
(*pos)++;
return;
}
/* End (and possibly restart) a row. */
if (',' == p[*pos]) {
(*pos)++;
goto row;
} else if ('\0' == p[*pos])
return;
if ( ! cell(tbl, rp, ln, p, pos))
return;
goto cell;
/* NOTREACHED */
}
int
tbl_layout(struct tbl *tbl, int ln, const char *p)
{
int pos;
pos = 0;
row(tbl, ln, p, &pos);
/* Always succeed. */
return(1);
}
|