Annotation of mandoc/tbl_data.c, Revision 1.45
1.45 ! schwarze 1: /* $Id: tbl_data.c,v 1.44 2017/07/04 21:08:29 schwarze Exp $ */
1.1 kristaps 2: /*
1.20 schwarze 3: * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.42 schwarze 4: * Copyright (c) 2011, 2015, 2017 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>
24: #include <stdlib.h>
25: #include <string.h>
1.6 kristaps 26: #include <time.h>
1.1 kristaps 27:
28: #include "mandoc.h"
1.29 schwarze 29: #include "mandoc_aux.h"
1.1 kristaps 30: #include "libmandoc.h"
31: #include "libroff.h"
32:
1.33 schwarze 33: static void getdata(struct tbl_node *, struct tbl_span *,
1.22 kristaps 34: int, const char *, int *);
1.30 schwarze 35: static struct tbl_span *newspan(struct tbl_node *, int,
1.22 kristaps 36: struct tbl_row *);
1.1 kristaps 37:
1.30 schwarze 38:
1.33 schwarze 39: static void
1.30 schwarze 40: getdata(struct tbl_node *tbl, struct tbl_span *dp,
1.1 kristaps 41: int ln, const char *p, int *pos)
42: {
43: struct tbl_dat *dat;
1.3 kristaps 44: struct tbl_cell *cp;
1.37 schwarze 45: int sv;
1.1 kristaps 46:
1.38 schwarze 47: /* Advance to the next layout cell, skipping spanners. */
48:
1.37 schwarze 49: cp = dp->last == NULL ? dp->layout->first : dp->last->layout->next;
50: while (cp != NULL && cp->pos == TBL_CELL_SPAN)
1.3 kristaps 51: cp = cp->next;
52:
1.16 kristaps 53: /*
1.43 schwarze 54: * If the current layout row is out of cells, allocate
55: * a new cell if another row of the table has at least
56: * this number of columns, or discard the input if we
57: * are beyond the last column of the table as a whole.
1.16 kristaps 58: */
59:
1.37 schwarze 60: if (cp == NULL) {
1.43 schwarze 61: if (dp->layout->last->col + 1 < dp->opts->cols) {
62: cp = mandoc_calloc(1, sizeof(*cp));
63: cp->pos = TBL_CELL_LEFT;
64: dp->layout->last->next = cp;
65: cp->col = dp->layout->last->col + 1;
66: dp->layout->last = cp;
67: } else {
68: mandoc_msg(MANDOCERR_TBLDATA_EXTRA, tbl->parse,
69: ln, *pos, p + *pos);
70: while (p[*pos])
71: (*pos)++;
72: return;
73: }
1.16 kristaps 74: }
75:
1.37 schwarze 76: dat = mandoc_calloc(1, sizeof(*dat));
1.3 kristaps 77: dat->layout = cp;
1.11 kristaps 78: dat->pos = TBL_DATA_NONE;
1.37 schwarze 79: dat->spans = 0;
80: for (cp = cp->next; cp != NULL; cp = cp->next)
81: if (cp->pos == TBL_CELL_SPAN)
82: dat->spans++;
1.16 kristaps 83: else
84: break;
1.30 schwarze 85:
1.37 schwarze 86: if (dp->last == NULL)
87: dp->first = dat;
88: else
1.1 kristaps 89: dp->last->next = dat;
1.37 schwarze 90: dp->last = dat;
1.1 kristaps 91:
92: sv = *pos;
1.8 kristaps 93: while (p[*pos] && p[*pos] != tbl->opts.tab)
1.1 kristaps 94: (*pos)++;
95:
1.11 kristaps 96: /*
97: * Check for a continued-data scope opening. This consists of a
98: * trailing `T{' at the end of the line. Subsequent lines,
99: * until a standalone `T}', are included in our cell.
100: */
101:
1.37 schwarze 102: if (*pos - sv == 2 && p[sv] == 'T' && p[sv + 1] == '{') {
1.11 kristaps 103: tbl->part = TBL_PART_CDATA;
1.33 schwarze 104: return;
1.11 kristaps 105: }
106:
1.37 schwarze 107: dat->string = mandoc_strndup(p + sv, *pos - sv);
1.1 kristaps 108:
109: if (p[*pos])
110: (*pos)++;
111:
112: if ( ! strcmp(dat->string, "_"))
1.5 kristaps 113: dat->pos = TBL_DATA_HORIZ;
1.1 kristaps 114: else if ( ! strcmp(dat->string, "="))
1.5 kristaps 115: dat->pos = TBL_DATA_DHORIZ;
1.1 kristaps 116: else if ( ! strcmp(dat->string, "\\_"))
1.5 kristaps 117: dat->pos = TBL_DATA_NHORIZ;
1.1 kristaps 118: else if ( ! strcmp(dat->string, "\\="))
1.5 kristaps 119: dat->pos = TBL_DATA_NDHORIZ;
120: else
121: dat->pos = TBL_DATA_DATA;
1.11 kristaps 122:
1.37 schwarze 123: if ((dat->layout->pos == TBL_CELL_HORIZ ||
124: dat->layout->pos == TBL_CELL_DHORIZ ||
125: dat->layout->pos == TBL_CELL_DOWN) &&
126: dat->pos == TBL_DATA_DATA && *dat->string != '\0')
127: mandoc_msg(MANDOCERR_TBLDATA_SPAN,
128: tbl->parse, ln, sv, dat->string);
1.11 kristaps 129: }
130:
1.45 ! schwarze 131: void
1.35 schwarze 132: tbl_cdata(struct tbl_node *tbl, int ln, const char *p, int pos)
1.11 kristaps 133: {
134: struct tbl_dat *dat;
1.30 schwarze 135: size_t sz;
1.11 kristaps 136:
137: dat = tbl->last_span->last;
1.14 kristaps 138:
139: if (p[pos] == 'T' && p[pos + 1] == '}') {
140: pos += 2;
141: if (p[pos] == tbl->opts.tab) {
142: tbl->part = TBL_PART_DATA;
143: pos++;
1.40 schwarze 144: while (p[pos] != '\0')
145: getdata(tbl, tbl->last_span, ln, p, &pos);
1.45 ! schwarze 146: return;
1.37 schwarze 147: } else if (p[pos] == '\0') {
1.14 kristaps 148: tbl->part = TBL_PART_DATA;
1.45 ! schwarze 149: return;
1.14 kristaps 150: }
151:
152: /* Fallthrough: T} is part of a word. */
153: }
1.17 kristaps 154:
155: dat->pos = TBL_DATA_DATA;
1.42 schwarze 156: dat->block = 1;
1.11 kristaps 157:
1.37 schwarze 158: if (dat->string != NULL) {
1.36 schwarze 159: sz = strlen(p + pos) + strlen(dat->string) + 2;
1.11 kristaps 160: dat->string = mandoc_realloc(dat->string, sz);
1.31 schwarze 161: (void)strlcat(dat->string, " ", sz);
1.36 schwarze 162: (void)strlcat(dat->string, p + pos, sz);
1.11 kristaps 163: } else
1.36 schwarze 164: dat->string = mandoc_strdup(p + pos);
1.19 kristaps 165:
1.37 schwarze 166: if (dat->layout->pos == TBL_CELL_DOWN)
1.36 schwarze 167: mandoc_msg(MANDOCERR_TBLDATA_SPAN, tbl->parse,
168: ln, pos, dat->string);
1.1 kristaps 169: }
170:
1.20 schwarze 171: static struct tbl_span *
1.22 kristaps 172: newspan(struct tbl_node *tbl, int line, struct tbl_row *rp)
1.20 schwarze 173: {
174: struct tbl_span *dp;
175:
1.37 schwarze 176: dp = mandoc_calloc(1, sizeof(*dp));
1.22 kristaps 177: dp->line = line;
1.26 schwarze 178: dp->opts = &tbl->opts;
1.20 schwarze 179: dp->layout = rp;
1.34 schwarze 180: dp->prev = tbl->last_span;
1.20 schwarze 181:
1.34 schwarze 182: if (dp->prev == NULL) {
183: tbl->first_span = dp;
1.21 schwarze 184: tbl->current_span = NULL;
1.34 schwarze 185: } else
186: dp->prev->next = dp;
187: tbl->last_span = dp;
1.20 schwarze 188:
1.41 schwarze 189: return dp;
1.20 schwarze 190: }
191:
1.33 schwarze 192: void
1.35 schwarze 193: tbl_data(struct tbl_node *tbl, int ln, const char *p, int pos)
1.1 kristaps 194: {
1.3 kristaps 195: struct tbl_row *rp;
1.43 schwarze 196: struct tbl_cell *cp;
1.44 schwarze 197: struct tbl_span *sp;
1.1 kristaps 198:
1.43 schwarze 199: rp = (sp = tbl->last_span) == NULL ? tbl->first_row :
200: sp->pos == TBL_SPAN_DATA && sp->layout->next != NULL ?
201: sp->layout->next : sp->layout;
1.18 kristaps 202:
1.43 schwarze 203: assert(rp != NULL);
1.3 kristaps 204:
1.1 kristaps 205: if ( ! strcmp(p, "_")) {
1.44 schwarze 206: sp = newspan(tbl, ln, rp);
1.43 schwarze 207: sp->pos = TBL_SPAN_HORIZ;
1.33 schwarze 208: return;
1.1 kristaps 209: } else if ( ! strcmp(p, "=")) {
1.44 schwarze 210: sp = newspan(tbl, ln, rp);
1.43 schwarze 211: sp->pos = TBL_SPAN_DHORIZ;
1.33 schwarze 212: return;
1.1 kristaps 213: }
1.43 schwarze 214:
215: /*
1.44 schwarze 216: * If the layout row contains nothing but horizontal lines,
217: * allocate an empty span for it and assign the current span
218: * to the next layout row accepting data.
1.43 schwarze 219: */
220:
1.44 schwarze 221: while (rp->next != NULL) {
222: if (rp->last->col + 1 < tbl->opts.cols)
223: break;
224: for (cp = rp->first; cp != NULL; cp = cp->next)
225: if (cp->pos != TBL_CELL_HORIZ &&
226: cp->pos != TBL_CELL_DHORIZ)
227: break;
228: if (cp != NULL)
229: break;
230: sp = newspan(tbl, ln, rp);
231: sp->pos = TBL_SPAN_DATA;
232: rp = rp->next;
1.43 schwarze 233: }
1.5 kristaps 234:
1.44 schwarze 235: /* Process a real data row. */
1.1 kristaps 236:
1.44 schwarze 237: sp = newspan(tbl, ln, rp);
238: sp->pos = TBL_SPAN_DATA;
239: while (p[pos] != '\0')
240: getdata(tbl, sp, ln, p, &pos);
1.1 kristaps 241: }
CVSweb