1*0a6a1f1dSLionel Sambuc /* Id: tbl_data.c,v 1.28 2014/01/05 18:37:53 joerg Exp */
2d65f6f70SBen Gras /*
392395e9cSLionel Sambuc * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
492395e9cSLionel Sambuc * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
5d65f6f70SBen Gras *
6d65f6f70SBen Gras * Permission to use, copy, modify, and distribute this software for any
7d65f6f70SBen Gras * purpose with or without fee is hereby granted, provided that the above
8d65f6f70SBen Gras * copyright notice and this permission notice appear in all copies.
9d65f6f70SBen Gras *
10d65f6f70SBen Gras * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11d65f6f70SBen Gras * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12d65f6f70SBen Gras * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13d65f6f70SBen Gras * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14d65f6f70SBen Gras * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15d65f6f70SBen Gras * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16d65f6f70SBen Gras * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17d65f6f70SBen Gras */
18d65f6f70SBen Gras #ifdef HAVE_CONFIG_H
19d65f6f70SBen Gras #include "config.h"
20d65f6f70SBen Gras #endif
21d65f6f70SBen Gras
22d65f6f70SBen Gras #include <assert.h>
23d65f6f70SBen Gras #include <ctype.h>
24d65f6f70SBen Gras #include <stdlib.h>
25d65f6f70SBen Gras #include <string.h>
26d65f6f70SBen Gras #include <time.h>
27d65f6f70SBen Gras
28d65f6f70SBen Gras #include "mandoc.h"
29d65f6f70SBen Gras #include "libmandoc.h"
30d65f6f70SBen Gras #include "libroff.h"
31d65f6f70SBen Gras
3284d9c625SLionel Sambuc static int getdata(struct tbl_node *, struct tbl_span *,
33d65f6f70SBen Gras int, const char *, int *);
3492395e9cSLionel Sambuc static struct tbl_span *newspan(struct tbl_node *, int,
3592395e9cSLionel Sambuc struct tbl_row *);
36d65f6f70SBen Gras
37d65f6f70SBen Gras static int
getdata(struct tbl_node * tbl,struct tbl_span * dp,int ln,const char * p,int * pos)3884d9c625SLionel Sambuc getdata(struct tbl_node *tbl, struct tbl_span *dp,
39d65f6f70SBen Gras int ln, const char *p, int *pos)
40d65f6f70SBen Gras {
41d65f6f70SBen Gras struct tbl_dat *dat;
42d65f6f70SBen Gras struct tbl_cell *cp;
4392395e9cSLionel Sambuc int sv, spans;
44d65f6f70SBen Gras
45d65f6f70SBen Gras cp = NULL;
46d65f6f70SBen Gras if (dp->last && dp->last->layout)
47d65f6f70SBen Gras cp = dp->last->layout->next;
48d65f6f70SBen Gras else if (NULL == dp->last)
49d65f6f70SBen Gras cp = dp->layout->first;
50d65f6f70SBen Gras
51d65f6f70SBen Gras /*
52*0a6a1f1dSLionel Sambuc * Skip over spanners, since
53d65f6f70SBen Gras * we want to match data with data layout cells in the header.
54d65f6f70SBen Gras */
55d65f6f70SBen Gras
56*0a6a1f1dSLionel Sambuc while (cp && TBL_CELL_SPAN == cp->pos)
57d65f6f70SBen Gras cp = cp->next;
58d65f6f70SBen Gras
5992395e9cSLionel Sambuc /*
6092395e9cSLionel Sambuc * Stop processing when we reach the end of the available layout
6192395e9cSLionel Sambuc * cells. This means that we have extra input.
6292395e9cSLionel Sambuc */
6392395e9cSLionel Sambuc
6492395e9cSLionel Sambuc if (NULL == cp) {
6592395e9cSLionel Sambuc mandoc_msg(MANDOCERR_TBLEXTRADAT,
6692395e9cSLionel Sambuc tbl->parse, ln, *pos, NULL);
6792395e9cSLionel Sambuc /* Skip to the end... */
6892395e9cSLionel Sambuc while (p[*pos])
6992395e9cSLionel Sambuc (*pos)++;
7092395e9cSLionel Sambuc return(1);
7192395e9cSLionel Sambuc }
7292395e9cSLionel Sambuc
73d65f6f70SBen Gras dat = mandoc_calloc(1, sizeof(struct tbl_dat));
74d65f6f70SBen Gras dat->layout = cp;
75d65f6f70SBen Gras dat->pos = TBL_DATA_NONE;
76d65f6f70SBen Gras
7792395e9cSLionel Sambuc assert(TBL_CELL_SPAN != cp->pos);
7892395e9cSLionel Sambuc
7992395e9cSLionel Sambuc for (spans = 0, cp = cp->next; cp; cp = cp->next)
8092395e9cSLionel Sambuc if (TBL_CELL_SPAN == cp->pos)
8192395e9cSLionel Sambuc spans++;
8292395e9cSLionel Sambuc else
8392395e9cSLionel Sambuc break;
8492395e9cSLionel Sambuc
8592395e9cSLionel Sambuc dat->spans = spans;
86d65f6f70SBen Gras
87d65f6f70SBen Gras if (dp->last) {
88d65f6f70SBen Gras dp->last->next = dat;
89d65f6f70SBen Gras dp->last = dat;
90d65f6f70SBen Gras } else
91d65f6f70SBen Gras dp->last = dp->first = dat;
92d65f6f70SBen Gras
93d65f6f70SBen Gras sv = *pos;
94d65f6f70SBen Gras while (p[*pos] && p[*pos] != tbl->opts.tab)
95d65f6f70SBen Gras (*pos)++;
96d65f6f70SBen Gras
97d65f6f70SBen Gras /*
98d65f6f70SBen Gras * Check for a continued-data scope opening. This consists of a
99d65f6f70SBen Gras * trailing `T{' at the end of the line. Subsequent lines,
100d65f6f70SBen Gras * until a standalone `T}', are included in our cell.
101d65f6f70SBen Gras */
102d65f6f70SBen Gras
103d65f6f70SBen Gras if (*pos - sv == 2 && 'T' == p[sv] && '{' == p[sv + 1]) {
104d65f6f70SBen Gras tbl->part = TBL_PART_CDATA;
105*0a6a1f1dSLionel Sambuc return(1);
106d65f6f70SBen Gras }
107d65f6f70SBen Gras
10892395e9cSLionel Sambuc assert(*pos - sv >= 0);
10992395e9cSLionel Sambuc
11092395e9cSLionel Sambuc dat->string = mandoc_malloc((size_t)(*pos - sv + 1));
11192395e9cSLionel Sambuc memcpy(dat->string, &p[sv], (size_t)(*pos - sv));
112d65f6f70SBen Gras dat->string[*pos - sv] = '\0';
113d65f6f70SBen Gras
114d65f6f70SBen Gras if (p[*pos])
115d65f6f70SBen Gras (*pos)++;
116d65f6f70SBen Gras
117d65f6f70SBen Gras if ( ! strcmp(dat->string, "_"))
118d65f6f70SBen Gras dat->pos = TBL_DATA_HORIZ;
119d65f6f70SBen Gras else if ( ! strcmp(dat->string, "="))
120d65f6f70SBen Gras dat->pos = TBL_DATA_DHORIZ;
121d65f6f70SBen Gras else if ( ! strcmp(dat->string, "\\_"))
122d65f6f70SBen Gras dat->pos = TBL_DATA_NHORIZ;
123d65f6f70SBen Gras else if ( ! strcmp(dat->string, "\\="))
124d65f6f70SBen Gras dat->pos = TBL_DATA_NDHORIZ;
125d65f6f70SBen Gras else
126d65f6f70SBen Gras dat->pos = TBL_DATA_DATA;
127d65f6f70SBen Gras
128d65f6f70SBen Gras if (TBL_CELL_HORIZ == dat->layout->pos ||
12992395e9cSLionel Sambuc TBL_CELL_DHORIZ == dat->layout->pos ||
13092395e9cSLionel Sambuc TBL_CELL_DOWN == dat->layout->pos)
131d65f6f70SBen Gras if (TBL_DATA_DATA == dat->pos && '\0' != *dat->string)
13292395e9cSLionel Sambuc mandoc_msg(MANDOCERR_TBLIGNDATA,
13392395e9cSLionel Sambuc tbl->parse, ln, sv, NULL);
134d65f6f70SBen Gras
135d65f6f70SBen Gras return(1);
136d65f6f70SBen Gras }
137d65f6f70SBen Gras
138d65f6f70SBen Gras /* ARGSUSED */
139d65f6f70SBen Gras int
tbl_cdata(struct tbl_node * tbl,int ln,const char * p)140d65f6f70SBen Gras tbl_cdata(struct tbl_node *tbl, int ln, const char *p)
141d65f6f70SBen Gras {
142d65f6f70SBen Gras struct tbl_dat *dat;
143d65f6f70SBen Gras size_t sz;
144d65f6f70SBen Gras int pos;
145d65f6f70SBen Gras
146d65f6f70SBen Gras pos = 0;
147d65f6f70SBen Gras
148d65f6f70SBen Gras dat = tbl->last_span->last;
149d65f6f70SBen Gras
150d65f6f70SBen Gras if (p[pos] == 'T' && p[pos + 1] == '}') {
151d65f6f70SBen Gras pos += 2;
152d65f6f70SBen Gras if (p[pos] == tbl->opts.tab) {
153d65f6f70SBen Gras tbl->part = TBL_PART_DATA;
154d65f6f70SBen Gras pos++;
15584d9c625SLionel Sambuc return(getdata(tbl, tbl->last_span, ln, p, &pos));
156d65f6f70SBen Gras } else if ('\0' == p[pos]) {
157d65f6f70SBen Gras tbl->part = TBL_PART_DATA;
158d65f6f70SBen Gras return(1);
159d65f6f70SBen Gras }
160d65f6f70SBen Gras
161d65f6f70SBen Gras /* Fallthrough: T} is part of a word. */
162d65f6f70SBen Gras }
163d65f6f70SBen Gras
16492395e9cSLionel Sambuc dat->pos = TBL_DATA_DATA;
16592395e9cSLionel Sambuc
166d65f6f70SBen Gras if (dat->string) {
167d65f6f70SBen Gras sz = strlen(p) + strlen(dat->string) + 2;
168d65f6f70SBen Gras dat->string = mandoc_realloc(dat->string, sz);
169d65f6f70SBen Gras strlcat(dat->string, " ", sz);
170d65f6f70SBen Gras strlcat(dat->string, p, sz);
171d65f6f70SBen Gras } else
172d65f6f70SBen Gras dat->string = mandoc_strdup(p);
173d65f6f70SBen Gras
17492395e9cSLionel Sambuc if (TBL_CELL_DOWN == dat->layout->pos)
17592395e9cSLionel Sambuc mandoc_msg(MANDOCERR_TBLIGNDATA,
17692395e9cSLionel Sambuc tbl->parse, ln, pos, NULL);
17792395e9cSLionel Sambuc
178d65f6f70SBen Gras return(0);
179d65f6f70SBen Gras }
180d65f6f70SBen Gras
18192395e9cSLionel Sambuc static struct tbl_span *
newspan(struct tbl_node * tbl,int line,struct tbl_row * rp)18292395e9cSLionel Sambuc newspan(struct tbl_node *tbl, int line, struct tbl_row *rp)
18392395e9cSLionel Sambuc {
18492395e9cSLionel Sambuc struct tbl_span *dp;
18592395e9cSLionel Sambuc
18692395e9cSLionel Sambuc dp = mandoc_calloc(1, sizeof(struct tbl_span));
18792395e9cSLionel Sambuc dp->line = line;
188*0a6a1f1dSLionel Sambuc dp->opts = &tbl->opts;
18992395e9cSLionel Sambuc dp->layout = rp;
19092395e9cSLionel Sambuc dp->head = tbl->first_head;
19192395e9cSLionel Sambuc
19292395e9cSLionel Sambuc if (tbl->last_span) {
19392395e9cSLionel Sambuc tbl->last_span->next = dp;
19492395e9cSLionel Sambuc tbl->last_span = dp;
19592395e9cSLionel Sambuc } else {
19692395e9cSLionel Sambuc tbl->last_span = tbl->first_span = dp;
19792395e9cSLionel Sambuc tbl->current_span = NULL;
19892395e9cSLionel Sambuc dp->flags |= TBL_SPAN_FIRST;
19992395e9cSLionel Sambuc }
20092395e9cSLionel Sambuc
20192395e9cSLionel Sambuc return(dp);
20292395e9cSLionel Sambuc }
20392395e9cSLionel Sambuc
204d65f6f70SBen Gras int
tbl_data(struct tbl_node * tbl,int ln,const char * p)205d65f6f70SBen Gras tbl_data(struct tbl_node *tbl, int ln, const char *p)
206d65f6f70SBen Gras {
207d65f6f70SBen Gras struct tbl_span *dp;
208d65f6f70SBen Gras struct tbl_row *rp;
209d65f6f70SBen Gras int pos;
210d65f6f70SBen Gras
211d65f6f70SBen Gras pos = 0;
212d65f6f70SBen Gras
213d65f6f70SBen Gras if ('\0' == p[pos]) {
21492395e9cSLionel Sambuc mandoc_msg(MANDOCERR_TBL, tbl->parse, ln, pos, NULL);
215d65f6f70SBen Gras return(0);
216d65f6f70SBen Gras }
217d65f6f70SBen Gras
218d65f6f70SBen Gras /*
219d65f6f70SBen Gras * Choose a layout row: take the one following the last parsed
220d65f6f70SBen Gras * span's. If that doesn't exist, use the last parsed span's.
221d65f6f70SBen Gras * If there's no last parsed span, use the first row. Lastly,
222d65f6f70SBen Gras * if the last span was a horizontal line, use the same layout
223d65f6f70SBen Gras * (it doesn't "consume" the layout).
224d65f6f70SBen Gras */
225d65f6f70SBen Gras
226d65f6f70SBen Gras if (tbl->last_span) {
227d65f6f70SBen Gras assert(tbl->last_span->layout);
22892395e9cSLionel Sambuc if (tbl->last_span->pos == TBL_SPAN_DATA) {
22992395e9cSLionel Sambuc for (rp = tbl->last_span->layout->next;
23092395e9cSLionel Sambuc rp && rp->first; rp = rp->next) {
23192395e9cSLionel Sambuc switch (rp->first->pos) {
23292395e9cSLionel Sambuc case (TBL_CELL_HORIZ):
23392395e9cSLionel Sambuc dp = newspan(tbl, ln, rp);
23492395e9cSLionel Sambuc dp->pos = TBL_SPAN_HORIZ;
23592395e9cSLionel Sambuc continue;
23692395e9cSLionel Sambuc case (TBL_CELL_DHORIZ):
23792395e9cSLionel Sambuc dp = newspan(tbl, ln, rp);
23892395e9cSLionel Sambuc dp->pos = TBL_SPAN_DHORIZ;
23992395e9cSLionel Sambuc continue;
24092395e9cSLionel Sambuc default:
24192395e9cSLionel Sambuc break;
24292395e9cSLionel Sambuc }
24392395e9cSLionel Sambuc break;
24492395e9cSLionel Sambuc }
24592395e9cSLionel Sambuc } else
246d65f6f70SBen Gras rp = tbl->last_span->layout;
24792395e9cSLionel Sambuc
248d65f6f70SBen Gras if (NULL == rp)
249d65f6f70SBen Gras rp = tbl->last_span->layout;
250d65f6f70SBen Gras } else
251d65f6f70SBen Gras rp = tbl->first_row;
252d65f6f70SBen Gras
25392395e9cSLionel Sambuc assert(rp);
254d65f6f70SBen Gras
25592395e9cSLionel Sambuc dp = newspan(tbl, ln, rp);
256d65f6f70SBen Gras
257d65f6f70SBen Gras if ( ! strcmp(p, "_")) {
258d65f6f70SBen Gras dp->pos = TBL_SPAN_HORIZ;
259d65f6f70SBen Gras return(1);
260d65f6f70SBen Gras } else if ( ! strcmp(p, "=")) {
261d65f6f70SBen Gras dp->pos = TBL_SPAN_DHORIZ;
262d65f6f70SBen Gras return(1);
263d65f6f70SBen Gras }
264d65f6f70SBen Gras
265d65f6f70SBen Gras dp->pos = TBL_SPAN_DATA;
266d65f6f70SBen Gras
267d65f6f70SBen Gras /* This returns 0 when TBL_PART_CDATA is entered. */
268d65f6f70SBen Gras
269d65f6f70SBen Gras while ('\0' != p[pos])
27084d9c625SLionel Sambuc if ( ! getdata(tbl, dp, ln, p, &pos))
271d65f6f70SBen Gras return(0);
272d65f6f70SBen Gras
273d65f6f70SBen Gras return(1);
274d65f6f70SBen Gras }
275