Annotation of src/usr.bin/mandoc/tbl_data.c, Revision 1.35
1.35 ! schwarze 1: /* $OpenBSD: tbl_data.c,v 1.34 2018/11/25 21:17:30 schwarze Exp $ */
1.1 schwarze 2: /*
1.4 schwarze 3: * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.33 schwarze 4: * Copyright (c) 2011, 2015, 2017, 2018 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.19 schwarze 18: #include <sys/types.h>
19:
1.1 schwarze 20: #include <assert.h>
21: #include <ctype.h>
22: #include <stdlib.h>
23: #include <string.h>
1.4 schwarze 24: #include <time.h>
1.1 schwarze 25:
1.35 ! schwarze 26: #include "mandoc_aux.h"
1.4 schwarze 27: #include "mandoc.h"
1.35 ! schwarze 28: #include "tbl.h"
1.4 schwarze 29: #include "libmandoc.h"
30: #include "libroff.h"
31:
1.20 schwarze 32: static void getdata(struct tbl_node *, struct tbl_span *,
1.9 schwarze 33: int, const char *, int *);
1.17 schwarze 34: static struct tbl_span *newspan(struct tbl_node *, int,
1.9 schwarze 35: struct tbl_row *);
1.4 schwarze 36:
1.17 schwarze 37:
1.20 schwarze 38: static void
1.17 schwarze 39: getdata(struct tbl_node *tbl, struct tbl_span *dp,
1.4 schwarze 40: int ln, const char *p, int *pos)
1.1 schwarze 41: {
1.33 schwarze 42: struct tbl_dat *dat, *pdat;
1.4 schwarze 43: struct tbl_cell *cp;
1.33 schwarze 44: struct tbl_span *pdp;
1.24 schwarze 45: int sv;
1.4 schwarze 46:
1.34 schwarze 47: /*
48: * Determine the length of the string in the cell
49: * and advance the parse point to the end of the cell.
50: */
51:
52: sv = *pos;
53: while (p[*pos] != '\0' && p[*pos] != tbl->opts.tab)
54: (*pos)++;
55:
1.25 schwarze 56: /* Advance to the next layout cell, skipping spanners. */
57:
1.24 schwarze 58: cp = dp->last == NULL ? dp->layout->first : dp->last->layout->next;
59: while (cp != NULL && cp->pos == TBL_CELL_SPAN)
1.4 schwarze 60: cp = cp->next;
61:
1.6 schwarze 62: /*
1.30 schwarze 63: * If the current layout row is out of cells, allocate
64: * a new cell if another row of the table has at least
65: * this number of columns, or discard the input if we
66: * are beyond the last column of the table as a whole.
1.6 schwarze 67: */
68:
1.24 schwarze 69: if (cp == NULL) {
1.30 schwarze 70: if (dp->layout->last->col + 1 < dp->opts->cols) {
71: cp = mandoc_calloc(1, sizeof(*cp));
72: cp->pos = TBL_CELL_LEFT;
73: dp->layout->last->next = cp;
74: cp->col = dp->layout->last->col + 1;
75: dp->layout->last = cp;
76: } else {
77: mandoc_msg(MANDOCERR_TBLDATA_EXTRA, tbl->parse,
1.34 schwarze 78: ln, sv, p + sv);
79: while (p[*pos] != '\0')
1.30 schwarze 80: (*pos)++;
81: return;
82: }
1.6 schwarze 83: }
84:
1.33 schwarze 85: dat = mandoc_malloc(sizeof(*dat));
1.4 schwarze 86: dat->layout = cp;
1.33 schwarze 87: dat->next = NULL;
88: dat->string = NULL;
89: dat->hspans = 0;
90: dat->vspans = 0;
91: dat->block = 0;
1.4 schwarze 92: dat->pos = TBL_DATA_NONE;
1.33 schwarze 93:
94: /*
95: * Increment the number of vertical spans in a data cell above,
96: * if this cell vertically extends one or more cells above.
97: * The iteration must be done over data rows,
98: * not over layout rows, because one layout row
99: * can be reused for more than one data row.
100: */
101:
1.34 schwarze 102: if (cp->pos == TBL_CELL_DOWN ||
103: (*pos - sv == 2 && p[sv] == '\\' && p[sv + 1] == '^')) {
1.33 schwarze 104: pdp = dp;
105: while ((pdp = pdp->prev) != NULL) {
106: pdat = pdp->first;
107: while (pdat != NULL &&
108: pdat->layout->col < dat->layout->col)
109: pdat = pdat->next;
110: if (pdat == NULL)
111: break;
1.34 schwarze 112: if (pdat->layout->pos != TBL_CELL_DOWN &&
113: strcmp(pdat->string, "\\^") != 0) {
1.33 schwarze 114: pdat->vspans++;
115: break;
116: }
117: }
118: }
119:
120: /*
121: * Count the number of horizontal spans to the right of this cell.
122: * This is purely a matter of the layout, independent of the data.
123: */
124:
1.24 schwarze 125: for (cp = cp->next; cp != NULL; cp = cp->next)
126: if (cp->pos == TBL_CELL_SPAN)
1.33 schwarze 127: dat->hspans++;
1.6 schwarze 128: else
129: break;
1.33 schwarze 130:
131: /* Append the new data cell to the data row. */
1.17 schwarze 132:
1.24 schwarze 133: if (dp->last == NULL)
134: dp->first = dat;
135: else
1.4 schwarze 136: dp->last->next = dat;
1.24 schwarze 137: dp->last = dat;
1.4 schwarze 138:
139: /*
140: * Check for a continued-data scope opening. This consists of a
141: * trailing `T{' at the end of the line. Subsequent lines,
142: * until a standalone `T}', are included in our cell.
143: */
1.1 schwarze 144:
1.24 schwarze 145: if (*pos - sv == 2 && p[sv] == 'T' && p[sv + 1] == '{') {
1.4 schwarze 146: tbl->part = TBL_PART_CDATA;
1.20 schwarze 147: return;
1.4 schwarze 148: }
1.1 schwarze 149:
1.24 schwarze 150: dat->string = mandoc_strndup(p + sv, *pos - sv);
1.1 schwarze 151:
1.34 schwarze 152: if (p[*pos] != '\0')
1.4 schwarze 153: (*pos)++;
1.1 schwarze 154:
155: if ( ! strcmp(dat->string, "_"))
1.4 schwarze 156: dat->pos = TBL_DATA_HORIZ;
1.1 schwarze 157: else if ( ! strcmp(dat->string, "="))
1.4 schwarze 158: dat->pos = TBL_DATA_DHORIZ;
1.1 schwarze 159: else if ( ! strcmp(dat->string, "\\_"))
1.4 schwarze 160: dat->pos = TBL_DATA_NHORIZ;
1.1 schwarze 161: else if ( ! strcmp(dat->string, "\\="))
1.4 schwarze 162: dat->pos = TBL_DATA_NDHORIZ;
1.1 schwarze 163: else
1.4 schwarze 164: dat->pos = TBL_DATA_DATA;
165:
1.24 schwarze 166: if ((dat->layout->pos == TBL_CELL_HORIZ ||
167: dat->layout->pos == TBL_CELL_DHORIZ ||
168: dat->layout->pos == TBL_CELL_DOWN) &&
169: dat->pos == TBL_DATA_DATA && *dat->string != '\0')
170: mandoc_msg(MANDOCERR_TBLDATA_SPAN,
171: tbl->parse, ln, sv, dat->string);
1.1 schwarze 172: }
173:
1.32 schwarze 174: void
1.22 schwarze 175: tbl_cdata(struct tbl_node *tbl, int ln, const char *p, int pos)
1.4 schwarze 176: {
177: struct tbl_dat *dat;
1.17 schwarze 178: size_t sz;
1.4 schwarze 179:
180: dat = tbl->last_span->last;
1.5 schwarze 181:
182: if (p[pos] == 'T' && p[pos + 1] == '}') {
183: pos += 2;
184: if (p[pos] == tbl->opts.tab) {
185: tbl->part = TBL_PART_DATA;
186: pos++;
1.27 schwarze 187: while (p[pos] != '\0')
188: getdata(tbl, tbl->last_span, ln, p, &pos);
1.32 schwarze 189: return;
1.24 schwarze 190: } else if (p[pos] == '\0') {
1.5 schwarze 191: tbl->part = TBL_PART_DATA;
1.32 schwarze 192: return;
1.5 schwarze 193: }
194:
195: /* Fallthrough: T} is part of a word. */
196: }
1.4 schwarze 197:
1.6 schwarze 198: dat->pos = TBL_DATA_DATA;
1.29 schwarze 199: dat->block = 1;
1.6 schwarze 200:
1.24 schwarze 201: if (dat->string != NULL) {
1.23 schwarze 202: sz = strlen(p + pos) + strlen(dat->string) + 2;
1.4 schwarze 203: dat->string = mandoc_realloc(dat->string, sz);
1.18 schwarze 204: (void)strlcat(dat->string, " ", sz);
1.23 schwarze 205: (void)strlcat(dat->string, p + pos, sz);
1.4 schwarze 206: } else
1.23 schwarze 207: dat->string = mandoc_strdup(p + pos);
1.4 schwarze 208:
1.24 schwarze 209: if (dat->layout->pos == TBL_CELL_DOWN)
1.23 schwarze 210: mandoc_msg(MANDOCERR_TBLDATA_SPAN, tbl->parse,
211: ln, pos, dat->string);
1.4 schwarze 212: }
1.1 schwarze 213:
1.7 schwarze 214: static struct tbl_span *
1.9 schwarze 215: newspan(struct tbl_node *tbl, int line, struct tbl_row *rp)
1.7 schwarze 216: {
217: struct tbl_span *dp;
218:
1.24 schwarze 219: dp = mandoc_calloc(1, sizeof(*dp));
1.9 schwarze 220: dp->line = line;
1.13 schwarze 221: dp->opts = &tbl->opts;
1.7 schwarze 222: dp->layout = rp;
1.21 schwarze 223: dp->prev = tbl->last_span;
1.7 schwarze 224:
1.21 schwarze 225: if (dp->prev == NULL) {
226: tbl->first_span = dp;
1.8 schwarze 227: tbl->current_span = NULL;
1.21 schwarze 228: } else
229: dp->prev->next = dp;
230: tbl->last_span = dp;
1.7 schwarze 231:
1.28 schwarze 232: return dp;
1.7 schwarze 233: }
234:
1.20 schwarze 235: void
1.22 schwarze 236: tbl_data(struct tbl_node *tbl, int ln, const char *p, int pos)
1.1 schwarze 237: {
1.4 schwarze 238: struct tbl_row *rp;
1.30 schwarze 239: struct tbl_cell *cp;
1.31 schwarze 240: struct tbl_span *sp;
1.4 schwarze 241:
1.30 schwarze 242: rp = (sp = tbl->last_span) == NULL ? tbl->first_row :
243: sp->pos == TBL_SPAN_DATA && sp->layout->next != NULL ?
244: sp->layout->next : sp->layout;
1.6 schwarze 245:
1.30 schwarze 246: assert(rp != NULL);
1.4 schwarze 247:
1.1 schwarze 248: if ( ! strcmp(p, "_")) {
1.31 schwarze 249: sp = newspan(tbl, ln, rp);
1.30 schwarze 250: sp->pos = TBL_SPAN_HORIZ;
1.20 schwarze 251: return;
1.1 schwarze 252: } else if ( ! strcmp(p, "=")) {
1.31 schwarze 253: sp = newspan(tbl, ln, rp);
1.30 schwarze 254: sp->pos = TBL_SPAN_DHORIZ;
1.20 schwarze 255: return;
1.1 schwarze 256: }
1.30 schwarze 257:
258: /*
1.31 schwarze 259: * If the layout row contains nothing but horizontal lines,
260: * allocate an empty span for it and assign the current span
261: * to the next layout row accepting data.
1.30 schwarze 262: */
263:
1.31 schwarze 264: while (rp->next != NULL) {
265: if (rp->last->col + 1 < tbl->opts.cols)
266: break;
267: for (cp = rp->first; cp != NULL; cp = cp->next)
268: if (cp->pos != TBL_CELL_HORIZ &&
269: cp->pos != TBL_CELL_DHORIZ)
270: break;
271: if (cp != NULL)
272: break;
273: sp = newspan(tbl, ln, rp);
274: sp->pos = TBL_SPAN_DATA;
275: rp = rp->next;
1.30 schwarze 276: }
1.1 schwarze 277:
1.31 schwarze 278: /* Process a real data row. */
1.1 schwarze 279:
1.31 schwarze 280: sp = newspan(tbl, ln, rp);
281: sp->pos = TBL_SPAN_DATA;
282: while (p[pos] != '\0')
283: getdata(tbl, sp, ln, p, &pos);
1.1 schwarze 284: }