Home | History | Annotate | Line # | Download | only in dist
tbl_data.c revision 1.1.1.6
      1  1.1.1.6  christos /*	Id: tbl_data.c,v 1.45 2017/07/08 17:52:50 schwarze Exp  */
      2      1.1     joerg /*
      3  1.1.1.2     joerg  * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps (at) bsd.lv>
      4  1.1.1.6  christos  * Copyright (c) 2011, 2015, 2017 Ingo Schwarze <schwarze (at) openbsd.org>
      5      1.1     joerg  *
      6      1.1     joerg  * Permission to use, copy, modify, and distribute this software for any
      7      1.1     joerg  * purpose with or without fee is hereby granted, provided that the above
      8      1.1     joerg  * copyright notice and this permission notice appear in all copies.
      9      1.1     joerg  *
     10      1.1     joerg  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11      1.1     joerg  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12      1.1     joerg  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13      1.1     joerg  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14      1.1     joerg  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     15      1.1     joerg  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     16      1.1     joerg  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17      1.1     joerg  */
     18      1.1     joerg #include "config.h"
     19  1.1.1.4  christos 
     20  1.1.1.4  christos #include <sys/types.h>
     21      1.1     joerg 
     22      1.1     joerg #include <assert.h>
     23      1.1     joerg #include <ctype.h>
     24      1.1     joerg #include <stdlib.h>
     25      1.1     joerg #include <string.h>
     26      1.1     joerg #include <time.h>
     27      1.1     joerg 
     28      1.1     joerg #include "mandoc.h"
     29  1.1.1.4  christos #include "mandoc_aux.h"
     30      1.1     joerg #include "libmandoc.h"
     31      1.1     joerg #include "libroff.h"
     32      1.1     joerg 
     33  1.1.1.4  christos static	void		 getdata(struct tbl_node *, struct tbl_span *,
     34  1.1.1.2     joerg 				int, const char *, int *);
     35  1.1.1.4  christos static	struct tbl_span	*newspan(struct tbl_node *, int,
     36  1.1.1.2     joerg 				struct tbl_row *);
     37      1.1     joerg 
     38  1.1.1.4  christos 
     39  1.1.1.4  christos static void
     40  1.1.1.4  christos getdata(struct tbl_node *tbl, struct tbl_span *dp,
     41      1.1     joerg 		int ln, const char *p, int *pos)
     42      1.1     joerg {
     43      1.1     joerg 	struct tbl_dat	*dat;
     44      1.1     joerg 	struct tbl_cell	*cp;
     45  1.1.1.4  christos 	int		 sv;
     46      1.1     joerg 
     47  1.1.1.4  christos 	/* Advance to the next layout cell, skipping spanners. */
     48      1.1     joerg 
     49  1.1.1.4  christos 	cp = dp->last == NULL ? dp->layout->first : dp->last->layout->next;
     50  1.1.1.4  christos 	while (cp != NULL && cp->pos == TBL_CELL_SPAN)
     51      1.1     joerg 		cp = cp->next;
     52      1.1     joerg 
     53  1.1.1.2     joerg 	/*
     54  1.1.1.6  christos 	 * If the current layout row is out of cells, allocate
     55  1.1.1.6  christos 	 * a new cell if another row of the table has at least
     56  1.1.1.6  christos 	 * this number of columns, or discard the input if we
     57  1.1.1.6  christos 	 * are beyond the last column of the table as a whole.
     58  1.1.1.2     joerg 	 */
     59  1.1.1.2     joerg 
     60  1.1.1.4  christos 	if (cp == NULL) {
     61  1.1.1.6  christos 		if (dp->layout->last->col + 1 < dp->opts->cols) {
     62  1.1.1.6  christos 			cp = mandoc_calloc(1, sizeof(*cp));
     63  1.1.1.6  christos 			cp->pos = TBL_CELL_LEFT;
     64  1.1.1.6  christos 			dp->layout->last->next = cp;
     65  1.1.1.6  christos 			cp->col = dp->layout->last->col + 1;
     66  1.1.1.6  christos 			dp->layout->last = cp;
     67  1.1.1.6  christos 		} else {
     68  1.1.1.6  christos 			mandoc_msg(MANDOCERR_TBLDATA_EXTRA, tbl->parse,
     69  1.1.1.6  christos 			    ln, *pos, p + *pos);
     70  1.1.1.6  christos 			while (p[*pos])
     71  1.1.1.6  christos 				(*pos)++;
     72  1.1.1.6  christos 			return;
     73  1.1.1.6  christos 		}
     74  1.1.1.2     joerg 	}
     75  1.1.1.2     joerg 
     76  1.1.1.4  christos 	dat = mandoc_calloc(1, sizeof(*dat));
     77      1.1     joerg 	dat->layout = cp;
     78      1.1     joerg 	dat->pos = TBL_DATA_NONE;
     79  1.1.1.4  christos 	dat->spans = 0;
     80  1.1.1.4  christos 	for (cp = cp->next; cp != NULL; cp = cp->next)
     81  1.1.1.4  christos 		if (cp->pos == TBL_CELL_SPAN)
     82  1.1.1.4  christos 			dat->spans++;
     83  1.1.1.2     joerg 		else
     84  1.1.1.2     joerg 			break;
     85      1.1     joerg 
     86  1.1.1.4  christos 	if (dp->last == NULL)
     87  1.1.1.4  christos 		dp->first = dat;
     88  1.1.1.4  christos 	else
     89      1.1     joerg 		dp->last->next = dat;
     90  1.1.1.4  christos 	dp->last = dat;
     91      1.1     joerg 
     92      1.1     joerg 	sv = *pos;
     93      1.1     joerg 	while (p[*pos] && p[*pos] != tbl->opts.tab)
     94      1.1     joerg 		(*pos)++;
     95      1.1     joerg 
     96      1.1     joerg 	/*
     97      1.1     joerg 	 * Check for a continued-data scope opening.  This consists of a
     98      1.1     joerg 	 * trailing `T{' at the end of the line.  Subsequent lines,
     99      1.1     joerg 	 * until a standalone `T}', are included in our cell.
    100      1.1     joerg 	 */
    101      1.1     joerg 
    102  1.1.1.4  christos 	if (*pos - sv == 2 && p[sv] == 'T' && p[sv + 1] == '{') {
    103      1.1     joerg 		tbl->part = TBL_PART_CDATA;
    104  1.1.1.4  christos 		return;
    105      1.1     joerg 	}
    106      1.1     joerg 
    107  1.1.1.4  christos 	dat->string = mandoc_strndup(p + sv, *pos - sv);
    108      1.1     joerg 
    109      1.1     joerg 	if (p[*pos])
    110      1.1     joerg 		(*pos)++;
    111      1.1     joerg 
    112      1.1     joerg 	if ( ! strcmp(dat->string, "_"))
    113      1.1     joerg 		dat->pos = TBL_DATA_HORIZ;
    114      1.1     joerg 	else if ( ! strcmp(dat->string, "="))
    115      1.1     joerg 		dat->pos = TBL_DATA_DHORIZ;
    116      1.1     joerg 	else if ( ! strcmp(dat->string, "\\_"))
    117      1.1     joerg 		dat->pos = TBL_DATA_NHORIZ;
    118      1.1     joerg 	else if ( ! strcmp(dat->string, "\\="))
    119      1.1     joerg 		dat->pos = TBL_DATA_NDHORIZ;
    120      1.1     joerg 	else
    121      1.1     joerg 		dat->pos = TBL_DATA_DATA;
    122      1.1     joerg 
    123  1.1.1.4  christos 	if ((dat->layout->pos == TBL_CELL_HORIZ ||
    124  1.1.1.4  christos 	    dat->layout->pos == TBL_CELL_DHORIZ ||
    125  1.1.1.4  christos 	    dat->layout->pos == TBL_CELL_DOWN) &&
    126  1.1.1.4  christos 	    dat->pos == TBL_DATA_DATA && *dat->string != '\0')
    127  1.1.1.4  christos 		mandoc_msg(MANDOCERR_TBLDATA_SPAN,
    128  1.1.1.4  christos 		    tbl->parse, ln, sv, dat->string);
    129      1.1     joerg }
    130      1.1     joerg 
    131  1.1.1.6  christos void
    132  1.1.1.4  christos tbl_cdata(struct tbl_node *tbl, int ln, const char *p, int pos)
    133      1.1     joerg {
    134      1.1     joerg 	struct tbl_dat	*dat;
    135  1.1.1.4  christos 	size_t		 sz;
    136      1.1     joerg 
    137      1.1     joerg 	dat = tbl->last_span->last;
    138      1.1     joerg 
    139      1.1     joerg 	if (p[pos] == 'T' && p[pos + 1] == '}') {
    140      1.1     joerg 		pos += 2;
    141      1.1     joerg 		if (p[pos] == tbl->opts.tab) {
    142      1.1     joerg 			tbl->part = TBL_PART_DATA;
    143      1.1     joerg 			pos++;
    144  1.1.1.5  christos 			while (p[pos] != '\0')
    145  1.1.1.5  christos 				getdata(tbl, tbl->last_span, ln, p, &pos);
    146  1.1.1.6  christos 			return;
    147  1.1.1.4  christos 		} else if (p[pos] == '\0') {
    148      1.1     joerg 			tbl->part = TBL_PART_DATA;
    149  1.1.1.6  christos 			return;
    150      1.1     joerg 		}
    151      1.1     joerg 
    152      1.1     joerg 		/* Fallthrough: T} is part of a word. */
    153      1.1     joerg 	}
    154      1.1     joerg 
    155  1.1.1.2     joerg 	dat->pos = TBL_DATA_DATA;
    156  1.1.1.6  christos 	dat->block = 1;
    157  1.1.1.2     joerg 
    158  1.1.1.4  christos 	if (dat->string != NULL) {
    159  1.1.1.4  christos 		sz = strlen(p + pos) + strlen(dat->string) + 2;
    160      1.1     joerg 		dat->string = mandoc_realloc(dat->string, sz);
    161  1.1.1.4  christos 		(void)strlcat(dat->string, " ", sz);
    162  1.1.1.4  christos 		(void)strlcat(dat->string, p + pos, sz);
    163      1.1     joerg 	} else
    164  1.1.1.4  christos 		dat->string = mandoc_strdup(p + pos);
    165      1.1     joerg 
    166  1.1.1.4  christos 	if (dat->layout->pos == TBL_CELL_DOWN)
    167  1.1.1.4  christos 		mandoc_msg(MANDOCERR_TBLDATA_SPAN, tbl->parse,
    168  1.1.1.4  christos 		    ln, pos, dat->string);
    169      1.1     joerg }
    170      1.1     joerg 
    171  1.1.1.2     joerg static struct tbl_span *
    172  1.1.1.2     joerg newspan(struct tbl_node *tbl, int line, struct tbl_row *rp)
    173  1.1.1.2     joerg {
    174  1.1.1.2     joerg 	struct tbl_span	*dp;
    175  1.1.1.2     joerg 
    176  1.1.1.4  christos 	dp = mandoc_calloc(1, sizeof(*dp));
    177  1.1.1.2     joerg 	dp->line = line;
    178  1.1.1.3     joerg 	dp->opts = &tbl->opts;
    179  1.1.1.2     joerg 	dp->layout = rp;
    180  1.1.1.4  christos 	dp->prev = tbl->last_span;
    181  1.1.1.2     joerg 
    182  1.1.1.4  christos 	if (dp->prev == NULL) {
    183  1.1.1.4  christos 		tbl->first_span = dp;
    184  1.1.1.2     joerg 		tbl->current_span = NULL;
    185  1.1.1.4  christos 	} else
    186  1.1.1.4  christos 		dp->prev->next = dp;
    187  1.1.1.4  christos 	tbl->last_span = dp;
    188  1.1.1.2     joerg 
    189  1.1.1.5  christos 	return dp;
    190  1.1.1.2     joerg }
    191  1.1.1.2     joerg 
    192  1.1.1.4  christos void
    193  1.1.1.4  christos tbl_data(struct tbl_node *tbl, int ln, const char *p, int pos)
    194      1.1     joerg {
    195      1.1     joerg 	struct tbl_row	*rp;
    196  1.1.1.6  christos 	struct tbl_cell	*cp;
    197  1.1.1.6  christos 	struct tbl_span	*sp;
    198      1.1     joerg 
    199  1.1.1.6  christos 	rp = (sp = tbl->last_span) == NULL ? tbl->first_row :
    200  1.1.1.6  christos 	    sp->pos == TBL_SPAN_DATA && sp->layout->next != NULL ?
    201  1.1.1.6  christos 	    sp->layout->next : sp->layout;
    202      1.1     joerg 
    203  1.1.1.6  christos 	assert(rp != NULL);
    204      1.1     joerg 
    205      1.1     joerg 	if ( ! strcmp(p, "_")) {
    206  1.1.1.6  christos 		sp = newspan(tbl, ln, rp);
    207  1.1.1.6  christos 		sp->pos = TBL_SPAN_HORIZ;
    208  1.1.1.4  christos 		return;
    209      1.1     joerg 	} else if ( ! strcmp(p, "=")) {
    210  1.1.1.6  christos 		sp = newspan(tbl, ln, rp);
    211  1.1.1.6  christos 		sp->pos = TBL_SPAN_DHORIZ;
    212  1.1.1.4  christos 		return;
    213      1.1     joerg 	}
    214      1.1     joerg 
    215  1.1.1.6  christos 	/*
    216  1.1.1.6  christos 	 * If the layout row contains nothing but horizontal lines,
    217  1.1.1.6  christos 	 * allocate an empty span for it and assign the current span
    218  1.1.1.6  christos 	 * to the next layout row accepting data.
    219  1.1.1.6  christos 	 */
    220  1.1.1.6  christos 
    221  1.1.1.6  christos 	while (rp->next != NULL) {
    222  1.1.1.6  christos 		if (rp->last->col + 1 < tbl->opts.cols)
    223  1.1.1.6  christos 			break;
    224  1.1.1.6  christos 		for (cp = rp->first; cp != NULL; cp = cp->next)
    225  1.1.1.6  christos 			if (cp->pos != TBL_CELL_HORIZ &&
    226  1.1.1.6  christos 			    cp->pos != TBL_CELL_DHORIZ)
    227  1.1.1.6  christos 				break;
    228  1.1.1.6  christos 		if (cp != NULL)
    229  1.1.1.6  christos 			break;
    230  1.1.1.6  christos 		sp = newspan(tbl, ln, rp);
    231  1.1.1.6  christos 		sp->pos = TBL_SPAN_DATA;
    232  1.1.1.6  christos 		rp = rp->next;
    233  1.1.1.6  christos 	}
    234  1.1.1.6  christos 
    235  1.1.1.6  christos 	/* Process a real data row. */
    236      1.1     joerg 
    237  1.1.1.6  christos 	sp = newspan(tbl, ln, rp);
    238  1.1.1.6  christos 	sp->pos = TBL_SPAN_DATA;
    239  1.1.1.4  christos 	while (p[pos] != '\0')
    240  1.1.1.6  christos 		getdata(tbl, sp, ln, p, &pos);
    241      1.1     joerg }
    242