Annotation of mandoc/tbl_layout.c, Revision 1.33
1.33 ! schwarze 1: /* $Id: tbl_layout.c,v 1.32 2015/01/26 18:42:30 schwarze Exp $ */
1.1 kristaps 2: /*
1.22 schwarze 3: * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.32 schwarze 4: * Copyright (c) 2012, 2014, 2015 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: #include "config.h"
1.27 schwarze 19:
20: #include <sys/types.h>
1.18 kristaps 21:
1.1 kristaps 22: #include <ctype.h>
23: #include <stdlib.h>
24: #include <string.h>
1.4 kristaps 25: #include <time.h>
1.1 kristaps 26:
27: #include "mandoc.h"
1.24 schwarze 28: #include "mandoc_aux.h"
1.1 kristaps 29: #include "libmandoc.h"
30: #include "libroff.h"
31:
32: struct tbl_phrase {
33: char name;
34: enum tbl_cellt key;
35: };
36:
1.32 schwarze 37: static const struct tbl_phrase keys[] = {
1.1 kristaps 38: { 'c', TBL_CELL_CENTRE },
39: { 'r', TBL_CELL_RIGHT },
40: { 'l', TBL_CELL_LEFT },
41: { 'n', TBL_CELL_NUMBER },
42: { 's', TBL_CELL_SPAN },
43: { 'a', TBL_CELL_LONG },
44: { '^', TBL_CELL_DOWN },
45: { '-', TBL_CELL_HORIZ },
46: { '_', TBL_CELL_HORIZ },
1.23 schwarze 47: { '=', TBL_CELL_DHORIZ }
1.1 kristaps 48: };
49:
1.32 schwarze 50: #define KEYS_MAX ((int)(sizeof(keys)/sizeof(keys[0])))
51:
52: static void mods(struct tbl_node *, struct tbl_cell *,
1.5 kristaps 53: int, const char *, int *);
1.32 schwarze 54: static void cell(struct tbl_node *, struct tbl_row *,
1.5 kristaps 55: int, const char *, int *);
1.23 schwarze 56: static struct tbl_cell *cell_alloc(struct tbl_node *, struct tbl_row *,
1.33 ! schwarze 57: enum tbl_cellt);
1.1 kristaps 58:
1.26 schwarze 59:
1.32 schwarze 60: static void
1.26 schwarze 61: mods(struct tbl_node *tbl, struct tbl_cell *cp,
1.1 kristaps 62: int ln, const char *p, int *pos)
63: {
1.32 schwarze 64: char *endptr;
1.20 kristaps 65:
1.32 schwarze 66: mod:
67: while (p[*pos] == ' ' || p[*pos] == '\t')
68: (*pos)++;
1.20 kristaps 69:
1.32 schwarze 70: /* Row delimiters and cell specifiers end modifier lists. */
1.1 kristaps 71:
1.33 ! schwarze 72: if (strchr(".,-=^_ACLNRSaclnrs", p[*pos]) != NULL)
1.32 schwarze 73: return;
1.12 kristaps 74:
75: /* Throw away parenthesised expression. */
76:
77: if ('(' == p[*pos]) {
78: (*pos)++;
79: while (p[*pos] && ')' != p[*pos])
80: (*pos)++;
81: if (')' == p[*pos]) {
82: (*pos)++;
83: goto mod;
84: }
1.32 schwarze 85: mandoc_msg(MANDOCERR_TBLLAYOUT_PAR, tbl->parse,
1.26 schwarze 86: ln, *pos, NULL);
1.32 schwarze 87: return;
1.1 kristaps 88: }
89:
90: /* Parse numerical spacing from modifier string. */
91:
92: if (isdigit((unsigned char)p[*pos])) {
1.32 schwarze 93: cp->spacing = strtoull(p + *pos, &endptr, 10);
94: *pos = endptr - p;
1.1 kristaps 95: goto mod;
1.26 schwarze 96: }
1.1 kristaps 97:
1.13 joerg 98: switch (tolower((unsigned char)p[(*pos)++])) {
1.32 schwarze 99: case 'b':
100: /* FALLTHROUGH */
101: case 'i':
102: /* FALLTHROUGH */
103: case 'r':
104: (*pos)--;
105: break;
106: case 'd':
107: cp->flags |= TBL_CELL_BALIGN;
1.1 kristaps 108: goto mod;
1.26 schwarze 109: case 'e':
1.1 kristaps 110: cp->flags |= TBL_CELL_EQUAL;
111: goto mod;
1.32 schwarze 112: case 'f':
113: break;
114: case 'm':
115: mandoc_msg(MANDOCERR_TBLLAYOUT_MOD, tbl->parse,
116: ln, *pos, "m");
117: goto mod;
118: case 'p':
119: /* FALLTHROUGH */
120: case 'v':
121: if (p[*pos] == '-' || p[*pos] == '+')
122: (*pos)++;
123: while (isdigit((unsigned char)p[*pos]))
124: (*pos)++;
125: goto mod;
1.26 schwarze 126: case 't':
1.1 kristaps 127: cp->flags |= TBL_CELL_TALIGN;
128: goto mod;
1.32 schwarze 129: case 'u':
130: cp->flags |= TBL_CELL_UP;
1.10 schwarze 131: goto mod;
1.26 schwarze 132: case 'w': /* XXX for now, ignore minimal column width */
1.29 schwarze 133: goto mod;
134: case 'x':
135: cp->flags |= TBL_CELL_WMAX;
1.1 kristaps 136: goto mod;
1.32 schwarze 137: case 'z':
138: cp->flags |= TBL_CELL_WIGN;
139: goto mod;
1.33 ! schwarze 140: case '|':
! 141: if (cp->vert < 2)
! 142: cp->vert++;
! 143: else
! 144: mandoc_msg(MANDOCERR_TBLLAYOUT_VERT,
! 145: tbl->parse, ln, *pos - 1, NULL);
! 146: goto mod;
1.1 kristaps 147: default:
1.32 schwarze 148: mandoc_vmsg(MANDOCERR_TBLLAYOUT_CHAR, tbl->parse,
149: ln, *pos - 1, "%c", p[*pos - 1]);
150: goto mod;
1.1 kristaps 151: }
152:
1.13 joerg 153: switch (tolower((unsigned char)p[(*pos)++])) {
1.26 schwarze 154: case '3':
1.21 kristaps 155: /* FALLTHROUGH */
1.26 schwarze 156: case 'b':
1.1 kristaps 157: cp->flags |= TBL_CELL_BOLD;
158: goto mod;
1.26 schwarze 159: case '2':
1.21 kristaps 160: /* FALLTHROUGH */
1.26 schwarze 161: case 'i':
1.1 kristaps 162: cp->flags |= TBL_CELL_ITALIC;
1.21 kristaps 163: goto mod;
1.26 schwarze 164: case '1':
1.21 kristaps 165: /* FALLTHROUGH */
1.26 schwarze 166: case 'r':
1.1 kristaps 167: goto mod;
168: default:
1.28 schwarze 169: mandoc_vmsg(MANDOCERR_FT_BAD, tbl->parse,
170: ln, *pos - 1, "TS f%c", p[*pos - 1]);
171: goto mod;
1.1 kristaps 172: }
173: }
174:
1.32 schwarze 175: static void
1.26 schwarze 176: cell(struct tbl_node *tbl, struct tbl_row *rp,
1.1 kristaps 177: int ln, const char *p, int *pos)
178: {
1.33 ! schwarze 179: int i;
1.1 kristaps 180: enum tbl_cellt c;
181:
1.33 ! schwarze 182: /* Handle leading vertical lines */
1.23 schwarze 183:
1.32 schwarze 184: while (p[*pos] == ' ' || p[*pos] == '\t' || p[*pos] == '|') {
185: if (p[*pos] == '|') {
1.33 ! schwarze 186: if (rp->vert < 2)
! 187: rp->vert++;
1.32 schwarze 188: else
189: mandoc_msg(MANDOCERR_TBLLAYOUT_VERT,
190: tbl->parse, ln, *pos, NULL);
191: }
1.23 schwarze 192: (*pos)++;
1.32 schwarze 193: }
1.25 schwarze 194:
1.33 ! schwarze 195: again:
! 196: while (p[*pos] == ' ' || p[*pos] == '\t')
! 197: (*pos)++;
1.25 schwarze 198:
1.33 ! schwarze 199: if (p[*pos] == '.' || p[*pos] == '\0')
1.32 schwarze 200: return;
1.23 schwarze 201:
202: /* Parse the column position (`c', `l', `r', ...). */
1.1 kristaps 203:
204: for (i = 0; i < KEYS_MAX; i++)
1.13 joerg 205: if (tolower((unsigned char)p[*pos]) == keys[i].name)
1.1 kristaps 206: break;
207:
1.32 schwarze 208: if (i == KEYS_MAX) {
209: mandoc_vmsg(MANDOCERR_TBLLAYOUT_CHAR, tbl->parse,
210: ln, *pos, "%c", p[*pos]);
211: (*pos)++;
212: goto again;
1.1 kristaps 213: }
1.11 kristaps 214: c = keys[i].key;
215:
1.32 schwarze 216: /* Special cases of spanners. */
1.16 kristaps 217:
1.32 schwarze 218: if (c == TBL_CELL_SPAN) {
219: if (rp->last == NULL)
220: mandoc_msg(MANDOCERR_TBLLAYOUT_SPAN,
221: tbl->parse, ln, *pos, NULL);
222: else if (rp->last->pos == TBL_CELL_HORIZ ||
223: rp->last->pos == TBL_CELL_DHORIZ)
224: c = rp->last->pos;
225: } else if (c == TBL_CELL_DOWN && rp == tbl->first_row)
226: mandoc_msg(MANDOCERR_TBLLAYOUT_DOWN,
227: tbl->parse, ln, *pos, NULL);
1.11 kristaps 228:
1.1 kristaps 229: (*pos)++;
230:
231: /* Allocate cell then parse its modifiers. */
232:
1.33 ! schwarze 233: mods(tbl, cell_alloc(tbl, rp, c), ln, p, pos);
1.1 kristaps 234: }
235:
1.31 schwarze 236: void
1.30 schwarze 237: tbl_layout(struct tbl_node *tbl, int ln, const char *p)
1.1 kristaps 238: {
239: struct tbl_row *rp;
1.30 schwarze 240: int pos;
1.1 kristaps 241:
1.30 schwarze 242: pos = 0;
243: rp = NULL;
1.1 kristaps 244:
1.30 schwarze 245: for (;;) {
246: /* Skip whitespace before and after each cell. */
1.1 kristaps 247:
1.32 schwarze 248: while (p[pos] == ' ' || p[pos] == '\t')
1.30 schwarze 249: pos++;
1.1 kristaps 250:
1.30 schwarze 251: switch (p[pos]) {
252: case ',': /* Next row on this input line. */
253: pos++;
254: rp = NULL;
255: continue;
256: case '\0': /* Next row on next input line. */
1.31 schwarze 257: return;
1.30 schwarze 258: case '.': /* End of layout. */
259: pos++;
260: tbl->part = TBL_PART_DATA;
1.33 ! schwarze 261:
! 262: /*
! 263: * When the layout is completely empty,
! 264: * default to one left-justified column.
! 265: */
! 266:
! 267: if (tbl->first_row == NULL) {
! 268: mandoc_msg(MANDOCERR_TBLLAYOUT_NONE,
! 269: tbl->parse, ln, pos, NULL);
! 270: rp = mandoc_calloc(1, sizeof(*rp));
! 271: cell_alloc(tbl, rp, TBL_CELL_LEFT);
! 272: tbl->first_row = tbl->last_row = rp;
1.31 schwarze 273: return;
1.33 ! schwarze 274: }
! 275:
! 276: /*
! 277: * Search for the widest line
! 278: * along the left and right margins.
! 279: */
! 280:
! 281: for (rp = tbl->first_row; rp; rp = rp->next) {
! 282: if (tbl->opts.lvert < rp->vert)
! 283: tbl->opts.lvert = rp->vert;
! 284: if (rp->last != NULL &&
! 285: rp->last->head == tbl->last_head &&
! 286: tbl->opts.rvert < rp->last->vert)
! 287: tbl->opts.rvert = rp->last->vert;
! 288: }
1.31 schwarze 289: return;
1.30 schwarze 290: default: /* Cell. */
291: break;
292: }
293:
294: if (rp == NULL) { /* First cell on this line. */
295: rp = mandoc_calloc(1, sizeof(*rp));
296: if (tbl->last_row)
297: tbl->last_row->next = rp;
298: else
299: tbl->first_row = rp;
300: tbl->last_row = rp;
301: }
1.32 schwarze 302: cell(tbl, rp, ln, p, &pos);
1.1 kristaps 303: }
304: }
1.5 kristaps 305:
306: static struct tbl_cell *
1.33 ! schwarze 307: cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos)
1.5 kristaps 308: {
309: struct tbl_cell *p, *pp;
310: struct tbl_head *h, *hp;
311:
312: p = mandoc_calloc(1, sizeof(struct tbl_cell));
313:
314: if (NULL != (pp = rp->last)) {
1.23 schwarze 315: pp->next = p;
316: h = pp->head->next;
317: } else {
318: rp->first = p;
319: h = tbl->first_head;
320: }
321: rp->last = p;
1.5 kristaps 322:
323: p->pos = pos;
324:
1.23 schwarze 325: /* Re-use header. */
1.5 kristaps 326:
327: if (h) {
1.23 schwarze 328: p->head = h;
329: return(p);
1.5 kristaps 330: }
331:
332: hp = mandoc_calloc(1, sizeof(struct tbl_head));
1.9 kristaps 333: hp->ident = tbl->opts.cols++;
1.5 kristaps 334:
335: if (tbl->last_head) {
336: hp->prev = tbl->last_head;
337: tbl->last_head->next = hp;
338: } else
1.23 schwarze 339: tbl->first_head = hp;
340: tbl->last_head = hp;
1.5 kristaps 341:
342: p->head = hp;
343: return(p);
344: }
CVSweb