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

Annotation of mandoc/tbl_layout.c, Revision 1.30

1.30    ! schwarze    1: /*     $Id: tbl_layout.c,v 1.29 2014/10/14 02:16:06 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.23      schwarze   62: static struct tbl_cell *cell_alloc(struct tbl_node *, struct tbl_row *,
                     63:                                enum tbl_cellt, int vert);
1.1       kristaps   64:
1.26      schwarze   65:
1.1       kristaps   66: static int
1.26      schwarze   67: mods(struct tbl_node *tbl, struct tbl_cell *cp,
1.1       kristaps   68:                int ln, const char *p, int *pos)
                     69: {
                     70:        char             buf[5];
                     71:        int              i;
1.20      kristaps   72:
                     73:        /* Not all types accept modifiers. */
                     74:
                     75:        switch (cp->pos) {
1.26      schwarze   76:        case TBL_CELL_DOWN:
1.20      kristaps   77:                /* FALLTHROUGH */
1.26      schwarze   78:        case TBL_CELL_HORIZ:
1.20      kristaps   79:                /* FALLTHROUGH */
1.26      schwarze   80:        case TBL_CELL_DHORIZ:
1.20      kristaps   81:                return(1);
                     82:        default:
                     83:                break;
                     84:        }
1.1       kristaps   85:
                     86: mod:
1.26      schwarze   87:        /*
1.1       kristaps   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
                     91:         * this happen.
                     92:         */
                     93:        switch (p[*pos]) {
1.26      schwarze   94:        case '\0':
1.1       kristaps   95:                /* FALLTHROUGH */
1.26      schwarze   96:        case ' ':
1.1       kristaps   97:                /* FALLTHROUGH */
1.26      schwarze   98:        case '\t':
1.1       kristaps   99:                /* FALLTHROUGH */
1.26      schwarze  100:        case ',':
1.1       kristaps  101:                /* FALLTHROUGH */
1.26      schwarze  102:        case '.':
1.25      schwarze  103:                /* FALLTHROUGH */
1.26      schwarze  104:        case '|':
1.1       kristaps  105:                return(1);
                    106:        default:
                    107:                break;
1.12      kristaps  108:        }
                    109:
                    110:        /* Throw away parenthesised expression. */
                    111:
                    112:        if ('(' == p[*pos]) {
                    113:                (*pos)++;
                    114:                while (p[*pos] && ')' != p[*pos])
                    115:                        (*pos)++;
                    116:                if (')' == p[*pos]) {
                    117:                        (*pos)++;
                    118:                        goto mod;
                    119:                }
1.26      schwarze  120:                mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
                    121:                    ln, *pos, NULL);
1.12      kristaps  122:                return(0);
1.1       kristaps  123:        }
                    124:
                    125:        /* Parse numerical spacing from modifier string. */
                    126:
                    127:        if (isdigit((unsigned char)p[*pos])) {
                    128:                for (i = 0; i < 4; i++) {
                    129:                        if ( ! isdigit((unsigned char)p[*pos + i]))
                    130:                                break;
                    131:                        buf[i] = p[*pos + i];
                    132:                }
                    133:                buf[i] = '\0';
                    134:
                    135:                /* No greater than 4 digits. */
                    136:
                    137:                if (4 == i) {
1.26      schwarze  138:                        mandoc_msg(MANDOCERR_TBLLAYOUT,
                    139:                            tbl->parse, ln, *pos, NULL);
1.1       kristaps  140:                        return(0);
                    141:                }
                    142:
                    143:                *pos += i;
1.15      kristaps  144:                cp->spacing = (size_t)atoi(buf);
1.1       kristaps  145:
                    146:                goto mod;
                    147:                /* NOTREACHED */
1.26      schwarze  148:        }
1.1       kristaps  149:
                    150:        /* TODO: GNU has many more extensions. */
                    151:
1.13      joerg     152:        switch (tolower((unsigned char)p[(*pos)++])) {
1.26      schwarze  153:        case 'z':
1.1       kristaps  154:                cp->flags |= TBL_CELL_WIGN;
                    155:                goto mod;
1.26      schwarze  156:        case 'u':
1.1       kristaps  157:                cp->flags |= TBL_CELL_UP;
                    158:                goto mod;
1.26      schwarze  159:        case 'e':
1.1       kristaps  160:                cp->flags |= TBL_CELL_EQUAL;
                    161:                goto mod;
1.26      schwarze  162:        case 't':
1.1       kristaps  163:                cp->flags |= TBL_CELL_TALIGN;
                    164:                goto mod;
1.26      schwarze  165:        case 'd':
1.1       kristaps  166:                cp->flags |= TBL_CELL_BALIGN;
1.10      schwarze  167:                goto mod;
1.26      schwarze  168:        case 'w':  /* XXX for now, ignore minimal column width */
1.29      schwarze  169:                goto mod;
                    170:        case 'x':
                    171:                cp->flags |= TBL_CELL_WMAX;
1.1       kristaps  172:                goto mod;
1.26      schwarze  173:        case 'f':
1.2       kristaps  174:                break;
1.26      schwarze  175:        case 'r':
1.21      kristaps  176:                /* FALLTHROUGH */
1.26      schwarze  177:        case 'b':
1.1       kristaps  178:                /* FALLTHROUGH */
1.26      schwarze  179:        case 'i':
1.2       kristaps  180:                (*pos)--;
1.1       kristaps  181:                break;
                    182:        default:
1.17      kristaps  183:                mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
1.26      schwarze  184:                    ln, *pos - 1, NULL);
1.1       kristaps  185:                return(0);
                    186:        }
                    187:
1.13      joerg     188:        switch (tolower((unsigned char)p[(*pos)++])) {
1.26      schwarze  189:        case '3':
1.21      kristaps  190:                /* FALLTHROUGH */
1.26      schwarze  191:        case 'b':
1.1       kristaps  192:                cp->flags |= TBL_CELL_BOLD;
                    193:                goto mod;
1.26      schwarze  194:        case '2':
1.21      kristaps  195:                /* FALLTHROUGH */
1.26      schwarze  196:        case 'i':
1.1       kristaps  197:                cp->flags |= TBL_CELL_ITALIC;
1.21      kristaps  198:                goto mod;
1.26      schwarze  199:        case '1':
1.21      kristaps  200:                /* FALLTHROUGH */
1.26      schwarze  201:        case 'r':
1.1       kristaps  202:                goto mod;
                    203:        default:
                    204:                break;
1.28      schwarze  205:        }
                    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]);
                    209:                goto mod;
1.1       kristaps  210:        }
                    211:
1.17      kristaps  212:        mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
1.26      schwarze  213:            ln, *pos - 1, NULL);
1.1       kristaps  214:        return(0);
                    215: }
                    216:
                    217: static int
1.26      schwarze  218: cell(struct tbl_node *tbl, struct tbl_row *rp,
1.1       kristaps  219:                int ln, const char *p, int *pos)
                    220: {
1.23      schwarze  221:        int              vert, i;
1.1       kristaps  222:        enum tbl_cellt   c;
                    223:
1.23      schwarze  224:        /* Handle vertical lines. */
                    225:
                    226:        for (vert = 0; '|' == p[*pos]; ++*pos)
                    227:                vert++;
                    228:        while (' ' == p[*pos])
                    229:                (*pos)++;
1.25      schwarze  230:
                    231:        /* Handle trailing vertical lines */
                    232:
                    233:        if ('.' == p[*pos] || '\0' == p[*pos]) {
                    234:                rp->vert = vert;
                    235:                return(1);
                    236:        }
1.23      schwarze  237:
                    238:        /* Parse the column position (`c', `l', `r', ...). */
1.1       kristaps  239:
                    240:        for (i = 0; i < KEYS_MAX; i++)
1.13      joerg     241:                if (tolower((unsigned char)p[*pos]) == keys[i].name)
1.1       kristaps  242:                        break;
                    243:
                    244:        if (KEYS_MAX == i) {
1.26      schwarze  245:                mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
                    246:                    ln, *pos, NULL);
1.1       kristaps  247:                return(0);
                    248:        }
                    249:
1.11      kristaps  250:        c = keys[i].key;
                    251:
                    252:        /*
                    253:         * If a span cell is found first, raise a warning and abort the
1.14      kristaps  254:         * parse.  If a span cell is found and the last layout element
                    255:         * isn't a "normal" layout, bail.
                    256:         *
                    257:         * FIXME: recover from this somehow?
1.11      kristaps  258:         */
                    259:
1.14      kristaps  260:        if (TBL_CELL_SPAN == c) {
                    261:                if (NULL == rp->first) {
1.17      kristaps  262:                        mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
1.26      schwarze  263:                            ln, *pos, NULL);
1.14      kristaps  264:                        return(0);
                    265:                } else if (rp->last)
                    266:                        switch (rp->last->pos) {
1.26      schwarze  267:                        case TBL_CELL_HORIZ:
                    268:                                /* FALLTHROUGH */
                    269:                        case TBL_CELL_DHORIZ:
                    270:                                mandoc_msg(MANDOCERR_TBLLAYOUT,
                    271:                                    tbl->parse, ln, *pos, NULL);
1.14      kristaps  272:                                return(0);
                    273:                        default:
                    274:                                break;
                    275:                        }
