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

1.26    ! schwarze    1: /*     $OpenBSD: tbl_layout.c,v 1.25 2015/02/10 11:02:19 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:                /* FALLTHROUGH */
                    117:        case 'v':
                    118:                if (p[*pos] == '-' || p[*pos] == '+')
                    119:                        (*pos)++;
                    120:                while (isdigit((unsigned char)p[*pos]))
                    121:                        (*pos)++;
                    122:                goto mod;
1.14      schwarze  123:        case 't':
1.1       schwarze  124:                cp->flags |= TBL_CELL_TALIGN;
1.4       schwarze  125:                goto mod;
1.19      schwarze  126:        case 'u':
                    127:                cp->flags |= TBL_CELL_UP;
1.5       schwarze  128:                goto mod;
1.14      schwarze  129:        case 'w':  /* XXX for now, ignore minimal column width */
1.16      schwarze  130:                goto mod;
                    131:        case 'x':
                    132:                cp->flags |= TBL_CELL_WMAX;
1.4       schwarze  133:                goto mod;
1.19      schwarze  134:        case 'z':
                    135:                cp->flags |= TBL_CELL_WIGN;
                    136:                goto mod;
1.20      schwarze  137:        case '|':
                    138:                if (cp->vert < 2)
                    139:                        cp->vert++;
                    140:                else
                    141:                        mandoc_msg(MANDOCERR_TBLLAYOUT_VERT,
                    142:                            tbl->parse, ln, *pos - 1, NULL);
                    143:                goto mod;
1.1       schwarze  144:        default:
1.19      schwarze  145:                mandoc_vmsg(MANDOCERR_TBLLAYOUT_CHAR, tbl->parse,
                    146:                    ln, *pos - 1, "%c", p[*pos - 1]);
                    147:                goto mod;
1.1       schwarze  148:        }
                    149:
1.25      schwarze  150:        /* Ignore parenthised font names for now. */
                    151:
                    152:        if (p[*pos] == '(')
                    153:                goto mod;
                    154:
                    155:        /* Support only one-character font-names for now. */
                    156:
                    157:        if (p[*pos] == '\0' || (p[*pos + 1] != ' ' && p[*pos + 1] != '.')) {
                    158:                mandoc_vmsg(MANDOCERR_FT_BAD, tbl->parse,
                    159:                    ln, *pos, "TS %s", p + *pos - 1);
                    160:                if (p[*pos] != '\0')
                    161:                        (*pos)++;
                    162:                if (p[*pos] != '\0')
                    163:                        (*pos)++;
                    164:                goto mod;
                    165:        }
                    166:
                    167:        switch (p[(*pos)++]) {
1.14      schwarze  168:        case '3':
1.10      schwarze  169:                /* FALLTHROUGH */
1.25      schwarze  170:        case 'B':
1.1       schwarze  171:                cp->flags |= TBL_CELL_BOLD;
1.4       schwarze  172:                goto mod;
1.14      schwarze  173:        case '2':
1.10      schwarze  174:                /* FALLTHROUGH */
1.25      schwarze  175:        case 'I':
1.1       schwarze  176:                cp->flags |= TBL_CELL_ITALIC;
1.10      schwarze  177:                goto mod;
1.14      schwarze  178:        case '1':
1.10      schwarze  179:                /* FALLTHROUGH */
1.25      schwarze  180:        case 'R':
1.4       schwarze  181:                goto mod;
1.1       schwarze  182:        default:
1.15      schwarze  183:                mandoc_vmsg(MANDOCERR_FT_BAD, tbl->parse,
                    184:                    ln, *pos - 1, "TS f%c", p[*pos - 1]);
                    185:                goto mod;
1.1       schwarze  186:        }
                    187: }
                    188:
1.19      schwarze  189: static void
1.14      schwarze  190: cell(struct tbl_node *tbl, struct tbl_row *rp,
1.4       schwarze  191:                int ln, const char *p, int *pos)
1.1       schwarze  192: {
1.20      schwarze  193:        int              i;
1.1       schwarze  194:        enum tbl_cellt   c;
                    195:
1.20      schwarze  196:        /* Handle leading vertical lines */
1.11      schwarze  197:
1.19      schwarze  198:        while (p[*pos] == ' ' || p[*pos] == '\t' || p[*pos] == '|') {
                    199:                if (p[*pos] == '|') {
1.20      schwarze  200:                        if (rp->vert < 2)
                    201:                                rp->vert++;
1.19      schwarze  202:                        else
                    203:                                mandoc_msg(MANDOCERR_TBLLAYOUT_VERT,
                    204:                                    tbl->parse, ln, *pos, NULL);
                    205:                }
1.11      schwarze  206:                (*pos)++;
1.19      schwarze  207:        }
1.13      schwarze  208:
1.20      schwarze  209: again:
                    210:        while (p[*pos] == ' ' || p[*pos] == '\t')
                    211:                (*pos)++;
1.13      schwarze  212:
1.20      schwarze  213:        if (p[*pos] == '.' || p[*pos] == '\0')
1.19      schwarze  214:                return;
1.11      schwarze  215:
                    216:        /* Parse the column position (`c', `l', `r', ...). */
1.1       schwarze  217:
1.4       schwarze  218:        for (i = 0; i < KEYS_MAX; i++)
1.6       schwarze  219:                if (tolower((unsigned char)p[*pos]) == keys[i].name)
1.4       schwarze  220:                        break;
                    221:
1.19      schwarze  222:        if (i == KEYS_MAX) {
                    223:                mandoc_vmsg(MANDOCERR_TBLLAYOUT_CHAR, tbl->parse,
                    224:                    ln, *pos, "%c", p[*pos]);
                    225:                (*pos)++;
                    226:                goto again;
1.1       schwarze  227:        }
1.6       schwarze  228:        c = keys[i].key;
                    229:
1.19      schwarze  230:        /* Special cases of spanners. */
1.7       schwarze  231:
1.19      schwarze  232:        if (c == TBL_CELL_SPAN) {
                    233:                if (rp->last == NULL)
                    234:                        mandoc_msg(MANDOCERR_TBLLAYOUT_SPAN,
                    235:                            tbl->parse, ln, *pos, NULL);
                    236:                else if (rp->last->pos == TBL_CELL_HORIZ ||
                    237:                    rp->last->pos == TBL_CELL_DHORIZ)
                    238:                        c = rp->last->pos;
                    239:        } else if (c == TBL_CELL_DOWN && rp == tbl->first_row)
                    240:                mandoc_msg(MANDOCERR_TBLLAYOUT_DOWN,
                    241:                    tbl->parse, ln, *pos, NULL);
1.6       schwarze  242:
1.4       schwarze  243:        (*pos)++;
1.1       schwarze  244:
                    245:        /* Allocate cell then parse its modifiers. */
                    246:
1.20      schwarze  247:        mods(tbl, cell_alloc(tbl, rp, c), ln, p, pos);
1.1       schwarze  248: }
                    249:
1.18      schwarze  250: void
1.21      schwarze  251: tbl_layout(struct tbl_node *tbl, int ln, const char *p, int pos)
1.1       schwarze  252: {
                    253:        struct tbl_row  *rp;
                    254:
1.17      schwarze  255:        rp = NULL;
                    256:        for (;;) {
                    257:                /* Skip whitespace before and after each cell. */
1.4       schwarze  258:
1.19      schwarze  259:                while (p[pos] == ' ' || p[pos] == '\t')
1.17      schwarze  260:                        pos++;
1.4       schwarze  261:
1.17      schwarze  262:                switch (p[pos]) {
                    263:                case ',':  /* Next row on this input line. */
                    264:                        pos++;
                    265:                        rp = NULL;
                    266:                        continue;
                    267:                case '\0':  /* Next row on next input line. */
1.18      schwarze  268:                        return;
1.17      schwarze  269:                case '.':  /* End of layout. */
                    270:                        pos++;
                    271:                        tbl->part = TBL_PART_DATA;
1.20      schwarze  272:
                    273:                        /*
                    274:                         * When the layout is completely empty,
                    275:                         * default to one left-justified column.
                    276:                         */
                    277:
                    278:                        if (tbl->first_row == NULL) {
1.22      schwarze  279:                                tbl->first_row = tbl->last_row =
                    280:                                    mandoc_calloc(1, sizeof(*rp));
                    281:                        }
                    282:                        if (tbl->first_row->first == NULL) {
1.20      schwarze  283:                                mandoc_msg(MANDOCERR_TBLLAYOUT_NONE,
                    284:                                    tbl->parse, ln, pos, NULL);
1.22      schwarze  285:                                cell_alloc(tbl, tbl->first_row,
                    286:                                    TBL_CELL_LEFT);
1.18      schwarze  287:                                return;
1.20      schwarze  288:                        }
                    289:
                    290:                        /*
                    291:                         * Search for the widest line
                    292:                         * along the left and right margins.
                    293:                         */
                    294:
                    295:                        for (rp = tbl->first_row; rp; rp = rp->next) {
                    296:                                if (tbl->opts.lvert < rp->vert)
                    297:                                        tbl->opts.lvert = rp->vert;
                    298:                                if (rp->last != NULL &&
1.24      schwarze  299:                                    rp->last->col + 1 == tbl->opts.cols &&
1.20      schwarze  300:                                    tbl->opts.rvert < rp->last->vert)
                    301:                                        tbl->opts.rvert = rp->last->vert;
1.22      schwarze  302:
                    303:                                /* If the last line is empty, drop it. */
                    304:
                    305:                                if (rp->next != NULL &&
                    306:                                    rp->next->first == NULL) {
                    307:                                        free(rp->next);
                    308:                                        rp->next = NULL;
1.26    ! schwarze  309:                                        tbl->last_row = rp;
1.22      schwarze  310:                                }
1.20      schwarze  311:                        }
1.18      schwarze  312:                        return;
1.17      schwarze  313:                default:  /* Cell. */
                    314:                        break;
                    315:                }
                    316:
1.22      schwarze  317:                /*
                    318:                 * If the last line had at least one cell,
                    319:                 * start a new one; otherwise, continue it.
                    320:                 */
                    321:
                    322:                if (rp == NULL) {
                    323:                        if (tbl->last_row == NULL ||
                    324:                            tbl->last_row->first != NULL) {
                    325:                                rp = mandoc_calloc(1, sizeof(*rp));
                    326:                                if (tbl->last_row)
                    327:                                        tbl->last_row->next = rp;
                    328:                                else
                    329:                                        tbl->first_row = rp;
                    330:                                tbl->last_row = rp;
                    331:                        } else
                    332:                                rp = tbl->last_row;
1.17      schwarze  333:                }
1.19      schwarze  334:                cell(tbl, rp, ln, p, &pos);
1.1       schwarze  335:        }
                    336: }
1.4       schwarze  337:
                    338: static struct tbl_cell *
1.20      schwarze  339: cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos)
1.4       schwarze  340: {
                    341:        struct tbl_cell *p, *pp;
                    342:
1.23      schwarze  343:        p = mandoc_calloc(1, sizeof(*p));
1.24      schwarze  344:        p->pos = pos;
1.4       schwarze  345:
1.23      schwarze  346:        if ((pp = rp->last) != NULL) {
1.11      schwarze  347:                pp->next = p;
1.24      schwarze  348:                p->col = pp->col + 1;
                    349:        } else
1.11      schwarze  350:                rp->first = p;
                    351:        rp->last = p;
1.4       schwarze  352:
1.24      schwarze  353:        if (tbl->opts.cols <= p->col)
                    354:                tbl->opts.cols = p->col + 1;
1.4       schwarze  355:
                    356:        return(p);
                    357: }