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

Annotation of mandoc/tbl_data.c, Revision 1.43

1.43    ! schwarze    1: /*     $Id: tbl_data.c,v 1.42 2017/06/08 18:11:22 schwarze Exp $ */
1.1       kristaps    2: /*
1.20      schwarze    3:  * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.42      schwarze    4:  * Copyright (c) 2011, 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.11      kristaps   18: #include "config.h"
1.32      schwarze   19:
                     20: #include <sys/types.h>
1.11      kristaps   21:
1.1       kristaps   22: #include <assert.h>
                     23: #include <ctype.h>
                     24: #include <stdlib.h>
                     25: #include <string.h>
1.6       kristaps   26: #include <time.h>
1.1       kristaps   27:
                     28: #include "mandoc.h"
1.29      schwarze   29: #include "mandoc_aux.h"
1.1       kristaps   30: #include "libmandoc.h"
                     31: #include "libroff.h"
                     32:
1.33      schwarze   33: static void             getdata(struct tbl_node *, struct tbl_span *,
1.22      kristaps   34:                                int, const char *, int *);
1.30      schwarze   35: static struct tbl_span *newspan(struct tbl_node *, int,
1.22      kristaps   36:                                struct tbl_row *);
1.1       kristaps   37:
1.30      schwarze   38:
1.33      schwarze   39: static void
1.30      schwarze   40: getdata(struct tbl_node *tbl, struct tbl_span *dp,
1.1       kristaps   41:                int ln, const char *p, int *pos)
                     42: {
                     43:        struct tbl_dat  *dat;
1.3       kristaps   44:        struct tbl_cell *cp;
1.37      schwarze   45:        int              sv;
1.1       kristaps   46:
1.38      schwarze   47:        /* Advance to the next layout cell, skipping spanners. */
                     48:
1.37      schwarze   49:        cp = dp->last == NULL ? dp->layout->first : dp->last->layout->next;
                     50:        while (cp != NULL && cp->pos == TBL_CELL_SPAN)
1.3       kristaps   51:                cp = cp->next;
                     52:
1.16      kristaps   53:        /*
1.43    ! schwarze   54:         * If the current layout row is out of cells, allocate
        !            55:         * a new cell if another row of the table has at least
        !            56:         * this number of columns, or discard the input if we
        !            57:         * are beyond the last column of the table as a whole.
1.16      kristaps   58:         */
                     59:
1.37      schwarze   60:        if (cp == NULL) {
1.43    ! schwarze   61:                if (dp->layout->last->col + 1 < dp->opts->cols) {
        !            62:                        cp = mandoc_calloc(1, sizeof(*cp));
        !            63:                        cp->pos = TBL_CELL_LEFT;
        !            64:                        dp->layout->last->next = cp;
        !            65:                        cp->col = dp->layout->last->col + 1;
        !            66:                        dp->layout->last = cp;
        !            67:                } else {
        !            68:                        mandoc_msg(MANDOCERR_TBLDATA_EXTRA, tbl->parse,
        !            69:                            ln, *pos, p + *pos);
        !            70:                        while (p[*pos])
        !            71:                                (*pos)++;
        !            72:                        return;
        !            73:                }
1.16      kristaps   74:        }
                     75:
1.37      schwarze   76:        dat = mandoc_calloc(1, sizeof(*dat));
1.3       kristaps   77:        dat->layout = cp;
1.11      kristaps   78:        dat->pos = TBL_DATA_NONE;
1.37      schwarze   79:        dat->spans = 0;
                     80:        for (cp = cp->next; cp != NULL; cp = cp->next)
                     81:                if (cp->pos == TBL_CELL_SPAN)
                     82:                        dat->spans++;
1.16      kristaps   83:                else
                     84:                        break;
1.30      schwarze   85:
1.37      schwarze   86:        if (dp->last == NULL)
                     87:                dp->first = dat;
                     88:        else
1.1       kristaps   89:                dp->last->next = dat;
1.37      schwarze   90:        dp->last = dat;
1.1       kristaps   91:
                     92:        sv = *pos;
1.8       kristaps   93:        while (p[*pos] && p[*pos] != tbl->opts.tab)
1.1       kristaps   94:                (*pos)++;
                     95:
1.11      kristaps   96:        /*
                     97:         * Check for a continued-data scope opening.  This consists of a
                     98:         * trailing `T{' at the end of the line.  Subsequent lines,
                     99:         * until a standalone `T}', are included in our cell.
                    100:         */
                    101:
1.37      schwarze  102:        if (*pos - sv == 2 && p[sv] == 'T' && p[sv + 1] == '{') {
1.11      kristaps  103:                tbl->part = TBL_PART_CDATA;
1.33      schwarze  104:                return;
1.11      kristaps  105:        }
                    106:
1.37      schwarze  107:        dat->string = mandoc_strndup(p + sv, *pos - sv);
1.1       kristaps  108:
                    109:        if (p[*pos])
                    110:                (*pos)++;
                    111:
                    112:        if ( ! strcmp(dat->string, "_"))
1.5       kristaps  113:                dat->pos = TBL_DATA_HORIZ;
1.1       kristaps  114:        else if ( ! strcmp(dat->string, "="))
1.5       kristaps  115:                dat->pos = TBL_DATA_DHORIZ;
1.1       kristaps  116:        else if ( ! strcmp(dat->string, "\\_"))
1.5       kristaps  117:                dat->pos = TBL_DATA_NHORIZ;
1.1       kristaps  118:        else if ( ! strcmp(dat->string, "\\="))
1.5       kristaps  119:                dat->pos = TBL_DATA_NDHORIZ;
                    120:        else
                    121:                dat->pos = TBL_DATA_DATA;
1.11      kristaps  122:
1.37      schwarze  123:        if ((dat->layout->pos == TBL_CELL_HORIZ ||
                    124:            dat->layout->pos == TBL_CELL_DHORIZ ||
                    125:            dat->layout->pos == TBL_CELL_DOWN) &&
                    126:            dat->pos == TBL_DATA_DATA && *dat->string != '\0')
                    127:                mandoc_msg(MANDOCERR_TBLDATA_SPAN,
                    128:                    tbl->parse, ln, sv, dat->string);
1.11      kristaps  129: }
                    130:
                    131: int
1.35      schwarze  132: tbl_cdata(struct tbl_node *tbl, int ln, const char *p, int pos)
1.11      kristaps  133: {
                    134:        struct tbl_dat  *dat;
1.30      schwarze  135:        size_t           sz;
1.11      kristaps  136:
                    137:        dat = tbl->last_span->last;
1.14      kristaps  138:
                    139:        if (p[pos] == 'T' && p[pos + 1] == '}') {
                    140:                pos += 2;
                    141:                if (p[pos] == tbl->opts.tab) {
                    142:                        tbl->part = TBL_PART_DATA;
                    143:                        pos++;
1.40      schwarze  144:                        while (p[pos] != '\0')
                    145:                                getdata(tbl, tbl->last_span, ln, p, &pos);
1.41      schwarze  146:                        return 1;
1.37      schwarze  147:                } else if (p[pos] == '\0') {
1.14      kristaps  148:                        tbl->part = TBL_PART_DATA;
1.41      schwarze  149:                        return 1;
1.14      kristaps  150:                }
                    151:
                    152:                /* Fallthrough: T} is part of a word. */
                    153:        }
1.17      kristaps  154:
                    155:        dat->pos = TBL_DATA_DATA;
1.42      schwarze  156:        dat->block = 1;
1.11      kristaps  157:
1.37      schwarze  158:        if (dat->string != NULL) {
1.36      schwarze  159:                sz = strlen(p + pos) + strlen(dat->string) + 2;
1.11      kristaps  160:                dat->string = mandoc_realloc(dat->string, sz);
1.31      schwarze  161:                (void)strlcat(dat->string, " ", sz);
1.36      schwarze  162:                (void)strlcat(dat->string, p + pos, sz);
1.11      kristaps  163:        } else
1.36      schwarze  164:                dat->string = mandoc_strdup(p + pos);
1.19      kristaps  165:
1.37      schwarze  166:        if (dat->layout->pos == TBL_CELL_DOWN)
1.36      schwarze  167:                mandoc_msg(MANDOCERR_TBLDATA_SPAN, tbl->parse,
                    168:                    ln, pos, dat->string);
1.11      kristaps  169:
1.41      schwarze  170:        return 0;
1.1       kristaps  171: }
                    172:
