[BACK]Return to tbl_layout.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/tbl_layout.c, Revision 1.28

1.28    ! schwarze    1: /*     $Id: tbl_layout.c,v 1.27 2014/08/10 23:54:41 schwarze Exp $ */
1.1       kristaps    2: /*
1.22      schwarze    3:  * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.25      schwarze    4:  * Copyright (c) 2012, 2014 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    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:  */
1.18      kristaps   18: #include "config.h"
1.27      schwarze   19:
                     20: #include <sys/types.h>
1.18      kristaps   21:
1.1       kristaps   22: #include <ctype.h>
                     23: #include <stdlib.h>
                     24: #include <string.h>
1.4       kristaps   25: #include <time.h>
1.1       kristaps   26:
                     27: #include "mandoc.h"
1.24      schwarze   28: #include "mandoc_aux.h"
1.1       kristaps   29: #include "libmandoc.h"
                     30: #include "libroff.h"
                     31:
                     32: struct tbl_phrase {
                     33:        char             name;
                     34:        enum tbl_cellt   key;
                     35: };
                     36:
1.11      kristaps   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:
1.2       kristaps   43: #define        KEYS_MAX         11
1.1       kristaps   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 },
1.23      schwarze   55:        { '=',           TBL_CELL_DHORIZ }
1.1       kristaps   56: };
                     57:
1.26      schwarze   58: static int              mods(struct tbl_node *, struct tbl_cell *,
1.5       kristaps   59:                                int, const char *, int *);
1.26      schwarze   60: static int              cell(struct tbl_node *, struct tbl_row *,
1.5       kristaps   61:                                int, const char *, int *);
1.6       kristaps   62: static void             row(struct tbl_node *, int, const char *, int *);
1.23      schwarze   63: static struct tbl_cell *cell_alloc(struct tbl_node *, struct tbl_row *,
                     64:                                enum tbl_cellt, int vert);
1.1       kristaps   65:
1.26      schwarze   66:
1.1       kristaps   67: static int
1.26      schwarze   68: mods(struct tbl_node *tbl, struct tbl_cell *cp,
1.1       kristaps   69:                int ln, const char *p, int *pos)
                     70: {
                     71:        char             buf[5];
                     72:        int              i;
1.20      kristaps   73:
                     74:        /* Not all types accept modifiers. */
                     75:
                     76:        switch (cp->pos) {
1.26      schwarze   77:        case TBL_CELL_DOWN:
1.20      kristaps   78:                /* FALLTHROUGH */
1.26      schwarze   79:        case TBL_CELL_HORIZ:
1.20      kristaps   80:                /* FALLTHROUGH */
1.26      schwarze   81:        case TBL_CELL_DHORIZ:
1.20      kristaps   82:                return(1);
                     83:        default:
                     84:                break;
                     85:        }
1.1       kristaps   86:
                     87: mod:
1.26      schwarze   88:        /*
1.1       kristaps   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]) {
1.26      schwarze   95:        case '\0':
1.1       kristaps   96:                /* FALLTHROUGH */
1.26      schwarze   97:        case ' ':
1.1       kristaps   98:                /* FALLTHROUGH */
1.26      schwarze   99:        case '\t':
1.1       kristaps  100:                /* FALLTHROUGH */
1.26      schwarze  101:        case ',':
1.1       kristaps  102:                /* FALLTHROUGH */
1.26      schwarze  103:        case '.':
1.25      schwarze  104:                /* FALLTHROUGH */
1.26      schwarze  105:        case '|':
1.1       kristaps  106:                return(1);
                    107:        default:
                    108:                break;
1.12      kristaps  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:                }
1.26      schwarze  121:                mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
                    122:                    ln, *pos, NULL);
1.12      kristaps  123:                return(0);
1.1       kristaps  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) {
1.26      schwarze  139:                        mandoc_msg(MANDOCERR_TBLLAYOUT,
                    140:                            tbl->parse, ln, *pos, NULL);
1.1       kristaps  141:                        return(0);
                    142:                }
                    143:
                    144:                *pos += i;
1.15      kristaps  145:                cp->spacing = (size_t)atoi(buf);
1.1       kristaps  146:
                    147:                goto mod;
                    148:                /* NOTREACHED */
1.26      schwarze  149:        }
1.1       kristaps  150:
                    151:        /* TODO: GNU has many more extensions. */
                    152:
1.13      joerg     153:        switch (tolower((unsigned char)p[(*pos)++])) {
1.26      schwarze  154:        case 'z':
1.1       kristaps  155:                cp->flags |= TBL_CELL_WIGN;
                    156:                goto mod;
1.26      schwarze  157:        case 'u':
1.1       kristaps  158:                cp->flags |= TBL_CELL_UP;
                    159:                goto mod;
1.26      schwarze  160:        case 'e':
1.1       kristaps  161:                cp->flags |= TBL_CELL_EQUAL;
                    162:                goto mod;
1.26      schwarze  163:        case 't':
1.1       kristaps  164:                cp->flags |= TBL_CELL_TALIGN;
                    165:                goto mod;
1.26      schwarze  166:        case 'd':
1.1       kristaps  167:                cp->flags |= TBL_CELL_BALIGN;
1.10      schwarze  168:                goto mod;
1.26      schwarze  169:        case 'w':  /* XXX for now, ignore minimal column width */
1.1       kristaps  170:                goto mod;
1.26      schwarze  171:        case 'f':
1.2       kristaps  172:                break;
1.26      schwarze  173:        case 'r':
1.21      kristaps  174:                /* FALLTHROUGH */
1.26      schwarze  175:        case 'b':
1.1       kristaps  176:                /* FALLTHROUGH */
1.26      schwarze  177:        case 'i':
1.2       kristaps  178:                (*pos)--;
1.1       kristaps  179:                break;
                    180:        default:
1.17      kristaps  181:                mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
1.26      schwarze  182:                    ln, *pos - 1, NULL);
1.1       kristaps  183:                return(0);
                    184:        }
                    185:
1.13      joerg     186:        switch (tolower((unsigned char)p[(*pos)++])) {
1.26      schwarze  187:        case '3':
1.21      kristaps  188:                /* FALLTHROUGH */
1.26      schwarze  189:        case 'b':
1.1       kristaps  190:                cp->flags |= TBL_CELL_BOLD;
                    191:                goto mod;
1.26      schwarze  192:        case '2':
1.21      kristaps  193:                /* FALLTHROUGH */
1.26      schwarze  194:        case 'i':
1.1       kristaps  195:                cp->flags |= TBL_CELL_ITALIC;
1.21      kristaps  196:                goto mod;
1.26      schwarze  197:        case '1':
1.21      kristaps  198:                /* FALLTHROUGH */
1.26      schwarze  199:        case 'r':
1.1       kristaps  200:                goto mod;
                    201:        default:
                    202:                break;
1.28    ! schwarze  203:        }
        !           204:        if (isalnum((unsigned char)p[*pos - 1])) {
        !           205:                mandoc_vmsg(MANDOCERR_FT_BAD, tbl->parse,
        !           206:                    ln, *pos - 1, "TS f%c", p[*pos - 1]);
        !           207:                goto mod;
1.1       kristaps  208:        }
                    209:
1.17      kristaps  210:        mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
1.26      schwarze  211:            ln, *pos - 1, NULL);
1.1       kristaps  212:        return(0);
                    213: }
                    214:
                    215: static int
1.26      schwarze  216: cell(struct tbl_node *tbl, struct tbl_row *rp,
1.1       kristaps  217:                int ln, const char *p, int *pos)
                    218: {
1.23      schwarze  219:        int              vert, i;
1.1       kristaps  220:        enum tbl_cellt   c;
                    221:
1.23      schwarze  222:        /* Handle vertical lines. */
                    223:
                    224:        for (vert = 0; '|' == p[*pos]; ++*pos)
                    225:                vert++;
                    226:        while (' ' == p[*pos])
                    227:                (*pos)++;
1.25      schwarze  228:
                    229:        /* Handle trailing vertical lines */
                    230:
                    231:        if ('.' == p[*pos] || '\0' == p[*pos]) {
                    232:                rp->vert = vert;
                    233:                return(1);
                    234:        }
1.23      schwarze  235:
                    236:        /* Parse the column position (`c', `l', `r', ...). */
1.1       kristaps  237:
                    238:        for (i = 0; i < KEYS_MAX; i++)
1.13      joerg     239:                if (tolower((unsigned char)p[*pos]) == keys[i].name)
1.1       kristaps  240:                        break;
                    241:
                    242:        if (KEYS_MAX == i) {
1.26      schwarze  243:                mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
                    244:                    ln, *pos, NULL);
1.1       kristaps  245:                return(0);
                    246:        }
                    247:
1.11      kristaps  248:        c = keys[i].key;
                    249:
                    250:        /*
                    251:         * If a span cell is found first, raise a warning and abort the
1.14      kristaps  252:         * parse.  If a span cell is found and the last layout element
                    253:         * isn't a "normal" layout, bail.
                    254:         *
                    255:         * FIXME: recover from this somehow?
1.11      kristaps  256:         */
                    257:
1.14      kristaps  258:        if (TBL_CELL_SPAN == c) {
                    259:                if (NULL == rp->first) {
1.17      kristaps  260:                        mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
1.26      schwarze  261:                            ln, *pos, NULL);
1.14      kristaps  262:                        return(0);
                    263:                } else if (rp->last)
                    264:                        switch (rp->last->pos) {
1.26      schwarze  265:                        case TBL_CELL_HORIZ:
                    266:                                /* FALLTHROUGH */
                    267:                        case TBL_CELL_DHORIZ:
                    268:                                mandoc_msg(MANDOCERR_TBLLAYOUT,
                    269:                                    tbl->parse, ln, *pos, NULL);
1.14      kristaps  270:                                return(0);
                    271:                        default:
                    272:                                break;
                    273:                        }
