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

Annotation of mandoc/tbl_layout.c, Revision 1.20

1.20    ! kristaps    1: /*     $Id: tbl_layout.c,v 1.19 2011/04/07 01:08:42 joerg Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
1.18      kristaps   17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
1.1       kristaps   21: #include <assert.h>
                     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"
                     28: #include "libmandoc.h"
                     29: #include "libroff.h"
                     30:
                     31: struct tbl_phrase {
                     32:        char             name;
                     33:        enum tbl_cellt   key;
                     34: };
                     35:
1.11      kristaps   36: /*
                     37:  * FIXME: we can make this parse a lot nicer by, when an error is
                     38:  * encountered in a layout key, bailing to the next key (i.e. to the
                     39:  * next whitespace then continuing).
                     40:  */
                     41:
1.2       kristaps   42: #define        KEYS_MAX         11
1.1       kristaps   43:
                     44: static const struct tbl_phrase keys[KEYS_MAX] = {
                     45:        { 'c',           TBL_CELL_CENTRE },
                     46:        { 'r',           TBL_CELL_RIGHT },
                     47:        { 'l',           TBL_CELL_LEFT },
                     48:        { 'n',           TBL_CELL_NUMBER },
                     49:        { 's',           TBL_CELL_SPAN },
                     50:        { 'a',           TBL_CELL_LONG },
                     51:        { '^',           TBL_CELL_DOWN },
                     52:        { '-',           TBL_CELL_HORIZ },
                     53:        { '_',           TBL_CELL_HORIZ },
                     54:        { '=',           TBL_CELL_DHORIZ },
                     55:        { '|',           TBL_CELL_VERT }
                     56: };
                     57:
1.6       kristaps   58: static int              mods(struct tbl_node *, struct tbl_cell *,
1.5       kristaps   59:                                int, const char *, int *);
1.6       kristaps   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 *);
                     63: static struct tbl_cell *cell_alloc(struct tbl_node *,
1.5       kristaps   64:                                struct tbl_row *, enum tbl_cellt);
                     65: static void             head_adjust(const struct tbl_cell *,
                     66:                                struct tbl_head *);
1.1       kristaps   67:
                     68: static int
1.6       kristaps   69: mods(struct tbl_node *tbl, struct tbl_cell *cp,
1.1       kristaps   70:                int ln, const char *p, int *pos)
                     71: {
                     72:        char             buf[5];
                     73:        int              i;
1.20    ! kristaps   74:
        !            75:        /* Not all types accept modifiers. */
        !            76:
        !            77:        switch (cp->pos) {
        !            78:        case (TBL_CELL_DOWN):
        !            79:                /* FALLTHROUGH */
        !            80:        case (TBL_CELL_HORIZ):
        !            81:                /* FALLTHROUGH */
        !            82:        case (TBL_CELL_DHORIZ):
        !            83:                /* FALLTHROUGH */
        !            84:        case (TBL_CELL_VERT):
        !            85:                /* FALLTHROUGH */
        !            86:        case (TBL_CELL_DVERT):
        !            87:                return(1);
        !            88:        default:
        !            89:                break;
        !            90:        }
1.1       kristaps   91:
                     92: mod:
                     93:        /*
                     94:         * XXX: since, at least for now, modifiers are non-conflicting
                     95:         * (are separable by value, regardless of position), we let
                     96:         * modifiers come in any order.  The existing tbl doesn't let
                     97:         * this happen.
                     98:         */
                     99:        switch (p[*pos]) {
                    100:        case ('\0'):
                    101:                /* FALLTHROUGH */
                    102:        case (' '):
                    103:                /* FALLTHROUGH */
                    104:        case ('\t'):
                    105:                /* FALLTHROUGH */
                    106:        case (','):
                    107:                /* FALLTHROUGH */
                    108:        case ('.'):
                    109:                return(1);
                    110:        default:
                    111:                break;
1.12      kristaps  112:        }
                    113:
                    114:        /* Throw away parenthesised expression. */
                    115:
                    116:        if ('(' == p[*pos]) {
                    117:                (*pos)++;
                    118:                while (p[*pos] && ')' != p[*pos])
                    119:                        (*pos)++;
                    120:                if (')' == p[*pos]) {
                    121:                        (*pos)++;
                    122:                        goto mod;
                    123:                }
1.17      kristaps  124:                mandoc_msg(MANDOCERR_TBLLAYOUT,
                    125:                                tbl->parse, ln, *pos, NULL);
1.12      kristaps  126:                return(0);
1.1       kristaps  127:        }
                    128:
                    129:        /* Parse numerical spacing from modifier string. */
                    130:
                    131:        if (isdigit((unsigned char)p[*pos])) {
                    132:                for (i = 0; i < 4; i++) {
                    133:                        if ( ! isdigit((unsigned char)p[*pos + i]))
                    134:                                break;
                    135:                        buf[i] = p[*pos + i];
                    136:                }
                    137:                buf[i] = '\0';
                    138:
                    139:                /* No greater than 4 digits. */
                    140:
                    141:                if (4 == i) {
1.17      kristaps  142:                        mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
                    143:                                        ln, *pos, NULL);
1.1       kristaps  144:                        return(0);
                    145:                }
                    146:
                    147:                *pos += i;
1.15      kristaps  148:                cp->spacing = (size_t)atoi(buf);
1.1       kristaps  149:
                    150:                goto mod;
                    151:                /* NOTREACHED */
                    152:        }
                    153:
                    154:        /* TODO: GNU has many more extensions. */
                    155:
