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