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