1.13      joerg     156:        switch (tolower((unsigned char)p[(*pos)++])) {
1.1       kristaps  157:        case ('z'):
                    158:                cp->flags |= TBL_CELL_WIGN;
                    159:                goto mod;
                    160:        case ('u'):
                    161:                cp->flags |= TBL_CELL_UP;
                    162:                goto mod;
                    163:        case ('e'):
                    164:                cp->flags |= TBL_CELL_EQUAL;
                    165:                goto mod;
                    166:        case ('t'):
                    167:                cp->flags |= TBL_CELL_TALIGN;
                    168:                goto mod;
                    169:        case ('d'):
                    170:                cp->flags |= TBL_CELL_BALIGN;
1.10      schwarze  171:                goto mod;
                    172:        case ('w'):  /* XXX for now, ignore minimal column width */
1.1       kristaps  173:                goto mod;
                    174:        case ('f'):
1.2       kristaps  175:                break;
1.1       kristaps  176:        case ('b'):
                    177:                /* FALLTHROUGH */
                    178:        case ('i'):
1.2       kristaps  179:                (*pos)--;
1.1       kristaps  180:                break;
                    181:        default:
1.17      kristaps  182:                mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
                    183:                                ln, *pos - 1, NULL);
1.1       kristaps  184:                return(0);
                    185:        }
                    186:
1.13      joerg     187:        switch (tolower((unsigned char)p[(*pos)++])) {
1.1       kristaps  188:        case ('b'):
                    189:                cp->flags |= TBL_CELL_BOLD;
                    190:                goto mod;
                    191:        case ('i'):
                    192:                cp->flags |= TBL_CELL_ITALIC;
                    193:                goto mod;
                    194:        default:
                    195:                break;
                    196:        }
                    197:
1.17      kristaps  198:        mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
                    199:                        ln, *pos - 1, NULL);
1.1       kristaps  200:        return(0);
                    201: }
                    202:
                    203: static int
1.6       kristaps  204: cell(struct tbl_node *tbl, struct tbl_row *rp,
1.1       kristaps  205:                int ln, const char *p, int *pos)
                    206: {
                    207:        int              i;
                    208:        enum tbl_cellt   c;
                    209:
                    210:        /* Parse the column position (`r', `R', `|', ...). */
                    211:
                    212:        for (i = 0; i < KEYS_MAX; i++)
1.13      joerg     213:                if (tolower((unsigned char)p[*pos]) == keys[i].name)
1.1       kristaps  214:                        break;
                    215:
                    216:        if (KEYS_MAX == i) {
1.17      kristaps  217:                mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
                    218:                                ln, *pos, NULL);
1.1       kristaps  219:                return(0);
                    220:        }
                    221:
1.11      kristaps  222:        c = keys[i].key;
                    223:
                    224:        /*
                    225:         * If a span cell is found first, raise a warning and abort the
1.14      kristaps  226:         * parse.  If a span cell is found and the last layout element
                    227:         * isn't a "normal" layout, bail.
                    228:         *
                    229:         * FIXME: recover from this somehow?
1.11      kristaps  230:         */
                    231:
1.14      kristaps  232:        if (TBL_CELL_SPAN == c) {
                    233:                if (NULL == rp->first) {
1.17      kristaps  234:                        mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
                    235:                                        ln, *pos, NULL);
1.14      kristaps  236:                        return(0);
                    237:                } else if (rp->last)
                    238:                        switch (rp->last->pos) {
                    239:                        case (TBL_CELL_VERT):
                    240:                        case (TBL_CELL_DVERT):
                    241:                        case (TBL_CELL_HORIZ):
                    242:                        case (TBL_CELL_DHORIZ):
1.17      kristaps  243:                                mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse,
                    244:                                                ln, *pos, NULL);
