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