1 /* $Id: tbl_layout.c,v 1.26 2014/04/20 16:46:05 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.
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 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);
68 mods(struct tbl_node *tbl, struct tbl_cell *cp,
69 int ln, const char *p, int *pos)
74 /* Not all types accept modifiers. */
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
111 /* Throw away parenthesised expression. */
113 if ('(' == p[*pos]) {
115 while (p[*pos] && ')' != p[*pos])
117 if (')' == p[*pos]) {
121 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
126 /* Parse numerical spacing from modifier string. */
128 if (isdigit((unsigned char)p[*pos])) {
129 for (i = 0; i < 4; i++) {
130 if ( ! isdigit((unsigned char)p[*pos + i]))
132 buf[i] = p[*pos + i];
136 /* No greater than 4 digits. */
139 mandoc_msg(MANDOCERR_TBLLAYOUT,
140 tbl->parse, ln, *pos, NULL);
145 cp->spacing = (size_t)atoi(buf);
151 /* TODO: GNU has many more extensions. */
153 switch (tolower((unsigned char)p[(*pos)++])) {
155 cp->flags |= TBL_CELL_WIGN;
158 cp->flags |= TBL_CELL_UP;
161 cp->flags |= TBL_CELL_EQUAL;
164 cp->flags |= TBL_CELL_TALIGN;
167 cp->flags |= TBL_CELL_BALIGN;
169 case 'w': /* XXX for now, ignore minimal column width */
181 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
186 switch (tolower((unsigned char)p[(*pos)++])) {
190 cp->flags |= TBL_CELL_BOLD;
195 cp->flags |= TBL_CELL_ITALIC;
205 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
211 cell(struct tbl_node *tbl, struct tbl_row *rp,
212 int ln, const char *p, int *pos)
217 /* Handle vertical lines. */
219 for (vert = 0; '|' == p[*pos]; ++*pos)
221 while (' ' == p[*pos])
224 /* Handle trailing vertical lines */
226 if ('.' == p[*pos] || '\0' == p[*pos]) {
231 /* Parse the column position (`c', `l', `r', ...). */
233 for (i = 0; i < KEYS_MAX; i++)
234 if (tolower((unsigned char)p[*pos]) == keys[i].name)
238 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
246 * If a span cell is found first, raise a warning and abort the
247 * parse. If a span cell is found and the last layout element
248 * isn't a "normal" layout, bail.
250 * FIXME: recover from this somehow?
253 if (TBL_CELL_SPAN == c) {
254 if (NULL == rp->first) {
255 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
259 switch (rp->last->pos) {
262 case TBL_CELL_DHORIZ:
263 mandoc_msg(MANDOCERR_TBLLAYOUT,
264 tbl->parse, ln, *pos, NULL);
272 * If a vertical spanner is found, we may not be in the first
276 if (TBL_CELL_DOWN == c && rp == tbl->first_row) {
277 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse, ln, *pos, NULL);
283 /* Disallow adjacent spacers. */
286 mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse, ln, *pos - 1, NULL);
290 /* Allocate cell then parse its modifiers. */
292 return(mods(tbl, cell_alloc(tbl, rp, c, vert), ln, p, pos));
296 row(struct tbl_node *tbl, int ln, const char *p, int *pos)
301 * EBNF describing this section:
303 * row ::= row_list [:space:]* [.]?[\n]
304 * row_list ::= [:space:]* row_elem row_tail
305 * row_tail ::= [:space:]*[,] row_list |
307 * row_elem ::= [\t\ ]*[:alpha:]+
310 rp = mandoc_calloc(1, sizeof(struct tbl_row));
312 tbl->last_row->next = rp;
318 while (isspace((unsigned char)p[*pos]))
321 /* Safely exit layout context. */
323 if ('.' == p[*pos]) {
324 tbl->part = TBL_PART_DATA;
325 if (NULL == tbl->first_row)
326 mandoc_msg(MANDOCERR_TBLNOLAYOUT,
327 tbl->parse, ln, *pos, NULL);
332 /* End (and possibly restart) a row. */
334 if (',' == p[*pos]) {
337 } else if ('\0' == p[*pos])
340 if ( ! cell(tbl, rp, ln, p, pos))
348 tbl_layout(struct tbl_node *tbl, int ln, const char *p)
353 row(tbl, ln, p, &pos);
355 /* Always succeed. */
359 static struct tbl_cell *
360 cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos,
363 struct tbl_cell *p, *pp;
364 struct tbl_head *h, *hp;
366 p = mandoc_calloc(1, sizeof(struct tbl_cell));
368 if (NULL != (pp = rp->last)) {
387 hp = mandoc_calloc(1, sizeof(struct tbl_head));
388 hp->ident = tbl->opts.cols++;
391 if (tbl->last_head) {
392 hp->prev = tbl->last_head;
393 tbl->last_head->next = hp;
395 tbl->first_head = hp;