[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.28

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