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