Annotation of src/usr.bin/mandoc/tbl_layout.c, Revision 1.24
1.24 ! schwarze 1: /* $OpenBSD: tbl_layout.c,v 1.23 2015/01/30 02:08:37 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.21 schwarze 235: tbl_layout(struct tbl_node *tbl, int ln, const char *p, int pos)
1.1 schwarze 236: {
237: struct tbl_row *rp;
238:
1.17 schwarze 239: rp = NULL;
240: for (;;) {
241: /* Skip whitespace before and after each cell. */
1.4 schwarze 242:
1.19 schwarze 243: while (p[pos] == ' ' || p[pos] == '\t')
1.17 schwarze 244: pos++;
1.4 schwarze 245:
1.17 schwarze 246: switch (p[pos]) {
247: case ',': /* Next row on this input line. */
248: pos++;
249: rp = NULL;
250: continue;
251: case '\0': /* Next row on next input line. */
1.18 schwarze 252: return;
1.17 schwarze 253: case '.': /* End of layout. */
254: pos++;
255: tbl->part = TBL_PART_DATA;
1.20 schwarze 256:
257: /*
258: * When the layout is completely empty,
259: * default to one left-justified column.
260: */
261:
262: if (tbl->first_row == NULL) {
1.22 schwarze 263: tbl->first_row = tbl->last_row =
264: mandoc_calloc(1, sizeof(*rp));
265: }
266: if (tbl->first_row->first == NULL) {
1.20 schwarze 267: mandoc_msg(MANDOCERR_TBLLAYOUT_NONE,
268: tbl->parse, ln, pos, NULL);
1.22 schwarze 269: cell_alloc(tbl, tbl->first_row,
270: TBL_CELL_LEFT);
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 &&
1.24 ! schwarze 283: rp->last->col + 1 == tbl->opts.cols &&
1.20 schwarze 284: tbl->opts.rvert < rp->last->vert)
285: tbl->opts.rvert = rp->last->vert;
1.22 schwarze 286:
287: /* If the last line is empty, drop it. */
288:
289: if (rp->next != NULL &&
290: rp->next->first == NULL) {
291: free(rp->next);
292: rp->next = NULL;
293: }
1.20 schwarze 294: }
1.18 schwarze 295: return;
1.17 schwarze 296: default: /* Cell. */
297: break;
298: }
299:
1.22 schwarze 300: /*
301: * If the last line had at least one cell,
302: * start a new one; otherwise, continue it.
303: */
304:
305: if (rp == NULL) {
306: if (tbl->last_row == NULL ||
307: tbl->last_row->first != NULL) {
308: rp = mandoc_calloc(1, sizeof(*rp));
309: if (tbl->last_row)
310: tbl->last_row->next = rp;
311: else
312: tbl->first_row = rp;
313: tbl->last_row = rp;
314: } else
315: rp = tbl->last_row;
1.17 schwarze 316: }
1.19 schwarze 317: cell(tbl, rp, ln, p, &pos);
1.1 schwarze 318: }
319: }
1.4 schwarze 320:
321: static struct tbl_cell *
1.20 schwarze 322: cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos)
1.4 schwarze 323: {
324: struct tbl_cell *p, *pp;
325:
1.23 schwarze 326: p = mandoc_calloc(1, sizeof(*p));
1.24 ! schwarze 327: p->pos = pos;
1.4 schwarze 328:
1.23 schwarze 329: if ((pp = rp->last) != NULL) {
1.11 schwarze 330: pp->next = p;
1.24 ! schwarze 331: p->col = pp->col + 1;
! 332: } else
1.11 schwarze 333: rp->first = p;
334: rp->last = p;
1.4 schwarze 335:
1.24 ! schwarze 336: if (tbl->opts.cols <= p->col)
! 337: tbl->opts.cols = p->col + 1;
1.4 schwarze 338:
339: return(p);
340: }