Annotation of mandoc/tbl_data.c, Revision 1.58
1.58 ! schwarze 1: /* $Id: tbl_data.c,v 1.57 2021/09/07 11:48:19 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.57 schwarze 50: int startpos, endpos;
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:
1.57 schwarze 57: startpos = *pos;
58: ccp = p + startpos;
1.55 schwarze 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.56 schwarze 81: cp->font = ESCAPE_FONTROMAN;
1.53 schwarze 82: cp->spacing = SIZE_MAX;
1.43 schwarze 83: dp->layout->last->next = cp;
84: cp->col = dp->layout->last->col + 1;
85: dp->layout->last = cp;
86: } else {
1.51 schwarze 87: mandoc_msg(MANDOCERR_TBLDATA_EXTRA,
1.57 schwarze 88: ln, startpos, "%s", p + startpos);
1.47 schwarze 89: while (p[*pos] != '\0')
1.43 schwarze 90: (*pos)++;
91: return;
92: }
1.16 kristaps 93: }
94:
1.46 schwarze 95: dat = mandoc_malloc(sizeof(*dat));
1.3 kristaps 96: dat->layout = cp;
1.46 schwarze 97: dat->next = NULL;
98: dat->string = NULL;
99: dat->hspans = 0;
100: dat->vspans = 0;
101: dat->block = 0;
1.11 kristaps 102: dat->pos = TBL_DATA_NONE;
1.46 schwarze 103:
104: /*
105: * Increment the number of vertical spans in a data cell above,
106: * if this cell vertically extends one or more cells above.
107: * The iteration must be done over data rows,
108: * not over layout rows, because one layout row
109: * can be reused for more than one data row.
110: */
111:
1.47 schwarze 112: if (cp->pos == TBL_CELL_DOWN ||
1.57 schwarze 113: (*pos - startpos == 2 &&
114: p[startpos] == '\\' && p[startpos + 1] == '^')) {
1.46 schwarze 115: pdp = dp;
116: while ((pdp = pdp->prev) != NULL) {
117: pdat = pdp->first;
118: while (pdat != NULL &&
119: pdat->layout->col < dat->layout->col)
120: pdat = pdat->next;
121: if (pdat == NULL)
122: break;
1.47 schwarze 123: if (pdat->layout->pos != TBL_CELL_DOWN &&
124: strcmp(pdat->string, "\\^") != 0) {
1.46 schwarze 125: pdat->vspans++;
126: break;
127: }
128: }
129: }
130:
131: /*
132: * Count the number of horizontal spans to the right of this cell.
133: * This is purely a matter of the layout, independent of the data.
134: */
135:
1.37 schwarze 136: for (cp = cp->next; cp != NULL; cp = cp->next)
137: if (cp->pos == TBL_CELL_SPAN)
1.46 schwarze 138: dat->hspans++;
1.16 kristaps 139: else
140: break;
1.46 schwarze 141:
142: /* Append the new data cell to the data row. */
1.30 schwarze 143:
1.37 schwarze 144: if (dp->last == NULL)
145: dp->first = dat;
146: else
1.1 kristaps 147: dp->last->next = dat;
1.37 schwarze 148: dp->last = dat;
1.1 kristaps 149:
1.11 kristaps 150: /*
151: * Check for a continued-data scope opening. This consists of a
152: * trailing `T{' at the end of the line. Subsequent lines,
153: * until a standalone `T}', are included in our cell.
154: */
155:
1.57 schwarze 156: if (*pos - startpos == 2 &&
157: p[startpos] == 'T' && p[startpos + 1] == '{') {
1.11 kristaps 158: tbl->part = TBL_PART_CDATA;
1.33 schwarze 159: return;
1.11 kristaps 160: }
161:
1.57 schwarze 162: endpos = *pos;
163: if (dp->opts->opts & TBL_OPT_NOSPACE) {
164: while (p[startpos] == ' ')
165: startpos++;
166: while (endpos > startpos && p[endpos - 1] == ' ')
167: endpos--;
168: }
169: dat->string = mandoc_strndup(p + startpos, endpos - startpos);
1.1 kristaps 170:
1.47 schwarze 171: if (p[*pos] != '\0')
1.1 kristaps 172: (*pos)++;
173:
174: if ( ! strcmp(dat->string, "_"))
1.5 kristaps 175: dat->pos = TBL_DATA_HORIZ;
1.1 kristaps 176: else if ( ! strcmp(dat->string, "="))
1.5 kristaps 177: dat->pos = TBL_DATA_DHORIZ;
1.1 kristaps 178: else if ( ! strcmp(dat->string, "\\_"))
1.5 kristaps 179: dat->pos = TBL_DATA_NHORIZ;
1.1 kristaps 180: else if ( ! strcmp(dat->string, "\\="))
1.5 kristaps 181: dat->pos = TBL_DATA_NDHORIZ;
182: else
183: dat->pos = TBL_DATA_DATA;
1.11 kristaps 184:
1.37 schwarze 185: if ((dat->layout->pos == TBL_CELL_HORIZ ||
186: dat->layout->pos == TBL_CELL_DHORIZ ||
187: dat->layout->pos == TBL_CELL_DOWN) &&
188: dat->pos == TBL_DATA_DATA && *dat->string != '\0')
189: mandoc_msg(MANDOCERR_TBLDATA_SPAN,
1.57 schwarze 190: ln, startpos, "%s", dat->string);
1.11 kristaps 191: }
192:
1.45 schwarze 193: void
1.35 schwarze 194: tbl_cdata(struct tbl_node *tbl, int ln, const char *p, int pos)
1.11 kristaps 195: {
196: struct tbl_dat *dat;
1.30 schwarze 197: size_t sz;
1.11 kristaps 198:
199: dat = tbl->last_span->last;
1.14 kristaps 200:
201: if (p[pos] == 'T' && p[pos + 1] == '}') {
202: pos += 2;
1.58 ! schwarze 203: if (tbl->opts.opts & TBL_OPT_NOSPACE)
! 204: while (p[pos] == ' ')
! 205: pos++;
1.14 kristaps 206: if (p[pos] == tbl->opts.tab) {
207: tbl->part = TBL_PART_DATA;
208: pos++;
1.40 schwarze 209: while (p[pos] != '\0')
210: getdata(tbl, tbl->last_span, ln, p, &pos);
1.45 schwarze 211: return;
1.37 schwarze 212: } else if (p[pos] == '\0') {
1.14 kristaps 213: tbl->part = TBL_PART_DATA;
1.45 schwarze 214: return;
1.14 kristaps 215: }
216:
217: /* Fallthrough: T} is part of a word. */
218: }
1.17 kristaps 219:
220: dat->pos = TBL_DATA_DATA;
1.42 schwarze 221: dat->block = 1;
1.11 kristaps 222:
1.37 schwarze 223: if (dat->string != NULL) {
1.36 schwarze 224: sz = strlen(p + pos) + strlen(dat->string) + 2;
1.11 kristaps 225: dat->string = mandoc_realloc(dat->string, sz);
1.31 schwarze 226: (void)strlcat(dat->string, " ", sz);
1.36 schwarze 227: (void)strlcat(dat->string, p + pos, sz);
1.11 kristaps 228: } else
1.36 schwarze 229: dat->string = mandoc_strdup(p + pos);
1.19 kristaps 230:
1.37 schwarze 231: if (dat->layout->pos == TBL_CELL_DOWN)
1.51 schwarze 232: mandoc_msg(MANDOCERR_TBLDATA_SPAN,
233: ln, pos, "%s", dat->string);
1.1 kristaps 234: }
235:
1.20 schwarze 236: static struct tbl_span *
1.22 kristaps 237: newspan(struct tbl_node *tbl, int line, struct tbl_row *rp)
1.20 schwarze 238: {
239: struct tbl_span *dp;
240:
1.37 schwarze 241: dp = mandoc_calloc(1, sizeof(*dp));
1.22 kristaps 242: dp->line = line;
1.26 schwarze 243: dp->opts = &tbl->opts;
1.20 schwarze 244: dp->layout = rp;
1.34 schwarze 245: dp->prev = tbl->last_span;
1.20 schwarze 246:
1.34 schwarze 247: if (dp->prev == NULL) {
248: tbl->first_span = dp;
1.21 schwarze 249: tbl->current_span = NULL;
1.34 schwarze 250: } else
251: dp->prev->next = dp;
252: tbl->last_span = dp;
1.20 schwarze 253:
1.41 schwarze 254: return dp;
1.20 schwarze 255: }
256:
1.33 schwarze 257: void
1.35 schwarze 258: tbl_data(struct tbl_node *tbl, int ln, const char *p, int pos)
1.1 kristaps 259: {
1.3 kristaps 260: struct tbl_row *rp;
1.43 schwarze 261: struct tbl_cell *cp;
1.44 schwarze 262: struct tbl_span *sp;
1.1 kristaps 263:
1.54 schwarze 264: for (sp = tbl->last_span; sp != NULL; sp = sp->prev)
265: if (sp->pos == TBL_SPAN_DATA)
266: break;
267: rp = sp == NULL ? tbl->first_row :
268: sp->layout->next == NULL ? sp->layout : sp->layout->next;
1.43 schwarze 269: assert(rp != NULL);
1.3 kristaps 270:
1.52 schwarze 271: if (p[1] == '\0') {
272: switch (p[0]) {
273: case '.':
274: /*
275: * Empty request lines must be handled here
276: * and cannot be discarded in roff_parseln()
277: * because in the layout section, they
278: * are significant and end the layout.
279: */
280: return;
281: case '_':
282: sp = newspan(tbl, ln, rp);
283: sp->pos = TBL_SPAN_HORIZ;
284: return;
285: case '=':
286: sp = newspan(tbl, ln, rp);
287: sp->pos = TBL_SPAN_DHORIZ;
288: return;
289: default:
290: break;
291: }
1.1 kristaps 292: }
1.43 schwarze 293:
294: /*
1.44 schwarze 295: * If the layout row contains nothing but horizontal lines,
296: * allocate an empty span for it and assign the current span
297: * to the next layout row accepting data.
1.43 schwarze 298: */
299:
1.44 schwarze 300: while (rp->next != NULL) {
301: if (rp->last->col + 1 < tbl->opts.cols)
302: break;
303: for (cp = rp->first; cp != NULL; cp = cp->next)
304: if (cp->pos != TBL_CELL_HORIZ &&
305: cp->pos != TBL_CELL_DHORIZ)
306: break;
307: if (cp != NULL)
308: break;
309: sp = newspan(tbl, ln, rp);
310: sp->pos = TBL_SPAN_DATA;
311: rp = rp->next;
1.43 schwarze 312: }
1.5 kristaps 313:
1.44 schwarze 314: /* Process a real data row. */
1.1 kristaps 315:
1.44 schwarze 316: sp = newspan(tbl, ln, rp);
317: sp->pos = TBL_SPAN_DATA;
318: while (p[pos] != '\0')
319: getdata(tbl, sp, ln, p, &pos);
1.1 kristaps 320: }
CVSweb