tbl_data.c revision 1.4 1 1.2 christos /* $Vendor-Id: tbl_data.c,v 1.24 2011/03/20 16:02:05 kristaps Exp $ */
2 1.1 joerg /*
3 1.2 christos * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps (at) bsd.lv>
4 1.2 christos * Copyright (c) 2011 Ingo Schwarze <schwarze (at) openbsd.org>
5 1.1 joerg *
6 1.1 joerg * Permission to use, copy, modify, and distribute this software for any
7 1.1 joerg * purpose with or without fee is hereby granted, provided that the above
8 1.1 joerg * copyright notice and this permission notice appear in all copies.
9 1.1 joerg *
10 1.1 joerg * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1 joerg * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1 joerg * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1 joerg * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1 joerg * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 1.1 joerg * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 1.1 joerg * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1 joerg */
18 1.1 joerg #ifdef HAVE_CONFIG_H
19 1.1 joerg #include "config.h"
20 1.1 joerg #endif
21 1.1 joerg
22 1.1 joerg #include <assert.h>
23 1.1 joerg #include <ctype.h>
24 1.1 joerg #include <stdlib.h>
25 1.1 joerg #include <string.h>
26 1.1 joerg #include <time.h>
27 1.1 joerg
28 1.1 joerg #include "mandoc.h"
29 1.1 joerg #include "libmandoc.h"
30 1.1 joerg #include "libroff.h"
31 1.1 joerg
32 1.4 christos static int getdata(struct tbl_node *, struct tbl_span *,
33 1.2 christos int, const char *, int *);
34 1.2 christos static struct tbl_span *newspan(struct tbl_node *, int,
35 1.2 christos struct tbl_row *);
36 1.1 joerg
37 1.1 joerg static int
38 1.4 christos getdata(struct tbl_node *tbl, struct tbl_span *dp,
39 1.1 joerg int ln, const char *p, int *pos)
40 1.1 joerg {
41 1.1 joerg struct tbl_dat *dat;
42 1.1 joerg struct tbl_cell *cp;
43 1.2 christos int sv, spans;
44 1.1 joerg
45 1.1 joerg cp = NULL;
46 1.1 joerg if (dp->last && dp->last->layout)
47 1.1 joerg cp = dp->last->layout->next;
48 1.1 joerg else if (NULL == dp->last)
49 1.1 joerg cp = dp->layout->first;
50 1.1 joerg
51 1.1 joerg /*
52 1.1 joerg * Skip over spanners and vertical lines to data formats, since
53 1.1 joerg * we want to match data with data layout cells in the header.
54 1.1 joerg */
55 1.1 joerg
56 1.1 joerg while (cp && (TBL_CELL_VERT == cp->pos ||
57 1.1 joerg TBL_CELL_DVERT == cp->pos ||
58 1.1 joerg TBL_CELL_SPAN == cp->pos))
59 1.1 joerg cp = cp->next;
60 1.1 joerg
61 1.2 christos /*
62 1.2 christos * Stop processing when we reach the end of the available layout
63 1.2 christos * cells. This means that we have extra input.
64 1.2 christos */
65 1.2 christos
66 1.2 christos if (NULL == cp) {
67 1.2 christos mandoc_msg(MANDOCERR_TBLEXTRADAT,
68 1.2 christos tbl->parse, ln, *pos, NULL);
69 1.2 christos /* Skip to the end... */
70 1.2 christos while (p[*pos])
71 1.2 christos (*pos)++;
72 1.2 christos return(1);
73 1.2 christos }
74 1.2 christos
75 1.1 joerg dat = mandoc_calloc(1, sizeof(struct tbl_dat));
76 1.1 joerg dat->layout = cp;
77 1.1 joerg dat->pos = TBL_DATA_NONE;
78 1.1 joerg
79 1.2 christos assert(TBL_CELL_SPAN != cp->pos);
80 1.2 christos
81 1.2 christos for (spans = 0, cp = cp->next; cp; cp = cp->next)
82 1.2 christos if (TBL_CELL_SPAN == cp->pos)
83 1.2 christos spans++;
84 1.2 christos else
85 1.2 christos break;
86 1.2 christos
87 1.2 christos dat->spans = spans;
88 1.1 joerg
89 1.1 joerg if (dp->last) {
90 1.1 joerg dp->last->next = dat;
91 1.1 joerg dp->last = dat;
92 1.1 joerg } else
93 1.1 joerg dp->last = dp->first = dat;
94 1.1 joerg
95 1.1 joerg sv = *pos;
96 1.1 joerg while (p[*pos] && p[*pos] != tbl->opts.tab)
97 1.1 joerg (*pos)++;
98 1.1 joerg
99 1.1 joerg /*
100 1.1 joerg * Check for a continued-data scope opening. This consists of a
101 1.1 joerg * trailing `T{' at the end of the line. Subsequent lines,
102 1.1 joerg * until a standalone `T}', are included in our cell.
103 1.1 joerg */
104 1.1 joerg
105 1.1 joerg if (*pos - sv == 2 && 'T' == p[sv] && '{' == p[sv + 1]) {
106 1.1 joerg tbl->part = TBL_PART_CDATA;
107 1.1 joerg return(0);
108 1.1 joerg }
109 1.1 joerg
110 1.2 christos assert(*pos - sv >= 0);
111 1.2 christos
112 1.2 christos dat->string = mandoc_malloc((size_t)(*pos - sv + 1));
113 1.2 christos memcpy(dat->string, &p[sv], (size_t)(*pos - sv));
114 1.1 joerg dat->string[*pos - sv] = '\0';
115 1.1 joerg
116 1.1 joerg if (p[*pos])
117 1.1 joerg (*pos)++;
118 1.1 joerg
119 1.1 joerg if ( ! strcmp(dat->string, "_"))
120 1.1 joerg dat->pos = TBL_DATA_HORIZ;
121 1.1 joerg else if ( ! strcmp(dat->string, "="))
122 1.1 joerg dat->pos = TBL_DATA_DHORIZ;
123 1.1 joerg else if ( ! strcmp(dat->string, "\\_"))
124 1.1 joerg dat->pos = TBL_DATA_NHORIZ;
125 1.1 joerg else if ( ! strcmp(dat->string, "\\="))
126 1.1 joerg dat->pos = TBL_DATA_NDHORIZ;
127 1.1 joerg else
128 1.1 joerg dat->pos = TBL_DATA_DATA;
129 1.1 joerg
130 1.1 joerg if (TBL_CELL_HORIZ == dat->layout->pos ||
131 1.2 christos TBL_CELL_DHORIZ == dat->layout->pos ||
132 1.2 christos TBL_CELL_DOWN == dat->layout->pos)
133 1.1 joerg if (TBL_DATA_DATA == dat->pos && '\0' != *dat->string)
134 1.2 christos mandoc_msg(MANDOCERR_TBLIGNDATA,
135 1.2 christos tbl->parse, ln, sv, NULL);
136 1.1 joerg
137 1.1 joerg return(1);
138 1.1 joerg }
139 1.1 joerg
140 1.1 joerg /* ARGSUSED */
141 1.1 joerg int
142 1.1 joerg tbl_cdata(struct tbl_node *tbl, int ln, const char *p)
143 1.1 joerg {
144 1.1 joerg struct tbl_dat *dat;
145 1.1 joerg size_t sz;
146 1.1 joerg int pos;
147 1.1 joerg
148 1.1 joerg pos = 0;
149 1.1 joerg
150 1.1 joerg dat = tbl->last_span->last;
151 1.1 joerg
152 1.1 joerg if (p[pos] == 'T' && p[pos + 1] == '}') {
153 1.1 joerg pos += 2;
154 1.1 joerg if (p[pos] == tbl->opts.tab) {
155 1.1 joerg tbl->part = TBL_PART_DATA;
156 1.1 joerg pos++;
157 1.4 christos return(getdata(tbl, tbl->last_span, ln, p, &pos));
158 1.1 joerg } else if ('\0' == p[pos]) {
159 1.1 joerg tbl->part = TBL_PART_DATA;
160 1.1 joerg return(1);
161 1.1 joerg }
162 1.1 joerg
163 1.1 joerg /* Fallthrough: T} is part of a word. */
164 1.1 joerg }
165 1.1 joerg
166 1.2 christos dat->pos = TBL_DATA_DATA;
167 1.2 christos
168 1.1 joerg if (dat->string) {
169 1.1 joerg sz = strlen(p) + strlen(dat->string) + 2;
170 1.1 joerg dat->string = mandoc_realloc(dat->string, sz);
171 1.1 joerg strlcat(dat->string, " ", sz);
172 1.1 joerg strlcat(dat->string, p, sz);
173 1.1 joerg } else
174 1.1 joerg dat->string = mandoc_strdup(p);
175 1.1 joerg
176 1.2 christos if (TBL_CELL_DOWN == dat->layout->pos)
177 1.2 christos mandoc_msg(MANDOCERR_TBLIGNDATA,
178 1.2 christos tbl->parse, ln, pos, NULL);
179 1.2 christos
180 1.1 joerg return(0);
181 1.1 joerg }
182 1.1 joerg
183 1.2 christos static struct tbl_span *
184 1.2 christos newspan(struct tbl_node *tbl, int line, struct tbl_row *rp)
185 1.2 christos {
186 1.2 christos struct tbl_span *dp;
187 1.2 christos
188 1.2 christos dp = mandoc_calloc(1, sizeof(struct tbl_span));
189 1.2 christos dp->line = line;
190 1.2 christos dp->tbl = &tbl->opts;
191 1.2 christos dp->layout = rp;
192 1.2 christos dp->head = tbl->first_head;
193 1.2 christos
194 1.2 christos if (tbl->last_span) {
195 1.2 christos tbl->last_span->next = dp;
196 1.2 christos tbl->last_span = dp;
197 1.2 christos } else {
198 1.2 christos tbl->last_span = tbl->first_span = dp;
199 1.2 christos tbl->current_span = NULL;
200 1.2 christos dp->flags |= TBL_SPAN_FIRST;
201 1.2 christos }
202 1.2 christos
203 1.2 christos return(dp);
204 1.2 christos }
205 1.2 christos
206 1.1 joerg int
207 1.1 joerg tbl_data(struct tbl_node *tbl, int ln, const char *p)
208 1.1 joerg {
209 1.1 joerg struct tbl_span *dp;
210 1.1 joerg struct tbl_row *rp;
211 1.1 joerg int pos;
212 1.1 joerg
213 1.1 joerg pos = 0;
214 1.1 joerg
215 1.1 joerg if ('\0' == p[pos]) {
216 1.2 christos mandoc_msg(MANDOCERR_TBL, tbl->parse, ln, pos, NULL);
217 1.1 joerg return(0);
218 1.1 joerg }
219 1.1 joerg
220 1.1 joerg /*
221 1.1 joerg * Choose a layout row: take the one following the last parsed
222 1.1 joerg * span's. If that doesn't exist, use the last parsed span's.
223 1.1 joerg * If there's no last parsed span, use the first row. Lastly,
224 1.1 joerg * if the last span was a horizontal line, use the same layout
225 1.1 joerg * (it doesn't "consume" the layout).
226 1.1 joerg */
227 1.1 joerg
228 1.1 joerg if (tbl->last_span) {
229 1.1 joerg assert(tbl->last_span->layout);
230 1.2 christos if (tbl->last_span->pos == TBL_SPAN_DATA) {
231 1.2 christos for (rp = tbl->last_span->layout->next;
232 1.2 christos rp && rp->first; rp = rp->next) {
233 1.2 christos switch (rp->first->pos) {
234 1.2 christos case (TBL_CELL_HORIZ):
235 1.2 christos dp = newspan(tbl, ln, rp);
236 1.2 christos dp->pos = TBL_SPAN_HORIZ;
237 1.2 christos continue;
238 1.2 christos case (TBL_CELL_DHORIZ):
239 1.2 christos dp = newspan(tbl, ln, rp);
240 1.2 christos dp->pos = TBL_SPAN_DHORIZ;
241 1.2 christos continue;
242 1.2 christos default:
243 1.2 christos break;
244 1.2 christos }
245 1.2 christos break;
246 1.2 christos }
247 1.2 christos } else
248 1.1 joerg rp = tbl->last_span->layout;
249 1.2 christos
250 1.1 joerg if (NULL == rp)
251 1.1 joerg rp = tbl->last_span->layout;
252 1.1 joerg } else
253 1.1 joerg rp = tbl->first_row;
254 1.1 joerg
255 1.2 christos assert(rp);
256 1.1 joerg
257 1.2 christos dp = newspan(tbl, ln, rp);
258 1.1 joerg
259 1.1 joerg if ( ! strcmp(p, "_")) {
260 1.1 joerg dp->pos = TBL_SPAN_HORIZ;
261 1.1 joerg return(1);
262 1.1 joerg } else if ( ! strcmp(p, "=")) {
263 1.1 joerg dp->pos = TBL_SPAN_DHORIZ;
264 1.1 joerg return(1);
265 1.1 joerg }
266 1.1 joerg
267 1.1 joerg dp->pos = TBL_SPAN_DATA;
268 1.1 joerg
269 1.1 joerg /* This returns 0 when TBL_PART_CDATA is entered. */
270 1.1 joerg
271 1.1 joerg while ('\0' != p[pos])
272 1.4 christos if ( ! getdata(tbl, dp, ln, p, &pos))
273 1.1 joerg return(0);
274 1.1 joerg
275 1.1 joerg return(1);
276 1.1 joerg }
277