1.16      kristaps  274:        }
                    275:
                    276:        /*
                    277:         * If a vertical spanner is found, we may not be in the first
                    278:         * row.
                    279:         */
                    280:
                    281:        if (TBL_CELL_DOWN == c && rp == tbl->first_row) {
1.17      kristaps  282:                mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse, ln, *pos, NULL);
1.16      kristaps  283:                return(0);
1.11      kristaps  284:        }
                    285:
1.1       kristaps  286:        (*pos)++;
                    287:
                    288:        /* Disallow adjacent spacers. */
                    289:
1.23      schwarze  290:        if (vert > 2) {
1.17      kristaps  291:                mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse, ln, *pos - 1, NULL);
1.1       kristaps  292:                return(0);
                    293:        }
                    294:
                    295:        /* Allocate cell then parse its modifiers. */
                    296:
1.23      schwarze  297:        return(mods(tbl, cell_alloc(tbl, rp, c, vert), ln, p, pos));
1.1       kristaps  298: }
                    299:
                    300: static void
1.6       kristaps  301: row(struct tbl_node *tbl, int ln, const char *p, int *pos)
1.1       kristaps  302: {
                    303:        struct tbl_row  *rp;
                    304:
                    305: row:   /*
                    306:         * EBNF describing this section:
                    307:         *
                    308:         * row          ::= row_list [:space:]* [.]?[\n]
                    309:         * row_list     ::= [:space:]* row_elem row_tail
                    310:         * row_tail     ::= [:space:]*[,] row_list |
                    311:         *                  epsilon
                    312:         * row_elem     ::= [\t\ ]*[:alpha:]+
                    313:         */
                    314:
                    315:        rp = mandoc_calloc(1, sizeof(struct tbl_row));
1.23      schwarze  316:        if (tbl->last_row)
1.3       kristaps  317:                tbl->last_row->next = rp;
1.23      schwarze  318:        else
                    319:                tbl->first_row = rp;
                    320:        tbl->last_row = rp;
1.1       kristaps  321:
                    322: cell:
                    323:        while (isspace((unsigned char)p[*pos]))
                    324:                (*pos)++;
                    325:
                    326:        /* Safely exit layout context. */
                    327:
                    328:        if ('.' == p[*pos]) {
                    329:                tbl->part = TBL_PART_DATA;
1.26      schwarze  330:                if (NULL == tbl->first_row)
                    331:                        mandoc_msg(MANDOCERR_TBLNOLAYOUT,
                    332:                            tbl->parse, ln, *pos, NULL);
1.1       kristaps  333:                (*pos)++;
                    334:                return;
                    335:        }
                    336:
                    337:        /* End (and possibly restart) a row. */
                    338:
                    339:        if (',' == p[*pos]) {
                    340:                (*pos)++;
                    341:                goto row;
                    342:        } else if ('\0' == p[*pos])
                    343:                return;
                    344:
                    345:        if ( ! cell(tbl, rp, ln, p, pos))
                    346:                return;
                    347:
                    348:        goto cell;
                    349:        /* NOTREACHED */
                    350: }
                    351:
                    352: int
1.6       kristaps  353: tbl_layout(struct tbl_node *tbl, int ln, const char *p)
1.1       kristaps  354: {
                    355:        int              pos;
                    356:
                    357:        pos = 0;
                    358:        row(tbl, ln, p, &pos);
                    359:
                    360:        /* Always succeed. */
                    361:        return(1);
                    362: }
1.5       kristaps  363:
                    364: static struct tbl_cell *
1.23      schwarze  365: cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos,
                    366:                int vert)
1.5       kristaps  367: {
                    368:        struct tbl_cell *p, *pp;
                    369:        struct tbl_head *h, *hp;
                    370:
                    371:        p = mandoc_calloc(1, sizeof(struct tbl_cell));
                    372:
                    373:        if (NULL != (pp = rp->last)) {
1.23      schwarze  374:                pp->next = p;
                    375:                h = pp->head->next;
                    376:        } else {
                    377:                rp->first = p;
                    378:                h = tbl->first_head;
                    379:        }
                    380:        rp->last = p;
1.5       kristaps  381:
                    382:        p->pos = pos;
1.23      schwarze  383:        p->vert = vert;
1.5       kristaps  384:
1.23      schwarze  385:        /* Re-use header. */
1.5       kristaps  386:
                    387:        if (h) {
1.23      schwarze  388:                p->head = h;
                    389:                return(p);
1.5       kristaps  390:        }
                    391:
                    392:        hp = mandoc_calloc(1, sizeof(struct tbl_head));
1.9       kristaps  393:        hp->ident = tbl->opts.cols++;
1.23      schwarze  394:        hp->vert = vert;
1.5       kristaps  395:
                    396:        if (tbl->last_head) {
                    397:                hp->prev = tbl->last_head;
                    398:                tbl->last_head->next = hp;
                    399:        } else
1.23      schwarze  400:                tbl->first_head = hp;
                    401:        tbl->last_head = hp;
1.5       kristaps  402:
                    403:        p->head = hp;
                    404:        return(p);
                    405: }

CVSweb