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

Annotation of mandoc/tbl_data.c, Revision 1.47

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

CVSweb