]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_data.c
529d6c9c51e212e0c914696c6e389b81d10333ec
1 /* $Id: tbl_data.c,v 1.49 2018/12/13 02:06:07 schwarze Exp $ */
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2015, 2017, 2018 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>
28 #include "mandoc_aux.h"
31 #include "libmandoc.h"
34 static void getdata(struct tbl_node
*, struct tbl_span
*,
35 int, const char *, int *);
36 static struct tbl_span
*newspan(struct tbl_node
*, int,
41 getdata(struct tbl_node
*tbl
, struct tbl_span
*dp
,
42 int ln
, const char *p
, int *pos
)
44 struct tbl_dat
*dat
, *pdat
;
50 * Determine the length of the string in the cell
51 * and advance the parse point to the end of the cell.
55 while (p
[*pos
] != '\0' && p
[*pos
] != tbl
->opts
.tab
)
58 /* Advance to the next layout cell, skipping spanners. */
60 cp
= dp
->last
== NULL
? dp
->layout
->first
: dp
->last
->layout
->next
;
61 while (cp
!= NULL
&& cp
->pos
== TBL_CELL_SPAN
)
65 * If the current layout row is out of cells, allocate
66 * a new cell if another row of the table has at least
67 * this number of columns, or discard the input if we
68 * are beyond the last column of the table as a whole.
72 if (dp
->layout
->last
->col
+ 1 < dp
->opts
->cols
) {
73 cp
= mandoc_calloc(1, sizeof(*cp
));
74 cp
->pos
= TBL_CELL_LEFT
;
75 dp
->layout
->last
->next
= cp
;
76 cp
->col
= dp
->layout
->last
->col
+ 1;
77 dp
->layout
->last
= cp
;
79 mandoc_msg(MANDOCERR_TBLDATA_EXTRA
, tbl
->parse
,
81 while (p
[*pos
] != '\0')
87 dat
= mandoc_malloc(sizeof(*dat
));
94 dat
->pos
= TBL_DATA_NONE
;
97 * Increment the number of vertical spans in a data cell above,
98 * if this cell vertically extends one or more cells above.
99 * The iteration must be done over data rows,
100 * not over layout rows, because one layout row
101 * can be reused for more than one data row.
104 if (cp
->pos
== TBL_CELL_DOWN
||
105 (*pos
- sv
== 2 && p
[sv
] == '\\' && p
[sv
+ 1] == '^')) {
107 while ((pdp
= pdp
->prev
) != NULL
) {
109 while (pdat
!= NULL
&&
110 pdat
->layout
->col
< dat
->layout
->col
)
114 if (pdat
->layout
->pos
!= TBL_CELL_DOWN
&&
115 strcmp(pdat
->string
, "\\^") != 0) {
123 * Count the number of horizontal spans to the right of this cell.
124 * This is purely a matter of the layout, independent of the data.
127 for (cp
= cp
->next
; cp
!= NULL
; cp
= cp
->next
)
128 if (cp
->pos
== TBL_CELL_SPAN
)
133 /* Append the new data cell to the data row. */
135 if (dp
->last
== NULL
)
138 dp
->last
->next
= dat
;
142 * Check for a continued-data scope opening. This consists of a
143 * trailing `T{' at the end of the line. Subsequent lines,
144 * until a standalone `T}', are included in our cell.
147 if (*pos
- sv
== 2 && p
[sv
] == 'T' && p
[sv
+ 1] == '{') {
148 tbl
->part
= TBL_PART_CDATA
;
152 dat
->string
= mandoc_strndup(p
+ sv
, *pos
- sv
);
157 if ( ! strcmp(dat
->string
, "_"))
158 dat
->pos
= TBL_DATA_HORIZ
;
159 else if ( ! strcmp(dat
->string
, "="))
160 dat
->pos
= TBL_DATA_DHORIZ
;
161 else if ( ! strcmp(dat
->string
, "\\_"))
162 dat
->pos
= TBL_DATA_NHORIZ
;
163 else if ( ! strcmp(dat
->string
, "\\="))
164 dat
->pos
= TBL_DATA_NDHORIZ
;
166 dat
->pos
= TBL_DATA_DATA
;
168 if ((dat
->layout
->pos
== TBL_CELL_HORIZ
||
169 dat
->layout
->pos
== TBL_CELL_DHORIZ
||
170 dat
->layout
->pos
== TBL_CELL_DOWN
) &&
171 dat
->pos
== TBL_DATA_DATA
&& *dat
->string
!= '\0')
172 mandoc_msg(MANDOCERR_TBLDATA_SPAN
,
173 tbl
->parse
, ln
, sv
, dat
->string
);
177 tbl_cdata(struct tbl_node
*tbl
, int ln
, const char *p
, int pos
)
182 dat
= tbl
->last_span
->last
;
184 if (p
[pos
] == 'T' && p
[pos
+ 1] == '}') {
186 if (p
[pos
] == tbl
->opts
.tab
) {
187 tbl
->part
= TBL_PART_DATA
;
189 while (p
[pos
] != '\0')
190 getdata(tbl
, tbl
->last_span
, ln
, p
, &pos
);
192 } else if (p
[pos
] == '\0') {
193 tbl
->part
= TBL_PART_DATA
;
197 /* Fallthrough: T} is part of a word. */
200 dat
->pos
= TBL_DATA_DATA
;
203 if (dat
->string
!= NULL
) {
204 sz
= strlen(p
+ pos
) + strlen(dat
->string
) + 2;
205 dat
->string
= mandoc_realloc(dat
->string
, sz
);
206 (void)strlcat(dat
->string
, " ", sz
);
207 (void)strlcat(dat
->string
, p
+ pos
, sz
);
209 dat
->string
= mandoc_strdup(p
+ pos
);
211 if (dat
->layout
->pos
== TBL_CELL_DOWN
)
212 mandoc_msg(MANDOCERR_TBLDATA_SPAN
, tbl
->parse
,
213 ln
, pos
, dat
->string
);
216 static struct tbl_span
*
217 newspan(struct tbl_node
*tbl
, int line
, struct tbl_row
*rp
)
221 dp
= mandoc_calloc(1, sizeof(*dp
));
223 dp
->opts
= &tbl
->opts
;
225 dp
->prev
= tbl
->last_span
;
227 if (dp
->prev
== NULL
) {
228 tbl
->first_span
= dp
;
229 tbl
->current_span
= NULL
;
238 tbl_data(struct tbl_node
*tbl
, int ln
, const char *p
, int pos
)
244 rp
= (sp
= tbl
->last_span
) == NULL
? tbl
->first_row
:
245 sp
->pos
== TBL_SPAN_DATA
&& sp
->layout
->next
!= NULL
?
246 sp
->layout
->next
: sp
->layout
;
250 if ( ! strcmp(p
, "_")) {
251 sp
= newspan(tbl
, ln
, rp
);
252 sp
->pos
= TBL_SPAN_HORIZ
;
254 } else if ( ! strcmp(p
, "=")) {
255 sp
= newspan(tbl
, ln
, rp
);
256 sp
->pos
= TBL_SPAN_DHORIZ
;
261 * If the layout row contains nothing but horizontal lines,
262 * allocate an empty span for it and assign the current span
263 * to the next layout row accepting data.
266 while (rp
->next
!= NULL
) {
267 if (rp
->last
->col
+ 1 < tbl
->opts
.cols
)
269 for (cp
= rp
->first
; cp
!= NULL
; cp
= cp
->next
)
270 if (cp
->pos
!= TBL_CELL_HORIZ
&&
271 cp
->pos
!= TBL_CELL_DHORIZ
)
275 sp
= newspan(tbl
, ln
, rp
);
276 sp
->pos
= TBL_SPAN_DATA
;
280 /* Process a real data row. */
282 sp
= newspan(tbl
, ln
, rp
);
283 sp
->pos
= TBL_SPAN_DATA
;
284 while (p
[pos
] != '\0')
285 getdata(tbl
, sp
, ln
, p
, &pos
);