1 /* $Id: tbl_data.c,v 1.45 2017/07/08 17:52:50 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 * If the current layout row is out of cells, allocate
55 * a new cell if another row of the table has at least
56 * this number of columns, or discard the input if we
57 * are beyond the last column of the table as a whole.
61 if (dp->layout->last->col + 1 < dp->opts->cols) {
62 cp = mandoc_calloc(1, sizeof(*cp));
63 cp->pos = TBL_CELL_LEFT;
64 dp->layout->last->next = cp;
65 cp->col = dp->layout->last->col + 1;
66 dp->layout->last = cp;
68 mandoc_msg(MANDOCERR_TBLDATA_EXTRA, tbl->parse,
76 dat = mandoc_calloc(1, sizeof(*dat));
78 dat->pos = TBL_DATA_NONE;
80 for (cp = cp->next; cp != NULL; cp = cp->next)
81 if (cp->pos == TBL_CELL_SPAN)
93 while (p[*pos] && p[*pos] != tbl->opts.tab)
97 * Check for a continued-data scope opening. This consists of a
98 * trailing `T{' at the end of the line. Subsequent lines,
99 * until a standalone `T}', are included in our cell.
102 if (*pos - sv == 2 && p[sv] == 'T' && p[sv + 1] == '{') {
103 tbl->part = TBL_PART_CDATA;
107 dat->string = mandoc_strndup(p + sv, *pos - sv);
112 if ( ! strcmp(dat->string, "_"))
113 dat->pos = TBL_DATA_HORIZ;
114 else if ( ! strcmp(dat->string, "="))
115 dat->pos = TBL_DATA_DHORIZ;
116 else if ( ! strcmp(dat->string, "\\_"))
117 dat->pos = TBL_DATA_NHORIZ;
118 else if ( ! strcmp(dat->string, "\\="))
119 dat->pos = TBL_DATA_NDHORIZ;
121 dat->pos = TBL_DATA_DATA;
123 if ((dat->layout->pos == TBL_CELL_HORIZ ||
124 dat->layout->pos == TBL_CELL_DHORIZ ||
125 dat->layout->pos == TBL_CELL_DOWN) &&
126 dat->pos == TBL_DATA_DATA && *dat->string != '\0')
127 mandoc_msg(MANDOCERR_TBLDATA_SPAN,
128 tbl->parse, ln, sv, dat->string);
132 tbl_cdata(struct tbl_node *tbl, int ln, const char *p, int pos)
137 dat = tbl->last_span->last;
139 if (p[pos] == 'T' && p[pos + 1] == '}') {
141 if (p[pos] == tbl->opts.tab) {
142 tbl->part = TBL_PART_DATA;
144 while (p[pos] != '\0')
145 getdata(tbl, tbl->last_span, ln, p, &pos);
147 } else if (p[pos] == '\0') {
148 tbl->part = TBL_PART_DATA;
152 /* Fallthrough: T} is part of a word. */
155 dat->pos = TBL_DATA_DATA;
158 if (dat->string != NULL) {
159 sz = strlen(p + pos) + strlen(dat->string) + 2;
160 dat->string = mandoc_realloc(dat->string, sz);
161 (void)strlcat(dat->string, " ", sz);
162 (void)strlcat(dat->string, p + pos, sz);
164 dat->string = mandoc_strdup(p + pos);
166 if (dat->layout->pos == TBL_CELL_DOWN)
167 mandoc_msg(MANDOCERR_TBLDATA_SPAN, tbl->parse,
168 ln, pos, dat->string);
171 static struct tbl_span *
172 newspan(struct tbl_node *tbl, int line, struct tbl_row *rp)
176 dp = mandoc_calloc(1, sizeof(*dp));
178 dp->opts = &tbl->opts;
180 dp->prev = tbl->last_span;
182 if (dp->prev == NULL) {
183 tbl->first_span = dp;
184 tbl->current_span = NULL;
193 tbl_data(struct tbl_node *tbl, int ln, const char *p, int pos)
199 rp = (sp = tbl->last_span) == NULL ? tbl->first_row :
200 sp->pos == TBL_SPAN_DATA && sp->layout->next != NULL ?
201 sp->layout->next : sp->layout;
205 if ( ! strcmp(p, "_")) {
206 sp = newspan(tbl, ln, rp);
207 sp->pos = TBL_SPAN_HORIZ;
209 } else if ( ! strcmp(p, "=")) {
210 sp = newspan(tbl, ln, rp);
211 sp->pos = TBL_SPAN_DHORIZ;
216 * If the layout row contains nothing but horizontal lines,
217 * allocate an empty span for it and assign the current span
218 * to the next layout row accepting data.
221 while (rp->next != NULL) {
222 if (rp->last->col + 1 < tbl->opts.cols)
224 for (cp = rp->first; cp != NULL; cp = cp->next)
225 if (cp->pos != TBL_CELL_HORIZ &&
226 cp->pos != TBL_CELL_DHORIZ)
230 sp = newspan(tbl, ln, rp);
231 sp->pos = TBL_SPAN_DATA;
235 /* Process a real data row. */
237 sp = newspan(tbl, ln, rp);
238 sp->pos = TBL_SPAN_DATA;
239 while (p[pos] != '\0')
240 getdata(tbl, sp, ln, p, &pos);