1.20      schwarze  173: static struct tbl_span *
1.22      kristaps  174: newspan(struct tbl_node *tbl, int line, struct tbl_row *rp)
1.20      schwarze  175: {
                    176:        struct tbl_span *dp;
                    177:
1.37      schwarze  178:        dp = mandoc_calloc(1, sizeof(*dp));
1.22      kristaps  179:        dp->line = line;
1.26      schwarze  180:        dp->opts = &tbl->opts;
1.20      schwarze  181:        dp->layout = rp;
1.34      schwarze  182:        dp->prev = tbl->last_span;
1.20      schwarze  183:
1.34      schwarze  184:        if (dp->prev == NULL) {
                    185:                tbl->first_span = dp;
1.21      schwarze  186:                tbl->current_span = NULL;
1.34      schwarze  187:        } else
                    188:                dp->prev->next = dp;
                    189:        tbl->last_span = dp;
1.20      schwarze  190:
1.41      schwarze  191:        return dp;
1.20      schwarze  192: }
                    193:
1.33      schwarze  194: void
1.35      schwarze  195: tbl_data(struct tbl_node *tbl, int ln, const char *p, int pos)
1.1       kristaps  196: {
1.3       kristaps  197:        struct tbl_row  *rp;
1.43    ! schwarze  198:        struct tbl_cell *cp;
        !           199:        struct tbl_span *sp, *spi;
        !           200:        struct tbl_dat  *dp;
        !           201:        int              have_data, spans;
1.1       kristaps  202:
1.43    ! schwarze  203:        rp = (sp = tbl->last_span) == NULL ? tbl->first_row :
        !           204:            sp->pos == TBL_SPAN_DATA && sp->layout->next != NULL ?
        !           205:            sp->layout->next : sp->layout;
1.18      kristaps  206:
1.43    ! schwarze  207:        assert(rp != NULL);
1.3       kristaps  208:
1.43    ! schwarze  209:        sp = newspan(tbl, ln, rp);
1.2       kristaps  210:
1.1       kristaps  211:        if ( ! strcmp(p, "_")) {
1.43    ! schwarze  212:                sp->pos = TBL_SPAN_HORIZ;
1.33      schwarze  213:                return;
1.1       kristaps  214:        } else if ( ! strcmp(p, "=")) {
1.43    ! schwarze  215:                sp->pos = TBL_SPAN_DHORIZ;
1.33      schwarze  216:                return;
1.1       kristaps  217:        }
1.43    ! schwarze  218:        sp->pos = TBL_SPAN_DATA;
        !           219:
        !           220:        while (p[pos] != '\0')
        !           221:                getdata(tbl, sp, ln, p, &pos);
        !           222:
        !           223:        /*
        !           224:         * If this span contains some data,
        !           225:         * make sure at least part of it gets printed.
        !           226:         */
        !           227:
        !           228:        have_data = 0;
        !           229:        cp = rp->first;
        !           230:        for (dp = sp->first; dp != NULL; dp = dp->next) {
        !           231:                if (dp->pos == TBL_DATA_DATA && *dp->string != '\0') {
        !           232:                        if (cp == NULL ||
        !           233:                            (cp->pos != TBL_CELL_HORIZ &&
        !           234:                             cp->pos != TBL_CELL_DHORIZ))
        !           235:                                return;
        !           236:                        have_data = 1;
        !           237:                }
        !           238:                spans = dp->spans;
        !           239:                while (spans-- >= 0) {
        !           240:                        if (cp != NULL)
        !           241:                                cp = cp->next;
        !           242:                }
        !           243:        }
        !           244:        if (have_data == 0 || rp->next == NULL)
        !           245:                return;
1.5       kristaps  246:
1.43    ! schwarze  247:        /*
        !           248:         * There is some data, but it would all get lost
        !           249:         * due to horizontal lines in the layout.
        !           250:         * Insert an empty span to consume the layout row.
        !           251:         */
1.1       kristaps  252:
1.43    ! schwarze  253:        tbl->last_span = sp->prev;
        !           254:        spi = newspan(tbl, ln, rp);
        !           255:        spi->pos = TBL_SPAN_DATA;
        !           256:        spi->next = sp;
        !           257:        tbl->last_span = sp;
        !           258:        sp->prev = spi;
        !           259:        sp->layout = rp->next;
        !           260:        cp = sp->layout->first;
        !           261:        for (dp = sp->first; dp != NULL; dp = dp->next) {
        !           262:                dp->layout = cp;
        !           263:                dp->spans = 0;
        !           264:                if (cp != NULL)
        !           265:                        cp = cp->next;
        !           266:                while (cp != NULL && cp->pos == TBL_CELL_SPAN) {
        !           267:                        dp->spans++;
        !           268:                        cp = cp->next;
        !           269:                }
        !           270:        }
1.1       kristaps  271: }

CVSweb