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

Annotation of mandoc/tbl_data.c, Revision 1.35

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

CVSweb