Annotation of src/usr.bin/mandoc/tbl_layout.c, Revision 1.19
1.19 ! schwarze 1: /* $OpenBSD: tbl_layout.c,v 1.18 2015/01/14 22:44:51 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 *,
55: enum tbl_cellt, int vert);
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.19 ! schwarze 70: if (strchr(".,-=^_ACLNRSaclnrs|", p[*pos]) != NULL)
! 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.1 schwarze 138: default:
1.19 ! schwarze 139: mandoc_vmsg(MANDOCERR_TBLLAYOUT_CHAR, tbl->parse,
! 140: ln, *pos - 1, "%c", p[*pos - 1]);
! 141: goto mod;
1.1 schwarze 142: }
143:
1.6 schwarze 144: switch (tolower((unsigned char)p[(*pos)++])) {
1.14 schwarze 145: case '3':
1.10 schwarze 146: /* FALLTHROUGH */
1.14 schwarze 147: case 'b':
1.1 schwarze 148: cp->flags |= TBL_CELL_BOLD;
1.4 schwarze 149: goto mod;
1.14 schwarze 150: case '2':
1.10 schwarze 151: /* FALLTHROUGH */
1.14 schwarze 152: case 'i':
1.1 schwarze 153: cp->flags |= TBL_CELL_ITALIC;
1.10 schwarze 154: goto mod;
1.14 schwarze 155: case '1':
1.10 schwarze 156: /* FALLTHROUGH */
1.14 schwarze 157: case 'r':
1.4 schwarze 158: goto mod;
1.1 schwarze 159: default:
1.15 schwarze 160: mandoc_vmsg(MANDOCERR_FT_BAD, tbl->parse,
161: ln, *pos - 1, "TS f%c", p[*pos - 1]);
162: goto mod;
1.1 schwarze 163: }
164: }
165:
1.19 ! schwarze 166: static void
1.14 schwarze 167: cell(struct tbl_node *tbl, struct tbl_row *rp,
1.4 schwarze 168: int ln, const char *p, int *pos)
1.1 schwarze 169: {
1.11 schwarze 170: int vert, i;
1.1 schwarze 171: enum tbl_cellt c;
172:
1.11 schwarze 173: /* Handle vertical lines. */
174:
1.19 ! schwarze 175: vert = 0;
! 176: again:
! 177: while (p[*pos] == ' ' || p[*pos] == '\t' || p[*pos] == '|') {
! 178: if (p[*pos] == '|') {
! 179: if (vert < 2)
! 180: vert++;
! 181: else
! 182: mandoc_msg(MANDOCERR_TBLLAYOUT_VERT,
! 183: tbl->parse, ln, *pos, NULL);
! 184: }
1.11 schwarze 185: (*pos)++;
1.19 ! schwarze 186: }
1.13 schwarze 187:
188: /* Handle trailing vertical lines */
189:
190: if ('.' == p[*pos] || '\0' == p[*pos]) {
191: rp->vert = vert;
1.19 ! schwarze 192: return;
1.13 schwarze 193: }
1.11 schwarze 194:
195: /* Parse the column position (`c', `l', `r', ...). */
1.1 schwarze 196:
1.4 schwarze 197: for (i = 0; i < KEYS_MAX; i++)
1.6 schwarze 198: if (tolower((unsigned char)p[*pos]) == keys[i].name)
1.4 schwarze 199: break;
200:
1.19 ! schwarze 201: if (i == KEYS_MAX) {
! 202: mandoc_vmsg(MANDOCERR_TBLLAYOUT_CHAR, tbl->parse,
! 203: ln, *pos, "%c", p[*pos]);
! 204: (*pos)++;
! 205: goto again;
1.1 schwarze 206: }
1.6 schwarze 207: c = keys[i].key;
208:
1.19 ! schwarze 209: /* Special cases of spanners. */
1.7 schwarze 210:
1.19 ! schwarze 211: if (c == TBL_CELL_SPAN) {
! 212: if (rp->last == NULL)
! 213: mandoc_msg(MANDOCERR_TBLLAYOUT_SPAN,
! 214: tbl->parse, ln, *pos, NULL);
! 215: else if (rp->last->pos == TBL_CELL_HORIZ ||
! 216: rp->last->pos == TBL_CELL_DHORIZ)
! 217: c = rp->last->pos;
! 218: } else if (c == TBL_CELL_DOWN && rp == tbl->first_row)
! 219: mandoc_msg(MANDOCERR_TBLLAYOUT_DOWN,
! 220: tbl->parse, ln, *pos, NULL);
1.6 schwarze 221:
1.4 schwarze 222: (*pos)++;
1.1 schwarze 223:
224: /* Allocate cell then parse its modifiers. */
225:
1.19 ! schwarze 226: mods(tbl, cell_alloc(tbl, rp, c, vert), ln, p, pos);
1.1 schwarze 227: }
228:
1.18 schwarze 229: void
1.17 schwarze 230: tbl_layout(struct tbl_node *tbl, int ln, const char *p)
1.1 schwarze 231: {
232: struct tbl_row *rp;
1.17 schwarze 233: int pos;
1.1 schwarze 234:
1.17 schwarze 235: pos = 0;
236: rp = NULL;
1.4 schwarze 237:
1.17 schwarze 238: for (;;) {
239: /* Skip whitespace before and after each cell. */
1.4 schwarze 240:
1.19 ! schwarze 241: while (p[pos] == ' ' || p[pos] == '\t')
1.17 schwarze 242: pos++;
1.4 schwarze 243:
1.17 schwarze 244: switch (p[pos]) {
245: case ',': /* Next row on this input line. */
246: pos++;
247: rp = NULL;
248: continue;
249: case '\0': /* Next row on next input line. */
1.18 schwarze 250: return;
1.17 schwarze 251: case '.': /* End of layout. */
252: pos++;
253: tbl->part = TBL_PART_DATA;
254: if (tbl->first_row != NULL)
1.18 schwarze 255: return;
1.19 ! schwarze 256: mandoc_msg(MANDOCERR_TBLLAYOUT_NONE,
1.17 schwarze 257: tbl->parse, ln, pos, NULL);
258: rp = mandoc_calloc(1, sizeof(*rp));
259: cell_alloc(tbl, rp, TBL_CELL_LEFT, 0);
260: tbl->first_row = tbl->last_row = rp;
1.18 schwarze 261: return;
1.17 schwarze 262: default: /* Cell. */
263: break;
264: }
265:
266: if (rp == NULL) { /* First cell on this line. */
267: rp = mandoc_calloc(1, sizeof(*rp));
268: if (tbl->last_row)
269: tbl->last_row->next = rp;
270: else
271: tbl->first_row = rp;
272: tbl->last_row = rp;
273: }
1.19 ! schwarze 274: cell(tbl, rp, ln, p, &pos);
1.1 schwarze 275: }
276: }
1.4 schwarze 277:
278: static struct tbl_cell *
1.11 schwarze 279: cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos,
280: int vert)
1.4 schwarze 281: {
282: struct tbl_cell *p, *pp;
283: struct tbl_head *h, *hp;
284:
285: p = mandoc_calloc(1, sizeof(struct tbl_cell));
286:
287: if (NULL != (pp = rp->last)) {
1.11 schwarze 288: pp->next = p;
289: h = pp->head->next;
290: } else {
291: rp->first = p;
292: h = tbl->first_head;
293: }
294: rp->last = p;
1.4 schwarze 295:
296: p->pos = pos;
1.11 schwarze 297: p->vert = vert;
1.4 schwarze 298:
1.11 schwarze 299: /* Re-use header. */
1.4 schwarze 300:
301: if (h) {
1.11 schwarze 302: p->head = h;
303: return(p);
1.4 schwarze 304: }
305:
306: hp = mandoc_calloc(1, sizeof(struct tbl_head));
307: hp->ident = tbl->opts.cols++;
1.11 schwarze 308: hp->vert = vert;
1.4 schwarze 309:
310: if (tbl->last_head) {
311: hp->prev = tbl->last_head;
312: tbl->last_head->next = hp;
313: } else
1.11 schwarze 314: tbl->first_head = hp;
315: tbl->last_head = hp;
1.4 schwarze 316:
317: p->head = hp;
318: return(p);
319: }