]> git.cameronkatri.com Git - mandoc.git/blob - tbl_data.c
style message about duplicate RCS ids; inspired by mdoclint
[mandoc.git] / tbl_data.c
1 /* $Id: tbl_data.c,v 1.43 2017/06/16 20:01:06 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 int
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 1;
147 } else if (p[pos] == '\0') {
148 tbl->part = TBL_PART_DATA;
149 return 1;
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 return 0;
171 }
172
173 static struct tbl_span *
174 newspan(struct tbl_node *tbl, int line, struct tbl_row *rp)
175 {
176 struct tbl_span *dp;
177
178 dp = mandoc_calloc(1, sizeof(*dp));
179 dp->line = line;
180 dp->opts = &tbl->opts;
181 dp->layout = rp;
182 dp->prev = tbl->last_span;
183
184 if (dp->prev == NULL) {
185 tbl->first_span = dp;
186 tbl->current_span = NULL;
187 } else
188 dp->prev->next = dp;
189 tbl->last_span = dp;
190
191 return dp;
192 }
193
194 void
195 tbl_data(struct tbl_node *tbl, int ln, const char *p, int pos)
196 {
197 struct tbl_row *rp;
198 struct tbl_cell *cp;
199 struct tbl_span *sp, *spi;
200 struct tbl_dat *dp;
201 int have_data, spans;
202
203 rp = (sp = tbl->last_span) == NULL ? tbl->first_row :
204 sp->pos == TBL_SPAN_DATA && sp->layout->next != NULL ?
205 sp->layout->next : sp->layout;
206
207 assert(rp != NULL);
208
209 sp = newspan(tbl, ln, rp);
210
211 if ( ! strcmp(p, "_")) {
212 sp->pos = TBL_SPAN_HORIZ;
213 return;
214 } else if ( ! strcmp(p, "=")) {
215 sp->pos = TBL_SPAN_DHORIZ;
216 return;
217 }
218 sp->pos = TBL_SPAN_DATA;
219
220 while (p[pos] != '\0')
221 getdata(tbl, sp, ln, p, &pos);
222
223 /*
224 * If this span contains some data,
225 * make sure at least part of it gets printed.
226 */
227
228 have_data = 0;
229 cp = rp->first;
230 for (dp = sp->first; dp != NULL; dp = dp->next) {
231 if (dp->pos == TBL_DATA_DATA && *dp->string != '\0') {
232 if (cp == NULL ||
233 (cp->pos != TBL_CELL_HORIZ &&
234 cp->pos != TBL_CELL_DHORIZ))
235 return;
236 have_data = 1;
237 }
238 spans = dp->spans;
239 while (spans-- >= 0) {
240 if (cp != NULL)
241 cp = cp->next;
242 }
243 }
244 if (have_data == 0 || rp->next == NULL)
245 return;
246
247 /*
248 * There is some data, but it would all get lost
249 * due to horizontal lines in the layout.
250 * Insert an empty span to consume the layout row.
251 */
252
253 tbl->last_span = sp->prev;
254 spi = newspan(tbl, ln, rp);
255 spi->pos = TBL_SPAN_DATA;
256 spi->next = sp;
257 tbl->last_span = sp;
258 sp->prev = spi;
259 sp->layout = rp->next;
260 cp = sp->layout->first;
261 for (dp = sp->first; dp != NULL; dp = dp->next) {
262 dp->layout = cp;
263 dp->spans = 0;
264 if (cp != NULL)
265 cp = cp->next;
266 while (cp != NULL && cp->pos == TBL_CELL_SPAN) {
267 dp->spans++;
268 cp = cp->next;
269 }
270 }
271 }