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

Annotation of mandoc/tbl_layout.c, Revision 1.49

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

CVSweb