1 /* $Id: tbl_layout.c,v 1.30 2014/11/25 21:41:47 schwarze Exp $ */
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2012, 2014 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>
28 #include "mandoc_aux.h"
29 #include "libmandoc.h"
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).
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 }
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 struct tbl_cell *cell_alloc(struct tbl_node *, struct tbl_row *,
63 enum tbl_cellt, int vert);
67 mods(struct tbl_node *tbl, struct tbl_cell *cp,
68 int ln, const char *p, int *pos)
73 /* Not all types accept modifiers. */
88 * XXX: since, at least for now, modifiers are non-conflicting
89 * (are separable by value, regardless of position), we let
90 * modifiers come in any order. The existing tbl doesn't let
110 /* Throw away parenthesised expression. */
112 if ('(' == p[*pos]) {
114 while (p[*pos] && ')' != p[*pos])
116 if (')' == p[*pos]) {
120 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
125 /* Parse numerical spacing from modifier string. */
127 if (isdigit((unsigned char)p[*pos])) {
128 for (i = 0; i < 4; i++) {
129 if ( ! isdigit((unsigned char)p[*pos + i]))
131 buf[i] = p[*pos + i];
135 /* No greater than 4 digits. */
138 mandoc_msg(MANDOCERR_TBLLAYOUT,
139 tbl->parse, ln, *pos, NULL);
144 cp->spacing = (size_t)atoi(buf);
150 /* TODO: GNU has many more extensions. */
152 switch (tolower((unsigned char)p[(*pos)++])) {
154 cp->flags |= TBL_CELL_WIGN;
157 cp->flags |= TBL_CELL_UP;
160 cp->flags |= TBL_CELL_EQUAL;
163 cp->flags |= TBL_CELL_TALIGN;
166 cp->flags |= TBL_CELL_BALIGN;
168 case 'w': /* XXX for now, ignore minimal column width */
171 cp->flags |= TBL_CELL_WMAX;
183 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
188 switch (tolower((unsigned char)p[(*pos)++])) {
192 cp->flags |= TBL_CELL_BOLD;
197 cp->flags |= TBL_CELL_ITALIC;
206 if (isalnum((unsigned char)p[*pos - 1])) {
207 mandoc_vmsg(MANDOCERR_FT_BAD, tbl->parse,
208 ln, *pos - 1, "TS f%c", p[*pos - 1]);
212 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
218 cell(struct tbl_node *tbl, struct tbl_row *rp,
219 int ln, const char *p, int *pos)
224 /* Handle vertical lines. */
226 for (vert = 0; '|' == p[*pos]; ++*pos)
228 while (' ' == p[*pos])
231 /* Handle trailing vertical lines */
233 if ('.' == p[*pos] || '\0' == p[*pos]) {
238 /* Parse the column position (`c', `l', `r', ...). */
240 for (i = 0; i < KEYS_MAX; i++)
241 if (tolower((unsigned char)p[*pos]) == keys[i].name)
245 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
253 * If a span cell is found first, raise a warning and abort the
254 * parse. If a span cell is found and the last layout element
255 * isn't a "normal" layout, bail.
257 * FIXME: recover from this somehow?
260 if (TBL_CELL_SPAN == c) {
261 if (NULL == rp->first) {
262 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
266 switch (rp->last->pos) {
269 case TBL_CELL_DHORIZ:
270 mandoc_msg(MANDOCERR_TBLLAYOUT,
271 tbl->parse, ln, *pos, NULL);
279 * If a vertical spanner is found, we may not be in the first
283 if (TBL_CELL_DOWN == c && rp == tbl->first_row) {
284 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse, ln, *pos, NULL);
290 /* Disallow adjacent spacers. */
293 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse, ln, *pos - 1, NULL);
297 /* Allocate cell then parse its modifiers. */
299 return(mods(tbl, cell_alloc(tbl, rp, c, vert), ln, p, pos));
303 tbl_layout(struct tbl_node *tbl, int ln, const char *p)
312 /* Skip whitespace before and after each cell. */
314 while (isspace((unsigned char)p[pos]))
318 case ',': /* Next row on this input line. */
322 case '\0': /* Next row on next input line. */
324 case '.': /* End of layout. */
326 tbl->part = TBL_PART_DATA;
327 if (tbl->first_row != NULL)
329 mandoc_msg(MANDOCERR_TBLNOLAYOUT,
330 tbl->parse, ln, pos, NULL);
331 rp = mandoc_calloc(1, sizeof(*rp));
332 cell_alloc(tbl, rp, TBL_CELL_LEFT, 0);
333 tbl->first_row = tbl->last_row = rp;
339 if (rp == NULL) { /* First cell on this line. */
340 rp = mandoc_calloc(1, sizeof(*rp));
342 tbl->last_row->next = rp;
347 if ( ! cell(tbl, rp, ln, p, &pos))
352 static struct tbl_cell *
353 cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos,
356 struct tbl_cell *p, *pp;
357 struct tbl_head *h, *hp;
359 p = mandoc_calloc(1, sizeof(struct tbl_cell));
361 if (NULL != (pp = rp->last)) {
380 hp = mandoc_calloc(1, sizeof(struct tbl_head));
381 hp->ident = tbl->opts.cols++;
384 if (tbl->last_head) {
385 hp->prev = tbl->last_head;
386 tbl->last_head->next = hp;
388 tbl->first_head = hp;