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

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