Annotation of src/usr.bin/mandoc/tbl_data.c, Revision 1.20
1.20 ! schwarze 1: /* $OpenBSD: tbl_data.c,v 1.19 2014/11/28 19:25:03 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: */
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.20 ! schwarze 31: static void 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.20 ! schwarze 37: static void
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)++;
1.20 ! schwarze 70: return;
1.6 schwarze 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.20 ! schwarze 105: return;
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.20 ! schwarze 135: return;
1.1 schwarze 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.20 ! schwarze 154: getdata(tbl, tbl->last_span, ln, p, &pos);
! 155: return(1);
1.5 schwarze 156: } else if ('\0' == p[pos]) {
157: tbl->part = TBL_PART_DATA;
158: return(1);
159: }
160:
161: /* Fallthrough: T} is part of a word. */
162: }
1.4 schwarze 163:
1.6 schwarze 164: dat->pos = TBL_DATA_DATA;
165:
1.4 schwarze 166: if (dat->string) {
167: sz = strlen(p) + strlen(dat->string) + 2;
168: dat->string = mandoc_realloc(dat->string, sz);
1.18 schwarze 169: (void)strlcat(dat->string, " ", sz);
170: (void)strlcat(dat->string, p, sz);
1.4 schwarze 171: } else
172: dat->string = mandoc_strdup(p);
173:
1.17 schwarze 174: if (TBL_CELL_DOWN == dat->layout->pos)
175: mandoc_msg(MANDOCERR_TBLIGNDATA, tbl->parse,
176: ln, pos, NULL);
1.6 schwarze 177:
1.4 schwarze 178: return(0);
179: }
1.1 schwarze 180:
1.7 schwarze 181: static struct tbl_span *
1.9 schwarze 182: newspan(struct tbl_node *tbl, int line, struct tbl_row *rp)
1.7 schwarze 183: {
184: struct tbl_span *dp;
185:
186: dp = mandoc_calloc(1, sizeof(struct tbl_span));
1.9 schwarze 187: dp->line = line;
1.13 schwarze 188: dp->opts = &tbl->opts;
1.7 schwarze 189: dp->layout = rp;
190: dp->head = tbl->first_head;
191:
192: if (tbl->last_span) {
193: tbl->last_span->next = dp;
194: tbl->last_span = dp;
195: } else {
196: tbl->last_span = tbl->first_span = dp;
1.8 schwarze 197: tbl->current_span = NULL;
1.7 schwarze 198: dp->flags |= TBL_SPAN_FIRST;
199: }
200:
201: return(dp);
202: }
203:
1.20 ! schwarze 204: void
1.4 schwarze 205: tbl_data(struct tbl_node *tbl, int ln, const char *p)
1.1 schwarze 206: {
207: struct tbl_span *dp;
1.4 schwarze 208: struct tbl_row *rp;
209: int pos;
210:
1.17 schwarze 211: /*
1.4 schwarze 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) {
1.17 schwarze 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;
1.17 schwarze 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.20 ! schwarze 252: return;
1.1 schwarze 253: } else if ( ! strcmp(p, "=")) {
1.4 schwarze 254: dp->pos = TBL_SPAN_DHORIZ;
1.20 ! schwarze 255: return;
1.1 schwarze 256: }
257:
1.4 schwarze 258: dp->pos = TBL_SPAN_DATA;
1.1 schwarze 259:
1.20 ! schwarze 260: pos = 0;
1.4 schwarze 261: while ('\0' != p[pos])
1.20 ! schwarze 262: getdata(tbl, dp, ln, p, &pos);
1.1 schwarze 263: }