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