]> git.cameronkatri.com Git - mandoc.git/blob - tbl_layout.c
Improve -Tascii output for Unicode escape sequences: For the first 512
[mandoc.git] / tbl_layout.c
1 /* $Id: tbl_layout.c,v 1.29 2014/10/14 02:16:06 schwarze Exp $ */
2 /*
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2012, 2014 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 <ctype.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26
27 #include "mandoc.h"
28 #include "mandoc_aux.h"
29 #include "libmandoc.h"
30 #include "libroff.h"
31
32 struct tbl_phrase {
33 char name;
34 enum tbl_cellt key;
35 };
36
37 /*
38 * FIXME: we can make this parse a lot nicer by, when an error is
39 * encountered in a layout key, bailing to the next key (i.e. to the
40 * next whitespace then continuing).
41 */
42
43 #define KEYS_MAX 11
44
45 static const struct tbl_phrase keys[KEYS_MAX] = {
46 { 'c', TBL_CELL_CENTRE },
47 { 'r', TBL_CELL_RIGHT },
48 { 'l', TBL_CELL_LEFT },
49 { 'n', TBL_CELL_NUMBER },
50 { 's', TBL_CELL_SPAN },
51 { 'a', TBL_CELL_LONG },
52 { '^', TBL_CELL_DOWN },
53 { '-', TBL_CELL_HORIZ },
54 { '_', TBL_CELL_HORIZ },
55 { '=', TBL_CELL_DHORIZ }
56 };
57
58 static int mods(struct tbl_node *, struct tbl_cell *,
59 int, const char *, int *);
60 static int cell(struct tbl_node *, struct tbl_row *,
61 int, const char *, int *);
62 static void row(struct tbl_node *, int, const char *, int *);
63 static struct tbl_cell *cell_alloc(struct tbl_node *, struct tbl_row *,
64 enum tbl_cellt, int vert);
65
66
67 static int
68 mods(struct tbl_node *tbl, struct tbl_cell *cp,
69 int ln, const char *p, int *pos)
70 {
71 char buf[5];
72 int i;
73
74 /* Not all types accept modifiers. */
75
76 switch (cp->pos) {
77 case TBL_CELL_DOWN:
78 /* FALLTHROUGH */
79 case TBL_CELL_HORIZ:
80 /* FALLTHROUGH */
81 case TBL_CELL_DHORIZ:
82 return(1);
83 default:
84 break;
85 }
86
87 mod:
88 /*
89 * XXX: since, at least for now, modifiers are non-conflicting
90 * (are separable by value, regardless of position), we let
91 * modifiers come in any order. The existing tbl doesn't let
92 * this happen.
93 */
94 switch (p[*pos]) {
95 case '\0':
96 /* FALLTHROUGH */
97 case ' ':
98 /* FALLTHROUGH */
99 case '\t':
100 /* FALLTHROUGH */
101 case ',':
102 /* FALLTHROUGH */
103 case '.':
104 /* FALLTHROUGH */
105 case '|':
106 return(1);
107 default:
108 break;
109 }
110
111 /* Throw away parenthesised expression. */
112
113 if ('(' == p[*pos]) {
114 (*pos)++;
115 while (p[*pos] && ')' != p[*pos])
116 (*pos)++;
117 if (')' == p[*pos]) {
118 (*pos)++;
119 goto mod;
120 }
121 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
122 ln, *pos, NULL);
123 return(0);
124 }
125
126 /* Parse numerical spacing from modifier string. */
127
128 if (isdigit((unsigned char)p[*pos])) {
129 for (i = 0; i < 4; i++) {
130 if ( ! isdigit((unsigned char)p[*pos + i]))
131 break;
132 buf[i] = p[*pos + i];
133 }
134 buf[i] = '\0';
135
136 /* No greater than 4 digits. */
137
138 if (4 == i) {
139 mandoc_msg(MANDOCERR_TBLLAYOUT,
140 tbl->parse, ln, *pos, NULL);
141 return(0);
142 }
143
144 *pos += i;
145 cp->spacing = (size_t)atoi(buf);
146
147 goto mod;
148 /* NOTREACHED */
149 }
150
151 /* TODO: GNU has many more extensions. */
152
153 switch (tolower((unsigned char)p[(*pos)++])) {
154 case 'z':
155 cp->flags |= TBL_CELL_WIGN;
156 goto mod;
157 case 'u':
158 cp->flags |= TBL_CELL_UP;
159 goto mod;
160 case 'e':
161 cp->flags |= TBL_CELL_EQUAL;
162 goto mod;
163 case 't':
164 cp->flags |= TBL_CELL_TALIGN;
165 goto mod;
166 case 'd':
167 cp->flags |= TBL_CELL_BALIGN;
168 goto mod;
169 case 'w': /* XXX for now, ignore minimal column width */
170 goto mod;
171 case 'x':
172 cp->flags |= TBL_CELL_WMAX;
173 goto mod;
174 case 'f':
175 break;
176 case 'r':
177 /* FALLTHROUGH */
178 case 'b':
179 /* FALLTHROUGH */
180 case 'i':
181 (*pos)--;
182 break;
183 default:
184 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
185 ln, *pos - 1, NULL);
186 return(0);
187 }
188
189 switch (tolower((unsigned char)p[(*pos)++])) {
190 case '3':
191 /* FALLTHROUGH */
192 case 'b':
193 cp->flags |= TBL_CELL_BOLD;
194 goto mod;
195 case '2':
196 /* FALLTHROUGH */
197 case 'i':
198 cp->flags |= TBL_CELL_ITALIC;
199 goto mod;
200 case '1':
201 /* FALLTHROUGH */
202 case 'r':
203 goto mod;
204 default:
205 break;
206 }
207 if (isalnum((unsigned char)p[*pos - 1])) {
208 mandoc_vmsg(MANDOCERR_FT_BAD, tbl->parse,
209 ln, *pos - 1, "TS f%c", p[*pos - 1]);
210 goto mod;
211 }
212
213 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
214 ln, *pos - 1, NULL);
215 return(0);
216 }
217
218 static int
219 cell(struct tbl_node *tbl, struct tbl_row *rp,
220 int ln, const char *p, int *pos)
221 {
222 int vert, i;
223 enum tbl_cellt c;
224
225 /* Handle vertical lines. */
226
227 for (vert = 0; '|' == p[*pos]; ++*pos)
228 vert++;
229 while (' ' == p[*pos])
230 (*pos)++;
231
232 /* Handle trailing vertical lines */
233
234 if ('.' == p[*pos] || '\0' == p[*pos]) {
235 rp->vert = vert;
236 return(1);
237 }
238
239 /* Parse the column position (`c', `l', `r', ...). */
240
241 for (i = 0; i < KEYS_MAX; i++)
242 if (tolower((unsigned char)p[*pos]) == keys[i].name)
243 break;
244
245 if (KEYS_MAX == i) {
246 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
247 ln, *pos, NULL);
248 return(0);
249 }
250
251 c = keys[i].key;
252
253 /*
254 * If a span cell is found first, raise a warning and abort the
255 * parse. If a span cell is found and the last layout element
256 * isn't a "normal" layout, bail.
257 *
258 * FIXME: recover from this somehow?
259 */
260
261 if (TBL_CELL_SPAN == c) {
262 if (NULL == rp->first) {
263 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
264 ln, *pos, NULL);
265 return(0);
266 } else if (rp->last)
267 switch (rp->last->pos) {
268 case TBL_CELL_HORIZ:
269 /* FALLTHROUGH */
270 case TBL_CELL_DHORIZ:
271 mandoc_msg(MANDOCERR_TBLLAYOUT,
272 tbl->parse, ln, *pos, NULL);
273 return(0);
274 default:
275 break;
276 }
277 }
278
279 /*
280 * If a vertical spanner is found, we may not be in the first
281 * row.
282 */
283
284 if (TBL_CELL_DOWN == c && rp == tbl->first_row) {
285 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse, ln, *pos, NULL);
286 return(0);
287 }
288
289 (*pos)++;
290
291 /* Disallow adjacent spacers. */
292
293 if (vert > 2) {
294 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse, ln, *pos - 1, NULL);
295 return(0);
296 }
297
298 /* Allocate cell then parse its modifiers. */
299
300 return(mods(tbl, cell_alloc(tbl, rp, c, vert), ln, p, pos));
301 }
302
303 static void
304 row(struct tbl_node *tbl, int ln, const char *p, int *pos)
305 {
306 struct tbl_row *rp;
307
308 row: /*
309 * EBNF describing this section:
310 *
311 * row ::= row_list [:space:]* [.]?[\n]
312 * row_list ::= [:space:]* row_elem row_tail
313 * row_tail ::= [:space:]*[,] row_list |
314 * epsilon
315 * row_elem ::= [\t\ ]*[:alpha:]+
316 */
317
318 rp = mandoc_calloc(1, sizeof(struct tbl_row));
319 if (tbl->last_row)
320 tbl->last_row->next = rp;
321 else
322 tbl->first_row = rp;
323 tbl->last_row = rp;
324
325 cell:
326 while (isspace((unsigned char)p[*pos]))
327 (*pos)++;
328
329 /* Safely exit layout context. */
330
331 if ('.' == p[*pos]) {
332 tbl->part = TBL_PART_DATA;
333 if (NULL == tbl->first_row)
334 mandoc_msg(MANDOCERR_TBLNOLAYOUT,
335 tbl->parse, ln, *pos, NULL);
336 (*pos)++;
337 return;
338 }
339
340 /* End (and possibly restart) a row. */
341
342 if (',' == p[*pos]) {
343 (*pos)++;
344 goto row;
345 } else if ('\0' == p[*pos])
346 return;
347
348 if ( ! cell(tbl, rp, ln, p, pos))
349 return;
350
351 goto cell;
352 /* NOTREACHED */
353 }
354
355 int
356 tbl_layout(struct tbl_node *tbl, int ln, const char *p)
357 {
358 int pos;
359
360 pos = 0;
361 row(tbl, ln, p, &pos);
362
363 /* Always succeed. */
364 return(1);
365 }
366
367 static struct tbl_cell *
368 cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos,
369 int vert)
370 {
371 struct tbl_cell *p, *pp;
372 struct tbl_head *h, *hp;
373
374 p = mandoc_calloc(1, sizeof(struct tbl_cell));
375
376 if (NULL != (pp = rp->last)) {
377 pp->next = p;
378 h = pp->head->next;
379 } else {
380 rp->first = p;
381 h = tbl->first_head;
382 }
383 rp->last = p;
384
385 p->pos = pos;
386 p->vert = vert;
387
388 /* Re-use header. */
389
390 if (h) {
391 p->head = h;
392 return(p);
393 }
394
395 hp = mandoc_calloc(1, sizeof(struct tbl_head));
396 hp->ident = tbl->opts.cols++;
397 hp->vert = vert;
398
399 if (tbl->last_head) {
400 hp->prev = tbl->last_head;
401 tbl->last_head->next = hp;
402 } else
403 tbl->first_head = hp;
404 tbl->last_head = hp;
405
406 p->head = hp;
407 return(p);
408 }