[BACK]Return to tbl_data.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/tbl_data.c, Revision 1.11

1.11    ! schwarze    1: /*     $Id: tbl_data.c,v 1.10 2011/04/21 22:59:54 schwarze Exp $ */
1.1       schwarze    2: /*
1.4       schwarze    3:  * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.7       schwarze    4:  * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1       schwarze    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:  */
                     18: #include <assert.h>
                     19: #include <ctype.h>
                     20: #include <stdlib.h>
                     21: #include <string.h>
1.4       schwarze   22: #include <time.h>
1.1       schwarze   23:
1.4       schwarze   24: #include "mandoc.h"
                     25: #include "libmandoc.h"
                     26: #include "libroff.h"
                     27:
1.9       schwarze   28: static int              data(struct tbl_node *, struct tbl_span *,
                     29:                                int, const char *, int *);
                     30: static struct tbl_span *newspan(struct tbl_node *, int,
                     31:                                struct tbl_row *);
1.4       schwarze   32:
                     33: static int
                     34: data(struct tbl_node *tbl, struct tbl_span *dp,
                     35:                int ln, const char *p, int *pos)
1.1       schwarze   36: {
1.4       schwarze   37:        struct tbl_dat  *dat;
                     38:        struct tbl_cell *cp;
1.6       schwarze   39:        int              sv, spans;
1.4       schwarze   40:
                     41:        cp = NULL;
                     42:        if (dp->last && dp->last->layout)
                     43:                cp = dp->last->layout->next;
                     44:        else if (NULL == dp->last)
                     45:                cp = dp->layout->first;
                     46:
1.5       schwarze   47:        /*
                     48:         * Skip over spanners and vertical lines to data formats, since
                     49:         * we want to match data with data layout cells in the header.
                     50:         */
1.4       schwarze   51:
                     52:        while (cp && (TBL_CELL_VERT == cp->pos ||
1.5       schwarze   53:                                TBL_CELL_DVERT == cp->pos ||
                     54:                                TBL_CELL_SPAN == cp->pos))
1.4       schwarze   55:                cp = cp->next;
                     56:
1.6       schwarze   57:        /*
                     58:         * Stop processing when we reach the end of the available layout
                     59:         * cells.  This means that we have extra input.
                     60:         */
                     61:
                     62:        if (NULL == cp) {
1.11    ! schwarze   63:                mandoc_msg(MANDOCERR_TBLEXTRADAT,
        !            64:                                tbl->parse, ln, *pos, NULL);
1.6       schwarze   65:                /* Skip to the end... */
                     66:                while (p[*pos])
                     67:                        (*pos)++;
                     68:                return(1);
                     69:        }
                     70:
1.4       schwarze   71:        dat = mandoc_calloc(1, sizeof(struct tbl_dat));
                     72:        dat->layout = cp;
                     73:        dat->pos = TBL_DATA_NONE;
                     74:
1.6       schwarze   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.4       schwarze   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;
                     92:        while (p[*pos] && p[*pos] != tbl->opts.tab)
                     93:                (*pos)++;
                     94:
                     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:         */
1.1       schwarze  100:
1.4       schwarze  101:        if (*pos - sv == 2 && 'T' == p[sv] && '{' == p[sv + 1]) {
                    102:                tbl->part = TBL_PART_CDATA;
1.1       schwarze  103:                return(0);
1.4       schwarze  104:        }
1.1       schwarze  105:
1.10      schwarze  106:        assert(*pos - sv >= 0);
                    107:
                    108:        dat->string = mandoc_malloc((size_t)(*pos - sv + 1));
                    109:        memcpy(dat->string, &p[sv], (size_t)(*pos - sv));
1.4       schwarze  110:        dat->string[*pos - sv] = '\0';
1.1       schwarze  111:
1.4       schwarze  112:        if (p[*pos])
                    113:                (*pos)++;
1.1       schwarze  114:
                    115:        if ( ! strcmp(dat->string, "_"))
1.4       schwarze  116:                dat->pos = TBL_DATA_HORIZ;
1.1       schwarze  117:        else if ( ! strcmp(dat->string, "="))
1.4       schwarze  118:                dat->pos = TBL_DATA_DHORIZ;
1.1       schwarze  119:        else if ( ! strcmp(dat->string, "\\_"))
1.4       schwarze  120:                dat->pos = TBL_DATA_NHORIZ;
1.1       schwarze  121:        else if ( ! strcmp(dat->string, "\\="))
1.4       schwarze  122:                dat->pos = TBL_DATA_NDHORIZ;
1.1       schwarze  123:        else
1.4       schwarze  124:                dat->pos = TBL_DATA_DATA;
                    125:
                    126:        if (TBL_CELL_HORIZ == dat->layout->pos ||
1.6       schwarze  127:                        TBL_CELL_DHORIZ == dat->layout->pos ||
                    128:                        TBL_CELL_DOWN == dat->layout->pos)
1.4       schwarze  129:                if (TBL_DATA_DATA == dat->pos && '\0' != *dat->string)
1.11    ! schwarze  130:                        mandoc_msg(MANDOCERR_TBLIGNDATA,
        !           131:                                        tbl->parse, ln, sv, NULL);
1.4       schwarze  132:
1.1       schwarze  133:        return(1);
                    134: }
                    135:
1.5       schwarze  136: /* ARGSUSED */
1.4       schwarze  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.5       schwarze  142:        int              pos;
1.4       schwarze  143:
1.5       schwarze  144:        pos = 0;
1.4       schwarze  145:
                    146:        dat = tbl->last_span->last;
1.5       schwarze  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.4       schwarze  161:
1.6       schwarze  162:        dat->pos = TBL_DATA_DATA;
                    163:
1.4       schwarze  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);
                    171:
1.6       schwarze  172:        if (TBL_CELL_DOWN == dat->layout->pos)
1.11    ! schwarze  173:                mandoc_msg(MANDOCERR_TBLIGNDATA,
        !           174:                                tbl->parse, ln, pos, NULL);
1.6       schwarze  175:
1.4       schwarze  176:        return(0);
                    177: }
1.1       schwarze  178:
1.7       schwarze  179: static struct tbl_span *
1.9       schwarze  180: newspan(struct tbl_node *tbl, int line, struct tbl_row *rp)
1.7       schwarze  181: {
                    182:        struct tbl_span *dp;
                    183:
                    184:        dp = mandoc_calloc(1, sizeof(struct tbl_span));
1.9       schwarze  185:        dp->line = line;
1.7       schwarze  186:        dp->tbl = &tbl->opts;
                    187:        dp->layout = rp;
                    188:        dp->head = tbl->first_head;
                    189:
                    190:        if (tbl->last_span) {
                    191:                tbl->last_span->next = dp;
                    192:                tbl->last_span = dp;
                    193:        } else {
                    194:                tbl->last_span = tbl->first_span = dp;
1.8       schwarze  195:                tbl->current_span = NULL;
1.7       schwarze  196:                dp->flags |= TBL_SPAN_FIRST;
                    197:        }
                    198:
                    199:        return(dp);
                    200: }
                    201:
1.1       schwarze  202: int
1.4       schwarze  203: tbl_data(struct tbl_node *tbl, int ln, const char *p)
1.1       schwarze  204: {
                    205:        struct tbl_span *dp;
1.4       schwarze  206:        struct tbl_row  *rp;
                    207:        int              pos;
                    208:
                    209:        pos = 0;
1.1       schwarze  210:
1.4       schwarze  211:        if ('\0' == p[pos]) {
1.11    ! schwarze  212:                mandoc_msg(MANDOCERR_TBL, tbl->parse, ln, pos, NULL);
1.4       schwarze  213:                return(0);
1.1       schwarze  214:        }
                    215:
1.4       schwarze  216:        /*
                    217:         * Choose a layout row: take the one following the last parsed
                    218:         * span's.  If that doesn't exist, use the last parsed span's.
1.6       schwarze  219:         * If there's no last parsed span, use the first row.  Lastly,
                    220:         * if the last span was a horizontal line, use the same layout
                    221:         * (it doesn't "consume" the layout).
1.4       schwarze  222:         */
                    223:
                    224:        if (tbl->last_span) {
                    225:                assert(tbl->last_span->layout);
1.7       schwarze  226:                if (tbl->last_span->pos == TBL_SPAN_DATA) {
                    227:                        for (rp = tbl->last_span->layout->next;
                    228:                                        rp && rp->first; rp = rp->next) {
                    229:                                switch (rp->first->pos) {
                    230:                                case (TBL_CELL_HORIZ):
1.9       schwarze  231:                                        dp = newspan(tbl, ln, rp);
1.7       schwarze  232:                                        dp->pos = TBL_SPAN_HORIZ;
                    233:                                        continue;
                    234:                                case (TBL_CELL_DHORIZ):
1.9       schwarze  235:                                        dp = newspan(tbl, ln, rp);
1.7       schwarze  236:                                        dp->pos = TBL_SPAN_DHORIZ;
                    237:                                        continue;
                    238:                                default:
                    239:                                        break;
                    240:                                }
                    241:                                break;
                    242:                        }
                    243:                } else
1.6       schwarze  244:                        rp = tbl->last_span->layout;
                    245:
1.4       schwarze  246:                if (NULL == rp)
                    247:                        rp = tbl->last_span->layout;
                    248:        } else
                    249:                rp = tbl->first_row;
1.6       schwarze  250:
                    251:        assert(rp);
1.4       schwarze  252:
1.9       schwarze  253:        dp = newspan(tbl, ln, rp);
1.1       schwarze  254:
                    255:        if ( ! strcmp(p, "_")) {
1.4       schwarze  256:                dp->pos = TBL_SPAN_HORIZ;
1.1       schwarze  257:                return(1);
                    258:        } else if ( ! strcmp(p, "=")) {
1.4       schwarze  259:                dp->pos = TBL_SPAN_DHORIZ;
1.1       schwarze  260:                return(1);
                    261:        }
                    262:
1.4       schwarze  263:        dp->pos = TBL_SPAN_DATA;
1.1       schwarze  264:
1.4       schwarze  265:        /* This returns 0 when TBL_PART_CDATA is entered. */
1.1       schwarze  266:
1.4       schwarze  267:        while ('\0' != p[pos])
                    268:                if ( ! data(tbl, dp, ln, p, &pos))
                    269:                        return(0);
1.1       schwarze  270:
                    271:        return(1);
                    272: }