]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_data.c
2502672184f49bc70eae1ca88ee8f9232c57a251
1 /* $Id: tbl_data.c,v 1.42 2017/06/08 18:11:22 schwarze Exp $ */
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2015, 2017 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>
29 #include "mandoc_aux.h"
30 #include "libmandoc.h"
33 static void getdata(struct tbl_node
*, struct tbl_span
*,
34 int, const char *, int *);
35 static struct tbl_span
*newspan(struct tbl_node
*, int,
40 getdata(struct tbl_node
*tbl
, struct tbl_span
*dp
,
41 int ln
, const char *p
, int *pos
)
47 /* Advance to the next layout cell, skipping spanners. */
49 cp
= dp
->last
== NULL
? dp
->layout
->first
: dp
->last
->layout
->next
;
50 while (cp
!= NULL
&& cp
->pos
== TBL_CELL_SPAN
)
54 * Stop processing when we reach the end of the available layout
55 * cells. This means that we have extra input.
59 mandoc_msg(MANDOCERR_TBLDATA_EXTRA
, tbl
->parse
,
61 /* Skip to the end... */
67 dat
= mandoc_calloc(1, sizeof(*dat
));
69 dat
->pos
= TBL_DATA_NONE
;
71 for (cp
= cp
->next
; cp
!= NULL
; cp
= cp
->next
)
72 if (cp
->pos
== TBL_CELL_SPAN
)
84 while (p
[*pos
] && p
[*pos
] != tbl
->opts
.tab
)
88 * Check for a continued-data scope opening. This consists of a
89 * trailing `T{' at the end of the line. Subsequent lines,
90 * until a standalone `T}', are included in our cell.
93 if (*pos
- sv
== 2 && p
[sv
] == 'T' && p
[sv
+ 1] == '{') {
94 tbl
->part
= TBL_PART_CDATA
;
98 dat
->string
= mandoc_strndup(p
+ sv
, *pos
- sv
);
103 if ( ! strcmp(dat
->string
, "_"))
104 dat
->pos
= TBL_DATA_HORIZ
;
105 else if ( ! strcmp(dat
->string
, "="))
106 dat
->pos
= TBL_DATA_DHORIZ
;
107 else if ( ! strcmp(dat
->string
, "\\_"))
108 dat
->pos
= TBL_DATA_NHORIZ
;
109 else if ( ! strcmp(dat
->string
, "\\="))
110 dat
->pos
= TBL_DATA_NDHORIZ
;
112 dat
->pos
= TBL_DATA_DATA
;
114 if ((dat
->layout
->pos
== TBL_CELL_HORIZ
||
115 dat
->layout
->pos
== TBL_CELL_DHORIZ
||
116 dat
->layout
->pos
== TBL_CELL_DOWN
) &&
117 dat
->pos
== TBL_DATA_DATA
&& *dat
->string
!= '\0')
118 mandoc_msg(MANDOCERR_TBLDATA_SPAN
,
119 tbl
->parse
, ln
, sv
, dat
->string
);
123 tbl_cdata(struct tbl_node
*tbl
, int ln
, const char *p
, int pos
)
128 dat
= tbl
->last_span
->last
;
130 if (p
[pos
] == 'T' && p
[pos
+ 1] == '}') {
132 if (p
[pos
] == tbl
->opts
.tab
) {
133 tbl
->part
= TBL_PART_DATA
;
135 while (p
[pos
] != '\0')
136 getdata(tbl
, tbl
->last_span
, ln
, p
, &pos
);
138 } else if (p
[pos
] == '\0') {
139 tbl
->part
= TBL_PART_DATA
;
143 /* Fallthrough: T} is part of a word. */
146 dat
->pos
= TBL_DATA_DATA
;
149 if (dat
->string
!= NULL
) {
150 sz
= strlen(p
+ pos
) + strlen(dat
->string
) + 2;
151 dat
->string
= mandoc_realloc(dat
->string
, sz
);
152 (void)strlcat(dat
->string
, " ", sz
);
153 (void)strlcat(dat
->string
, p
+ pos
, sz
);
155 dat
->string
= mandoc_strdup(p
+ pos
);
157 if (dat
->layout
->pos
== TBL_CELL_DOWN
)
158 mandoc_msg(MANDOCERR_TBLDATA_SPAN
, tbl
->parse
,
159 ln
, pos
, dat
->string
);
164 static struct tbl_span
*
165 newspan(struct tbl_node
*tbl
, int line
, struct tbl_row
*rp
)
169 dp
= mandoc_calloc(1, sizeof(*dp
));
171 dp
->opts
= &tbl
->opts
;
173 dp
->prev
= tbl
->last_span
;
175 if (dp
->prev
== NULL
) {
176 tbl
->first_span
= dp
;
177 tbl
->current_span
= NULL
;
186 tbl_data(struct tbl_node
*tbl
, int ln
, const char *p
, int pos
)
192 * Choose a layout row: take the one following the last parsed
193 * span's. If that doesn't exist, use the last parsed span's.
194 * If there's no last parsed span, use the first row. Lastly,
195 * if the last span was a horizontal line, use the same layout
196 * (it doesn't "consume" the layout).
199 if (tbl
->last_span
!= NULL
) {
200 if (tbl
->last_span
->pos
== TBL_SPAN_DATA
) {
201 for (rp
= tbl
->last_span
->layout
->next
;
202 rp
!= NULL
&& rp
->first
!= NULL
;
204 switch (rp
->first
->pos
) {
206 dp
= newspan(tbl
, ln
, rp
);
207 dp
->pos
= TBL_SPAN_HORIZ
;
209 case TBL_CELL_DHORIZ
:
210 dp
= newspan(tbl
, ln
, rp
);
211 dp
->pos
= TBL_SPAN_DHORIZ
;
219 rp
= tbl
->last_span
->layout
;
222 rp
= tbl
->last_span
->layout
;
228 dp
= newspan(tbl
, ln
, rp
);
230 if ( ! strcmp(p
, "_")) {
231 dp
->pos
= TBL_SPAN_HORIZ
;
233 } else if ( ! strcmp(p
, "=")) {
234 dp
->pos
= TBL_SPAN_DHORIZ
;
238 dp
->pos
= TBL_SPAN_DATA
;
240 while (p
[pos
] != '\0')
241 getdata(tbl
, dp
, ln
, p
, &pos
);