]> git.cameronkatri.com Git - mandoc.git/blob - tbl_layout.c
Remove delims from struct tbl (not used anywhere and never will be).
[mandoc.git] / tbl_layout.c
1 /* $Id: tbl_layout.c,v 1.10 2011/01/04 23:48:39 schwarze Exp $ */
2 /*
3 * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 *
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.
8 *
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.
16 */
17 #include <assert.h>
18 #include <ctype.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <time.h>
22
23 #include "mandoc.h"
24 #include "libmandoc.h"
25 #include "libroff.h"
26
27 struct tbl_phrase {
28 char name;
29 enum tbl_cellt key;
30 };
31
32 #define KEYS_MAX 11
33
34 static const struct tbl_phrase keys[KEYS_MAX] = {
35 { 'c', TBL_CELL_CENTRE },
36 { 'r', TBL_CELL_RIGHT },
37 { 'l', TBL_CELL_LEFT },
38 { 'n', TBL_CELL_NUMBER },
39 { 's', TBL_CELL_SPAN },
40 { 'a', TBL_CELL_LONG },
41 { '^', TBL_CELL_DOWN },
42 { '-', TBL_CELL_HORIZ },
43 { '_', TBL_CELL_HORIZ },
44 { '=', TBL_CELL_DHORIZ },
45 { '|', TBL_CELL_VERT }
46 };
47
48 static int mods(struct tbl_node *, struct tbl_cell *,
49 int, const char *, int *);
50 static int cell(struct tbl_node *, struct tbl_row *,
51 int, const char *, int *);
52 static void row(struct tbl_node *, int, const char *, int *);
53 static struct tbl_cell *cell_alloc(struct tbl_node *,
54 struct tbl_row *, enum tbl_cellt);
55 static void head_adjust(const struct tbl_cell *,
56 struct tbl_head *);
57
58 static int
59 mods(struct tbl_node *tbl, struct tbl_cell *cp,
60 int ln, const char *p, int *pos)
61 {
62 char buf[5];
63 int i;
64
65 mod:
66 /*
67 * XXX: since, at least for now, modifiers are non-conflicting
68 * (are separable by value, regardless of position), we let
69 * modifiers come in any order. The existing tbl doesn't let
70 * this happen.
71 */
72 switch (p[*pos]) {
73 case ('\0'):
74 /* FALLTHROUGH */
75 case (' '):
76 /* FALLTHROUGH */
77 case ('\t'):
78 /* FALLTHROUGH */
79 case (','):
80 /* FALLTHROUGH */
81 case ('.'):
82 return(1);
83 default:
84 break;
85 }
86
87 /* Parse numerical spacing from modifier string. */
88
89 if (isdigit((unsigned char)p[*pos])) {
90 for (i = 0; i < 4; i++) {
91 if ( ! isdigit((unsigned char)p[*pos + i]))
92 break;
93 buf[i] = p[*pos + i];
94 }
95 buf[i] = '\0';
96
97 /* No greater than 4 digits. */
98
99 if (4 == i) {
100 TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos);
101 return(0);
102 }
103
104 *pos += i;
105 cp->spacing = atoi(buf);
106
107 goto mod;
108 /* NOTREACHED */
109 }
110
111 /* TODO: GNU has many more extensions. */
112
113 switch (tolower(p[(*pos)++])) {
114 case ('z'):
115 cp->flags |= TBL_CELL_WIGN;
116 goto mod;
117 case ('u'):
118 cp->flags |= TBL_CELL_UP;
119 goto mod;
120 case ('e'):
121 cp->flags |= TBL_CELL_EQUAL;
122 goto mod;
123 case ('t'):
124 cp->flags |= TBL_CELL_TALIGN;
125 goto mod;
126 case ('d'):
127 cp->flags |= TBL_CELL_BALIGN;
128 goto mod;
129 case ('w'): /* XXX for now, ignore minimal column width */
130 goto mod;
131 case ('f'):
132 break;
133 case ('b'):
134 /* FALLTHROUGH */
135 case ('i'):
136 (*pos)--;
137 break;
138 default:
139 TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos - 1);
140 return(0);
141 }
142
143 switch (tolower(p[(*pos)++])) {
144 case ('b'):
145 cp->flags |= TBL_CELL_BOLD;
146 goto mod;
147 case ('i'):
148 cp->flags |= TBL_CELL_ITALIC;
149 goto mod;
150 default:
151 break;
152 }
153
154 TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos - 1);
155 return(0);
156 }
157
158 static int
159 cell(struct tbl_node *tbl, struct tbl_row *rp,
160 int ln, const char *p, int *pos)
161 {
162 int i;
163 enum tbl_cellt c;
164
165 /* Parse the column position (`r', `R', `|', ...). */
166
167 for (i = 0; i < KEYS_MAX; i++)
168 if (tolower(p[*pos]) == keys[i].name)
169 break;
170
171 if (KEYS_MAX == i) {
172 TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos);
173 return(0);
174 }
175
176 (*pos)++;
177 c = keys[i].key;
178
179 /* Extra check for the double-vertical. */
180
181 if (TBL_CELL_VERT == c && '|' == p[*pos]) {
182 (*pos)++;
183 c = TBL_CELL_DVERT;
184 }
185
186 /* Disallow adjacent spacers. */
187
188 if (rp->last && (TBL_CELL_VERT == c || TBL_CELL_DVERT == c) &&
189 (TBL_CELL_VERT == rp->last->pos ||
190 TBL_CELL_DVERT == rp->last->pos)) {
191 TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos - 1);
192 return(0);
193 }
194
195 /* Allocate cell then parse its modifiers. */
196
197 return(mods(tbl, cell_alloc(tbl, rp, c), ln, p, pos));
198 }
199
200
201 static void
202 row(struct tbl_node *tbl, int ln, const char *p, int *pos)
203 {
204 struct tbl_row *rp;
205
206 row: /*
207 * EBNF describing this section:
208 *
209 * row ::= row_list [:space:]* [.]?[\n]
210 * row_list ::= [:space:]* row_elem row_tail
211 * row_tail ::= [:space:]*[,] row_list |
212 * epsilon
213 * row_elem ::= [\t\ ]*[:alpha:]+
214 */
215
216 rp = mandoc_calloc(1, sizeof(struct tbl_row));
217 if (tbl->last_row) {
218 tbl->last_row->next = rp;
219 tbl->last_row = rp;
220 } else
221 tbl->last_row = tbl->first_row = rp;
222
223 cell:
224 while (isspace((unsigned char)p[*pos]))
225 (*pos)++;
226
227 /* Safely exit layout context. */
228
229 if ('.' == p[*pos]) {
230 tbl->part = TBL_PART_DATA;
231 if (NULL == tbl->first_row)
232 TBL_MSG(tbl, MANDOCERR_TBLNOLAYOUT, ln, *pos);
233 (*pos)++;
234 return;
235 }
236
237 /* End (and possibly restart) a row. */
238
239 if (',' == p[*pos]) {
240 (*pos)++;
241 goto row;
242 } else if ('\0' == p[*pos])
243 return;
244
245 if ( ! cell(tbl, rp, ln, p, pos))
246 return;
247
248 goto cell;
249 /* NOTREACHED */
250 }
251
252 int
253 tbl_layout(struct tbl_node *tbl, int ln, const char *p)
254 {
255 int pos;
256
257 pos = 0;
258 row(tbl, ln, p, &pos);
259
260 /* Always succeed. */
261 return(1);
262 }
263
264 static struct tbl_cell *
265 cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos)
266 {
267 struct tbl_cell *p, *pp;
268 struct tbl_head *h, *hp;
269
270 p = mandoc_calloc(1, sizeof(struct tbl_cell));
271
272 if (NULL != (pp = rp->last)) {
273 rp->last->next = p;
274 rp->last = p;
275 } else
276 rp->last = rp->first = p;
277
278 p->pos = pos;
279
280 /*
281 * This is a little bit complicated. Here we determine the
282 * header the corresponds to a cell. We add headers dynamically
283 * when need be or re-use them, otherwise. As an example, given
284 * the following:
285 *
286 * 1 c || l
287 * 2 | c | l
288 * 3 l l
289 * 3 || c | l |.
290 *
291 * We first add the new headers (as there are none) in (1); then
292 * in (2) we insert the first spanner (as it doesn't match up
293 * with the header); then we re-use the prior data headers,
294 * skipping over the spanners; then we re-use everything and add
295 * a last spanner. Note that VERT headers are made into DVERT
296 * ones.
297 */
298
299 h = pp ? pp->head->next : tbl->first_head;
300
301 if (h) {
302 /* Re-use data header. */
303 if (TBL_HEAD_DATA == h->pos &&
304 (TBL_CELL_VERT != p->pos &&
305 TBL_CELL_DVERT != p->pos)) {
306 p->head = h;
307 return(p);
308 }
309
310 /* Re-use spanner header. */
311 if (TBL_HEAD_DATA != h->pos &&
312 (TBL_CELL_VERT == p->pos ||
313 TBL_CELL_DVERT == p->pos)) {
314 head_adjust(p, h);
315 p->head = h;
316 return(p);
317 }
318
319 /* Right-shift headers with a new spanner. */
320 if (TBL_HEAD_DATA == h->pos &&
321 (TBL_CELL_VERT == p->pos ||
322 TBL_CELL_DVERT == p->pos)) {
323 hp = mandoc_calloc(1, sizeof(struct tbl_head));
324 hp->ident = tbl->opts.cols++;
325 hp->prev = h->prev;
326 if (h->prev)
327 h->prev->next = hp;
328 if (h == tbl->first_head)
329 tbl->first_head = hp;
330 h->prev = hp;
331 hp->next = h;
332 head_adjust(p, hp);
333 p->head = hp;
334 return(p);
335 }
336
337 if (NULL != (h = h->next)) {
338 head_adjust(p, h);
339 p->head = h;
340 return(p);
341 }
342
343 /* Fall through to default case... */
344 }
345
346 hp = mandoc_calloc(1, sizeof(struct tbl_head));
347 hp->ident = tbl->opts.cols++;
348
349 if (tbl->last_head) {
350 hp->prev = tbl->last_head;
351 tbl->last_head->next = hp;
352 tbl->last_head = hp;
353 } else
354 tbl->last_head = tbl->first_head = hp;
355
356 head_adjust(p, hp);
357 p->head = hp;
358 return(p);
359 }
360
361 static void
362 head_adjust(const struct tbl_cell *cell, struct tbl_head *head)
363 {
364 if (TBL_CELL_VERT != cell->pos &&
365 TBL_CELL_DVERT != cell->pos) {
366 head->pos = TBL_HEAD_DATA;
367 return;
368 }
369
370 if (TBL_CELL_VERT == cell->pos)
371 if (TBL_HEAD_DVERT != head->pos)
372 head->pos = TBL_HEAD_VERT;
373
374 if (TBL_CELL_DVERT == cell->pos)
375 head->pos = TBL_HEAD_DVERT;
376 }
377