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

Annotation of mandoc/tbl_layout.c, Revision 1.11

1.11    ! kristaps    1: /*     $Id: tbl_layout.c,v 1.10 2011/01/04 23:48:39 schwarze 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;
                     91:        }
                     92:
                     93:        /* Parse numerical spacing from modifier string. */
                     94:
                     95:        if (isdigit((unsigned char)p[*pos])) {
                     96:                for (i = 0; i < 4; i++) {
                     97:                        if ( ! isdigit((unsigned char)p[*pos + i]))
                     98:                                break;
                     99:                        buf[i] = p[*pos + i];
                    100:                }
                    101:                buf[i] = '\0';
                    102:
                    103:                /* No greater than 4 digits. */
                    104:
                    105:                if (4 == i) {
                    106:                        TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos);
                    107:                        return(0);
                    108:                }
                    109:
                    110:                *pos += i;
                    111:                cp->spacing = atoi(buf);
                    112:
                    113:                goto mod;
                    114:                /* NOTREACHED */
                    115:        }
                    116:
                    117:        /* TODO: GNU has many more extensions. */
                    118:
1.2       kristaps  119:        switch (tolower(p[(*pos)++])) {
1.1       kristaps  120:        case ('z'):
                    121:                cp->flags |= TBL_CELL_WIGN;
                    122:                goto mod;
                    123:        case ('u'):
                    124:                cp->flags |= TBL_CELL_UP;
                    125:                goto mod;
                    126:        case ('e'):
                    127:                cp->flags |= TBL_CELL_EQUAL;
                    128:                goto mod;
                    129:        case ('t'):
                    130:                cp->flags |= TBL_CELL_TALIGN;
                    131:                goto mod;
                    132:        case ('d'):
                    133:                cp->flags |= TBL_CELL_BALIGN;
1.10      schwarze  134:                goto mod;
                    135:        case ('w'):  /* XXX for now, ignore minimal column width */
1.1       kristaps  136:                goto mod;
                    137:        case ('f'):
1.2       kristaps  138:                break;
1.1       kristaps  139:        case ('b'):
                    140:                /* FALLTHROUGH */
                    141:        case ('i'):
1.2       kristaps  142:                (*pos)--;
1.1       kristaps  143:                break;
                    144:        default:
                    145:                TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos - 1);
                    146:                return(0);
                    147:        }
                    148:
1.2       kristaps  149:        switch (tolower(p[(*pos)++])) {
1.1       kristaps  150:        case ('b'):
                    151:                cp->flags |= TBL_CELL_BOLD;
                    152:                goto mod;
                    153:        case ('i'):
                    154:                cp->flags |= TBL_CELL_ITALIC;
                    155:                goto mod;
                    156:        default:
                    157:                break;
                    158:        }
                    159:
                    160:        TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos - 1);
                    161:        return(0);
                    162: }
                    163:
                    164: static int
1.6       kristaps  165: cell(struct tbl_node *tbl, struct tbl_row *rp,
1.1       kristaps  166:                int ln, const char *p, int *pos)
                    167: {
                    168:        int              i;
                    169:        enum tbl_cellt   c;
                    170:
                    171:        /* Parse the column position (`r', `R', `|', ...). */
                    172:
                    173:        for (i = 0; i < KEYS_MAX; i++)
1.2       kristaps  174:                if (tolower(p[*pos]) == keys[i].name)
1.1       kristaps  175:                        break;
                    176:
                    177:        if (KEYS_MAX == i) {
                    178:                TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos);
                    179:                return(0);
                    180:        }
                    181:
1.11    ! kristaps  182:        c = keys[i].key;
        !           183:
        !           184:        /*
        !           185:         * If a span cell is found first, raise a warning and abort the
        !           186:         * parse.  FIXME: recover from this somehow?
        !           187:         */
        !           188:
        !           189:        if (NULL == rp->first && TBL_CELL_SPAN == c) {
        !           190:                TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos);
        !           191:                return(0);
        !           192:        }
        !           193:
