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

Annotation of mandoc/tbl_data.c, Revision 1.46

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

CVSweb