Annotation of mandoc/tbl_layout.c, Revision 1.23
1.23 ! schwarze 1: /* $Id: tbl_layout.c,v 1.22 2011/09/18 14:14:15 schwarze Exp $ */
1.1 kristaps 2: /*
1.22 schwarze 3: * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.23 ! schwarze 4: * Copyright (c) 2012 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 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: */
1.18 kristaps 18: #ifdef HAVE_CONFIG_H
19: #include "config.h"
20: #endif
21:
1.1 kristaps 22: #include <assert.h>
23: #include <ctype.h>
24: #include <stdlib.h>
25: #include <string.h>
1.4 kristaps 26: #include <time.h>
1.1 kristaps 27:
28: #include "mandoc.h"
29: #include "libmandoc.h"
30: #include "libroff.h"
31:
32: struct tbl_phrase {
33: char name;
34: enum tbl_cellt key;
35: };
36:
1.11 kristaps 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:
1.2 kristaps 43: #define KEYS_MAX 11
1.1 kristaps 44:
45: static 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 },
1.23 ! schwarze 55: { '=', TBL_CELL_DHORIZ }
1.1 kristaps 56: };
57:
1.6 kristaps 58: static int mods(struct tbl_node *, struct tbl_cell *,
1.5 kristaps 59: int, const char *, int *);
1.6 kristaps 60: static int cell(struct tbl_node *, struct tbl_row *,
1.5 kristaps 61: int, const char *, int *);
1.6 kristaps 62: static void row(struct tbl_node *, int, const char *, int *);
1.23 ! schwarze 63: static struct tbl_cell *cell_alloc(struct tbl_node *, struct tbl_row *,
! 64: enum tbl_cellt, int vert);
1.1 kristaps 65:
66: static int
1.6 kristaps 67: mods(struct tbl_node *tbl, struct tbl_cell *cp,
1.1 kristaps 68: int ln, const char *p, int *pos)
69: {
70: char buf[5];
71: int i;
1.20 kristaps 72:
73: /* Not all types accept modifiers. */
74:
75: switch (cp->pos) {
76: case (TBL_CELL_DOWN):
77: /* FALLTHROUGH */
78: case (TBL_CELL_HORIZ):
79: /* FALLTHROUGH */
80: case (TBL_CELL_DHORIZ):
81: return(1);
82: default:
83: break;
84: }
1.1 kristaps 85:
86: mod:
87: /*
88: * XXX: since, at least for now, modifiers are non-conflicting
89: * (are separable by value, regardless of position), we let
90: * modifiers come in any order. The existing tbl doesn't let
91: * this happen.
92: */
93: switch (p[*pos]) {
94: case ('\0'):
95: /* FALLTHROUGH */
96: case (' '):
97: /* FALLTHROUGH */
98: case ('\t'):
99: /* FALLTHROUGH */
100: case (','):
101: /* FALLTHROUGH */
102: case ('.'):
103: return(1);
104: default:
105: break;
1.12 kristaps 106: }
107:
108: /* Throw away parenthesised expression. */
109:
110: if ('(' == p[*pos]) {
111: (*pos)++;
112: while (p[*pos] && ')' != p[*pos])
113: (*pos)++;
114: if (')' == p[*pos]) {
115: (*pos)++;
116: goto mod;
117: }
1.17 kristaps 118: mandoc_msg(MANDOCERR_TBLLAYOUT,
119: tbl->parse, ln, *pos, NULL);
1.12 kristaps 120: return(0);
1.1 kristaps 121: }
122:
123: /* Parse numerical spacing from modifier string. */
124:
125: if (isdigit((unsigned char)p[*pos])) {
126: for (i = 0; i < 4; i++) {
127: if ( ! isdigit((unsigned char)p[*pos + i]))
128: break;
129: buf[i] = p[*pos + i];
130: }
131: buf[i] = '\0';
132:
133: /* No greater than 4 digits. */
134:
135: if (4 == i) {
1.17 kristaps 136: mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
137: ln, *pos, NULL);
1.1 kristaps 138: return(0);
139: }
140:
141: *pos += i;
1.15 kristaps 142: cp->spacing = (size_t)atoi(buf);
1.1 kristaps 143:
144: goto mod;
145: /* NOTREACHED */
146: }
147:
148: /* TODO: GNU has many more extensions. */
149:
1.13 joerg 150: switch (tolower((unsigned char)p[(*pos)++])) {
1.1 kristaps 151: case ('z'):
152: cp->flags |= TBL_CELL_WIGN;
153: goto mod;
154: case ('u'):
155: cp->flags |= TBL_CELL_UP;
156: goto mod;
157: case ('e'):
158: cp->flags |= TBL_CELL_EQUAL;
159: goto mod;
160: case ('t'):
161: cp->flags |= TBL_CELL_TALIGN;
162: goto mod;
163: case ('d'):
164: cp->flags |= TBL_CELL_BALIGN;
1.10 schwarze 165: goto mod;
166: case ('w'): /* XXX for now, ignore minimal column width */
1.1 kristaps 167: goto mod;
168: case ('f'):
1.2 kristaps 169: break;
1.21 kristaps 170: case ('r'):
171: /* FALLTHROUGH */
1.1 kristaps 172: case ('b'):
173: /* FALLTHROUGH */
174: case ('i'):
1.2 kristaps 175: (*pos)--;
1.1 kristaps 176: break;
177: default:
1.17 kristaps 178: mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
179: ln, *pos - 1, NULL);
1.1 kristaps 180: return(0);
181: }
182:
1.13 joerg 183: switch (tolower((unsigned char)p[(*pos)++])) {
1.21 kristaps 184: case ('3'):
185: /* FALLTHROUGH */
1.1 kristaps 186: case ('b'):
187: cp->flags |= TBL_CELL_BOLD;
188: goto mod;
1.21 kristaps 189: case ('2'):
190: /* FALLTHROUGH */
1.1 kristaps 191: case ('i'):
192: cp->flags |= TBL_CELL_ITALIC;
1.21 kristaps 193: goto mod;
194: case ('1'):
195: /* FALLTHROUGH */
196: case ('r'):
1.1 kristaps 197: goto mod;
198: default:
199: break;
200: }
201:
1.17 kristaps 202: mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
203: ln, *pos - 1, NULL);
1.1 kristaps 204: return(0);
205: }
206:
207: static int
1.6 kristaps 208: cell(struct tbl_node *tbl, struct tbl_row *rp,
1.1 kristaps 209: int ln, const char *p, int *pos)
210: {
1.23 ! schwarze 211: int vert, i;
1.1 kristaps 212: enum tbl_cellt c;
213:
1.23 ! schwarze 214: /* Handle vertical lines. */
! 215:
! 216: for (vert = 0; '|' == p[*pos]; ++*pos)
! 217: vert++;
! 218: while (' ' == p[*pos])
! 219: (*pos)++;
! 220:
! 221: /* Parse the column position (`c', `l', `r', ...). */
1.1 kristaps 222:
223: for (i = 0; i < KEYS_MAX; i++)
1.13 joerg 224: if (tolower((unsigned char)p[*pos]) == keys[i].name)
1.1 kristaps 225: break;
226:
227: if (KEYS_MAX == i) {
1.17 kristaps 228: mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
229: ln, *pos, NULL);
1.1 kristaps 230: return(0);
231: }
232:
1.11 kristaps 233: c = keys[i].key;
234:
235: /*
236: * If a span cell is found first, raise a warning and abort the
1.14 kristaps 237: * parse. If a span cell is found and the last layout element
238: * isn't a "normal" layout, bail.
239: *
240: * FIXME: recover from this somehow?
1.11 kristaps 241: */
242:
1.14 kristaps 243: if (TBL_CELL_SPAN == c) {
244: if (NULL == rp->first) {
1.17 kristaps 245: mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
246: ln, *pos, NULL);
1.14 kristaps 247: return(0);
248: } else if (rp->last)
249: switch (rp->last->pos) {
250: case (TBL_CELL_HORIZ):
251: case (TBL_CELL_DHORIZ):
1.17 kristaps 252: mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
253: ln, *pos, NULL);
1.14 kristaps 254: return(0);
255: default:
256: break;
257: }
1.16 kristaps 258: }
259:
260: /*
261: * If a vertical spanner is found, we may not be in the first
262: * row.
263: */
264:
265: if (TBL_CELL_DOWN == c && rp == tbl->first_row) {
1.17 kristaps 266: mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse, ln, *pos, NULL);
1.16 kristaps 267: return(0);
1.11 kristaps 268: }
269:
1.1 kristaps 270: (*pos)++;
271:
272: /* Disallow adjacent spacers. */
273:
1.23 ! schwarze 274: if (vert > 2) {
1.17 kristaps 275: mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse, ln, *pos - 1, NULL);
1.1 kristaps 276: return(0);
277: }
278:
279: /* Allocate cell then parse its modifiers. */
280:
1.23 ! schwarze 281: return(mods(tbl, cell_alloc(tbl, rp, c, vert), ln, p, pos));
1.1 kristaps 282: }
283:
284:
285: static void
1.6 kristaps 286: row(struct tbl_node *tbl, int ln, const char *p, int *pos)
1.1 kristaps 287: {
288: struct tbl_row *rp;
289:
290: row: /*
291: * EBNF describing this section:
292: *
293: * row ::= row_list [:space:]* [.]?[\n]
294: * row_list ::= [:space:]* row_elem row_tail
295: * row_tail ::= [:space:]*[,] row_list |
296: * epsilon
297: * row_elem ::= [\t\ ]*[:alpha:]+
298: */
299:
300: rp = mandoc_calloc(1, sizeof(struct tbl_row));
1.23 ! schwarze 301: if (tbl->last_row)
1.3 kristaps 302: tbl->last_row->next = rp;
1.23 ! schwarze 303: else
! 304: tbl->first_row = rp;
! 305: tbl->last_row = rp;
1.1 kristaps 306:
307: cell:
308: while (isspace((unsigned char)p[*pos]))
309: (*pos)++;
310:
311: /* Safely exit layout context. */
312:
313: if ('.' == p[*pos]) {
314: tbl->part = TBL_PART_DATA;
1.3 kristaps 315: if (NULL == tbl->first_row)
1.17 kristaps 316: mandoc_msg(MANDOCERR_TBLNOLAYOUT, tbl->parse,
317: ln, *pos, NULL);
1.1 kristaps 318: (*pos)++;
319: return;
320: }
321:
322: /* End (and possibly restart) a row. */
323:
324: if (',' == p[*pos]) {
325: (*pos)++;
326: goto row;
327: } else if ('\0' == p[*pos])
328: return;
329:
330: if ( ! cell(tbl, rp, ln, p, pos))
331: return;
332:
333: goto cell;
334: /* NOTREACHED */
335: }
336:
337: int
1.6 kristaps 338: tbl_layout(struct tbl_node *tbl, int ln, const char *p)
1.1 kristaps 339: {
340: int pos;
341:
342: pos = 0;
343: row(tbl, ln, p, &pos);
344:
345: /* Always succeed. */
346: return(1);
347: }
1.5 kristaps 348:
349: static struct tbl_cell *
1.23 ! schwarze 350: cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos,
! 351: int vert)
1.5 kristaps 352: {
353: struct tbl_cell *p, *pp;
354: struct tbl_head *h, *hp;
355:
356: p = mandoc_calloc(1, sizeof(struct tbl_cell));
357:
358: if (NULL != (pp = rp->last)) {
1.23 ! schwarze 359: pp->next = p;
! 360: h = pp->head->next;
! 361: } else {
! 362: rp->first = p;
! 363: h = tbl->first_head;
! 364: }
! 365: rp->last = p;
1.5 kristaps 366:
367: p->pos = pos;
1.23 ! schwarze 368: p->vert = vert;
1.5 kristaps 369:
1.23 ! schwarze 370: /* Re-use header. */
1.5 kristaps 371:
372: if (h) {
1.23 ! schwarze 373: p->head = h;
! 374: return(p);
1.5 kristaps 375: }
376:
377: hp = mandoc_calloc(1, sizeof(struct tbl_head));
1.9 kristaps 378: hp->ident = tbl->opts.cols++;
1.23 ! schwarze 379: hp->vert = vert;
1.5 kristaps 380:
381: if (tbl->last_head) {
382: hp->prev = tbl->last_head;
383: tbl->last_head->next = hp;
384: } else
1.23 ! schwarze 385: tbl->first_head = hp;
! 386: tbl->last_head = hp;
1.5 kristaps 387:
388: p->head = hp;
389: return(p);
390: }
CVSweb