1.1       kristaps  194:        (*pos)++;
                    195:
                    196:        /* Extra check for the double-vertical. */
                    197:
                    198:        if (TBL_CELL_VERT == c && '|' == p[*pos]) {
                    199:                (*pos)++;
                    200:                c = TBL_CELL_DVERT;
                    201:        }
                    202:
                    203:        /* Disallow adjacent spacers. */
                    204:
                    205:        if (rp->last && (TBL_CELL_VERT == c || TBL_CELL_DVERT == c) &&
                    206:                        (TBL_CELL_VERT == rp->last->pos ||
                    207:                         TBL_CELL_DVERT == rp->last->pos)) {
                    208:                TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos - 1);
                    209:                return(0);
                    210:        }
                    211:
                    212:        /* Allocate cell then parse its modifiers. */
                    213:
1.5       kristaps  214:        return(mods(tbl, cell_alloc(tbl, rp, c), ln, p, pos));
1.1       kristaps  215: }
                    216:
                    217:
                    218: static void
1.6       kristaps  219: row(struct tbl_node *tbl, int ln, const char *p, int *pos)
1.1       kristaps  220: {
                    221:        struct tbl_row  *rp;
                    222:
                    223: row:   /*
                    224:         * EBNF describing this section:
                    225:         *
                    226:         * row          ::= row_list [:space:]* [.]?[\n]
                    227:         * row_list     ::= [:space:]* row_elem row_tail
                    228:         * row_tail     ::= [:space:]*[,] row_list |
                    229:         *                  epsilon
                    230:         * row_elem     ::= [\t\ ]*[:alpha:]+
                    231:         */
                    232:
                    233:        rp = mandoc_calloc(1, sizeof(struct tbl_row));
1.3       kristaps  234:        if (tbl->last_row) {
                    235:                tbl->last_row->next = rp;
                    236:                tbl->last_row = rp;
1.1       kristaps  237:        } else
1.3       kristaps  238:                tbl->last_row = tbl->first_row = rp;
1.1       kristaps  239:
                    240: cell:
                    241:        while (isspace((unsigned char)p[*pos]))
                    242:                (*pos)++;
                    243:
                    244:        /* Safely exit layout context. */
                    245:
                    246:        if ('.' == p[*pos]) {
                    247:                tbl->part = TBL_PART_DATA;
1.3       kristaps  248:                if (NULL == tbl->first_row)
1.1       kristaps  249:                        TBL_MSG(tbl, MANDOCERR_TBLNOLAYOUT, ln, *pos);
                    250:                (*pos)++;
                    251:                return;
                    252:        }
                    253:
                    254:        /* End (and possibly restart) a row. */
                    255:
                    256:        if (',' == p[*pos]) {
                    257:                (*pos)++;
                    258:                goto row;
                    259:        } else if ('\0' == p[*pos])
                    260:                return;
                    261:
                    262:        if ( ! cell(tbl, rp, ln, p, pos))
                    263:                return;
                    264:
                    265:        goto cell;
                    266:        /* NOTREACHED */
                    267: }
                    268:
                    269: int
1.6       kristaps  270: tbl_layout(struct tbl_node *tbl, int ln, const char *p)
1.1       kristaps  271: {
                    272:        int              pos;
                    273:
                    274:        pos = 0;
                    275:        row(tbl, ln, p, &pos);
                    276:
                    277:        /* Always succeed. */
                    278:        return(1);
                    279: }
1.5       kristaps  280:
                    281: static struct tbl_cell *
