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