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

Annotation of mandoc/tbl_data.c, Revision 1.21

1.21    ! schwarze    1: /*     $Id: tbl_data.c,v 1.20 2011/01/25 12:16:22 schwarze Exp $ */
1.1       kristaps    2: /*
1.20      schwarze    3:  * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  * Copyright (c) 2011 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: #ifdef HAVE_CONFIG_H
                     19: #include "config.h"
                     20: #endif
                     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"
                     29: #include "libmandoc.h"
                     30: #include "libroff.h"
                     31:
1.11      kristaps   32: static int     data(struct tbl_node *, struct tbl_span *,
1.1       kristaps   33:                        int, const char *, int *);
1.20      schwarze   34: static struct tbl_span *newspan(struct tbl_node *, struct tbl_row *);
                     35:
1.1       kristaps   36:
1.11      kristaps   37: static int
1.8       kristaps   38: data(struct tbl_node *tbl, struct tbl_span *dp,
1.1       kristaps   39:                int ln, const char *p, int *pos)
                     40: {
                     41:        struct tbl_dat  *dat;
1.3       kristaps   42:        struct tbl_cell *cp;
1.16      kristaps   43:        int              sv, spans;
1.1       kristaps   44:
1.3       kristaps   45:        cp = NULL;
                     46:        if (dp->last && dp->last->layout)
                     47:                cp = dp->last->layout->next;
                     48:        else if (NULL == dp->last)
                     49:                cp = dp->layout->first;
                     50:
1.12      kristaps   51:        /*
                     52:         * Skip over spanners and vertical lines to data formats, since
                     53:         * we want to match data with data layout cells in the header.
                     54:         */
1.3       kristaps   55:
                     56:        while (cp && (TBL_CELL_VERT == cp->pos ||
1.12      kristaps   57:                                TBL_CELL_DVERT == cp->pos ||
                     58:                                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) {
                     67:                TBL_MSG(tbl, MANDOCERR_TBLEXTRADAT, ln, *pos);
                     68:                /* Skip to the end... */
                     69:                while (p[*pos])
                     70:                        (*pos)++;
                     71:                return(1);
                     72:        }
                     73:
1.1       kristaps   74:        dat = mandoc_calloc(1, sizeof(struct tbl_dat));
1.3       kristaps   75:        dat->layout = cp;
1.11      kristaps   76:        dat->pos = TBL_DATA_NONE;
1.7       kristaps   77:
1.16      kristaps   78:        assert(TBL_CELL_SPAN != cp->pos);
                     79:
                     80:        for (spans = 0, cp = cp->next; cp; cp = cp->next)
                     81:                if (TBL_CELL_SPAN == cp->pos)
                     82:                        spans++;
                     83:                else
                     84:                        break;
                     85:
                     86:        dat->spans = spans;
1.1       kristaps   87:
                     88:        if (dp->last) {
                     89:                dp->last->next = dat;
                     90:                dp->last = dat;
                     91:        } else
                     92:                dp->last = dp->first = dat;
                     93:
                     94:        sv = *pos;
1.8       kristaps   95:        while (p[*pos] && p[*pos] != tbl->opts.tab)
1.1       kristaps   96:                (*pos)++;
                     97:
1.11      kristaps   98:        /*
                     99:         * Check for a continued-data scope opening.  This consists of a
                    100:         * trailing `T{' at the end of the line.  Subsequent lines,
                    101:         * until a standalone `T}', are included in our cell.
                    102:         */
                    103:
                    104:        if (*pos - sv == 2 && 'T' == p[sv] && '{' == p[sv + 1]) {
                    105:                tbl->part = TBL_PART_CDATA;
                    106:                return(0);
                    107:        }
                    108:
1.1       kristaps  109:        dat->string = mandoc_malloc(*pos - sv + 1);
                    110:        memcpy(dat->string, &p[sv], *pos - sv);
                    111:        dat->string[*pos - sv] = '\0';
                    112:
                    113:        if (p[*pos])
                    114:                (*pos)++;
                    115:
                    116:        if ( ! strcmp(dat->string, "_"))
1.5       kristaps  117:                dat->pos = TBL_DATA_HORIZ;
1.1       kristaps  118:        else if ( ! strcmp(dat->string, "="))
1.5       kristaps  119:                dat->pos = TBL_DATA_DHORIZ;
1.1       kristaps  120:        else if ( ! strcmp(dat->string, "\\_"))
1.5       kristaps  121:                dat->pos = TBL_DATA_NHORIZ;
1.1       kristaps  122:        else if ( ! strcmp(dat->string, "\\="))
1.5       kristaps  123:                dat->pos = TBL_DATA_NDHORIZ;
                    124:        else
                    125:                dat->pos = TBL_DATA_DATA;
1.11      kristaps  126:
1.10      kristaps  127:        if (TBL_CELL_HORIZ == dat->layout->pos ||
1.19      kristaps  128:                        TBL_CELL_DHORIZ == dat->layout->pos ||
                    129:                        TBL_CELL_DOWN == dat->layout->pos)
1.10      kristaps  130:                if (TBL_DATA_DATA == dat->pos && '\0' != *dat->string)
                    131:                        TBL_MSG(tbl, MANDOCERR_TBLIGNDATA, ln, sv);
1.11      kristaps  132:
                    133:        return(1);
                    134: }
                    135:
1.13      kristaps  136: /* ARGSUSED */
1.11      kristaps  137: int
                    138: tbl_cdata(struct tbl_node *tbl, int ln, const char *p)
                    139: {
                    140:        struct tbl_dat  *dat;
                    141:        size_t           sz;
1.14      kristaps  142:        int              pos;
1.11      kristaps  143:
1.14      kristaps  144:        pos = 0;
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++;
                    153:                        return(data(tbl, tbl->last_span, ln, p, &pos));
                    154:                } else if ('\0' == p[pos]) {
                    155:                        tbl->part = TBL_PART_DATA;
                    156:                        return(1);
                    157:                }
                    158:
                    159:                /* Fallthrough: T} is part of a word. */
                    160:        }
1.17      kristaps  161:
                    162:        dat->pos = TBL_DATA_DATA;
1.11      kristaps  163:
                    164:        if (dat->string) {
                    165:                sz = strlen(p) + strlen(dat->string) + 2;
                    166:                dat->string = mandoc_realloc(dat->string, sz);
                    167:                strlcat(dat->string, " ", sz);
                    168:                strlcat(dat->string, p, sz);
                    169:        } else
                    170:                dat->string = mandoc_strdup(p);
1.19      kristaps  171:
                    172:        if (TBL_CELL_DOWN == dat->layout->pos)
                    173:                TBL_MSG(tbl, MANDOCERR_TBLIGNDATA, ln, pos);
1.11      kristaps  174:
                    175:        return(0);
1.1       kristaps  176: }
                    177:
1.20      schwarze  178: static struct tbl_span *
                    179: newspan(struct tbl_node *tbl, struct tbl_row *rp)
                    180: {
                    181:        struct tbl_span *dp;
                    182:
                    183:        dp = mandoc_calloc(1, sizeof(struct tbl_span));
                    184:        dp->tbl = &tbl->opts;
                    185:        dp->layout = rp;
                    186:        dp->head = tbl->first_head;
                    187:
                    188:        if (tbl->last_span) {
                    189:                tbl->last_span->next = dp;
                    190:                tbl->last_span = dp;
                    191:        } else {
                    192:                tbl->last_span = tbl->first_span = dp;
1.21    ! schwarze  193:                tbl->current_span = NULL;
1.20      schwarze  194:                dp->flags |= TBL_SPAN_FIRST;
                    195:        }
                    196:
                    197:        return(dp);
                    198: }
                    199:
1.2       kristaps  200: int
1.8       kristaps  201: tbl_data(struct tbl_node *tbl, int ln, const char *p)
1.1       kristaps  202: {
                    203:        struct tbl_span *dp;
1.3       kristaps  204:        struct tbl_row  *rp;
1.1       kristaps  205:        int              pos;
                    206:
                    207:        pos = 0;
                    208:
                    209:        if ('\0' == p[pos]) {
                    210:                TBL_MSG(tbl, MANDOCERR_TBL, ln, pos);
1.4       kristaps  211:                return(0);
1.1       kristaps  212:        }
                    213:
1.3       kristaps  214:        /*
                    215:         * Choose a layout row: take the one following the last parsed
                    216:         * span's.  If that doesn't exist, use the last parsed span's.
1.15      kristaps  217:         * If there's no last parsed span, use the first row.  Lastly,
                    218:         * if the last span was a horizontal line, use the same layout
                    219:         * (it doesn't "consume" the layout).
1.3       kristaps  220:         */
                    221:
                    222:        if (tbl->last_span) {
                    223:                assert(tbl->last_span->layout);
1.20      schwarze  224:                if (tbl->last_span->pos == TBL_SPAN_DATA) {
                    225:                        for (rp = tbl->last_span->layout->next;
                    226:                                        rp && rp->first; rp = rp->next) {
                    227:                                switch (rp->first->pos) {
                    228:                                case (TBL_CELL_HORIZ):
                    229:                                        dp = newspan(tbl, rp);
                    230:                                        dp->pos = TBL_SPAN_HORIZ;
                    231:                                        continue;
                    232:                                case (TBL_CELL_DHORIZ):
                    233:                                        dp = newspan(tbl, rp);
                    234:                                        dp->pos = TBL_SPAN_DHORIZ;
                    235:                                        continue;
                    236:                                default:
                    237:                                        break;
                    238:                                }
                    239:                                break;
                    240:                        }
                    241:                } else
1.15      kristaps  242:                        rp = tbl->last_span->layout;
1.18      kristaps  243:
1.3       kristaps  244:                if (NULL == rp)
                    245:                        rp = tbl->last_span->layout;
                    246:        } else
                    247:                rp = tbl->first_row;
1.18      kristaps  248:
                    249:        assert(rp);
1.3       kristaps  250:
1.20      schwarze  251:        dp = newspan(tbl, rp);
1.2       kristaps  252:
1.1       kristaps  253:        if ( ! strcmp(p, "_")) {
1.5       kristaps  254:                dp->pos = TBL_SPAN_HORIZ;
1.2       kristaps  255:                return(1);
1.1       kristaps  256:        } else if ( ! strcmp(p, "=")) {
1.5       kristaps  257:                dp->pos = TBL_SPAN_DHORIZ;
1.2       kristaps  258:                return(1);
1.1       kristaps  259:        }
1.5       kristaps  260:
                    261:        dp->pos = TBL_SPAN_DATA;
1.1       kristaps  262:
1.11      kristaps  263:        /* This returns 0 when TBL_PART_CDATA is entered. */
                    264:
1.1       kristaps  265:        while ('\0' != p[pos])
1.11      kristaps  266:                if ( ! data(tbl, dp, ln, p, &pos))
                    267:                        return(0);
1.1       kristaps  268:
1.2       kristaps  269:        return(1);
1.1       kristaps  270: }

CVSweb