Import mdocml-1.13.1
[dragonfly.git] / contrib / mdocml / tbl_layout.c
CommitLineData
070c62a6 1/* $Id: tbl_layout.c,v 1.26 2014/04/20 16:46:05 schwarze Exp $ */
80387638 2/*
36342e81 3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
070c62a6 4 * Copyright (c) 2012, 2014 Ingo Schwarze <schwarze@openbsd.org>
80387638
SW
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
a4c7eb57
SW
18#ifdef HAVE_CONFIG_H
19#include "config.h"
20#endif
21
80387638
SW
22#include <ctype.h>
23#include <stdlib.h>
24#include <string.h>
25#include <time.h>
26
27#include "mandoc.h"
070c62a6 28#include "mandoc_aux.h"
80387638
SW
29#include "libmandoc.h"
30#include "libroff.h"
31
32struct tbl_phrase {
33 char name;
34 enum tbl_cellt key;
35};
36
37/*
38 * FIXME: we can make this parse a lot nicer by, when an error is
39 * encountered in a layout key, bailing to the next key (i.e. to the
40 * next whitespace then continuing).
41 */
42
43#define KEYS_MAX 11
44
45static const struct tbl_phrase keys[KEYS_MAX] = {
46 { 'c', TBL_CELL_CENTRE },
47 { 'r', TBL_CELL_RIGHT },
48 { 'l', TBL_CELL_LEFT },
49 { 'n', TBL_CELL_NUMBER },
50 { 's', TBL_CELL_SPAN },
51 { 'a', TBL_CELL_LONG },
52 { '^', TBL_CELL_DOWN },
53 { '-', TBL_CELL_HORIZ },
54 { '_', TBL_CELL_HORIZ },
f88b6c16 55 { '=', TBL_CELL_DHORIZ }
80387638
SW
56};
57
070c62a6 58static int mods(struct tbl_node *, struct tbl_cell *,
80387638 59 int, const char *, int *);
070c62a6 60static int cell(struct tbl_node *, struct tbl_row *,
80387638
SW
61 int, const char *, int *);
62static void row(struct tbl_node *, int, const char *, int *);
f88b6c16
FF
63static struct tbl_cell *cell_alloc(struct tbl_node *, struct tbl_row *,
64 enum tbl_cellt, int vert);
80387638 65
070c62a6 66
80387638 67static int
070c62a6 68mods(struct tbl_node *tbl, struct tbl_cell *cp,
80387638
SW
69 int ln, const char *p, int *pos)
70{
71 char buf[5];
72 int i;
73
a4c7eb57
SW
74 /* Not all types accept modifiers. */
75
76 switch (cp->pos) {
070c62a6 77 case TBL_CELL_DOWN:
a4c7eb57 78 /* FALLTHROUGH */
070c62a6 79 case TBL_CELL_HORIZ:
a4c7eb57 80 /* FALLTHROUGH */
070c62a6 81 case TBL_CELL_DHORIZ:
a4c7eb57
SW
82 return(1);
83 default:
84 break;
85 }
86
80387638 87mod:
070c62a6 88 /*
80387638
SW
89 * XXX: since, at least for now, modifiers are non-conflicting
90 * (are separable by value, regardless of position), we let
91 * modifiers come in any order. The existing tbl doesn't let
92 * this happen.
93 */
94 switch (p[*pos]) {
070c62a6
FF
95 case '\0':
96 /* FALLTHROUGH */
97 case ' ':
80387638 98 /* FALLTHROUGH */
070c62a6 99 case '\t':
80387638 100 /* FALLTHROUGH */
070c62a6 101 case ',':
80387638 102 /* FALLTHROUGH */
070c62a6 103 case '.':
80387638 104 /* FALLTHROUGH */
070c62a6 105 case '|':
80387638
SW
106 return(1);
107 default:
108 break;
109 }
110
111 /* Throw away parenthesised expression. */
112
113 if ('(' == p[*pos]) {
114 (*pos)++;
115 while (p[*pos] && ')' != p[*pos])
116 (*pos)++;
117 if (')' == p[*pos]) {
118 (*pos)++;
119 goto mod;
120 }
070c62a6
FF
121 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
122 ln, *pos, NULL);
80387638
SW
123 return(0);
124 }
125
126 /* Parse numerical spacing from modifier string. */
127
128 if (isdigit((unsigned char)p[*pos])) {
129 for (i = 0; i < 4; i++) {
130 if ( ! isdigit((unsigned char)p[*pos + i]))
131 break;
132 buf[i] = p[*pos + i];
133 }
134 buf[i] = '\0';
135
136 /* No greater than 4 digits. */
137
138 if (4 == i) {
070c62a6
FF
139 mandoc_msg(MANDOCERR_TBLLAYOUT,
140 tbl->parse, ln, *pos, NULL);
80387638
SW
141 return(0);
142 }
143
144 *pos += i;
60e1e752 145 cp->spacing = (size_t)atoi(buf);
80387638
SW
146
147 goto mod;
148 /* NOTREACHED */
070c62a6 149 }
80387638
SW
150
151 /* TODO: GNU has many more extensions. */
152
60e1e752 153 switch (tolower((unsigned char)p[(*pos)++])) {
070c62a6 154 case 'z':
80387638
SW
155 cp->flags |= TBL_CELL_WIGN;
156 goto mod;
070c62a6 157 case 'u':
80387638
SW
158 cp->flags |= TBL_CELL_UP;
159 goto mod;
070c62a6 160 case 'e':
80387638
SW
161 cp->flags |= TBL_CELL_EQUAL;
162 goto mod;
070c62a6 163 case 't':
80387638
SW
164 cp->flags |= TBL_CELL_TALIGN;
165 goto mod;
070c62a6 166 case 'd':
80387638
SW
167 cp->flags |= TBL_CELL_BALIGN;
168 goto mod;
070c62a6 169 case 'w': /* XXX for now, ignore minimal column width */
80387638 170 goto mod;
070c62a6 171 case 'f':
80387638 172 break;
070c62a6 173 case 'r':
36342e81 174 /* FALLTHROUGH */
070c62a6 175 case 'b':
80387638 176 /* FALLTHROUGH */
070c62a6 177 case 'i':
80387638
SW
178 (*pos)--;
179 break;
180 default:
60e1e752 181 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
070c62a6 182 ln, *pos - 1, NULL);
80387638
SW
183 return(0);
184 }
185
60e1e752 186 switch (tolower((unsigned char)p[(*pos)++])) {
070c62a6 187 case '3':
36342e81 188 /* FALLTHROUGH */
070c62a6 189 case 'b':
80387638
SW
190 cp->flags |= TBL_CELL_BOLD;
191 goto mod;
070c62a6 192 case '2':
36342e81 193 /* FALLTHROUGH */
070c62a6 194 case 'i':
80387638
SW
195 cp->flags |= TBL_CELL_ITALIC;
196 goto mod;
070c62a6 197 case '1':
36342e81 198 /* FALLTHROUGH */
070c62a6 199 case 'r':
36342e81 200 goto mod;
80387638
SW
201 default:
202 break;
203 }
204
60e1e752 205 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
070c62a6 206 ln, *pos - 1, NULL);
80387638
SW
207 return(0);
208}
209
210static int
070c62a6 211cell(struct tbl_node *tbl, struct tbl_row *rp,
80387638
SW
212 int ln, const char *p, int *pos)
213{
f88b6c16 214 int vert, i;
80387638
SW
215 enum tbl_cellt c;
216
f88b6c16
FF
217 /* Handle vertical lines. */
218
219 for (vert = 0; '|' == p[*pos]; ++*pos)
220 vert++;
221 while (' ' == p[*pos])
222 (*pos)++;
223
070c62a6
FF
224 /* Handle trailing vertical lines */
225
226 if ('.' == p[*pos] || '\0' == p[*pos]) {
227 rp->vert = vert;
228 return(1);
229 }
230
f88b6c16 231 /* Parse the column position (`c', `l', `r', ...). */
80387638
SW
232
233 for (i = 0; i < KEYS_MAX; i++)
60e1e752 234 if (tolower((unsigned char)p[*pos]) == keys[i].name)
80387638
SW
235 break;
236
237 if (KEYS_MAX == i) {
070c62a6
FF
238 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
239 ln, *pos, NULL);
80387638
SW
240 return(0);
241 }
242
243 c = keys[i].key;
244
245 /*
246 * If a span cell is found first, raise a warning and abort the
60e1e752
SW
247 * parse. If a span cell is found and the last layout element
248 * isn't a "normal" layout, bail.
249 *
250 * FIXME: recover from this somehow?
251 */
252
253 if (TBL_CELL_SPAN == c) {
254 if (NULL == rp->first) {
255 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
070c62a6 256 ln, *pos, NULL);
60e1e752
SW
257 return(0);
258 } else if (rp->last)
259 switch (rp->last->pos) {
070c62a6
FF
260 case TBL_CELL_HORIZ:
261 /* FALLTHROUGH */
262 case TBL_CELL_DHORIZ:
263 mandoc_msg(MANDOCERR_TBLLAYOUT,
264 tbl->parse, ln, *pos, NULL);
60e1e752
SW
265 return(0);
266 default:
267 break;
268 }
269 }
270
271 /*
272 * If a vertical spanner is found, we may not be in the first
273 * row.
80387638
SW
274 */
275
60e1e752
SW
276 if (TBL_CELL_DOWN == c && rp == tbl->first_row) {
277 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse, ln, *pos, NULL);
80387638
SW
278 return(0);
279 }
280
281 (*pos)++;
282
80387638
SW
283 /* Disallow adjacent spacers. */
284
f88b6c16 285 if (vert > 2) {
60e1e752 286 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse, ln, *pos - 1, NULL);
80387638
SW
287 return(0);
288 }
289
290 /* Allocate cell then parse its modifiers. */
291
f88b6c16 292 return(mods(tbl, cell_alloc(tbl, rp, c, vert), ln, p, pos));
80387638
SW
293}
294
80387638
SW
295static void
296row(struct tbl_node *tbl, int ln, const char *p, int *pos)
297{
298 struct tbl_row *rp;
299
300row: /*
301 * EBNF describing this section:
302 *
303 * row ::= row_list [:space:]* [.]?[\n]
304 * row_list ::= [:space:]* row_elem row_tail
305 * row_tail ::= [:space:]*[,] row_list |
306 * epsilon
307 * row_elem ::= [\t\ ]*[:alpha:]+
308 */
309
310 rp = mandoc_calloc(1, sizeof(struct tbl_row));
f88b6c16 311 if (tbl->last_row)
80387638 312 tbl->last_row->next = rp;
f88b6c16
FF
313 else
314 tbl->first_row = rp;
315 tbl->last_row = rp;
80387638
SW
316
317cell:
318 while (isspace((unsigned char)p[*pos]))
319 (*pos)++;
320
321 /* Safely exit layout context. */
322
323 if ('.' == p[*pos]) {
324 tbl->part = TBL_PART_DATA;
070c62a6
FF
325 if (NULL == tbl->first_row)
326 mandoc_msg(MANDOCERR_TBLNOLAYOUT,
327 tbl->parse, ln, *pos, NULL);
80387638
SW
328 (*pos)++;
329 return;
330 }
331
332 /* End (and possibly restart) a row. */
333
334 if (',' == p[*pos]) {
335 (*pos)++;
336 goto row;
337 } else if ('\0' == p[*pos])
338 return;
339
340 if ( ! cell(tbl, rp, ln, p, pos))
341 return;
342
343 goto cell;
344 /* NOTREACHED */
345}
346
347int
348tbl_layout(struct tbl_node *tbl, int ln, const char *p)
349{
350 int pos;
351
352 pos = 0;
353 row(tbl, ln, p, &pos);
354
355 /* Always succeed. */
356 return(1);
357}
358
359static struct tbl_cell *
f88b6c16
FF
360cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos,
361 int vert)
80387638
SW
362{
363 struct tbl_cell *p, *pp;
364 struct tbl_head *h, *hp;
365
366 p = mandoc_calloc(1, sizeof(struct tbl_cell));
367
368 if (NULL != (pp = rp->last)) {
f88b6c16
FF
369 pp->next = p;
370 h = pp->head->next;
371 } else {
372 rp->first = p;
373 h = tbl->first_head;
374 }
375 rp->last = p;
80387638
SW
376
377 p->pos = pos;
f88b6c16 378 p->vert = vert;
80387638 379
f88b6c16 380 /* Re-use header. */
80387638
SW
381
382 if (h) {
f88b6c16
FF
383 p->head = h;
384 return(p);
80387638
SW
385 }
386
387 hp = mandoc_calloc(1, sizeof(struct tbl_head));
388 hp->ident = tbl->opts.cols++;
f88b6c16 389 hp->vert = vert;
80387638
SW
390
391 if (tbl->last_head) {
392 hp->prev = tbl->last_head;
393 tbl->last_head->next = hp;
80387638 394 } else
f88b6c16
FF
395 tbl->first_head = hp;
396 tbl->last_head = hp;
80387638 397
80387638
SW
398 p->head = hp;
399 return(p);
400}