[BACK]Return to tbl_data.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/tbl_data.c, Revision 1.44

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