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

Annotation of mandoc/tbl_layout.c, Revision 1.48

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