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

Annotation of mandoc/tbl_layout.c, Revision 1.39

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

CVSweb