Annotation of src/usr.bin/mandoc/tbl_layout.c, Revision 1.4
1.4 ! schwarze 1: /* $Id: tbl_layout.c,v 1.1 2010/12/29 14:38:14 kristaps Exp $ */
1.1 schwarze 2: /*
1.4 ! schwarze 3: * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.1 schwarze 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 ! schwarze 21: #include <time.h>
1.1 schwarze 22:
1.4 ! schwarze 23: #include "mandoc.h"
! 24: #include "libmandoc.h"
! 25: #include "libroff.h"
1.1 schwarze 26:
27: struct tbl_phrase {
28: char name;
29: enum tbl_cellt key;
30: };
31:
1.4 ! schwarze 32: #define KEYS_MAX 11
1.1 schwarze 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.4 ! schwarze 48: static int mods(struct tbl_node *, struct tbl_cell *,
! 49: int, const char *, int *);
! 50: static int cell(struct tbl_node *, struct tbl_row *,
1.1 schwarze 51: int, const char *, int *);
1.4 ! schwarze 52: static void row(struct tbl_node *, int, const char *, int *);
! 53: static struct tbl_cell *cell_alloc(struct tbl_node *,
! 54: struct tbl_row *, enum tbl_cellt);
! 55: static void head_adjust(const struct tbl_cell *,
! 56: struct tbl_head *);
1.1 schwarze 57:
58: static int
1.4 ! schwarze 59: mods(struct tbl_node *tbl, struct tbl_cell *cp,
! 60: int ln, const char *p, int *pos)
1.1 schwarze 61: {
62: char buf[5];
63: int i;
64:
1.4 ! schwarze 65: mod:
1.1 schwarze 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: */
1.4 ! schwarze 72: switch (p[*pos]) {
! 73: case ('\0'):
! 74: /* FALLTHROUGH */
! 75: case (' '):
! 76: /* FALLTHROUGH */
! 77: case ('\t'):
! 78: /* FALLTHROUGH */
! 79: case (','):
! 80: /* FALLTHROUGH */
! 81: case ('.'):
1.1 schwarze 82: return(1);
1.4 ! schwarze 83: default:
! 84: break;
! 85: }
1.1 schwarze 86:
87: /* Parse numerical spacing from modifier string. */
88:
1.4 ! schwarze 89: if (isdigit((unsigned char)p[*pos])) {
1.1 schwarze 90: for (i = 0; i < 4; i++) {
1.4 ! schwarze 91: if ( ! isdigit((unsigned char)p[*pos + i]))
1.1 schwarze 92: break;
1.4 ! schwarze 93: buf[i] = p[*pos + i];
1.1 schwarze 94: }
1.4 ! schwarze 95: buf[i] = '\0';
1.1 schwarze 96:
97: /* No greater than 4 digits. */
98:
1.4 ! schwarze 99: if (4 == i) {
! 100: TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos);
! 101: return(0);
! 102: }
1.1 schwarze 103:
1.4 ! schwarze 104: *pos += i;
! 105: cp->spacing = atoi(buf);
1.1 schwarze 106:
1.4 ! schwarze 107: goto mod;
! 108: /* NOTREACHED */
1.1 schwarze 109: }
110:
111: /* TODO: GNU has many more extensions. */
112:
1.4 ! schwarze 113: switch (tolower(p[(*pos)++])) {
1.1 schwarze 114: case ('z'):
115: cp->flags |= TBL_CELL_WIGN;
1.4 ! schwarze 116: goto mod;
1.1 schwarze 117: case ('u'):
118: cp->flags |= TBL_CELL_UP;
1.4 ! schwarze 119: goto mod;
1.1 schwarze 120: case ('e'):
121: cp->flags |= TBL_CELL_EQUAL;
1.4 ! schwarze 122: goto mod;
1.1 schwarze 123: case ('t'):
124: cp->flags |= TBL_CELL_TALIGN;
1.4 ! schwarze 125: goto mod;
1.1 schwarze 126: case ('d'):
127: cp->flags |= TBL_CELL_BALIGN;
1.4 ! schwarze 128: goto mod;
1.1 schwarze 129: case ('f'):
1.4 ! schwarze 130: break;
1.1 schwarze 131: case ('b'):
132: /* FALLTHROUGH */
133: case ('i'):
1.4 ! schwarze 134: (*pos)--;
1.1 schwarze 135: break;
136: default:
1.4 ! schwarze 137: TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos - 1);
! 138: return(0);
1.1 schwarze 139: }
140:
1.4 ! schwarze 141: switch (tolower(p[(*pos)++])) {
1.1 schwarze 142: case ('b'):
143: cp->flags |= TBL_CELL_BOLD;
1.4 ! schwarze 144: goto mod;
1.1 schwarze 145: case ('i'):
146: cp->flags |= TBL_CELL_ITALIC;
1.4 ! schwarze 147: goto mod;
1.1 schwarze 148: default:
149: break;
150: }
151:
1.4 ! schwarze 152: TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos - 1);
! 153: return(0);
1.1 schwarze 154: }
155:
156: static int
1.4 ! schwarze 157: cell(struct tbl_node *tbl, struct tbl_row *rp,
! 158: int ln, const char *p, int *pos)
1.1 schwarze 159: {
1.4 ! schwarze 160: int i;
1.1 schwarze 161: enum tbl_cellt c;
162:
163: /* Parse the column position (`r', `R', `|', ...). */
164:
1.4 ! schwarze 165: for (i = 0; i < KEYS_MAX; i++)
! 166: if (tolower(p[*pos]) == keys[i].name)
! 167: break;
! 168:
! 169: if (KEYS_MAX == i) {
! 170: TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos);
! 171: return(0);
1.1 schwarze 172: }
173:
1.4 ! schwarze 174: (*pos)++;
! 175: c = keys[i].key;
1.1 schwarze 176:
177: /* Extra check for the double-vertical. */
178:
1.4 ! schwarze 179: if (TBL_CELL_VERT == c && '|' == p[*pos]) {
! 180: (*pos)++;
1.1 schwarze 181: c = TBL_CELL_DVERT;
1.4 ! schwarze 182: }
1.1 schwarze 183:
1.4 ! schwarze 184: /* Disallow adjacent spacers. */
1.1 schwarze 185:
1.4 ! schwarze 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: }
1.1 schwarze 192:
193: /* Allocate cell then parse its modifiers. */
194:
1.4 ! schwarze 195: return(mods(tbl, cell_alloc(tbl, rp, c), ln, p, pos));
1.1 schwarze 196: }
197:
198:
1.4 ! schwarze 199: static void
! 200: row(struct tbl_node *tbl, int ln, const char *p, int *pos)
1.1 schwarze 201: {
202: struct tbl_row *rp;
203:
1.4 ! schwarze 204: row: /*
1.1 schwarze 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:
1.4 ! schwarze 214: rp = mandoc_calloc(1, sizeof(struct tbl_row));
! 215: if (tbl->last_row) {
! 216: tbl->last_row->next = rp;
! 217: tbl->last_row = rp;
! 218: } else
! 219: tbl->last_row = tbl->first_row = rp;
! 220:
! 221: cell:
! 222: while (isspace((unsigned char)p[*pos]))
! 223: (*pos)++;
! 224:
! 225: /* Safely exit layout context. */
! 226:
! 227: if ('.' == p[*pos]) {
1.1 schwarze 228: tbl->part = TBL_PART_DATA;
1.4 ! schwarze 229: if (NULL == tbl->first_row)
! 230: TBL_MSG(tbl, MANDOCERR_TBLNOLAYOUT, ln, *pos);
! 231: (*pos)++;
! 232: return;
1.1 schwarze 233: }
234:
1.4 ! schwarze 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 */
1.1 schwarze 248: }
249:
250: int
1.4 ! schwarze 251: tbl_layout(struct tbl_node *tbl, int ln, const char *p)
1.1 schwarze 252: {
253: int pos;
254:
255: pos = 0;
1.4 ! schwarze 256: row(tbl, ln, p, &pos);
! 257:
! 258: /* Always succeed. */
! 259: return(1);
1.1 schwarze 260: }
1.4 ! schwarze 261:
! 262: static struct tbl_cell *
! 263: cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos)
! 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->next : 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->ident = tbl->opts.cols++;
! 323: hp->prev = h->prev;
! 324: if (h->prev)
! 325: h->prev->next = hp;
! 326: if (h == tbl->first_head)
! 327: tbl->first_head = hp;
! 328: h->prev = hp;
! 329: hp->next = h;
! 330: head_adjust(p, hp);
! 331: p->head = hp;
! 332: return(p);
! 333: }
! 334:
! 335: if (NULL != (h = h->next)) {
! 336: head_adjust(p, h);
! 337: p->head = h;
! 338: return(p);
! 339: }
! 340:
! 341: /* Fall through to default case... */
! 342: }
! 343:
! 344: hp = mandoc_calloc(1, sizeof(struct tbl_head));
! 345: hp->ident = tbl->opts.cols++;
! 346:
! 347: if (tbl->last_head) {
! 348: hp->prev = tbl->last_head;
! 349: tbl->last_head->next = hp;
! 350: tbl->last_head = hp;
! 351: } else
! 352: tbl->last_head = tbl->first_head = hp;
! 353:
! 354: head_adjust(p, hp);
! 355: p->head = hp;
! 356: return(p);
! 357: }
! 358:
! 359: static void
! 360: head_adjust(const struct tbl_cell *cell, struct tbl_head *head)
! 361: {
! 362: if (TBL_CELL_VERT != cell->pos &&
! 363: TBL_CELL_DVERT != cell->pos) {
! 364: head->pos = TBL_HEAD_DATA;
! 365: return;
! 366: }
! 367:
! 368: if (TBL_CELL_VERT == cell->pos)
! 369: if (TBL_HEAD_DVERT != head->pos)
! 370: head->pos = TBL_HEAD_VERT;
! 371:
! 372: if (TBL_CELL_DVERT == cell->pos)
! 373: head->pos = TBL_HEAD_DVERT;
! 374: }
! 375: