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

Annotation of mandoc/tbl_layout.c, Revision 1.51

1.51    ! schwarze    1: /*     $Id: tbl_layout.c,v 1.50 2021/08/10 12:55:04 schwarze Exp $ */
1.1       kristaps    2: /*
1.51    ! schwarze    3:  * Copyright (c) 2012, 2014, 2015, 2017, 2020, 2021, 2025
        !             4:  *               Ingo Schwarze <schwarze@openbsd.org>
1.22      schwarze    5:  * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.1       kristaps    6:  *
                      7:  * Permission to use, copy, modify, and distribute this software for any
                      8:  * purpose with or without fee is hereby granted, provided that the above
                      9:  * copyright notice and this permission notice appear in all copies.
                     10:  *
                     11:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     12:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     13:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     14:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     15:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     16:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     17:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     18:  */
1.18      kristaps   19: #include "config.h"
1.27      schwarze   20:
                     21: #include <sys/types.h>
1.18      kristaps   22:
1.1       kristaps   23: #include <ctype.h>
1.44      schwarze   24: #include <stdint.h>
1.47      schwarze   25: #include <stdio.h>
1.1       kristaps   26: #include <stdlib.h>
                     27: #include <string.h>
1.4       kristaps   28: #include <time.h>
1.1       kristaps   29:
1.45      schwarze   30: #include "mandoc_aux.h"
1.1       kristaps   31: #include "mandoc.h"
1.45      schwarze   32: #include "tbl.h"
1.1       kristaps   33: #include "libmandoc.h"
1.46      schwarze   34: #include "tbl_int.h"
1.1       kristaps   35:
                     36: struct tbl_phrase {
                     37:        char             name;
                     38:        enum tbl_cellt   key;
                     39: };
                     40:
1.32      schwarze   41: static const struct tbl_phrase keys[] = {
1.1       kristaps   42:        { 'c',           TBL_CELL_CENTRE },
                     43:        { 'r',           TBL_CELL_RIGHT },
                     44:        { 'l',           TBL_CELL_LEFT },
                     45:        { 'n',           TBL_CELL_NUMBER },
                     46:        { 's',           TBL_CELL_SPAN },
                     47:        { 'a',           TBL_CELL_LONG },
                     48:        { '^',           TBL_CELL_DOWN },
                     49:        { '-',           TBL_CELL_HORIZ },
                     50:        { '_',           TBL_CELL_HORIZ },
1.23      schwarze   51:        { '=',           TBL_CELL_DHORIZ }
1.1       kristaps   52: };
                     53:
1.32      schwarze   54: #define KEYS_MAX ((int)(sizeof(keys)/sizeof(keys[0])))
                     55:
                     56: static void             mods(struct tbl_node *, struct tbl_cell *,
1.5       kristaps   57:                                int, const char *, int *);
1.32      schwarze   58: static void             cell(struct tbl_node *, struct tbl_row *,
1.5       kristaps   59:                                int, const char *, int *);
1.23      schwarze   60: static struct tbl_cell *cell_alloc(struct tbl_node *, struct tbl_row *,
1.33      schwarze   61:                                enum tbl_cellt);
1.1       kristaps   62:
1.26      schwarze   63:
1.32      schwarze   64: static void
1.26      schwarze   65: mods(struct tbl_node *tbl, struct tbl_cell *cp,
1.1       kristaps   66:                int ln, const char *p, int *pos)
                     67: {
1.32      schwarze   68:        char            *endptr;
1.49      schwarze   69:        unsigned long    spacing;
1.50      schwarze   70:        int              isz;
                     71:        enum mandoc_esc  fontesc;
1.20      kristaps   72:
1.32      schwarze   73: mod:
                     74:        while (p[*pos] == ' ' || p[*pos] == '\t')
                     75:                (*pos)++;
1.20      kristaps   76:
1.32      schwarze   77:        /* Row delimiters and cell specifiers end modifier lists. */
1.1       kristaps   78:
1.33      schwarze   79:        if (strchr(".,-=^_ACLNRSaclnrs", p[*pos]) != NULL)
1.32      schwarze   80:                return;
1.12      kristaps   81:
                     82:        /* Throw away parenthesised expression. */
                     83:
                     84:        if ('(' == p[*pos]) {
                     85:                (*pos)++;
                     86:                while (p[*pos] && ')' != p[*pos])
                     87:                        (*pos)++;
                     88:                if (')' == p[*pos]) {
                     89:                        (*pos)++;
                     90:                        goto mod;
                     91:                }
1.48      schwarze   92:                mandoc_msg(MANDOCERR_TBLLAYOUT_PAR, ln, *pos, NULL);
1.32      schwarze   93:                return;
1.1       kristaps   94:        }
                     95:
                     96:        /* Parse numerical spacing from modifier string. */
                     97:
                     98:        if (isdigit((unsigned char)p[*pos])) {
1.49      schwarze   99:                if ((spacing = strtoul(p + *pos, &endptr, 10)) > 9)
                    100:                        mandoc_msg(MANDOCERR_TBLLAYOUT_SPC, ln, *pos,
                    101:                            "%lu", spacing);
                    102:                else
                    103:                        cp->spacing = spacing;
1.32      schwarze  104:                *pos = endptr - p;
1.1       kristaps  105:                goto mod;
1.26      schwarze  106:        }
1.1       kristaps  107:
1.13      joerg     108:        switch (tolower((unsigned char)p[(*pos)++])) {
1.32      schwarze  109:        case 'b':
1.50      schwarze  110:                cp->font = ESCAPE_FONTBOLD;
1.38      schwarze  111:                goto mod;
1.32      schwarze  112:        case 'd':
                    113:                cp->flags |= TBL_CELL_BALIGN;
1.1       kristaps  114:                goto mod;
1.26      schwarze  115:        case 'e':
1.1       kristaps  116:                cp->flags |= TBL_CELL_EQUAL;
                    117:                goto mod;
1.32      schwarze  118:        case 'f':
                    119:                break;
1.38      schwarze  120:        case 'i':
1.50      schwarze  121:                cp->font = ESCAPE_FONTITALIC;
1.38      schwarze  122:                goto mod;
1.32      schwarze  123:        case 'm':
1.48      schwarze  124:                mandoc_msg(MANDOCERR_TBLLAYOUT_MOD, ln, *pos, "m");
1.32      schwarze  125:                goto mod;
                    126:        case 'p':
                    127:        case 'v':
                    128:                if (p[*pos] == '-' || p[*pos] == '+')
                    129:                        (*pos)++;
                    130:                while (isdigit((unsigned char)p[*pos]))
                    131:                        (*pos)++;
                    132:                goto mod;
1.26      schwarze  133:        case 't':
1.1       kristaps  134:                cp->flags |= TBL_CELL_TALIGN;
                    135:                goto mod;
1.32      schwarze  136:        case 'u':
                    137:                cp->flags |= TBL_CELL_UP;
1.10      schwarze  138:                goto mod;
1.42      schwarze  139:        case 'w':
                    140:                if (p[*pos] == '(') {
                    141:                        (*pos)++;
1.51    ! schwarze  142:                        isz = 0;
        !           143:                        if (roff_evalnum(ln, p, pos, &isz, 'n', 1) == 0 ||
        !           144:                            p[*pos] != ')')
        !           145:                                mandoc_msg(MANDOCERR_TBLLAYOUT_WIDTH,
        !           146:                                    ln, *pos, "%s", p + *pos);
        !           147:                        else {
        !           148:                                /* Convert from BU to EN and round. */
        !           149:                                cp->width = (isz + 11) /24;
1.42      schwarze  150:                                (*pos)++;
1.51    ! schwarze  151:                        }
        !           152:                } else {
        !           153:                        cp->width = 0;
        !           154:                        while (isdigit((unsigned char)p[*pos])) {
        !           155:                                cp->width *= 10;
        !           156:                                cp->width += p[(*pos)++] - '0';
        !           157:                        }
        !           158:                        if (cp->width == 0)
        !           159:                                mandoc_msg(MANDOCERR_TBLLAYOUT_WIDTH,
        !           160:                                    ln, *pos, "%s", p + *pos);
1.42      schwarze  161:                }
1.29      schwarze  162:                goto mod;
                    163:        case 'x':
                    164:                cp->flags |= TBL_CELL_WMAX;
1.1       kristaps  165:                goto mod;
1.32      schwarze  166:        case 'z':
                    167:                cp->flags |= TBL_CELL_WIGN;
                    168:                goto mod;
1.33      schwarze  169:        case '|':
                    170:                if (cp->vert < 2)
                    171:                        cp->vert++;
                    172:                else
                    173:                        mandoc_msg(MANDOCERR_TBLLAYOUT_VERT,
1.48      schwarze  174:                            ln, *pos - 1, NULL);
1.33      schwarze  175:                goto mod;
1.1       kristaps  176:        default:
1.48      schwarze  177:                mandoc_msg(MANDOCERR_TBLLAYOUT_CHAR,
1.32      schwarze  178:                    ln, *pos - 1, "%c", p[*pos - 1]);
                    179:                goto mod;
1.1       kristaps  180:        }
                    181:
1.50      schwarze  182:        while (p[*pos] == ' ' || p[*pos] == '\t')
                    183:                (*pos)++;
                    184:
1.38      schwarze  185:        /* Ignore parenthised font names for now. */
                    186:
                    187:        if (p[*pos] == '(')
                    188:                goto mod;
                    189:
1.50      schwarze  190:        isz = 0;
                    191:        if (p[*pos] != '\0')
                    192:                isz++;
                    193:        if (strchr(" \t.", p[*pos + isz]) == NULL)
                    194:                isz++;
                    195:
                    196:        fontesc = mandoc_font(p + *pos, isz);
                    197:
                    198:        switch (fontesc) {
                    199:        case ESCAPE_FONTPREV:
                    200:        case ESCAPE_ERROR:
1.48      schwarze  201:                mandoc_msg(MANDOCERR_FT_BAD,
1.38      schwarze  202:                    ln, *pos, "TS %s", p + *pos - 1);
1.50      schwarze  203:                break;
1.1       kristaps  204:        default:
1.50      schwarze  205:                cp->font = fontesc;
                    206:                break;
1.1       kristaps  207:        }
1.50      schwarze  208:        *pos += isz;
                    209:        goto mod;
1.1       kristaps  210: }
                    211:
