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