]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_data.c
1 /* $Id: tbl_data.c,v 1.11 2011/01/04 15:02: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
;
48 /* Skip over spanners to data formats. */
50 while (cp
&& (TBL_CELL_VERT
== cp
->pos
||
51 TBL_CELL_DVERT
== cp
->pos
))
54 dat
= mandoc_calloc(1, sizeof(struct tbl_dat
));
56 dat
->pos
= TBL_DATA_NONE
;
58 if (NULL
== dat
->layout
)
59 TBL_MSG(tbl
, MANDOCERR_TBLEXTRADAT
, ln
, *pos
);
65 dp
->last
= dp
->first
= dat
;
68 while (p
[*pos
] && p
[*pos
] != tbl
->opts
.tab
)
72 * Check for a continued-data scope opening. This consists of a
73 * trailing `T{' at the end of the line. Subsequent lines,
74 * until a standalone `T}', are included in our cell.
77 if (*pos
- sv
== 2 && 'T' == p
[sv
] && '{' == p
[sv
+ 1]) {
78 tbl
->part
= TBL_PART_CDATA
;
82 dat
->string
= mandoc_malloc(*pos
- sv
+ 1);
83 memcpy(dat
->string
, &p
[sv
], *pos
- sv
);
84 dat
->string
[*pos
- sv
] = '\0';
89 if ( ! strcmp(dat
->string
, "_"))
90 dat
->pos
= TBL_DATA_HORIZ
;
91 else if ( ! strcmp(dat
->string
, "="))
92 dat
->pos
= TBL_DATA_DHORIZ
;
93 else if ( ! strcmp(dat
->string
, "\\_"))
94 dat
->pos
= TBL_DATA_NHORIZ
;
95 else if ( ! strcmp(dat
->string
, "\\="))
96 dat
->pos
= TBL_DATA_NDHORIZ
;
98 dat
->pos
= TBL_DATA_DATA
;
100 if (NULL
== dat
->layout
)
103 if (TBL_CELL_HORIZ
== dat
->layout
->pos
||
104 TBL_CELL_DHORIZ
== dat
->layout
->pos
)
105 if (TBL_DATA_DATA
== dat
->pos
&& '\0' != *dat
->string
)
106 TBL_MSG(tbl
, MANDOCERR_TBLIGNDATA
, ln
, sv
);
112 tbl_cdata(struct tbl_node
*tbl
, int ln
, const char *p
)
117 if (0 == strcmp(p
, "T}")) {
118 tbl
->part
= TBL_PART_DATA
;
122 dat
= tbl
->last_span
->last
;
123 dat
->pos
= TBL_DATA_DATA
;
126 sz
= strlen(p
) + strlen(dat
->string
) + 2;
127 dat
->string
= mandoc_realloc(dat
->string
, sz
);
128 strlcat(dat
->string
, " ", sz
);
129 strlcat(dat
->string
, p
, sz
);
131 dat
->string
= mandoc_strdup(p
);
137 tbl_data(struct tbl_node
*tbl
, int ln
, const char *p
)
145 if ('\0' == p
[pos
]) {
146 TBL_MSG(tbl
, MANDOCERR_TBL
, ln
, pos
);
151 * Choose a layout row: take the one following the last parsed
152 * span's. If that doesn't exist, use the last parsed span's.
153 * If there's no last parsed span, use the first row. This can
157 if (tbl
->last_span
) {
158 assert(tbl
->last_span
->layout
);
159 rp
= tbl
->last_span
->layout
->next
;
161 rp
= tbl
->last_span
->layout
;
165 dp
= mandoc_calloc(1, sizeof(struct tbl_span
));
166 dp
->tbl
= &tbl
->opts
;
168 dp
->head
= tbl
->first_head
;
170 if (tbl
->last_span
) {
171 tbl
->last_span
->next
= dp
;
174 tbl
->last_span
= tbl
->first_span
= dp
;
175 dp
->flags
|= TBL_SPAN_FIRST
;
178 if ( ! strcmp(p
, "_")) {
179 dp
->pos
= TBL_SPAN_HORIZ
;
181 } else if ( ! strcmp(p
, "=")) {
182 dp
->pos
= TBL_SPAN_DHORIZ
;
186 dp
->pos
= TBL_SPAN_DATA
;
188 /* This returns 0 when TBL_PART_CDATA is entered. */
190 while ('\0' != p
[pos
])
191 if ( ! data(tbl
, dp
, ln
, p
, &pos
))