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

Annotation of mandoc/tbl_layout.c, Revision 1.16

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

CVSweb