]> git.cameronkatri.com Git - mandoc.git/blob - tbl_data.c
Manually tag the section option.
[mandoc.git] / tbl_data.c
1 /* $Id: tbl_data.c,v 1.53 2020/01/11 20:48:18 schwarze Exp $ */
2 /*
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011,2015,2017,2018,2019 Ingo Schwarze <schwarze@openbsd.org>
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 */
18 #include "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29
30 #include "mandoc_aux.h"
31 #include "mandoc.h"
32 #include "tbl.h"
33 #include "libmandoc.h"
34 #include "tbl_int.h"
35
36 static void getdata(struct tbl_node *, struct tbl_span *,
37 int, const char *, int *);
38 static struct tbl_span *newspan(struct tbl_node *, int,
39 struct tbl_row *);
40
41
42 static void
43 getdata(struct tbl_node *tbl, struct tbl_span *dp,
44 int ln, const char *p, int *pos)
45 {
46 struct tbl_dat *dat, *pdat;
47 struct tbl_cell *cp;
48 struct tbl_span *pdp;
49 int sv;
50
51 /*
52 * Determine the length of the string in the cell
53 * and advance the parse point to the end of the cell.
54 */
55
56 sv = *pos;
57 while (p[*pos] != '\0' && p[*pos] != tbl->opts.tab)
58 (*pos)++;
59
60 /* Advance to the next layout cell, skipping spanners. */
61
62 cp = dp->last == NULL ? dp->layout->first : dp->last->layout->next;
63 while (cp != NULL && cp->pos == TBL_CELL_SPAN)
64 cp = cp->next;
65
66 /*
67 * If the current layout row is out of cells, allocate
68 * a new cell if another row of the table has at least
69 * this number of columns, or discard the input if we
70 * are beyond the last column of the table as a whole.
71 */
72
73 if (cp == NULL) {
74 if (dp->layout->last->col + 1 < dp->opts->cols) {
75 cp = mandoc_calloc(1, sizeof(*cp));
76 cp->pos = TBL_CELL_LEFT;
77 cp->spacing = SIZE_MAX;
78 dp->layout->last->next = cp;
79 cp->col = dp->layout->last->col + 1;
80 dp->layout->last = cp;
81 } else {
82 mandoc_msg(MANDOCERR_TBLDATA_EXTRA,
83 ln, sv, "%s", p + sv);
84 while (p[*pos] != '\0')
85 (*pos)++;
86 return;
87 }
88 }
89
90 dat = mandoc_malloc(sizeof(*dat));
91 dat->layout = cp;
92 dat->next = NULL;
93 dat->string = NULL;
94 dat->hspans = 0;
95 dat->vspans = 0;
96 dat->block = 0;
97 dat->pos = TBL_DATA_NONE;
98
99 /*
100 * Increment the number of vertical spans in a data cell above,
101 * if this cell vertically extends one or more cells above.
102 * The iteration must be done over data rows,
103 * not over layout rows, because one layout row
104 * can be reused for more than one data row.
105 */
106
107 if (cp->pos == TBL_CELL_DOWN ||
108 (*pos - sv == 2 && p[sv] == '\\' && p[sv + 1] == '^')) {
109 pdp = dp;
110 while ((pdp = pdp->prev) != NULL) {
111 pdat = pdp->first;
112 while (pdat != NULL &&
113 pdat->layout->col < dat->layout->col)
114 pdat = pdat->next;
115 if (pdat == NULL)
116 break;
117 if (pdat->layout->pos != TBL_CELL_DOWN &&
118 strcmp(pdat->string, "\\^") != 0) {
119 pdat->vspans++;
120 break;
121 }
122 }
123 }
124
125 /*
126 * Count the number of horizontal spans to the right of this cell.
127 * This is purely a matter of the layout, independent of the data.
128 */
129
130 for (cp = cp->next; cp != NULL; cp = cp->next)
131 if (cp->pos == TBL_CELL_SPAN)
132 dat->hspans++;
133 else
134 break;
135
136 /* Append the new data cell to the data row. */
137
138 if (dp->last == NULL)
139 dp->first = dat;
140 else
141 dp->last->next = dat;
142 dp->last = dat;
143
144 /*
145 * Check for a continued-data scope opening. This consists of a
146 * trailing `T{' at the end of the line. Subsequent lines,
147 * until a standalone `T}', are included in our cell.
148 */
149
150 if (*pos - sv == 2 && p[sv] == 'T' && p[sv + 1] == '{') {
151 tbl->part = TBL_PART_CDATA;
152 return;
153 }
154
155 dat->string = mandoc_strndup(p + sv, *pos - sv);
156
157 if (p[*pos] != '\0')
158 (*pos)++;
159
160 if ( ! strcmp(dat->string, "_"))
161 dat->pos = TBL_DATA_HORIZ;
162 else if ( ! strcmp(dat->string, "="))
163 dat->pos = TBL_DATA_DHORIZ;
164 else if ( ! strcmp(dat->string, "\\_"))
165 dat->pos = TBL_DATA_NHORIZ;
166 else if ( ! strcmp(dat->string, "\\="))
167 dat->pos = TBL_DATA_NDHORIZ;
168 else
169 dat->pos = TBL_DATA_DATA;
170
171 if ((dat->layout->pos == TBL_CELL_HORIZ ||
172 dat->layout->pos == TBL_CELL_DHORIZ ||
173 dat->layout->pos == TBL_CELL_DOWN) &&
174 dat->pos == TBL_DATA_DATA && *dat->string != '\0')
175 mandoc_msg(MANDOCERR_TBLDATA_SPAN,
176 ln, sv, "%s", dat->string);
177 }
178
179 void
180 tbl_cdata(struct tbl_node *tbl, int ln, const char *p, int pos)
181 {
182 struct tbl_dat *dat;
183 size_t sz;
184
185 dat = tbl->last_span->last;
186
187 if (p[pos] == 'T' && p[pos + 1] == '}') {
188 pos += 2;
189 if (p[pos] == tbl->opts.tab) {
190 tbl->part = TBL_PART_DATA;
191 pos++;
192 while (p[pos] != '\0')
193 getdata(tbl, tbl->last_span, ln, p, &pos);
194 return;
195 } else if (p[pos] == '\0') {
196 tbl->part = TBL_PART_DATA;
197 return;
198 }
199
200 /* Fallthrough: T} is part of a word. */
201 }
202
203 dat->pos = TBL_DATA_DATA;
204 dat->block = 1;
205
206 if (dat->string != NULL) {
207 sz = strlen(p + pos) + strlen(dat->string) + 2;
208 dat->string = mandoc_realloc(dat->string, sz);
209 (void)strlcat(dat->string, " ", sz);
210 (void)strlcat(dat->string, p + pos, sz);
211 } else
212 dat->string = mandoc_strdup(p + pos);
213
214 if (dat->layout->pos == TBL_CELL_DOWN)
215 mandoc_msg(MANDOCERR_TBLDATA_SPAN,
216 ln, pos, "%s", dat->string);
217 }
218
219 static struct tbl_span *
220 newspan(struct tbl_node *tbl, int line, struct tbl_row *rp)
221 {
222 struct tbl_span *dp;
223
224 dp = mandoc_calloc(1, sizeof(*dp));
225 dp->line = line;
226 dp->opts = &tbl->opts;
227 dp->layout = rp;
228 dp->prev = tbl->last_span;
229
230 if (dp->prev == NULL) {
231 tbl->first_span = dp;
232 tbl->current_span = NULL;
233 } else
234 dp->prev->next = dp;
235 tbl->last_span = dp;
236
237 return dp;
238 }
239
240 void
241 tbl_data(struct tbl_node *tbl, int ln, const char *p, int pos)
242 {
243 struct tbl_row *rp;
244 struct tbl_cell *cp;
245 struct tbl_span *sp;
246
247 rp = (sp = tbl->last_span) == NULL ? tbl->first_row :
248 sp->pos == TBL_SPAN_DATA && sp->layout->next != NULL ?
249 sp->layout->next : sp->layout;
250
251 assert(rp != NULL);
252
253 if (p[1] == '\0') {
254 switch (p[0]) {
255 case '.':
256 /*
257 * Empty request lines must be handled here
258 * and cannot be discarded in roff_parseln()
259 * because in the layout section, they
260 * are significant and end the layout.
261 */
262 return;
263 case '_':
264 sp = newspan(tbl, ln, rp);
265 sp->pos = TBL_SPAN_HORIZ;
266 return;
267 case '=':
268 sp = newspan(tbl, ln, rp);
269 sp->pos = TBL_SPAN_DHORIZ;
270 return;
271 default:
272 break;
273 }
274 }
275
276 /*
277 * If the layout row contains nothing but horizontal lines,
278 * allocate an empty span for it and assign the current span
279 * to the next layout row accepting data.
280 */
281
282 while (rp->next != NULL) {
283 if (rp->last->col + 1 < tbl->opts.cols)
284 break;
285 for (cp = rp->first; cp != NULL; cp = cp->next)
286 if (cp->pos != TBL_CELL_HORIZ &&
287 cp->pos != TBL_CELL_DHORIZ)
288 break;
289 if (cp != NULL)
290 break;
291 sp = newspan(tbl, ln, rp);
292 sp->pos = TBL_SPAN_DATA;
293 rp = rp->next;
294 }
295
296 /* Process a real data row. */
297
298 sp = newspan(tbl, ln, rp);
299 sp->pos = TBL_SPAN_DATA;
300 while (p[pos] != '\0')
301 getdata(tbl, sp, ln, p, &pos);
302 }