1.32      schwarze  212: static void
1.26      schwarze  213: cell(struct tbl_node *tbl, struct tbl_row *rp,
1.1       kristaps  214:                int ln, const char *p, int *pos)
                    215: {
1.33      schwarze  216:        int              i;
1.1       kristaps  217:        enum tbl_cellt   c;
                    218:
1.33      schwarze  219:        /* Handle leading vertical lines */
1.23      schwarze  220:
1.32      schwarze  221:        while (p[*pos] == ' ' || p[*pos] == '\t' || p[*pos] == '|') {
                    222:                if (p[*pos] == '|') {
1.33      schwarze  223:                        if (rp->vert < 2)
                    224:                                rp->vert++;
1.32      schwarze  225:                        else
                    226:                                mandoc_msg(MANDOCERR_TBLLAYOUT_VERT,
1.48      schwarze  227:                                    ln, *pos, NULL);
1.32      schwarze  228:                }
1.23      schwarze  229:                (*pos)++;
1.32      schwarze  230:        }
1.25      schwarze  231:
1.33      schwarze  232: again:
                    233:        while (p[*pos] == ' ' || p[*pos] == '\t')
                    234:                (*pos)++;
1.25      schwarze  235:
1.33      schwarze  236:        if (p[*pos] == '.' || p[*pos] == '\0')
1.32      schwarze  237:                return;
1.23      schwarze  238:
                    239:        /* Parse the column position (`c', `l', `r', ...). */
1.1       kristaps  240:
                    241:        for (i = 0; i < KEYS_MAX; i++)
1.13      joerg     242:                if (tolower((unsigned char)p[*pos]) == keys[i].name)
1.1       kristaps  243:                        break;
                    244:
1.32      schwarze  245:        if (i == KEYS_MAX) {
1.48      schwarze  246:                mandoc_msg(MANDOCERR_TBLLAYOUT_CHAR,
1.32      schwarze  247:                    ln, *pos, "%c", p[*pos]);
                    248:                (*pos)++;
                    249:                goto again;
1.1       kristaps  250:        }
1.11      kristaps  251:        c = keys[i].key;
                    252:
1.32      schwarze  253:        /* Special cases of spanners. */
1.16      kristaps  254:
1.32      schwarze  255:        if (c == TBL_CELL_SPAN) {
                    256:                if (rp->last == NULL)
1.48      schwarze  257:                        mandoc_msg(MANDOCERR_TBLLAYOUT_SPAN, ln, *pos, NULL);
1.32      schwarze  258:                else if (rp->last->pos == TBL_CELL_HORIZ ||
                    259:                    rp->last->pos == TBL_CELL_DHORIZ)
                    260:                        c = rp->last->pos;
                    261:        } else if (c == TBL_CELL_DOWN && rp == tbl->first_row)
1.48      schwarze  262:                mandoc_msg(MANDOCERR_TBLLAYOUT_DOWN, ln, *pos, NULL);
1.11      kristaps  263:
1.1       kristaps  264:        (*pos)++;
                    265:
                    266:        /* Allocate cell then parse its modifiers. */
                    267:
1.33      schwarze  268:        mods(tbl, cell_alloc(tbl, rp, c), ln, p, pos);
1.1       kristaps  269: }
                    270:
