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

Annotation of mandoc/tbl_data.c, Revision 1.48

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

CVSweb