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