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

Annotation of mandoc/tbl_data.c, Revision 1.37

1.37    ! schwarze    1: /*     $Id: tbl_data.c,v 1.36 2015/01/28 17:32:07 schwarze Exp $ */
1.1       kristaps    2: /*
1.20      schwarze    3:  * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.34      schwarze    4:  * Copyright (c) 2011, 2015 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.37    ! schwarze   47:        cp = dp->last == NULL ? dp->layout->first : dp->last->layout->next;
1.3       kristaps   48:
1.30      schwarze   49:        /*
1.25      schwarze   50:         * Skip over spanners, since
1.12      kristaps   51:         * we want to match data with data layout cells in the header.
                     52:         */
1.3       kristaps   53:
1.37    ! schwarze   54:        while (cp != NULL && cp->pos == TBL_CELL_SPAN)
1.3       kristaps   55:                cp = cp->next;
                     56:
1.16      kristaps   57:        /*
                     58:         * Stop processing when we reach the end of the available layout
                     59:         * cells.  This means that we have extra input.
                     60:         */
                     61:
1.37    ! schwarze   62:        if (cp == NULL) {
1.36      schwarze   63:                mandoc_msg(MANDOCERR_TBLDATA_EXTRA, tbl->parse,
                     64:                    ln, *pos, p + *pos);
1.16      kristaps   65:                /* Skip to the end... */
                     66:                while (p[*pos])
                     67:                        (*pos)++;
1.33      schwarze   68:                return;
1.16      kristaps   69:        }
                     70:
1.37    ! schwarze   71:        dat = mandoc_calloc(1, sizeof(*dat));
1.3       kristaps   72:        dat->layout = cp;
1.11      kristaps   73:        dat->pos = TBL_DATA_NONE;
1.37    ! schwarze   74:        dat->spans = 0;
        !            75:        for (cp = cp->next; cp != NULL; cp = cp->next)
        !            76:                if (cp->pos == TBL_CELL_SPAN)
        !            77:                        dat->spans++;
1.16      kristaps   78:                else
                     79:                        break;
1.30      schwarze   80:
1.37    ! schwarze   81:        if (dp->last == NULL)
        !            82:                dp->first = dat;
        !            83:        else
1.1       kristaps   84:                dp->last->next = dat;
1.37    ! schwarze   85:        dp->last = dat;
1.1       kristaps   86:
                     87:        sv = *pos;
1.8       kristaps   88:        while (p[*pos] && p[*pos] != tbl->opts.tab)
1.1       kristaps   89:                (*pos)++;
                     90:
1.11      kristaps   91:        /*
                     92:         * Check for a continued-data scope opening.  This consists of a
                     93:         * trailing `T{' at the end of the line.  Subsequent lines,
                     94:         * until a standalone `T}', are included in our cell.
                     95:         */
                     96:
1.37    ! schwarze   97:        if (*pos - sv == 2 && p[sv] == 'T' && p[sv + 1] == '{') {
1.11      kristaps   98:                tbl->part = TBL_PART_CDATA;
1.33      schwarze   99:                return;
1.11      kristaps  100:        }
                    101:
1.37    ! schwarze  102:        dat->string = mandoc_strndup(p + sv, *pos - sv);
1.1       kristaps  103:
                    104:        if (p[*pos])
                    105:                (*pos)++;
                    106:
                    107:        if ( ! strcmp(dat->string, "_"))
1.5       kristaps  108:                dat->pos = TBL_DATA_HORIZ;
1.1       kristaps  109:        else if ( ! strcmp(dat->string, "="))
1.5       kristaps  110:                dat->pos = TBL_DATA_DHORIZ;
1.1       kristaps  111:        else if ( ! strcmp(dat->string, "\\_"))
1.5       kristaps  112:                dat->pos = TBL_DATA_NHORIZ;
1.1       kristaps  113:        else if ( ! strcmp(dat->string, "\\="))
1.5       kristaps  114:                dat->pos = TBL_DATA_NDHORIZ;
                    115:        else
                    116:                dat->pos = TBL_DATA_DATA;
1.11      kristaps  117:
1.37    ! schwarze  118:        if ((dat->layout->pos == TBL_CELL_HORIZ ||
        !           119:            dat->layout->pos == TBL_CELL_DHORIZ ||
        !           120:            dat->layout->pos == TBL_CELL_DOWN) &&
        !           121:            dat->pos == TBL_DATA_DATA && *dat->string != '\0')
        !           122:                mandoc_msg(MANDOCERR_TBLDATA_SPAN,
        !           123:                    tbl->parse, ln, sv, dat->string);
1.11      kristaps  124: }
                    125:
                    126: int
