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

Annotation of mandoc/tbl_layout.c, Revision 1.41

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

CVSweb