xref: /illumos-gate/usr/src/cmd/mandoc/tbl_data.c (revision fc8ae2ec4282de7ec96f48e11078345f3dc0ac3d)
1 /*	$Id: tbl_data.c,v 1.45 2017/07/08 17:52:50 schwarze Exp $ */
2 /*
3  * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4  * Copyright (c) 2011, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
5  *
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.
9  *
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.
17  */
18 #include "config.h"
19 
20 #include <sys/types.h>
21 
22 #include <assert.h>
23 #include <ctype.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 
28 #include "mandoc.h"
29 #include "mandoc_aux.h"
30 #include "libmandoc.h"
31 #include "libroff.h"
32 
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,
36 				struct tbl_row *);
37 
38 
39 static void
40 getdata(struct tbl_node *tbl, struct tbl_span *dp,
41 		int ln, const char *p, int *pos)
42 {
43 	struct tbl_dat	*dat;
44 	struct tbl_cell	*cp;
45 	int		 sv;
46 
47 	/* Advance to the next layout cell, skipping spanners. */
48 
49 	cp = dp->last == NULL ? dp->layout->first : dp->last->layout->next;
50 	while (cp != NULL && cp->pos == TBL_CELL_SPAN)
51 		cp = cp->next;
52 
53 	/*
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.
58 	 */
59 
60 	if (cp == NULL) {
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;
67 		} else {
68 			mandoc_msg(MANDOCERR_TBLDATA_EXTRA, tbl->parse,
69 			    ln, *pos, p + *pos);
70 			while (p[*pos])
71 				(*pos)++;
72 			return;
73 		}
74 	}
75 
76 	dat = mandoc_calloc(1, sizeof(*dat));
77 	dat->layout = cp;
78 	dat->pos = TBL_DATA_NONE;
79 	dat->spans = 0;
80 	for (cp = cp->next; cp != NULL; cp = cp->next)
81 		if (cp->pos == TBL_CELL_SPAN)
82 			dat->spans++;
83 		else
84 			break;
85 
86 	if (dp->last == NULL)
87 		dp->first = dat;
88 	else
89 		dp->last->next = dat;
90 	dp->last = dat;
91 
92 	sv = *pos;
93 	while (p[*pos] && p[*pos] != tbl->opts.tab)
94 		(*pos)++;
95 
96 	/*
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.
100 	 */
101 
102 	if (*pos - sv == 2 && p[sv] == 'T' && p[sv + 1] == '{') {
103 		tbl->part = TBL_PART_CDATA;
104 		return;
105 	}
106 
107 	dat->string = mandoc_strndup(p + sv, *pos - sv);
108 
109 	if (p[*pos])
110 		(*pos)++;
111 
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;
120 	else
121 		dat->pos = TBL_DATA_DATA;
122 
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);
129 }
130 
131 void
132 tbl_cdata(struct tbl_node *tbl, int ln, const char *p, int pos)
133 {
134 	struct tbl_dat	*dat;
135 	size_t		 sz;
136 
137 	dat = tbl->last_span->last;
138 
139 	if (p[pos] == 'T' && p[pos + 1] == '}') {
140 		pos += 2;
141 		if (p[pos] == tbl->opts.tab) {
142 			tbl->part = TBL_PART_DATA;
143 			pos++;
144 			while (p[pos] != '\0')
145 				getdata(tbl, tbl->last_span, ln, p, &pos);
146 			return;
147 		} else if (p[pos] == '\0') {
148 			tbl->part = TBL_PART_DATA;
149 			return;
150 		}
151 
152 		/* Fallthrough: T} is part of a word. */
153 	}
154 
155 	dat->pos = TBL_DATA_DATA;
156 	dat->block = 1;
157 
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);
163 	} else
164 		dat->string = mandoc_strdup(p + pos);
165 
166 	if (dat->layout->pos == TBL_CELL_DOWN)
167 		mandoc_msg(MANDOCERR_TBLDATA_SPAN, tbl->parse,
168 		    ln, pos, dat->string);
169 }
170 
171 static struct tbl_span *
172 newspan(struct tbl_node *tbl, int line, struct tbl_row *rp)
173 {
174 	struct tbl_span	*dp;
175 
176 	dp = mandoc_calloc(1, sizeof(*dp));
177 	dp->line = line;
178 	dp->opts = &tbl->opts;
179 	dp->layout = rp;
180 	dp->prev = tbl->last_span;
181 
182 	if (dp->prev == NULL) {
183 		tbl->first_span = dp;
184 		tbl->current_span = NULL;
185 	} else
186 		dp->prev->next = dp;
187 	tbl->last_span = dp;
188 
189 	return dp;
190 }
191 
192 void
193 tbl_data(struct tbl_node *tbl, int ln, const char *p, int pos)
194 {
195 	struct tbl_row	*rp;
196 	struct tbl_cell	*cp;
197 	struct tbl_span	*sp;
198 
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;
202 
203 	assert(rp != NULL);
204 
205 	if ( ! strcmp(p, "_")) {
206 		sp = newspan(tbl, ln, rp);
207 		sp->pos = TBL_SPAN_HORIZ;
208 		return;
209 	} else if ( ! strcmp(p, "=")) {
210 		sp = newspan(tbl, ln, rp);
211 		sp->pos = TBL_SPAN_DHORIZ;
212 		return;
213 	}
214 
215 	/*
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.
219 	 */
220 
221 	while (rp->next != NULL) {
222 		if (rp->last->col + 1 < tbl->opts.cols)
223 			break;
224 		for (cp = rp->first; cp != NULL; cp = cp->next)
225 			if (cp->pos != TBL_CELL_HORIZ &&
226 			    cp->pos != TBL_CELL_DHORIZ)
227 				break;
228 		if (cp != NULL)
229 			break;
230 		sp = newspan(tbl, ln, rp);
231 		sp->pos = TBL_SPAN_DATA;
232 		rp = rp->next;
233 	}
234 
235 	/* Process a real data row. */
236 
237 	sp = newspan(tbl, ln, rp);
238 	sp->pos = TBL_SPAN_DATA;
239 	while (p[pos] != '\0')
240 		getdata(tbl, sp, ln, p, &pos);
241 }
242