1.35      schwarze  127: tbl_cdata(struct tbl_node *tbl, int ln, const char *p, int pos)
1.11      kristaps  128: {
                    129:        struct tbl_dat  *dat;
1.30      schwarze  130:        size_t           sz;
1.11      kristaps  131:
                    132:        dat = tbl->last_span->last;
1.14      kristaps  133:
                    134:        if (p[pos] == 'T' && p[pos + 1] == '}') {
                    135:                pos += 2;
                    136:                if (p[pos] == tbl->opts.tab) {
                    137:                        tbl->part = TBL_PART_DATA;
                    138:                        pos++;
1.33      schwarze  139:                        getdata(tbl, tbl->last_span, ln, p, &pos);
                    140:                        return(1);
1.37    ! schwarze  141:                } else if (p[pos] == '\0') {
1.14      kristaps  142:                        tbl->part = TBL_PART_DATA;
                    143:                        return(1);
                    144:                }
                    145:
                    146:                /* Fallthrough: T} is part of a word. */
                    147:        }
1.17      kristaps  148:
                    149:        dat->pos = TBL_DATA_DATA;
1.11      kristaps  150:
1.37    ! schwarze  151:        if (dat->string != NULL) {
1.36      schwarze  152:                sz = strlen(p + pos) + strlen(dat->string) + 2;
1.11      kristaps  153:                dat->string = mandoc_realloc(dat->string, sz);
1.31      schwarze  154:                (void)strlcat(dat->string, " ", sz);
1.36      schwarze  155:                (void)strlcat(dat->string, p + pos, sz);
1.11      kristaps  156:        } else
1.36      schwarze  157:                dat->string = mandoc_strdup(p + pos);
1.19      kristaps  158:
1.37    ! schwarze  159:        if (dat->layout->pos == TBL_CELL_DOWN)
1.36      schwarze  160:                mandoc_msg(MANDOCERR_TBLDATA_SPAN, tbl->parse,
                    161:                    ln, pos, dat->string);
1.11      kristaps  162:
                    163:        return(0);
1.1       kristaps  164: }
                    165:
1.20      schwarze  166: static struct tbl_span *
1.22      kristaps  167: newspan(struct tbl_node *tbl, int line, struct tbl_row *rp)
1.20      schwarze  168: {
                    169:        struct tbl_span *dp;
                    170:
1.37    ! schwarze  171:        dp = mandoc_calloc(1, sizeof(*dp));
1.22      kristaps  172:        dp->line = line;
1.26      schwarze  173:        dp->opts = &tbl->opts;
1.20      schwarze  174:        dp->layout = rp;
                    175:        dp->head = tbl->first_head;
1.34      schwarze  176:        dp->prev = tbl->last_span;
1.20      schwarze  177:
1.34      schwarze  178:        if (dp->prev == NULL) {
                    179:                tbl->first_span = dp;
1.21      schwarze  180:                tbl->current_span = NULL;
1.20      schwarze  181:                dp->flags |= TBL_SPAN_FIRST;
1.34      schwarze  182:        } else
                    183:                dp->prev->next = dp;
                    184:        tbl->last_span = dp;
1.20      schwarze  185:
                    186:        return(dp);
                    187: }
                    188:
1.33      schwarze  189: void
1.35      schwarze  190: tbl_data(struct tbl_node *tbl, int ln, const char *p, int pos)
1.1       kristaps  191: {
                    192:        struct tbl_span *dp;
1.3       kristaps  193:        struct tbl_row  *rp;
1.1       kristaps  194:
1.30      schwarze  195:        /*
1.3       kristaps  196:         * Choose a layout row: take the one following the last parsed
                    197:         * span's.  If that doesn't exist, use the last parsed span's.
1.15      kristaps  198:         * If there's no last parsed span, use the first row.  Lastly,
                    199:         * if the last span was a horizontal line, use the same layout
                    200:         * (it doesn't "consume" the layout).
1.3       kristaps  201:         */
                    202:
1.37    ! schwarze  203:        if (tbl->last_span != NULL) {
1.20      schwarze  204:                if (tbl->last_span->pos == TBL_SPAN_DATA) {
                    205:                        for (rp = tbl->last_span->layout->next;
1.37    ! schwarze  206:                             rp != NULL && rp->first != NULL;
        !           207:                             rp = rp->next) {
1.20      schwarze  208:                                switch (rp->first->pos) {
1.30      schwarze  209:                                case TBL_CELL_HORIZ:
1.22      kristaps  210:                                        dp = newspan(tbl, ln, rp);
1.20      schwarze  211:                                        dp->pos = TBL_SPAN_HORIZ;
                    212:                                        continue;
1.30      schwarze  213:                                case TBL_CELL_DHORIZ:
1.22      kristaps  214:                                        dp = newspan(tbl, ln, rp);
1.20      schwarze  215:                                        dp->pos = TBL_SPAN_DHORIZ;
                    216:                                        continue;
                    217:                                default:
                    218:                                        break;
                    219:                                }
                    220:                                break;
                    221:                        }
                    222:                } else
1.15      kristaps  223:                        rp = tbl->last_span->layout;
1.18      kristaps  224:
1.37    ! schwarze  225:                if (rp == NULL)
1.3       kristaps  226:                        rp = tbl->last_span->layout;
                    227:        } else
                    228:                rp = tbl->first_row;
1.18      kristaps  229:
                    230:        assert(rp);
1.3       kristaps  231:
1.22      kristaps  232:        dp = newspan(tbl, ln, rp);
1.2       kristaps  233:
1.1       kristaps  234:        if ( ! strcmp(p, "_")) {
1.5       kristaps  235:                dp->pos = TBL_SPAN_HORIZ;
1.33      schwarze  236:                return;
1.1       kristaps  237:        } else if ( ! strcmp(p, "=")) {
1.5       kristaps  238:                dp->pos = TBL_SPAN_DHORIZ;
1.33      schwarze  239:                return;
1.1       kristaps  240:        }
1.5       kristaps  241:
                    242:        dp->pos = TBL_SPAN_DATA;
1.1       kristaps  243:
1.37    ! schwarze  244:        while (p[pos] != '\0')
1.33      schwarze  245:                getdata(tbl, dp, ln, p, &pos);
1.1       kristaps  246: }

CVSweb