]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_data.c
cfc489bb4547ca4818327f31399d85237e2d5e1f
1 /* $Id: tbl_data.c,v 1.55 2021/05/18 13:22:43 schwarze Exp $ */
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011,2015,2017-2019,2021 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
20 #include <sys/types.h>
30 #include "mandoc_aux.h"
33 #include "libmandoc.h"
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,
43 getdata(struct tbl_node
*tbl
, struct tbl_span
*dp
,
44 int ln
, const char *p
, int *pos
)
46 struct tbl_dat
*dat
, *pdat
;
53 * Determine the length of the string in the cell
54 * and advance the parse point to the end of the cell.
59 while (*ccp
!= '\0' && *ccp
!= tbl
->opts
.tab
)
61 mandoc_escape(&ccp
, NULL
, NULL
);
64 /* Advance to the next layout cell, skipping spanners. */
66 cp
= dp
->last
== NULL
? dp
->layout
->first
: dp
->last
->layout
->next
;
67 while (cp
!= NULL
&& cp
->pos
== TBL_CELL_SPAN
)
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.
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
->spacing
= SIZE_MAX
;
82 dp
->layout
->last
->next
= cp
;
83 cp
->col
= dp
->layout
->last
->col
+ 1;
84 dp
->layout
->last
= cp
;
86 mandoc_msg(MANDOCERR_TBLDATA_EXTRA
,
87 ln
, sv
, "%s", p
+ sv
);
88 while (p
[*pos
] != '\0')
94 dat
= mandoc_malloc(sizeof(*dat
));
101 dat
->pos
= TBL_DATA_NONE
;
104 * Increment the number of vertical spans in a data cell above,
105 * if this cell vertically extends one or more cells above.
106 * The iteration must be done over data rows,
107 * not over layout rows, because one layout row
108 * can be reused for more than one data row.
111 if (cp
->pos
== TBL_CELL_DOWN
||
112 (*pos
- sv
== 2 && p
[sv
] == '\\' && p
[sv
+ 1] == '^')) {
114 while ((pdp
= pdp
->prev
) != NULL
) {
116 while (pdat
!= NULL
&&
117 pdat
->layout
->col
< dat
->layout
->col
)
121 if (pdat
->layout
->pos
!= TBL_CELL_DOWN
&&
122 strcmp(pdat
->string
, "\\^") != 0) {
130 * Count the number of horizontal spans to the right of this cell.
131 * This is purely a matter of the layout, independent of the data.
134 for (cp
= cp
->next
; cp
!= NULL
; cp
= cp
->next
)
135 if (cp
->pos
== TBL_CELL_SPAN
)
140 /* Append the new data cell to the data row. */
142 if (dp
->last
== NULL
)
145 dp
->last
->next
= dat
;
149 * Check for a continued-data scope opening. This consists of a
150 * trailing `T{' at the end of the line. Subsequent lines,
151 * until a standalone `T}', are included in our cell.
154 if (*pos
- sv
== 2 && p
[sv
] == 'T' && p
[sv
+ 1] == '{') {
155 tbl
->part
= TBL_PART_CDATA
;
159 dat
->string
= mandoc_strndup(p
+ sv
, *pos
- sv
);
164 if ( ! strcmp(dat
->string
, "_"))
165 dat
->pos
= TBL_DATA_HORIZ
;
166 else if ( ! strcmp(dat
->string
, "="))
167 dat
->pos
= TBL_DATA_DHORIZ
;
168 else if ( ! strcmp(dat
->string
, "\\_"))
169 dat
->pos
= TBL_DATA_NHORIZ
;
170 else if ( ! strcmp(dat
->string
, "\\="))
171 dat
->pos
= TBL_DATA_NDHORIZ
;
173 dat
->pos
= TBL_DATA_DATA
;
175 if ((dat
->layout
->pos
== TBL_CELL_HORIZ
||
176 dat
->layout
->pos
== TBL_CELL_DHORIZ
||
177 dat
->layout
->pos
== TBL_CELL_DOWN
) &&
178 dat
->pos
== TBL_DATA_DATA
&& *dat
->string
!= '\0')
179 mandoc_msg(MANDOCERR_TBLDATA_SPAN
,
180 ln
, sv
, "%s", dat
->string
);
184 tbl_cdata(struct tbl_node
*tbl
, int ln
, const char *p
, int pos
)
189 dat
= tbl
->last_span
->last
;
191 if (p
[pos
] == 'T' && p
[pos
+ 1] == '}') {
193 if (p
[pos
] == tbl
->opts
.tab
) {
194 tbl
->part
= TBL_PART_DATA
;
196 while (p
[pos
] != '\0')
197 getdata(tbl
, tbl
->last_span
, ln
, p
, &pos
);
199 } else if (p
[pos
] == '\0') {
200 tbl
->part
= TBL_PART_DATA
;
204 /* Fallthrough: T} is part of a word. */
207 dat
->pos
= TBL_DATA_DATA
;
210 if (dat
->string
!= NULL
) {
211 sz
= strlen(p
+ pos
) + strlen(dat
->string
) + 2;
212 dat
->string
= mandoc_realloc(dat
->string
, sz
);
213 (void)strlcat(dat
->string
, " ", sz
);
214 (void)strlcat(dat
->string
, p
+ pos
, sz
);
216 dat
->string
= mandoc_strdup(p
+ pos
);
218 if (dat
->layout
->pos
== TBL_CELL_DOWN
)
219 mandoc_msg(MANDOCERR_TBLDATA_SPAN
,
220 ln
, pos
, "%s", dat
->string
);
223 static struct tbl_span
*
224 newspan(struct tbl_node
*tbl
, int line
, struct tbl_row
*rp
)
228 dp
= mandoc_calloc(1, sizeof(*dp
));
230 dp
->opts
= &tbl
->opts
;
232 dp
->prev
= tbl
->last_span
;
234 if (dp
->prev
== NULL
) {
235 tbl
->first_span
= dp
;
236 tbl
->current_span
= NULL
;
245 tbl_data(struct tbl_node
*tbl
, int ln
, const char *p
, int pos
)
251 for (sp
= tbl
->last_span
; sp
!= NULL
; sp
= sp
->prev
)
252 if (sp
->pos
== TBL_SPAN_DATA
)
254 rp
= sp
== NULL
? tbl
->first_row
:
255 sp
->layout
->next
== NULL
? sp
->layout
: sp
->layout
->next
;
262 * Empty request lines must be handled here
263 * and cannot be discarded in roff_parseln()
264 * because in the layout section, they
265 * are significant and end the layout.
269 sp
= newspan(tbl
, ln
, rp
);
270 sp
->pos
= TBL_SPAN_HORIZ
;
273 sp
= newspan(tbl
, ln
, rp
);
274 sp
->pos
= TBL_SPAN_DHORIZ
;
282 * If the layout row contains nothing but horizontal lines,
283 * allocate an empty span for it and assign the current span
284 * to the next layout row accepting data.
287 while (rp
->next
!= NULL
) {
288 if (rp
->last
->col
+ 1 < tbl
->opts
.cols
)
290 for (cp
= rp
->first
; cp
!= NULL
; cp
= cp
->next
)
291 if (cp
->pos
!= TBL_CELL_HORIZ
&&
292 cp
->pos
!= TBL_CELL_DHORIZ
)
296 sp
= newspan(tbl
, ln
, rp
);
297 sp
->pos
= TBL_SPAN_DATA
;
301 /* Process a real data row. */
303 sp
= newspan(tbl
, ln
, rp
);
304 sp
->pos
= TBL_SPAN_DATA
;
305 while (p
[pos
] != '\0')
306 getdata(tbl
, sp
, ln
, p
, &pos
);