[BACK]Return to tbl_layout.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/tbl_layout.c, Revision 1.5

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