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