1.16      kristaps  276:        }
                    277:
                    278:        /*
                    279:         * If a vertical spanner is found, we may not be in the first
                    280:         * row.
                    281:         */
                    282:
                    283:        if (TBL_CELL_DOWN == c && rp == tbl->first_row) {
1.17      kristaps  284:                mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse, ln, *pos, NULL);
1.16      kristaps  285:                return(0);
1.11      kristaps  286:        }
                    287:
1.1       kristaps  288:        (*pos)++;
                    289:
                    290:        /* Disallow adjacent spacers. */
                    291:
1.23      schwarze  292:        if (vert > 2) {
1.17      kristaps  293:                mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse, ln, *pos - 1, NULL);
1.1       kristaps  294:                return(0);
                    295:        }
                    296:
                    297:        /* Allocate cell then parse its modifiers. */
                    298:
1.23      schwarze  299:        return(mods(tbl, cell_alloc(tbl, rp, c, vert), ln, p, pos));
1.1       kristaps  300: }
                    301:
1.30    ! schwarze  302: int
        !           303: tbl_layout(struct tbl_node *tbl, int ln, const char *p)
1.1       kristaps  304: {
                    305:        struct tbl_row  *rp;
1.30    ! schwarze  306:        int              pos;
1.1       kristaps  307:
1.30    ! schwarze  308:        pos = 0;
        !           309:        rp = NULL;
1.1       kristaps  310:
1.30    ! schwarze  311:        for (;;) {
        !           312:                /* Skip whitespace before and after each cell. */
1.1       kristaps  313:
1.30    ! schwarze  314:                while (isspace((unsigned char)p[pos]))
        !           315:                        pos++;
1.1       kristaps  316:
1.30    ! schwarze  317:                switch (p[pos]) {
        !           318:                case ',':  /* Next row on this input line. */
        !           319:                        pos++;
        !           320:                        rp = NULL;
        !           321:                        continue;
        !           322:                case '\0':  /* Next row on next input line. */
        !           323:                        return(1);
        !           324:                case '.':  /* End of layout. */
        !           325:                        pos++;
        !           326:                        tbl->part = TBL_PART_DATA;
        !           327:                        if (tbl->first_row != NULL)
        !           328:                                return(1);
1.26      schwarze  329:                        mandoc_msg(MANDOCERR_TBLNOLAYOUT,
1.30    ! schwarze  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;
        !           334:                        return(1);
        !           335:                default:  /* Cell. */
        !           336:                        break;
        !           337:                }
        !           338:
        !           339:                if (rp == NULL) {  /* First cell on this line. */
        !           340:                        rp = mandoc_calloc(1, sizeof(*rp));
        !           341:                        if (tbl->last_row)
        !           342:                                tbl->last_row->next = rp;
        !           343:                        else
        !           344:                                tbl->first_row = rp;
        !           345:                        tbl->last_row = rp;
        !           346:                }
        !           347:                if ( ! cell(tbl, rp, ln, p, &pos))
        !           348:                        return(1);
1.1       kristaps  349:        }
                    350: }
1.5       kristaps  351:
                    352: static struct tbl_cell *
1.23      schwarze  353: cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos,
                    354:                int vert)
1.5       kristaps  355: {
                    356:        struct tbl_cell *p, *pp;
                    357:        struct tbl_head *h, *hp;
                    358:
                    359:        p = mandoc_calloc(1, sizeof(struct tbl_cell));
                    360:
                    361:        if (NULL != (pp = rp->last)) {
1.23      schwarze  362:                pp->next = p;
                    363:                h = pp->head->next;
                    364:        } else {
                    365:                rp->first = p;
                    366:                h = tbl->first_head;
                    367:        }
                    368:        rp->last = p;
1.5       kristaps  369:
                    370:        p->pos = pos;
1.23      schwarze  371:        p->vert = vert;
1.5       kristaps  372:
1.23      schwarze  373:        /* Re-use header. */
1.5       kristaps  374:
                    375:        if (h) {
1.23      schwarze  376:                p->head = h;
                    377:                return(p);
1.5       kristaps  378:        }
                    379:
                    380:        hp = mandoc_calloc(1, sizeof(struct tbl_head));
1.9       kristaps  381:        hp->ident = tbl->opts.cols++;
1.23      schwarze  382:        hp->vert = vert;
1.5       kristaps  383:
                    384:        if (tbl->last_head) {
                    385:                hp->prev = tbl->last_head;
                    386:                tbl->last_head->next = hp;
                    387:        } else
1.23      schwarze  388:                tbl->first_head = hp;
                    389:        tbl->last_head = hp;
1.5       kristaps  390:
                    391:        p->head = hp;
                    392:        return(p);
                    393: }

CVSweb