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

Annotation of mandoc/tbl_data.c, Revision 1.16

1.16    ! kristaps    1: /*     $Id: tbl_data.c,v 1.15 2011/01/09 23:14:41 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
1.11      kristaps   17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
1.1       kristaps   21: #include <assert.h>
                     22: #include <ctype.h>
                     23: #include <stdlib.h>
                     24: #include <string.h>
1.6       kristaps   25: #include <time.h>
1.1       kristaps   26:
                     27: #include "mandoc.h"
                     28: #include "libmandoc.h"
                     29: #include "libroff.h"
                     30:
1.11      kristaps   31: static int     data(struct tbl_node *, struct tbl_span *,
1.1       kristaps   32:                        int, const char *, int *);
                     33:
1.11      kristaps   34: static int
1.8       kristaps   35: data(struct tbl_node *tbl, struct tbl_span *dp,
1.1       kristaps   36:                int ln, const char *p, int *pos)
                     37: {
                     38:        struct tbl_dat  *dat;
1.3       kristaps   39:        struct tbl_cell *cp;
1.16    ! kristaps   40:        int              sv, spans;
1.1       kristaps   41:
1.3       kristaps   42:        cp = NULL;
                     43:        if (dp->last && dp->last->layout)
                     44:                cp = dp->last->layout->next;
                     45:        else if (NULL == dp->last)
                     46:                cp = dp->layout->first;
                     47:
1.12      kristaps   48:        /*
                     49:         * Skip over spanners and vertical lines to data formats, since
                     50:         * we want to match data with data layout cells in the header.
                     51:         */
1.3       kristaps   52:
                     53:        while (cp && (TBL_CELL_VERT == cp->pos ||
1.12      kristaps   54:                                TBL_CELL_DVERT == cp->pos ||
                     55:                                TBL_CELL_SPAN == cp->pos))
1.3       kristaps   56:                cp = cp->next;
                     57:
1.16    ! kristaps   58:        /*
        !            59:         * Stop processing when we reach the end of the available layout
        !            60:         * cells.  This means that we have extra input.
        !            61:         */
        !            62:
        !            63:        if (NULL == cp) {
        !            64:                TBL_MSG(tbl, MANDOCERR_TBLEXTRADAT, ln, *pos);
        !            65:                /* Skip to the end... */
        !            66:                while (p[*pos])
        !            67:                        (*pos)++;
        !            68:                return(1);
        !            69:        }
        !            70:
1.1       kristaps   71:        dat = mandoc_calloc(1, sizeof(struct tbl_dat));
1.3       kristaps   72:        dat->layout = cp;
1.11      kristaps   73:        dat->pos = TBL_DATA_NONE;
1.7       kristaps   74:
1.16    ! kristaps   75:        assert(TBL_CELL_SPAN != cp->pos);
        !            76:
        !            77:        for (spans = 0, cp = cp->next; cp; cp = cp->next)
        !            78:                if (TBL_CELL_SPAN == cp->pos)
        !            79:                        spans++;
        !            80:                else
        !            81:                        break;
        !            82:
        !            83:        dat->spans = spans;
1.1       kristaps   84:
                     85:        if (dp->last) {
                     86:                dp->last->next = dat;
                     87:                dp->last = dat;
                     88:        } else
                     89:                dp->last = dp->first = dat;
                     90:
                     91:        sv = *pos;
1.8       kristaps   92:        while (p[*pos] && p[*pos] != tbl->opts.tab)
1.1       kristaps   93:                (*pos)++;
                     94:
1.11      kristaps   95:        /*
                     96:         * Check for a continued-data scope opening.  This consists of a
                     97:         * trailing `T{' at the end of the line.  Subsequent lines,
                     98:         * until a standalone `T}', are included in our cell.
                     99:         */
                    100:
                    101:        if (*pos - sv == 2 && 'T' == p[sv] && '{' == p[sv + 1]) {
                    102:                tbl->part = TBL_PART_CDATA;
                    103:                return(0);
                    104:        }
                    105:
1.1       kristaps  106:        dat->string = mandoc_malloc(*pos - sv + 1);
                    107:        memcpy(dat->string, &p[sv], *pos - sv);
                    108:        dat->string[*pos - sv] = '\0';
                    109:
                    110:        if (p[*pos])
                    111:                (*pos)++;
                    112:
                    113:        if ( ! strcmp(dat->string, "_"))
1.5       kristaps  114:                dat->pos = TBL_DATA_HORIZ;
1.1       kristaps  115:        else if ( ! strcmp(dat->string, "="))
1.5       kristaps  116:                dat->pos = TBL_DATA_DHORIZ;
1.1       kristaps  117:        else if ( ! strcmp(dat->string, "\\_"))
1.5       kristaps  118:                dat->pos = TBL_DATA_NHORIZ;
1.1       kristaps  119:        else if ( ! strcmp(dat->string, "\\="))
1.5       kristaps  120:                dat->pos = TBL_DATA_NDHORIZ;
                    121:        else
                    122:                dat->pos = TBL_DATA_DATA;