1.14      kristaps  245:                                return(0);
                    246:                        default:
                    247:                                break;
                    248:                        }
1.16      kristaps  249:        }
                    250:
                    251:        /*
                    252:         * If a vertical spanner is found, we may not be in the first
                    253:         * row.
                    254:         */
                    255:
                    256:        if (TBL_CELL_DOWN == c && rp == tbl->first_row) {
1.17      kristaps  257:                mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse, ln, *pos, NULL);
1.16      kristaps  258:                return(0);
1.11      kristaps  259:        }
                    260:
1.1       kristaps  261:        (*pos)++;
                    262:
                    263:        /* Extra check for the double-vertical. */
                    264:
                    265:        if (TBL_CELL_VERT == c && '|' == p[*pos]) {
                    266:                (*pos)++;
                    267:                c = TBL_CELL_DVERT;
                    268:        }
                    269:
                    270:        /* Disallow adjacent spacers. */
                    271:
                    272:        if (rp->last && (TBL_CELL_VERT == c || TBL_CELL_DVERT == c) &&
                    273:                        (TBL_CELL_VERT == rp->last->pos ||
                    274:                         TBL_CELL_DVERT == rp->last->pos)) {
1.17      kristaps  275:                mandoc_msg(MANDOCERR_TBLLAYOUT, tbl->parse, ln, *pos - 1, NULL);
1.1       kristaps  276:                return(0);
                    277:        }
                    278:
                    279:        /* Allocate cell then parse its modifiers. */
                    280:
1.5       kristaps  281:        return(mods(tbl, cell_alloc(tbl, rp, c), ln, p, pos));
1.1       kristaps  282: }
                    283:
                    284:
                    285: static void
1.6       kristaps  286: row(struct tbl_node *tbl, int ln, const char *p, int *pos)
1.1       kristaps  287: {
                    288:        struct tbl_row  *rp;
                    289:
                    290: row:   /*
                    291:         * EBNF describing this section:
                    292:         *
                    293:         * row          ::= row_list [:space:]* [.]?[\n]
                    294:         * row_list     ::= [:space:]* row_elem row_tail
                    295:         * row_tail     ::= [:space:]*[,] row_list |
                    296:         *                  epsilon
                    297:         * row_elem     ::= [\t\ ]*[:alpha:]+
                    298:         */
                    299:
                    300:        rp = mandoc_calloc(1, sizeof(struct tbl_row));
1.3       kristaps  301:        if (tbl->last_row) {
                    302:                tbl->last_row->next = rp;
                    303:                tbl->last_row = rp;
1.1       kristaps  304:        } else
1.3       kristaps  305:                tbl->last_row = tbl->first_row = rp;
1.1       kristaps  306:
                    307: cell:
                    308:        while (isspace((unsigned char)p[*pos]))
                    309:                (*pos)++;
                    310:
                    311:        /* Safely exit layout context. */
                    312:
                    313:        if ('.' == p[*pos]) {
                    314:                tbl->part = TBL_PART_DATA;
1.3       kristaps  315:                if (NULL == tbl->first_row)
1.17      kristaps  316:                        mandoc_msg(MANDOCERR_TBLNOLAYOUT, tbl->parse,
                    317:                                        ln, *pos, NULL);
1.1       kristaps  318:                (*pos)++;
                    319:                return;
                    320:        }
                    321:
                    322:        /* End (and possibly restart) a row. */
                    323:
                    324:        if (',' == p[*pos]) {
                    325:                (*pos)++;
                    326:                goto row;
                    327:        } else if ('\0' == p[*pos])
                    328:                return;
                    329:
                    330:        if ( ! cell(tbl, rp, ln, p, pos))
                    331:                return;
                    332:
                    333:        goto cell;
                    334:        /* NOTREACHED */
                    335: }
                    336:
                    337: int
1.6       kristaps  338: tbl_layout(struct tbl_node *tbl, int ln, const char *p)
1.1       kristaps  339: {
                    340:        int              pos;
                    341:
                    342:        pos = 0;
                    343:        row(tbl, ln, p, &pos);
                    344:
                    345:        /* Always succeed. */
                    346:        return(1);
                    347: }
1.5       kristaps  348:
                    349: static struct tbl_cell *
