1 /* $Id: tbl_data.c,v 1.14 2011/01/07 14:59:52 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))
58 dat = mandoc_calloc(1, sizeof(struct tbl_dat));
60 dat->pos = TBL_DATA_NONE;
62 if (NULL == dat->layout)
63 TBL_MSG(tbl, MANDOCERR_TBLEXTRADAT, ln, *pos);
69 dp->last = dp->first = dat;
72 while (p[*pos] && p[*pos] != tbl->opts.tab)
76 * Check for a continued-data scope opening. This consists of a
77 * trailing `T{' at the end of the line. Subsequent lines,
78 * until a standalone `T}', are included in our cell.
81 if (*pos - sv == 2 && 'T' == p[sv] && '{' == p[sv + 1]) {
82 tbl->part = TBL_PART_CDATA;
86 dat->string = mandoc_malloc(*pos - sv + 1);
87 memcpy(dat->string, &p[sv], *pos - sv);
88 dat->string[*pos - sv] = '\0';
93 if ( ! strcmp(dat->string, "_"))
94 dat->pos = TBL_DATA_HORIZ;
95 else if ( ! strcmp(dat->string, "="))
96 dat->pos = TBL_DATA_DHORIZ;
97 else if ( ! strcmp(dat->string, "\\_"))
98 dat->pos = TBL_DATA_NHORIZ;
99 else if ( ! strcmp(dat->string, "\\="))
100 dat->pos = TBL_DATA_NDHORIZ;
102 dat->pos = TBL_DATA_DATA;
104 if (NULL == dat->layout)
107 if (TBL_CELL_HORIZ == dat->layout->pos ||
108 TBL_CELL_DHORIZ == dat->layout->pos)
109 if (TBL_DATA_DATA == dat->pos && '\0' != *dat->string)
110 TBL_MSG(tbl, MANDOCERR_TBLIGNDATA, ln, sv);
117 tbl_cdata(struct tbl_node *tbl, int ln, const char *p)
125 dat = tbl->last_span->last;
126 dat->pos = TBL_DATA_DATA;
128 if (p[pos] == 'T' && p[pos + 1] == '}') {
130 if (p[pos] == tbl->opts.tab) {
131 tbl->part = TBL_PART_DATA;
133 return(data(tbl, tbl->last_span, ln, p, &pos));
134 } else if ('\0' == p[pos]) {
135 tbl->part = TBL_PART_DATA;
139 /* Fallthrough: T} is part of a word. */
143 sz = strlen(p) + strlen(dat->string) + 2;
144 dat->string = mandoc_realloc(dat->string, sz);
145 strlcat(dat->string, " ", sz);
146 strlcat(dat->string, p, sz);
148 dat->string = mandoc_strdup(p);
154 tbl_data(struct tbl_node *tbl, int ln, const char *p)
162 if ('\0' == p[pos]) {
163 TBL_MSG(tbl, MANDOCERR_TBL, ln, pos);
168 * Choose a layout row: take the one following the last parsed
169 * span's. If that doesn't exist, use the last parsed span's.
170 * If there's no last parsed span, use the first row. This can
174 if (tbl->last_span) {
175 assert(tbl->last_span->layout);
176 rp = tbl->last_span->layout->next;
178 rp = tbl->last_span->layout;
182 dp = mandoc_calloc(1, sizeof(struct tbl_span));
183 dp->tbl = &tbl->opts;
185 dp->head = tbl->first_head;
187 if (tbl->last_span) {
188 tbl->last_span->next = dp;
191 tbl->last_span = tbl->first_span = dp;
192 dp->flags |= TBL_SPAN_FIRST;
195 if ( ! strcmp(p, "_")) {
196 dp->pos = TBL_SPAN_HORIZ;
198 } else if ( ! strcmp(p, "=")) {
199 dp->pos = TBL_SPAN_DHORIZ;
203 dp->pos = TBL_SPAN_DATA;
205 /* This returns 0 when TBL_PART_CDATA is entered. */
207 while ('\0' != p[pos])
208 if ( ! data(tbl, dp, ln, p, &pos))