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