[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.22

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