]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_data.c
1 /* $Id: tbl_data.c,v 1.18 2011/01/10 15:31:00 kristaps Exp $ */
3 * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 #include "libmandoc.h"
31 static int data(struct tbl_node
*, struct tbl_span
*,
32 int, const char *, int *);
35 data(struct tbl_node
*tbl
, struct tbl_span
*dp
,
36 int ln
, const char *p
, int *pos
)
43 if (dp
->last
&& dp
->last
->layout
)
44 cp
= dp
->last
->layout
->next
;
45 else if (NULL
== dp
->last
)
46 cp
= dp
->layout
->first
;
49 * Skip over spanners and vertical lines to data formats, since
50 * we want to match data with data layout cells in the header.
53 while (cp
&& (TBL_CELL_VERT
== cp
->pos
||
54 TBL_CELL_DVERT
== cp
->pos
||
55 TBL_CELL_SPAN
== cp
->pos
))
59 * Stop processing when we reach the end of the available layout
60 * cells. This means that we have extra input.
64 TBL_MSG(tbl
, MANDOCERR_TBLEXTRADAT
, ln
, *pos
);
65 /* Skip to the end... */
71 dat
= mandoc_calloc(1, sizeof(struct tbl_dat
));
73 dat
->pos
= TBL_DATA_NONE
;
75 assert(TBL_CELL_SPAN
!= cp
->pos
);
77 for (spans
= 0, cp
= cp
->next
; cp
; cp
= cp
->next
)
78 if (TBL_CELL_SPAN
== cp
->pos
)
89 dp
->last
= dp
->first
= dat
;
92 while (p
[*pos
] && p
[*pos
] != tbl
->opts
.tab
)
96 * Check for a continued-data scope opening. This consists of a
97 * trailing `T{' at the end of the line. Subsequent lines,
98 * until a standalone `T}', are included in our cell.
101 if (*pos
- sv
== 2 && 'T' == p
[sv
] && '{' == p
[sv
+ 1]) {
102 tbl
->part
= TBL_PART_CDATA
;
106 dat
->string
= mandoc_malloc(*pos
- sv
+ 1);
107 memcpy(dat
->string
, &p
[sv
], *pos
- sv
);
108 dat
->string
[*pos
- sv
] = '\0';
113 if ( ! strcmp(dat
->string
, "_"))
114 dat
->pos
= TBL_DATA_HORIZ
;
115 else if ( ! strcmp(dat
->string
, "="))
116 dat
->pos
= TBL_DATA_DHORIZ
;
117 else if ( ! strcmp(dat
->string
, "\\_"))
118 dat
->pos
= TBL_DATA_NHORIZ
;
119 else if ( ! strcmp(dat
->string
, "\\="))
120 dat
->pos
= TBL_DATA_NDHORIZ
;
122 dat
->pos
= TBL_DATA_DATA
;
124 if (TBL_CELL_HORIZ
== dat
->layout
->pos
||
125 TBL_CELL_DHORIZ
== dat
->layout
->pos
)
126 if (TBL_DATA_DATA
== dat
->pos
&& '\0' != *dat
->string
)
127 TBL_MSG(tbl
, MANDOCERR_TBLIGNDATA
, ln
, sv
);
134 tbl_cdata(struct tbl_node
*tbl
, int ln
, const char *p
)
142 dat
= tbl
->last_span
->last
;
144 if (p
[pos
] == 'T' && p
[pos
+ 1] == '}') {
146 if (p
[pos
] == tbl
->opts
.tab
) {
147 tbl
->part
= TBL_PART_DATA
;
149 return(data(tbl
, tbl
->last_span
, ln
, p
, &pos
));
150 } else if ('\0' == p
[pos
]) {
151 tbl
->part
= TBL_PART_DATA
;
155 /* Fallthrough: T} is part of a word. */
158 dat
->pos
= TBL_DATA_DATA
;
161 sz
= strlen(p
) + strlen(dat
->string
) + 2;
162 dat
->string
= mandoc_realloc(dat
->string
, sz
);
163 strlcat(dat
->string
, " ", sz
);
164 strlcat(dat
->string
, p
, sz
);
166 dat
->string
= mandoc_strdup(p
);
172 tbl_data(struct tbl_node
*tbl
, int ln
, const char *p
)
180 if ('\0' == p
[pos
]) {
181 TBL_MSG(tbl
, MANDOCERR_TBL
, ln
, pos
);
186 * Choose a layout row: take the one following the last parsed
187 * span's. If that doesn't exist, use the last parsed span's.
188 * If there's no last parsed span, use the first row. Lastly,
189 * if the last span was a horizontal line, use the same layout
190 * (it doesn't "consume" the layout).
193 if (tbl
->last_span
) {
194 assert(tbl
->last_span
->layout
);
195 if (tbl
->last_span
->pos
== TBL_SPAN_DATA
)
196 rp
= tbl
->last_span
->layout
->next
;
198 rp
= tbl
->last_span
->layout
;
201 rp
= tbl
->last_span
->layout
;
207 dp
= mandoc_calloc(1, sizeof(struct tbl_span
));
208 dp
->tbl
= &tbl
->opts
;
210 dp
->head
= tbl
->first_head
;
212 if (tbl
->last_span
) {
213 tbl
->last_span
->next
= dp
;
216 tbl
->last_span
= tbl
->first_span
= dp
;
217 dp
->flags
|= TBL_SPAN_FIRST
;
220 if ( ! strcmp(p
, "_")) {
221 dp
->pos
= TBL_SPAN_HORIZ
;
223 } else if ( ! strcmp(p
, "=")) {
224 dp
->pos
= TBL_SPAN_DHORIZ
;
228 dp
->pos
= TBL_SPAN_DATA
;
230 /* This returns 0 when TBL_PART_CDATA is entered. */
232 while ('\0' != p
[pos
])
233 if ( ! data(tbl
, dp
, ln
, p
, &pos
))