1.31      schwarze  271: void
1.34      schwarze  272: tbl_layout(struct tbl_node *tbl, int ln, const char *p, int pos)
1.1       kristaps  273: {
                    274:        struct tbl_row  *rp;
                    275:
1.30      schwarze  276:        rp = NULL;
                    277:        for (;;) {
                    278:                /* Skip whitespace before and after each cell. */
1.1       kristaps  279:
1.32      schwarze  280:                while (p[pos] == ' ' || p[pos] == '\t')
1.30      schwarze  281:                        pos++;
1.1       kristaps  282:
1.30      schwarze  283:                switch (p[pos]) {
                    284:                case ',':  /* Next row on this input line. */
                    285:                        pos++;
                    286:                        rp = NULL;
                    287:                        continue;
                    288:                case '\0':  /* Next row on next input line. */
1.31      schwarze  289:                        return;
1.30      schwarze  290:                case '.':  /* End of layout. */
                    291:                        pos++;
                    292:                        tbl->part = TBL_PART_DATA;
1.33      schwarze  293:
                    294:                        /*
                    295:                         * When the layout is completely empty,
                    296:                         * default to one left-justified column.
                    297:                         */
                    298:
                    299:                        if (tbl->first_row == NULL) {
1.35      schwarze  300:                                tbl->first_row = tbl->last_row =
                    301:                                    mandoc_calloc(1, sizeof(*rp));
                    302:                        }
                    303:                        if (tbl->first_row->first == NULL) {
1.33      schwarze  304:                                mandoc_msg(MANDOCERR_TBLLAYOUT_NONE,
1.48      schwarze  305:                                    ln, pos, NULL);
1.35      schwarze  306:                                cell_alloc(tbl, tbl->first_row,
                    307:                                    TBL_CELL_LEFT);
1.43      schwarze  308:                                if (tbl->opts.lvert < tbl->first_row->vert)
                    309:                                        tbl->opts.lvert = tbl->first_row->vert;
1.31      schwarze  310:                                return;
1.33      schwarze  311:                        }
                    312:
                    313:                        /*
                    314:                         * Search for the widest line
                    315:                         * along the left and right margins.
                    316:                         */
                    317:
                    318:                        for (rp = tbl->first_row; rp; rp = rp->next) {
                    319:                                if (tbl->opts.lvert < rp->vert)
                    320:                                        tbl->opts.lvert = rp->vert;
                    321:                                if (rp->last != NULL &&
1.37      schwarze  322:                                    rp->last->col + 1 == tbl->opts.cols &&
1.33      schwarze  323:                                    tbl->opts.rvert < rp->last->vert)
                    324:                                        tbl->opts.rvert = rp->last->vert;
1.35      schwarze  325:
                    326:                                /* If the last line is empty, drop it. */
                    327:
                    328:                                if (rp->next != NULL &&
                    329:                                    rp->next->first == NULL) {
                    330:                                        free(rp->next);
                    331:                                        rp->next = NULL;
1.39      schwarze  332:                                        tbl->last_row = rp;
1.35      schwarze  333:                                }
1.33      schwarze  334:                        }
1.31      schwarze  335:                        return;
1.30      schwarze  336:                default:  /* Cell. */
                    337:                        break;
                    338:                }
                    339:
1.35      schwarze  340:                /*
                    341:                 * If the last line had at least one cell,
                    342:                 * start a new one; otherwise, continue it.
                    343:                 */
                    344:
                    345:                if (rp == NULL) {
                    346:                        if (tbl->last_row == NULL ||
                    347:                            tbl->last_row->first != NULL) {
                    348:                                rp = mandoc_calloc(1, sizeof(*rp));
                    349:                                if (tbl->last_row)
                    350:                                        tbl->last_row->next = rp;
                    351:                                else
                    352:                                        tbl->first_row = rp;
                    353:                                tbl->last_row = rp;
                    354:                        } else
                    355:                                rp = tbl->last_row;
1.30      schwarze  356:                }
1.32      schwarze  357:                cell(tbl, rp, ln, p, &pos);
1.1       kristaps  358:        }
                    359: }
1.5       kristaps  360:
                    361: static struct tbl_cell *
1.33      schwarze  362: cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos)
1.5       kristaps  363: {
                    364:        struct tbl_cell *p, *pp;
                    365:
1.36      schwarze  366:        p = mandoc_calloc(1, sizeof(*p));
1.44      schwarze  367:        p->spacing = SIZE_MAX;
1.50      schwarze  368:        p->font = ESCAPE_FONTROMAN;
1.37      schwarze  369:        p->pos = pos;
1.5       kristaps  370:
1.36      schwarze  371:        if ((pp = rp->last) != NULL) {
1.23      schwarze  372:                pp->next = p;
1.37      schwarze  373:                p->col = pp->col + 1;
                    374:        } else
1.23      schwarze  375:                rp->first = p;
                    376:        rp->last = p;
1.5       kristaps  377:
1.37      schwarze  378:        if (tbl->opts.cols <= p->col)
                    379:                tbl->opts.cols = p->col + 1;
1.5       kristaps  380:
1.40      schwarze  381:        return p;
1.5       kristaps  382: }

CVSweb