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