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