Annotation of mandoc/tbl_layout.c, Revision 1.6
1.6 ! kristaps 1: /* $Id: tbl_layout.c,v 1.5 2011/01/01 22:19:15 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.2 kristaps 32: #define KEYS_MAX 11
1.1 kristaps 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:
1.6 ! kristaps 48: static int mods(struct tbl_node *, struct tbl_cell *,
1.5 kristaps 49: int, const char *, int *);
1.6 ! kristaps 50: static int cell(struct tbl_node *, struct tbl_row *,
1.5 kristaps 51: int, const char *, int *);
1.6 ! kristaps 52: static void row(struct tbl_node *, int, const char *, int *);
! 53: static struct tbl_cell *cell_alloc(struct tbl_node *,
1.5 kristaps 54: struct tbl_row *, enum tbl_cellt);
55: static void head_adjust(const struct tbl_cell *,
56: struct tbl_head *);
1.1 kristaps 57:
58: static int
1.6 ! kristaps 59: mods(struct tbl_node *tbl, struct tbl_cell *cp,
1.1 kristaps 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:
1.2 kristaps 113: switch (tolower(p[(*pos)++])) {
1.1 kristaps 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 ('f'):
1.2 kristaps 130: break;
1.1 kristaps 131: case ('b'):
132: /* FALLTHROUGH */
133: case ('i'):
1.2 kristaps 134: (*pos)--;
1.1 kristaps 135: break;
136: default:
137: TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos - 1);
138: return(0);
139: }
140:
1.2 kristaps 141: switch (tolower(p[(*pos)++])) {
1.1 kristaps 142: case ('b'):
143: cp->flags |= TBL_CELL_BOLD;
144: goto mod;
145: case ('i'):
146: cp->flags |= TBL_CELL_ITALIC;
147: goto mod;
148: default:
149: break;
150: }
151:
152: TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos - 1);
153: return(0);
154: }
155:
156: static int
1.6 ! kristaps 157: cell(struct tbl_node *tbl, struct tbl_row *rp,
1.1 kristaps 158: int ln, const char *p, int *pos)
159: {
160: int i;
161: enum tbl_cellt c;
162:
163: /* Parse the column position (`r', `R', `|', ...). */
164:
165: for (i = 0; i < KEYS_MAX; i++)
1.2 kristaps 166: if (tolower(p[*pos]) == keys[i].name)
1.1 kristaps 167: break;
168:
169: if (KEYS_MAX == i) {
170: TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos);
171: return(0);
172: }
173:
174: (*pos)++;
175: c = keys[i].key;
176:
177: /* Extra check for the double-vertical. */
178:
179: if (TBL_CELL_VERT == c && '|' == p[*pos]) {
180: (*pos)++;
181: c = TBL_CELL_DVERT;
182: }
183:
184: /* Disallow adjacent spacers. */
185:
186: if (rp->last && (TBL_CELL_VERT == c || TBL_CELL_DVERT == c) &&
187: (TBL_CELL_VERT == rp->last->pos ||
188: TBL_CELL_DVERT == rp->last->pos)) {
189: TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos - 1);
190: return(0);
191: }
192:
193: /* Allocate cell then parse its modifiers. */
194:
1.5 kristaps 195: return(mods(tbl, cell_alloc(tbl, rp, c), ln, p, pos));
1.1 kristaps 196: }
197:
198:
199: static void
1.6 ! kristaps 200: row(struct tbl_node *tbl, int ln, const char *p, int *pos)
1.1 kristaps 201: {
202: struct tbl_row *rp;
203:
204: row: /*
205: * EBNF describing this section:
206: *
207: * row ::= row_list [:space:]* [.]?[\n]
208: * row_list ::= [:space:]* row_elem row_tail
209: * row_tail ::= [:space:]*[,] row_list |
210: * epsilon
211: * row_elem ::= [\t\ ]*[:alpha:]+
212: */
213:
214: rp = mandoc_calloc(1, sizeof(struct tbl_row));
1.3 kristaps 215: if (tbl->last_row) {
216: tbl->last_row->next = rp;
217: tbl->last_row = rp;
1.1 kristaps 218: } else
1.3 kristaps 219: tbl->last_row = tbl->first_row = rp;
1.1 kristaps 220:
221: cell:
222: while (isspace((unsigned char)p[*pos]))
223: (*pos)++;
224:
225: /* Safely exit layout context. */
226:
227: if ('.' == p[*pos]) {
228: tbl->part = TBL_PART_DATA;
1.3 kristaps 229: if (NULL == tbl->first_row)
1.1 kristaps 230: TBL_MSG(tbl, MANDOCERR_TBLNOLAYOUT, ln, *pos);
231: (*pos)++;
232: return;
233: }
234:
235: /* End (and possibly restart) a row. */
236:
237: if (',' == p[*pos]) {
238: (*pos)++;
239: goto row;
240: } else if ('\0' == p[*pos])
241: return;
242:
243: if ( ! cell(tbl, rp, ln, p, pos))
244: return;
245:
246: goto cell;
247: /* NOTREACHED */
248: }
249:
250: int
1.6 ! kristaps 251: tbl_layout(struct tbl_node *tbl, int ln, const char *p)
1.1 kristaps 252: {
253: int pos;
254:
255: pos = 0;
256: row(tbl, ln, p, &pos);
257:
258: /* Always succeed. */
259: return(1);
260: }
1.5 kristaps 261:
262: static struct tbl_cell *
1.6 ! kristaps 263: cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos)
1.5 kristaps 264: {
265: struct tbl_cell *p, *pp;
266: struct tbl_head *h, *hp;
267:
268: p = mandoc_calloc(1, sizeof(struct tbl_cell));
269:
270: if (NULL != (pp = rp->last)) {
271: rp->last->next = p;
272: rp->last = p;
273: } else
274: rp->last = rp->first = p;
275:
276: p->pos = pos;
277:
278: /*
279: * This is a little bit complicated. Here we determine the
280: * header the corresponds to a cell. We add headers dynamically
281: * when need be or re-use them, otherwise. As an example, given
282: * the following:
283: *
284: * 1 c || l
285: * 2 | c | l
286: * 3 l l
287: * 3 || c | l |.
288: *
289: * We first add the new headers (as there are none) in (1); then
290: * in (2) we insert the first spanner (as it doesn't match up
291: * with the header); then we re-use the prior data headers,
292: * skipping over the spanners; then we re-use everything and add
293: * a last spanner. Note that VERT headers are made into DVERT
294: * ones.
295: */
296:
297: h = pp ? pp->head->prev : tbl->first_head;
298:
299: if (h) {
300: /* Re-use data header. */
301: if (TBL_HEAD_DATA == h->pos &&
302: (TBL_CELL_VERT != p->pos &&
303: TBL_CELL_DVERT != p->pos)) {
304: p->head = h;
305: return(p);
306: }
307:
308: /* Re-use spanner header. */
309: if (TBL_HEAD_DATA != h->pos &&
310: (TBL_CELL_VERT == p->pos ||
311: TBL_CELL_DVERT == p->pos)) {
312: head_adjust(p, h);
313: p->head = h;
314: return(p);
315: }
316:
317: /* Right-shift headers with a new spanner. */
318: if (TBL_HEAD_DATA == h->pos &&
319: (TBL_CELL_VERT == p->pos ||
320: TBL_CELL_DVERT == p->pos)) {
321: hp = mandoc_calloc(1, sizeof(struct tbl_head));
322: hp->prev = h->prev;
323: if (h->prev)
324: h->prev->next = hp;
325: h->prev = hp;
326: hp->next = h;
327: head_adjust(p, hp);
328: p->head = hp;
329: return(p);
330: }
331:
332: if (NULL != (h = h->next)) {
333: head_adjust(p, h);
334: p->head = h;
335: return(p);
336: }
337:
338: /* Fall through to default case... */
339: }
340:
341: hp = mandoc_calloc(1, sizeof(struct tbl_head));
342:
343: if (tbl->last_head) {
344: hp->prev = tbl->last_head;
345: tbl->last_head->next = hp;
346: tbl->last_head = hp;
347: } else
348: tbl->last_head = tbl->first_head = hp;
349:
350: head_adjust(p, hp);
351: p->head = hp;
352: return(p);
353: }
354:
355: static void
356: head_adjust(const struct tbl_cell *cell, struct tbl_head *head)
357: {
358: if (TBL_CELL_VERT != cell->pos &&
359: TBL_CELL_DVERT != cell->pos) {
360: head->pos = TBL_HEAD_DATA;
361: return;
362: }
363:
364: if (TBL_CELL_VERT == cell->pos)
365: if (TBL_HEAD_DVERT != head->pos)
366: head->pos = TBL_HEAD_VERT;
367:
368: if (TBL_CELL_DVERT == cell->pos)
369: head->pos = TBL_HEAD_DVERT;
370: }
371:
CVSweb