1.6       kristaps  282: cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos)
1.5       kristaps  283: {
                    284:        struct tbl_cell *p, *pp;
                    285:        struct tbl_head *h, *hp;
                    286:
                    287:        p = mandoc_calloc(1, sizeof(struct tbl_cell));
                    288:
                    289:        if (NULL != (pp = rp->last)) {
                    290:                rp->last->next = p;
                    291:                rp->last = p;
                    292:        } else
                    293:                rp->last = rp->first = p;
                    294:
                    295:        p->pos = pos;
                    296:
                    297:        /*
                    298:         * This is a little bit complicated.  Here we determine the
                    299:         * header the corresponds to a cell.  We add headers dynamically
                    300:         * when need be or re-use them, otherwise.  As an example, given
                    301:         * the following:
                    302:         *
                    303:         *      1  c || l
                    304:         *      2  | c | l
                    305:         *      3  l l
                    306:         *      3  || c | l |.
                    307:         *
                    308:         * We first add the new headers (as there are none) in (1); then
                    309:         * in (2) we insert the first spanner (as it doesn't match up
                    310:         * with the header); then we re-use the prior data headers,
                    311:         * skipping over the spanners; then we re-use everything and add
                    312:         * a last spanner.  Note that VERT headers are made into DVERT
                    313:         * ones.
                    314:         */
                    315:
1.8       kristaps  316:        h = pp ? pp->head->next : tbl->first_head;
1.5       kristaps  317:
                    318:        if (h) {
                    319:                /* Re-use data header. */
                    320:                if (TBL_HEAD_DATA == h->pos &&
                    321:                                (TBL_CELL_VERT != p->pos &&
                    322:                                 TBL_CELL_DVERT != p->pos)) {
                    323:                        p->head = h;
                    324:                        return(p);
                    325:                }
                    326:
                    327:                /* Re-use spanner header. */
                    328:                if (TBL_HEAD_DATA != h->pos &&
                    329:                                (TBL_CELL_VERT == p->pos ||
                    330:                                 TBL_CELL_DVERT == p->pos)) {
                    331:                        head_adjust(p, h);
                    332:                        p->head = h;
                    333:                        return(p);
                    334:                }
                    335:
                    336:                /* Right-shift headers with a new spanner. */
                    337:                if (TBL_HEAD_DATA == h->pos &&
                    338:                                (TBL_CELL_VERT == p->pos ||
                    339:                                 TBL_CELL_DVERT == p->pos)) {
                    340:                        hp = mandoc_calloc(1, sizeof(struct tbl_head));
1.9       kristaps  341:                        hp->ident = tbl->opts.cols++;
1.5       kristaps  342:                        hp->prev = h->prev;
                    343:                        if (h->prev)
                    344:                                h->prev->next = hp;
1.7       kristaps  345:                        if (h == tbl->first_head)
                    346:                                tbl->first_head = hp;
1.5       kristaps  347:                        h->prev = hp;
                    348:                        hp->next = h;
                    349:                        head_adjust(p, hp);
                    350:                        p->head = hp;
                    351:                        return(p);
                    352:                }
                    353:
                    354:                if (NULL != (h = h->next)) {
                    355:                        head_adjust(p, h);
                    356:                        p->head = h;
                    357:                        return(p);
                    358:                }
                    359:
                    360:                /* Fall through to default case... */
                    361:        }
                    362:
                    363:        hp = mandoc_calloc(1, sizeof(struct tbl_head));
1.9       kristaps  364:        hp->ident = tbl->opts.cols++;
1.5       kristaps  365:
                    366:        if (tbl->last_head) {
                    367:                hp->prev = tbl->last_head;
                    368:                tbl->last_head->next = hp;
                    369:                tbl->last_head = hp;
                    370:        } else
                    371:                tbl->last_head = tbl->first_head = hp;
                    372:
                    373:        head_adjust(p, hp);
                    374:        p->head = hp;
                    375:        return(p);
                    376: }
                    377:
                    378: static void
                    379: head_adjust(const struct tbl_cell *cell, struct tbl_head *head)
                    380: {
                    381:        if (TBL_CELL_VERT != cell->pos &&
                    382:                        TBL_CELL_DVERT != cell->pos) {
                    383:                head->pos = TBL_HEAD_DATA;
                    384:                return;
                    385:        }
                    386:
                    387:        if (TBL_CELL_VERT == cell->pos)
                    388:                if (TBL_HEAD_DVERT != head->pos)
                    389:                        head->pos = TBL_HEAD_VERT;
                    390:
                    391:        if (TBL_CELL_DVERT == cell->pos)
                    392:                head->pos = TBL_HEAD_DVERT;
                    393: }
                    394:

CVSweb