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