1.6       kristaps  350: cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos)
1.5       kristaps  351: {
                    352:        struct tbl_cell *p, *pp;
                    353:        struct tbl_head *h, *hp;
                    354:
                    355:        p = mandoc_calloc(1, sizeof(struct tbl_cell));
                    356:
                    357:        if (NULL != (pp = rp->last)) {
                    358:                rp->last->next = p;
                    359:                rp->last = p;
                    360:        } else
                    361:                rp->last = rp->first = p;
                    362:
                    363:        p->pos = pos;
                    364:
                    365:        /*
                    366:         * This is a little bit complicated.  Here we determine the
                    367:         * header the corresponds to a cell.  We add headers dynamically
                    368:         * when need be or re-use them, otherwise.  As an example, given
                    369:         * the following:
                    370:         *
                    371:         *      1  c || l
                    372:         *      2  | c | l
                    373:         *      3  l l
                    374:         *      3  || c | l |.
                    375:         *
                    376:         * We first add the new headers (as there are none) in (1); then
                    377:         * in (2) we insert the first spanner (as it doesn't match up
                    378:         * with the header); then we re-use the prior data headers,
                    379:         * skipping over the spanners; then we re-use everything and add
                    380:         * a last spanner.  Note that VERT headers are made into DVERT
                    381:         * ones.
                    382:         */
                    383:
1.8       kristaps  384:        h = pp ? pp->head->next : tbl->first_head;
1.5       kristaps  385:
                    386:        if (h) {
                    387:                /* Re-use data header. */
                    388:                if (TBL_HEAD_DATA == h->pos &&
                    389:                                (TBL_CELL_VERT != p->pos &&
                    390:                                 TBL_CELL_DVERT != p->pos)) {
                    391:                        p->head = h;
                    392:                        return(p);
                    393:                }
                    394:
                    395:                /* Re-use spanner header. */
                    396:                if (TBL_HEAD_DATA != h->pos &&
                    397:                                (TBL_CELL_VERT == p->pos ||
                    398:                                 TBL_CELL_DVERT == p->pos)) {
                    399:                        head_adjust(p, h);
                    400:                        p->head = h;
                    401:                        return(p);
                    402:                }
                    403:
                    404:                /* Right-shift headers with a new spanner. */
                    405:                if (TBL_HEAD_DATA == h->pos &&
                    406:                                (TBL_CELL_VERT == p->pos ||
                    407:                                 TBL_CELL_DVERT == p->pos)) {
                    408:                        hp = mandoc_calloc(1, sizeof(struct tbl_head));
1.9       kristaps  409:                        hp->ident = tbl->opts.cols++;
1.5       kristaps  410:                        hp->prev = h->prev;
                    411:                        if (h->prev)
                    412:                                h->prev->next = hp;
1.7       kristaps  413:                        if (h == tbl->first_head)
                    414:                                tbl->first_head = hp;
1.5       kristaps  415:                        h->prev = hp;
                    416:                        hp->next = h;
                    417:                        head_adjust(p, hp);
                    418:                        p->head = hp;
                    419:                        return(p);
                    420:                }
                    421:
                    422:                if (NULL != (h = h->next)) {
                    423:                        head_adjust(p, h);
                    424:                        p->head = h;
                    425:                        return(p);
                    426:                }
                    427:
                    428:                /* Fall through to default case... */
                    429:        }
                    430:
                    431:        hp = mandoc_calloc(1, sizeof(struct tbl_head));
1.9       kristaps  432:        hp->ident = tbl->opts.cols++;
1.5       kristaps  433:
                    434:        if (tbl->last_head) {
                    435:                hp->prev = tbl->last_head;
                    436:                tbl->last_head->next = hp;
                    437:                tbl->last_head = hp;
                    438:        } else
                    439:                tbl->last_head = tbl->first_head = hp;
                    440:
                    441:        head_adjust(p, hp);
                    442:        p->head = hp;
                    443:        return(p);
                    444: }
                    445:
                    446: static void
1.19      joerg     447: head_adjust(const struct tbl_cell *cellp, struct tbl_head *head)
1.5       kristaps  448: {
1.19      joerg     449:        if (TBL_CELL_VERT != cellp->pos &&
                    450:                        TBL_CELL_DVERT != cellp->pos) {
1.5       kristaps  451:                head->pos = TBL_HEAD_DATA;
                    452:                return;
                    453:        }
                    454:
1.19      joerg     455:        if (TBL_CELL_VERT == cellp->pos)
1.5       kristaps  456:                if (TBL_HEAD_DVERT != head->pos)
                    457:                        head->pos = TBL_HEAD_VERT;
                    458:
1.19      joerg     459:        if (TBL_CELL_DVERT == cellp->pos)
1.5       kristaps  460:                head->pos = TBL_HEAD_DVERT;
                    461: }
                    462:

CVSweb