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