tbl_layout.c revision 95c635efb7c3b86efc493e0447eaec7aecca3f0f
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore/* $Id: tbl_layout.c,v 1.22 2011/09/18 14:14:15 schwarze Exp $ */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Permission to use, copy, modify, and distribute this software for any
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * purpose with or without fee is hereby granted, provided that the above
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * copyright notice and this permission notice appear in all copies.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * FIXME: we can make this parse a lot nicer by, when an error is
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * encountered in a layout key, bailing to the next key (i.e. to the
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * next whitespace then continuing).
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic const struct tbl_phrase keys[KEYS_MAX] = {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic int mods(struct tbl_node *, struct tbl_cell *,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore int, const char *, int *);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic int cell(struct tbl_node *, struct tbl_row *,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore int, const char *, int *);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic void row(struct tbl_node *, int, const char *, int *);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic struct tbl_cell *cell_alloc(struct tbl_node *,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic void head_adjust(const struct tbl_cell *,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoremods(struct tbl_node *tbl, struct tbl_cell *cp,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Not all types accept modifiers. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* FALLTHROUGH */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* FALLTHROUGH */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* FALLTHROUGH */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* FALLTHROUGH */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * XXX: since, at least for now, modifiers are non-conflicting
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * (are separable by value, regardless of position), we let
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * modifiers come in any order. The existing tbl doesn't let
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * this happen.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore switch (p[*pos]) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* FALLTHROUGH */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* FALLTHROUGH */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* FALLTHROUGH */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* FALLTHROUGH */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Throw away parenthesised expression. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Parse numerical spacing from modifier string. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore for (i = 0; i < 4; i++) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* No greater than 4 digits. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (4 == i) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* NOTREACHED */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* TODO: GNU has many more extensions. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore switch (tolower((unsigned char)p[(*pos)++])) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore case ('w'): /* XXX for now, ignore minimal column width */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* FALLTHROUGH */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* FALLTHROUGH */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore switch (tolower((unsigned char)p[(*pos)++])) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* FALLTHROUGH */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* FALLTHROUGH */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* FALLTHROUGH */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorecell(struct tbl_node *tbl, struct tbl_row *rp,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Parse the column position (`r', `R', `|', ...). */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore for (i = 0; i < KEYS_MAX; i++)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (tolower((unsigned char)p[*pos]) == keys[i].name)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * If a span cell is found first, raise a warning and abort the
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * parse. If a span cell is found and the last layout element
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * isn't a "normal" layout, bail.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * FIXME: recover from this somehow?
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * If a vertical spanner is found, we may not be in the first
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (TBL_CELL_DOWN == c && rp == tbl->first_row) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse, ln, *pos, NULL);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Extra check for the double-vertical. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Disallow adjacent spacers. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (rp->last && (TBL_CELL_VERT == c || TBL_CELL_DVERT == c) &&
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse, ln, *pos - 1, NULL);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Allocate cell then parse its modifiers. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore return(mods(tbl, cell_alloc(tbl, rp, c), ln, p, pos));
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorerow(struct tbl_node *tbl, int ln, const char *p, int *pos)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * EBNF describing this section:
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * row ::= row_list [:space:]* [.]?[\n]
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * row_list ::= [:space:]* row_elem row_tail
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * row_tail ::= [:space:]*[,] row_list |
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * row_elem ::= [\t\ ]*[:alpha:]+
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore rp = mandoc_calloc(1, sizeof(struct tbl_row));
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Safely exit layout context. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore mandoc_msg(MANDOCERR_TBLNOLAYOUT, tbl->parse,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* End (and possibly restart) a row. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* NOTREACHED */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoretbl_layout(struct tbl_node *tbl, int ln, const char *p)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Always succeed. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic struct tbl_cell *
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorecell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p = mandoc_calloc(1, sizeof(struct tbl_cell));
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * This is a little bit complicated. Here we determine the
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * header the corresponds to a cell. We add headers dynamically
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * when need be or re-use them, otherwise. As an example, given
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * the following:
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * 3 || c | l |.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * We first add the new headers (as there are none) in (1); then
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * in (2) we insert the first spanner (as it doesn't match up
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * with the header); then we re-use the prior data headers,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * skipping over the spanners; then we re-use everything and add
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * a last spanner. Note that VERT headers are made into DVERT
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Re-use data header. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Re-use spanner header. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Right-shift headers with a new spanner. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore hp = mandoc_calloc(1, sizeof(struct tbl_head));
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Fall through to default case... */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore hp = mandoc_calloc(1, sizeof(struct tbl_head));
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorehead_adjust(const struct tbl_cell *cellp, struct tbl_head *head)