Annotation of src/usr.bin/mandoc/tbl_data.c, Revision 1.14
1.14 ! schwarze 1: /* $Id: tbl_data.c,v 1.13 2013/05/31 21:37:09 schwarze Exp $ */
1.1 schwarze 2: /*
1.4 schwarze 3: * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.7 schwarze 4: * Copyright (c) 2011 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: */
18: #include <assert.h>
19: #include <ctype.h>
20: #include <stdlib.h>
21: #include <string.h>
1.4 schwarze 22: #include <time.h>
1.1 schwarze 23:
1.4 schwarze 24: #include "mandoc.h"
25: #include "libmandoc.h"
26: #include "libroff.h"
27:
1.9 schwarze 28: static int data(struct tbl_node *, struct tbl_span *,
29: int, const char *, int *);
30: static struct tbl_span *newspan(struct tbl_node *, int,
31: struct tbl_row *);
1.4 schwarze 32:
33: static int
34: data(struct tbl_node *tbl, struct tbl_span *dp,
35: int ln, const char *p, int *pos)
1.1 schwarze 36: {
1.4 schwarze 37: struct tbl_dat *dat;
38: struct tbl_cell *cp;
1.6 schwarze 39: int sv, spans;
1.4 schwarze 40:
41: cp = NULL;
42: if (dp->last && dp->last->layout)
43: cp = dp->last->layout->next;
44: else if (NULL == dp->last)
45: cp = dp->layout->first;
46:
1.5 schwarze 47: /*
1.12 schwarze 48: * Skip over spanners, since
1.5 schwarze 49: * we want to match data with data layout cells in the header.
50: */
1.4 schwarze 51:
1.12 schwarze 52: while (cp && TBL_CELL_SPAN == cp->pos)
1.4 schwarze 53: cp = cp->next;
54:
1.6 schwarze 55: /*
56: * Stop processing when we reach the end of the available layout
57: * cells. This means that we have extra input.
58: */
59:
60: if (NULL == cp) {
1.11 schwarze 61: mandoc_msg(MANDOCERR_TBLEXTRADAT,
62: tbl->parse, ln, *pos, NULL);
1.6 schwarze 63: /* Skip to the end... */
64: while (p[*pos])
65: (*pos)++;
66: return(1);
67: }
68:
1.4 schwarze 69: dat = mandoc_calloc(1, sizeof(struct tbl_dat));
70: dat->layout = cp;
71: dat->pos = TBL_DATA_NONE;
72:
1.6 schwarze 73: assert(TBL_CELL_SPAN != cp->pos);
74:
75: for (spans = 0, cp = cp->next; cp; cp = cp->next)
76: if (TBL_CELL_SPAN == cp->pos)
77: spans++;
78: else
79: break;
80:
81: dat->spans = spans;
1.4 schwarze 82:
83: if (dp->last) {
84: dp->last->next = dat;
85: dp->last = dat;
86: } else
87: dp->last = dp->first = dat;
88:
89: sv = *pos;
90: while (p[*pos] && p[*pos] != tbl->opts.tab)
91: (*pos)++;
92:
93: /*
94: * Check for a continued-data scope opening. This consists of a
95: * trailing `T{' at the end of the line. Subsequent lines,
96: * until a standalone `T}', are included in our cell.
97: */
1.1 schwarze 98:
1.4 schwarze 99: if (*pos - sv == 2 && 'T' == p[sv] && '{' == p[sv + 1]) {
100: tbl->part = TBL_PART_CDATA;
1.14 ! schwarze 101: return(1);
1.4 schwarze 102: }
1.1 schwarze 103:
1.10 schwarze 104: assert(*pos - sv >= 0);
105:
106: dat->string = mandoc_malloc((size_t)(*pos - sv + 1));
107: memcpy(dat->string, &p[sv], (size_t)(*pos - sv));
1.4 schwarze 108: dat->string[*pos - sv] = '\0';
1.1 schwarze 109:
1.4 schwarze 110: if (p[*pos])
111: (*pos)++;
1.1 schwarze 112:
113: if ( ! strcmp(dat->string, "_"))
1.4 schwarze 114: dat->pos = TBL_DATA_HORIZ;
1.1 schwarze 115: else if ( ! strcmp(dat->string, "="))
1.4 schwarze 116: dat->pos = TBL_DATA_DHORIZ;
1.1 schwarze 117: else if ( ! strcmp(dat->string, "\\_"))
1.4 schwarze 118: dat->pos = TBL_DATA_NHORIZ;
1.1 schwarze 119: else if ( ! strcmp(dat->string, "\\="))
1.4 schwarze 120: dat->pos = TBL_DATA_NDHORIZ;
1.1 schwarze 121: else
1.4 schwarze 122: dat->pos = TBL_DATA_DATA;
123:
124: if (TBL_CELL_HORIZ == dat->layout->pos ||
1.6 schwarze 125: TBL_CELL_DHORIZ == dat->layout->pos ||
126: TBL_CELL_DOWN == dat->layout->pos)
1.4 schwarze 127: if (TBL_DATA_DATA == dat->pos && '\0' != *dat->string)
1.11 schwarze 128: mandoc_msg(MANDOCERR_TBLIGNDATA,
129: tbl->parse, ln, sv, NULL);
1.4 schwarze 130:
1.1 schwarze 131: return(1);
132: }
133:
1.5 schwarze 134: /* ARGSUSED */
1.4 schwarze 135: int
136: tbl_cdata(struct tbl_node *tbl, int ln, const char *p)
137: {
138: struct tbl_dat *dat;
139: size_t sz;
1.5 schwarze 140: int pos;
1.4 schwarze 141:
1.5 schwarze 142: pos = 0;
1.4 schwarze 143:
144: dat = tbl->last_span->last;
1.5 schwarze 145:
146: if (p[pos] == 'T' && p[pos + 1] == '}') {
147: pos += 2;
148: if (p[pos] == tbl->opts.tab) {
149: tbl->part = TBL_PART_DATA;
150: pos++;
151: return(data(tbl, tbl->last_span, ln, p, &pos));
152: } else if ('\0' == p[pos]) {
153: tbl->part = TBL_PART_DATA;
154: return(1);
155: }
156:
157: /* Fallthrough: T} is part of a word. */
158: }
1.4 schwarze 159:
1.6 schwarze 160: dat->pos = TBL_DATA_DATA;
161:
1.4 schwarze 162: if (dat->string) {
163: sz = strlen(p) + strlen(dat->string) + 2;
164: dat->string = mandoc_realloc(dat->string, sz);
165: strlcat(dat->string, " ", sz);
166: strlcat(dat->string, p, sz);
167: } else
168: dat->string = mandoc_strdup(p);
169:
1.6 schwarze 170: if (TBL_CELL_DOWN == dat->layout->pos)
1.11 schwarze 171: mandoc_msg(MANDOCERR_TBLIGNDATA,
172: tbl->parse, ln, pos, NULL);
1.6 schwarze 173:
1.4 schwarze 174: return(0);
175: }
1.1 schwarze 176:
1.7 schwarze 177: static struct tbl_span *
1.9 schwarze 178: newspan(struct tbl_node *tbl, int line, struct tbl_row *rp)
1.7 schwarze 179: {
180: struct tbl_span *dp;
181:
182: dp = mandoc_calloc(1, sizeof(struct tbl_span));
1.9 schwarze 183: dp->line = line;
1.13 schwarze 184: dp->opts = &tbl->opts;
1.7 schwarze 185: dp->layout = rp;
186: dp->head = tbl->first_head;
187:
188: if (tbl->last_span) {
189: tbl->last_span->next = dp;
190: tbl->last_span = dp;
191: } else {
192: tbl->last_span = tbl->first_span = dp;
1.8 schwarze 193: tbl->current_span = NULL;
1.7 schwarze 194: dp->flags |= TBL_SPAN_FIRST;
195: }
196:
197: return(dp);
198: }
199:
1.1 schwarze 200: int
1.4 schwarze 201: tbl_data(struct tbl_node *tbl, int ln, const char *p)
1.1 schwarze 202: {
203: struct tbl_span *dp;
1.4 schwarze 204: struct tbl_row *rp;
205: int pos;
206:
207: pos = 0;
1.1 schwarze 208:
1.4 schwarze 209: if ('\0' == p[pos]) {
1.11 schwarze 210: mandoc_msg(MANDOCERR_TBL, tbl->parse, ln, pos, NULL);
1.4 schwarze 211: return(0);
1.1 schwarze 212: }
213:
1.4 schwarze 214: /*
215: * Choose a layout row: take the one following the last parsed
216: * span's. If that doesn't exist, use the last parsed span's.
1.6 schwarze 217: * If there's no last parsed span, use the first row. Lastly,
218: * if the last span was a horizontal line, use the same layout
219: * (it doesn't "consume" the layout).
1.4 schwarze 220: */
221:
222: if (tbl->last_span) {
223: assert(tbl->last_span->layout);
1.7 schwarze 224: if (tbl->last_span->pos == TBL_SPAN_DATA) {
225: for (rp = tbl->last_span->layout->next;
226: rp && rp->first; rp = rp->next) {
227: switch (rp->first->pos) {
228: case (TBL_CELL_HORIZ):
1.9 schwarze 229: dp = newspan(tbl, ln, rp);
1.7 schwarze 230: dp->pos = TBL_SPAN_HORIZ;
231: continue;
232: case (TBL_CELL_DHORIZ):
1.9 schwarze 233: dp = newspan(tbl, ln, rp);
1.7 schwarze 234: dp->pos = TBL_SPAN_DHORIZ;
235: continue;
236: default:
237: break;
238: }
239: break;
240: }
241: } else
1.6 schwarze 242: rp = tbl->last_span->layout;
243:
1.4 schwarze 244: if (NULL == rp)
245: rp = tbl->last_span->layout;
246: } else
247: rp = tbl->first_row;
1.6 schwarze 248:
249: assert(rp);
1.4 schwarze 250:
1.9 schwarze 251: dp = newspan(tbl, ln, rp);
1.1 schwarze 252:
253: if ( ! strcmp(p, "_")) {
1.4 schwarze 254: dp->pos = TBL_SPAN_HORIZ;
1.1 schwarze 255: return(1);
256: } else if ( ! strcmp(p, "=")) {
1.4 schwarze 257: dp->pos = TBL_SPAN_DHORIZ;
1.1 schwarze 258: return(1);
259: }
260:
1.4 schwarze 261: dp->pos = TBL_SPAN_DATA;
1.1 schwarze 262:
1.4 schwarze 263: /* This returns 0 when TBL_PART_CDATA is entered. */
1.1 schwarze 264:
1.4 schwarze 265: while ('\0' != p[pos])
266: if ( ! data(tbl, dp, ln, p, &pos))
267: return(0);
1.1 schwarze 268:
269: return(1);
270: }