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

Annotation of mandoc/tbl_layout.c, Revision 1.43

1.43    ! schwarze    1: /*     $Id: tbl_layout.c,v 1.42 2017/06/08 18:11:22 schwarze Exp $ */
1.1       kristaps    2: /*
1.22      schwarze    3:  * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.42      schwarze    4:  * Copyright (c) 2012, 2014, 2015, 2017 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.32      schwarze   37: static const struct tbl_phrase keys[] = {
1.1       kristaps   38:        { 'c',           TBL_CELL_CENTRE },
                     39:        { 'r',           TBL_CELL_RIGHT },
                     40:        { 'l',           TBL_CELL_LEFT },
                     41:        { 'n',           TBL_CELL_NUMBER },
                     42:        { 's',           TBL_CELL_SPAN },
                     43:        { 'a',           TBL_CELL_LONG },
                     44:        { '^',           TBL_CELL_DOWN },
                     45:        { '-',           TBL_CELL_HORIZ },
                     46:        { '_',           TBL_CELL_HORIZ },
1.23      schwarze   47:        { '=',           TBL_CELL_DHORIZ }
1.1       kristaps   48: };
                     49:
1.32      schwarze   50: #define KEYS_MAX ((int)(sizeof(keys)/sizeof(keys[0])))
                     51:
                     52: static void             mods(struct tbl_node *, struct tbl_cell *,
1.5       kristaps   53:                                int, const char *, int *);
1.32      schwarze   54: static void             cell(struct tbl_node *, struct tbl_row *,
1.5       kristaps   55:                                int, const char *, int *);
1.23      schwarze   56: static struct tbl_cell *cell_alloc(struct tbl_node *, struct tbl_row *,
1.33      schwarze   57:                                enum tbl_cellt);
1.1       kristaps   58:
1.26      schwarze   59:
1.32      schwarze   60: static void
1.26      schwarze   61: mods(struct tbl_node *tbl, struct tbl_cell *cp,
1.1       kristaps   62:                int ln, const char *p, int *pos)
                     63: {
1.32      schwarze   64:        char            *endptr;
1.42      schwarze   65:        size_t           sz;
1.20      kristaps   66:
1.32      schwarze   67: mod:
                     68:        while (p[*pos] == ' ' || p[*pos] == '\t')
                     69:                (*pos)++;
1.20      kristaps   70:
1.32      schwarze   71:        /* Row delimiters and cell specifiers end modifier lists. */
1.1       kristaps   72:
1.33      schwarze   73:        if (strchr(".,-=^_ACLNRSaclnrs", p[*pos]) != NULL)
1.32      schwarze   74:                return;
1.12      kristaps   75:
                     76:        /* Throw away parenthesised expression. */
                     77:
                     78:        if ('(' == p[*pos]) {
                     79:                (*pos)++;
                     80:                while (p[*pos] && ')' != p[*pos])
                     81:                        (*pos)++;
                     82:                if (')' == p[*pos]) {
                     83:                        (*pos)++;
                     84:                        goto mod;
                     85:                }
1.32      schwarze   86:                mandoc_msg(MANDOCERR_TBLLAYOUT_PAR, tbl->parse,
1.26      schwarze   87:                    ln, *pos, NULL);
1.32      schwarze   88:                return;
1.1       kristaps   89:        }
                     90:
                     91:        /* Parse numerical spacing from modifier string. */
                     92:
                     93:        if (isdigit((unsigned char)p[*pos])) {
1.32      schwarze   94:                cp->spacing = strtoull(p + *pos, &endptr, 10);
                     95:                *pos = endptr - p;
1.1       kristaps   96:                goto mod;
1.26      schwarze   97:        }
1.1       kristaps   98:
1.13      joerg      99:        switch (tolower((unsigned char)p[(*pos)++])) {
1.32      schwarze  100:        case 'b':
1.38      schwarze  101:                cp->flags |= TBL_CELL_BOLD;
                    102:                goto mod;
1.32      schwarze  103:        case 'd':
                    104:                cp->flags |= TBL_CELL_BALIGN;
1.1       kristaps  105:                goto mod;
1.26      schwarze  106:        case 'e':
1.1       kristaps  107:                cp->flags |= TBL_CELL_EQUAL;
                    108:                goto mod;
1.32      schwarze  109:        case 'f':
                    110:                break;
1.38      schwarze  111:        case 'i':
                    112:                cp->flags |= TBL_CELL_ITALIC;
                    113:                goto mod;
1.32      schwarze  114:        case 'm':
                    115:                mandoc_msg(MANDOCERR_TBLLAYOUT_MOD, tbl->parse,
                    116:                    ln, *pos, "m");
                    117:                goto mod;
                    118:        case 'p':
                    119:        case 'v':
                    120:                if (p[*pos] == '-' || p[*pos] == '+')
                    121:                        (*pos)++;
                    122:                while (isdigit((unsigned char)p[*pos]))
                    123:                        (*pos)++;
                    124:                goto mod;
1.26      schwarze  125:        case 't':
1.1       kristaps  126:                cp->flags |= TBL_CELL_TALIGN;
                    127:                goto mod;
1.32      schwarze  128:        case 'u':
                    129:                cp->flags |= TBL_CELL_UP;
1.10      schwarze  130:                goto mod;
1.42      schwarze  131:        case 'w':
                    132:                sz = 0;
                    133:                if (p[*pos] == '(') {
                    134:                        (*pos)++;
                    135:                        while (p[*pos + sz] != '\0' && p[*pos + sz] != ')')
                    136:                                sz++;
                    137:                } else
                    138:                        while (isdigit((unsigned char)p[*pos + sz]))
                    139:                                sz++;
                    140:                if (sz) {
                    141:                        free(cp->wstr);
                    142:                        cp->wstr = mandoc_strndup(p + *pos, sz);
                    143:                        *pos += sz;
                    144:                        if (p[*pos] == ')')
                    145:                                (*pos)++;
                    146:                }
1.29      schwarze  147:                goto mod;
                    148:        case 'x':
                    149:                cp->flags |= TBL_CELL_WMAX;
1.1       kristaps  150:                goto mod;
1.32      schwarze  151:        case 'z':
                    152:                cp->flags |= TBL_CELL_WIGN;
                    153:                goto mod;
1.33      schwarze  154:        case '|':
                    155:                if (cp->vert < 2)
                    156:                        cp->vert++;
                    157:                else
                    158:                        mandoc_msg(MANDOCERR_TBLLAYOUT_VERT,
                    159:                            tbl->parse, ln, *pos - 1, NULL);
                    160:                goto mod;
1.1       kristaps  161:        default:
1.32      schwarze  162:                mandoc_vmsg(MANDOCERR_TBLLAYOUT_CHAR, tbl->parse,
                    163:                    ln, *pos - 1, "%c", p[*pos - 1]);
                    164:                goto mod;
1.1       kristaps  165:        }
                    166:
1.38      schwarze  167:        /* Ignore parenthised font names for now. */
                    168:
                    169:        if (p[*pos] == '(')
                    170:                goto mod;
                    171:
                    172:        /* Support only one-character font-names for now. */
                    173:
                    174:        if (p[*pos] == '\0' || (p[*pos + 1] != ' ' && p[*pos + 1] != '.')) {
                    175:                mandoc_vmsg(MANDOCERR_FT_BAD, tbl->parse,
                    176:                    ln, *pos, "TS %s", p + *pos - 1);
                    177:                if (p[*pos] != '\0')
                    178:                        (*pos)++;
                    179:                if (p[*pos] != '\0')
                    180:                        (*pos)++;
                    181:                goto mod;
                    182:        }
                    183:
                    184:        switch (p[(*pos)++]) {
1.26      schwarze  185:        case '3':
1.38      schwarze  186:        case 'B':
1.1       kristaps  187:                cp->flags |= TBL_CELL_BOLD;
                    188:                goto mod;
1.26      schwarze  189:        case '2':
1.38      schwarze  190:        case 'I':
1.1       kristaps  191:                cp->flags |= TBL_CELL_ITALIC;
1.21      kristaps  192:                goto mod;
1.26      schwarze  193:        case '1':
1.38      schwarze  194:        case 'R':
1.1       kristaps  195:                goto mod;
                    196:        default:
1.28      schwarze  197:                mandoc_vmsg(MANDOCERR_FT_BAD, tbl->parse,
                    198:                    ln, *pos - 1, "TS f%c", p[*pos - 1]);
                    199:                goto mod;
1.1       kristaps  200:        }
                    201: }
                    202:
1.32      schwarze  203: static void
1.26      schwarze  204: cell(struct tbl_node *tbl, struct tbl_row *rp,
1.1       kristaps  205:                int ln, const char *p, int *pos)
                    206: {
1.33      schwarze  207:        int              i;
1.1       kristaps  208:        enum tbl_cellt   c;
                    209:
1.33      schwarze  210:        /* Handle leading vertical lines */
1.23      schwarze  211:
1.32      schwarze  212:        while (p[*pos] == ' ' || p[*pos] == '\t' || p[*pos] == '|') {
                    213:                if (p[*pos] == '|') {
1.33      schwarze  214:                        if (rp->vert < 2)
                    215:                                rp->vert++;
1.32      schwarze  216:                        else
                    217:                                mandoc_msg(MANDOCERR_TBLLAYOUT_VERT,
                    218:                                    tbl->parse, ln, *pos, NULL);
                    219:                }
1.23      schwarze  220:                (*pos)++;
1.32      schwarze  221:        }
1.25      schwarze  222:
1.33      schwarze  223: again:
                    224:        while (p[*pos] == ' ' || p[*pos] == '\t')
                    225:                (*pos)++;
1.25      schwarze  226:
1.33      schwarze  227:        if (p[*pos] == '.' || p[*pos] == '\0')
1.32      schwarze  228:                return;
1.23      schwarze  229:
                    230:        /* Parse the column position (`c', `l', `r', ...). */
1.1       kristaps  231:
                    232:        for (i = 0; i < KEYS_MAX; i++)
1.13      joerg     233:                if (tolower((unsigned char)p[*pos]) == keys[i].name)
1.1       kristaps  234:                        break;
                    235:
1.32      schwarze  236:        if (i == KEYS_MAX) {
                    237:                mandoc_vmsg(MANDOCERR_TBLLAYOUT_CHAR, tbl->parse,
                    238:                    ln, *pos, "%c", p[*pos]);
                    239:                (*pos)++;
                    240:                goto again;
1.1       kristaps  241:        }
1.11      kristaps  242:        c = keys[i].key;
                    243:
1.32      schwarze  244:        /* Special cases of spanners. */
1.16      kristaps  245:
1.32      schwarze  246:        if (c == TBL_CELL_SPAN) {
                    247:                if (rp->last == NULL)
                    248:                        mandoc_msg(MANDOCERR_TBLLAYOUT_SPAN,
                    249:                            tbl->parse, ln, *pos, NULL);
                    250:                else if (rp->last->pos == TBL_CELL_HORIZ ||
                    251:                    rp->last->pos == TBL_CELL_DHORIZ)
                    252:                        c = rp->last->pos;
                    253:        } else if (c == TBL_CELL_DOWN && rp == tbl->first_row)
                    254:                mandoc_msg(MANDOCERR_TBLLAYOUT_DOWN,
                    255:                    tbl->parse, ln, *pos, NULL);
1.11      kristaps  256:
1.1       kristaps  257:        (*pos)++;
                    258:
                    259:        /* Allocate cell then parse its modifiers. */
                    260:
1.33      schwarze  261:        mods(tbl, cell_alloc(tbl, rp, c), ln, p, pos);
1.1       kristaps  262: }
                    263:
1.31      schwarze  264: void
1.34      schwarze  265: tbl_layout(struct tbl_node *tbl, int ln, const char *p, int pos)
1.1       kristaps  266: {
                    267:        struct tbl_row  *rp;
                    268:
1.30      schwarze  269:        rp = NULL;
                    270:        for (;;) {
                    271:                /* Skip whitespace before and after each cell. */
1.1       kristaps  272:
1.32      schwarze  273:                while (p[pos] == ' ' || p[pos] == '\t')
1.30      schwarze  274:                        pos++;
1.1       kristaps  275:
1.30      schwarze  276:                switch (p[pos]) {
                    277:                case ',':  /* Next row on this input line. */
                    278:                        pos++;
                    279:                        rp = NULL;
                    280:                        continue;
                    281:                case '\0':  /* Next row on next input line. */
1.31      schwarze  282:                        return;
1.30      schwarze  283:                case '.':  /* End of layout. */
                    284:                        pos++;
                    285:                        tbl->part = TBL_PART_DATA;
1.33      schwarze  286:
                    287:                        /*
                    288:                         * When the layout is completely empty,
                    289:                         * default to one left-justified column.
                    290:                         */
                    291:
                    292:                        if (tbl->first_row == NULL) {
1.35      schwarze  293:                                tbl->first_row = tbl->last_row =
                    294:                                    mandoc_calloc(1, sizeof(*rp));
                    295:                        }
                    296:                        if (tbl->first_row->first == NULL) {
1.33      schwarze  297:                                mandoc_msg(MANDOCERR_TBLLAYOUT_NONE,
                    298:                                    tbl->parse, ln, pos, NULL);
1.35      schwarze  299:                                cell_alloc(tbl, tbl->first_row,
                    300:                                    TBL_CELL_LEFT);
1.43    ! schwarze  301:                                if (tbl->opts.lvert < tbl->first_row->vert)
        !           302:                                        tbl->opts.lvert = tbl->first_row->vert;
1.31      schwarze  303:                                return;
1.33      schwarze  304:                        }
                    305:
                    306:                        /*
                    307:                         * Search for the widest line
                    308:                         * along the left and right margins.
                    309:                         */
                    310:
                    311:                        for (rp = tbl->first_row; rp; rp = rp->next) {
                    312:                                if (tbl->opts.lvert < rp->vert)
                    313:                                        tbl->opts.lvert = rp->vert;
                    314:                                if (rp->last != NULL &&
1.37      schwarze  315:                                    rp->last->col + 1 == tbl->opts.cols &&
1.33      schwarze  316:                                    tbl->opts.rvert < rp->last->vert)
                    317:                                        tbl->opts.rvert = rp->last->vert;
1.35      schwarze  318:
                    319:                                /* If the last line is empty, drop it. */
                    320:
                    321:                                if (rp->next != NULL &&
                    322:                                    rp->next->first == NULL) {
                    323:                                        free(rp->next);
                    324:                                        rp->next = NULL;
1.39      schwarze  325:                                        tbl->last_row = rp;
1.35      schwarze  326:                                }
1.33      schwarze  327:                        }
1.31      schwarze  328:                        return;
1.30      schwarze  329:                default:  /* Cell. */
                    330:                        break;
                    331:                }
                    332:
1.35      schwarze  333:                /*
                    334:                 * If the last line had at least one cell,
                    335:                 * start a new one; otherwise, continue it.
                    336:                 */
                    337:
                    338:                if (rp == NULL) {
                    339:                        if (tbl->last_row == NULL ||
                    340:                            tbl->last_row->first != NULL) {
                    341:                                rp = mandoc_calloc(1, sizeof(*rp));
                    342:                                if (tbl->last_row)
                    343:                                        tbl->last_row->next = rp;
                    344:                                else
                    345:                                        tbl->first_row = rp;
                    346:                                tbl->last_row = rp;
                    347:                        } else
                    348:                                rp = tbl->last_row;
1.30      schwarze  349:                }
1.32      schwarze  350:                cell(tbl, rp, ln, p, &pos);
1.1       kristaps  351:        }
                    352: }
1.5       kristaps  353:
                    354: static struct tbl_cell *
1.33      schwarze  355: cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos)
1.5       kristaps  356: {
                    357:        struct tbl_cell *p, *pp;
                    358:
1.36      schwarze  359:        p = mandoc_calloc(1, sizeof(*p));
1.37      schwarze  360:        p->pos = pos;
1.5       kristaps  361:
1.36      schwarze  362:        if ((pp = rp->last) != NULL) {
1.23      schwarze  363:                pp->next = p;
1.37      schwarze  364:                p->col = pp->col + 1;
                    365:        } else
1.23      schwarze  366:                rp->first = p;
                    367:        rp->last = p;
1.5       kristaps  368:
1.37      schwarze  369:        if (tbl->opts.cols <= p->col)
                    370:                tbl->opts.cols = p->col + 1;
1.5       kristaps  371:
1.40      schwarze  372:        return p;
1.5       kristaps  373: }

CVSweb