1.11      kristaps  123:
1.10      kristaps  124:        if (TBL_CELL_HORIZ == dat->layout->pos ||
                    125:                        TBL_CELL_DHORIZ == dat->layout->pos)
                    126:                if (TBL_DATA_DATA == dat->pos && '\0' != *dat->string)
                    127:                        TBL_MSG(tbl, MANDOCERR_TBLIGNDATA, ln, sv);
1.11      kristaps  128:
                    129:        return(1);
                    130: }
                    131:
1.13      kristaps  132: /* ARGSUSED */
1.11      kristaps  133: int
                    134: tbl_cdata(struct tbl_node *tbl, int ln, const char *p)
                    135: {
                    136:        struct tbl_dat  *dat;
                    137:        size_t           sz;
1.14      kristaps  138:        int              pos;
1.11      kristaps  139:
1.14      kristaps  140:        pos = 0;
1.11      kristaps  141:
                    142:        dat = tbl->last_span->last;
                    143:        dat->pos = TBL_DATA_DATA;
1.14      kristaps  144:
                    145:        if (p[pos] == 'T' && p[pos + 1] == '}') {
                    146:                pos += 2;
                    147:                if (p[pos] == tbl->opts.tab) {
                    148:                        tbl->part = TBL_PART_DATA;
                    149:                        pos++;
                    150:                        return(data(tbl, tbl->last_span, ln, p, &pos));
                    151:                } else if ('\0' == p[pos]) {
                    152:                        tbl->part = TBL_PART_DATA;
                    153:                        return(1);
                    154:                }
                    155:
                    156:                /* Fallthrough: T} is part of a word. */
                    157:        }
1.11      kristaps  158:
                    159:        if (dat->string) {
                    160:                sz = strlen(p) + strlen(dat->string) + 2;
                    161:                dat->string = mandoc_realloc(dat->string, sz);
                    162:                strlcat(dat->string, " ", sz);
                    163:                strlcat(dat->string, p, sz);
                    164:        } else
                    165:                dat->string = mandoc_strdup(p);
                    166:
                    167:        return(0);
1.1       kristaps  168: }
                    169:
1.2       kristaps  170: int
1.8       kristaps  171: tbl_data(struct tbl_node *tbl, int ln, const char *p)
1.1       kristaps  172: {
                    173:        struct tbl_span *dp;
1.3       kristaps  174:        struct tbl_row  *rp;
1.1       kristaps  175:        int              pos;
                    176:
                    177:        pos = 0;
                    178:
                    179:        if ('\0' == p[pos]) {
                    180:                TBL_MSG(tbl, MANDOCERR_TBL, ln, pos);
1.4       kristaps  181:                return(0);
1.1       kristaps  182:        }
                    183:
1.3       kristaps  184:        /*
                    185:         * Choose a layout row: take the one following the last parsed
                    186:         * span's.  If that doesn't exist, use the last parsed span's.
1.15      kristaps  187:         * If there's no last parsed span, use the first row.  Lastly,
                    188:         * if the last span was a horizontal line, use the same layout
                    189:         * (it doesn't "consume" the layout).
                    190:         *
                    191:         * In the end, this can be NULL!
1.3       kristaps  192:         */
                    193:
                    194:        if (tbl->last_span) {
                    195:                assert(tbl->last_span->layout);
1.15      kristaps  196:                if (tbl->last_span->pos == TBL_SPAN_DATA)
                    197:                        rp = tbl->last_span->layout->next;
                    198:                else
                    199:                        rp = tbl->last_span->layout;
1.3       kristaps  200:                if (NULL == rp)
                    201:                        rp = tbl->last_span->layout;
                    202:        } else
                    203:                rp = tbl->first_row;
                    204:
1.1       kristaps  205:        dp = mandoc_calloc(1, sizeof(struct tbl_span));
1.9       kristaps  206:        dp->tbl = &tbl->opts;
1.3       kristaps  207:        dp->layout = rp;
1.9       kristaps  208:        dp->head = tbl->first_head;
1.1       kristaps  209:
1.2       kristaps  210:        if (tbl->last_span) {
                    211:                tbl->last_span->next = dp;
                    212:                tbl->last_span = dp;
1.9       kristaps  213:        } else {
1.2       kristaps  214:                tbl->last_span = tbl->first_span = dp;
1.9       kristaps  215:                dp->flags |= TBL_SPAN_FIRST;
                    216:        }
1.2       kristaps  217:
1.1       kristaps  218:        if ( ! strcmp(p, "_")) {
1.5       kristaps  219:                dp->pos = TBL_SPAN_HORIZ;
1.2       kristaps  220:                return(1);
1.1       kristaps  221:        } else if ( ! strcmp(p, "=")) {
1.5       kristaps  222:                dp->pos = TBL_SPAN_DHORIZ;
1.2       kristaps  223:                return(1);
1.1       kristaps  224:        }
1.5       kristaps  225:
                    226:        dp->pos = TBL_SPAN_DATA;
1.1       kristaps  227:
1.11      kristaps  228:        /* This returns 0 when TBL_PART_CDATA is entered. */
                    229:
1.1       kristaps  230:        while ('\0' != p[pos])
1.11      kristaps  231:                if ( ! data(tbl, dp, ln, p, &pos))
                    232:                        return(0);
1.1       kristaps  233:
1.2       kristaps  234:        return(1);
1.1       kristaps  235: }

CVSweb