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

Annotation of mandoc/tbl_data.c, Revision 1.11

1.11    ! kristaps    1: /*     $Id: tbl_data.c,v 1.10 2011/01/04 12:06:21 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.1       kristaps   40:        int              sv;
                     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:
                     48:        /* Skip over spanners to data formats. */
                     49:
                     50:        while (cp && (TBL_CELL_VERT == cp->pos ||
                     51:                                TBL_CELL_DVERT == cp->pos))
                     52:                cp = cp->next;
                     53:
1.1       kristaps   54:        dat = mandoc_calloc(1, sizeof(struct tbl_dat));
1.3       kristaps   55:        dat->layout = cp;
1.11    ! kristaps   56:        dat->pos = TBL_DATA_NONE;
1.7       kristaps   57:
                     58:        if (NULL == dat->layout)
                     59:                TBL_MSG(tbl, MANDOCERR_TBLEXTRADAT, ln, *pos);
1.1       kristaps   60:
                     61:        if (dp->last) {
                     62:                dp->last->next = dat;
                     63:                dp->last = dat;
                     64:        } else
                     65:                dp->last = dp->first = dat;
                     66:
                     67:        sv = *pos;
1.8       kristaps   68:        while (p[*pos] && p[*pos] != tbl->opts.tab)
1.1       kristaps   69:                (*pos)++;
                     70:
1.11    ! kristaps   71:        /*
        !            72:         * Check for a continued-data scope opening.  This consists of a
        !            73:         * trailing `T{' at the end of the line.  Subsequent lines,
        !            74:         * until a standalone `T}', are included in our cell.
        !            75:         */
        !            76:
        !            77:        if (*pos - sv == 2 && 'T' == p[sv] && '{' == p[sv + 1]) {
        !            78:                tbl->part = TBL_PART_CDATA;
        !            79:                return(0);
        !            80:        }
        !            81:
1.1       kristaps   82:        dat->string = mandoc_malloc(*pos - sv + 1);
                     83:        memcpy(dat->string, &p[sv], *pos - sv);
                     84:        dat->string[*pos - sv] = '\0';
                     85:
                     86:        if (p[*pos])
                     87:                (*pos)++;
                     88:
                     89:        if ( ! strcmp(dat->string, "_"))
1.5       kristaps   90:                dat->pos = TBL_DATA_HORIZ;
1.1       kristaps   91:        else if ( ! strcmp(dat->string, "="))
1.5       kristaps   92:                dat->pos = TBL_DATA_DHORIZ;
1.1       kristaps   93:        else if ( ! strcmp(dat->string, "\\_"))
1.5       kristaps   94:                dat->pos = TBL_DATA_NHORIZ;
1.1       kristaps   95:        else if ( ! strcmp(dat->string, "\\="))
1.5       kristaps   96:                dat->pos = TBL_DATA_NDHORIZ;
                     97:        else
                     98:                dat->pos = TBL_DATA_DATA;
1.10      kristaps   99:
1.11    ! kristaps  100:        if (NULL == dat->layout)
        !           101:                return(1);
        !           102:
1.10      kristaps  103:        if (TBL_CELL_HORIZ == dat->layout->pos ||
                    104:                        TBL_CELL_DHORIZ == dat->layout->pos)
                    105:                if (TBL_DATA_DATA == dat->pos && '\0' != *dat->string)
                    106:                        TBL_MSG(tbl, MANDOCERR_TBLIGNDATA, ln, sv);
1.11    ! kristaps  107:
        !           108:        return(1);
        !           109: }
        !           110:
        !           111: int
        !           112: tbl_cdata(struct tbl_node *tbl, int ln, const char *p)
        !           113: {
        !           114:        struct tbl_dat  *dat;
        !           115:        size_t           sz;
        !           116:
        !           117:        if (0 == strcmp(p, "T}")) {
        !           118:                tbl->part = TBL_PART_DATA;
        !           119:                return(1);
        !           120:        }
        !           121:
        !           122:        dat = tbl->last_span->last;
        !           123:        dat->pos = TBL_DATA_DATA;
        !           124:
        !           125:        if (dat->string) {
        !           126:                sz = strlen(p) + strlen(dat->string) + 2;
        !           127:                dat->string = mandoc_realloc(dat->string, sz);
        !           128:                strlcat(dat->string, " ", sz);
        !           129:                strlcat(dat->string, p, sz);
        !           130:        } else
        !           131:                dat->string = mandoc_strdup(p);
        !           132:
        !           133:        return(0);
1.1       kristaps  134: }
                    135:
1.2       kristaps  136: int
1.8       kristaps  137: tbl_data(struct tbl_node *tbl, int ln, const char *p)
1.1       kristaps  138: {
                    139:        struct tbl_span *dp;
1.3       kristaps  140:        struct tbl_row  *rp;
1.1       kristaps  141:        int              pos;
                    142:
                    143:        pos = 0;
                    144:
                    145:        if ('\0' == p[pos]) {
                    146:                TBL_MSG(tbl, MANDOCERR_TBL, ln, pos);
1.4       kristaps  147:                return(0);
1.1       kristaps  148:        }
                    149:
1.3       kristaps  150:        /*
                    151:         * Choose a layout row: take the one following the last parsed
                    152:         * span's.  If that doesn't exist, use the last parsed span's.
                    153:         * If there's no last parsed span, use the first row.  This can
                    154:         * be NULL!
                    155:         */
                    156:
                    157:        if (tbl->last_span) {
                    158:                assert(tbl->last_span->layout);
                    159:                rp = tbl->last_span->layout->next;
                    160:                if (NULL == rp)
                    161:                        rp = tbl->last_span->layout;
                    162:        } else
                    163:                rp = tbl->first_row;
                    164:
1.1       kristaps  165:        dp = mandoc_calloc(1, sizeof(struct tbl_span));
1.9       kristaps  166:        dp->tbl = &tbl->opts;
1.3       kristaps  167:        dp->layout = rp;
1.9       kristaps  168:        dp->head = tbl->first_head;
1.1       kristaps  169:
1.2       kristaps  170:        if (tbl->last_span) {
                    171:                tbl->last_span->next = dp;
                    172:                tbl->last_span = dp;
1.9       kristaps  173:        } else {
1.2       kristaps  174:                tbl->last_span = tbl->first_span = dp;
1.9       kristaps  175:                dp->flags |= TBL_SPAN_FIRST;
                    176:        }
1.2       kristaps  177:
1.1       kristaps  178:        if ( ! strcmp(p, "_")) {
1.5       kristaps  179:                dp->pos = TBL_SPAN_HORIZ;
1.2       kristaps  180:                return(1);
1.1       kristaps  181:        } else if ( ! strcmp(p, "=")) {
1.5       kristaps  182:                dp->pos = TBL_SPAN_DHORIZ;
1.2       kristaps  183:                return(1);
1.1       kristaps  184:        }
1.5       kristaps  185:
                    186:        dp->pos = TBL_SPAN_DATA;
1.1       kristaps  187:
1.11    ! kristaps  188:        /* This returns 0 when TBL_PART_CDATA is entered. */
        !           189:
1.1       kristaps  190:        while ('\0' != p[pos])
1.11    ! kristaps  191:                if ( ! data(tbl, dp, ln, p, &pos))
        !           192:                        return(0);
1.1       kristaps  193:
1.2       kristaps  194:        return(1);
1.1       kristaps  195: }

CVSweb