Home | History | Annotate | Line # | Download | only in dist
tbl.c revision 1.7.14.1
      1  1.7.14.1  christos /*	Id: tbl.c,v 1.46 2018/12/14 06:33:14 schwarze Exp  */
      2       1.1     joerg /*
      3       1.3     joerg  * 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.2  nakayama #include "config.h"
     19       1.6  christos 
     20       1.6  christos #include <sys/types.h>
     21       1.2  nakayama 
     22       1.1     joerg #include <assert.h>
     23       1.1     joerg #include <stdio.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.6  christos #include "mandoc_aux.h"
     29  1.7.14.1  christos #include "mandoc.h"
     30  1.7.14.1  christos #include "tbl.h"
     31       1.1     joerg #include "libmandoc.h"
     32  1.7.14.1  christos #include "tbl_parse.h"
     33  1.7.14.1  christos #include "tbl_int.h"
     34       1.1     joerg 
     35       1.6  christos 
     36  1.7.14.1  christos void
     37       1.6  christos tbl_read(struct tbl_node *tbl, int ln, const char *p, int pos)
     38       1.1     joerg {
     39       1.1     joerg 	const char	*cp;
     40       1.6  christos 	int		 active;
     41       1.1     joerg 
     42       1.1     joerg 	/*
     43       1.6  christos 	 * In the options section, proceed to the layout section
     44       1.6  christos 	 * after a semicolon, or right away if there is no semicolon.
     45       1.6  christos 	 * Ignore semicolons in arguments.
     46       1.1     joerg 	 */
     47       1.1     joerg 
     48       1.6  christos 	if (tbl->part == TBL_PART_OPTS) {
     49       1.6  christos 		tbl->part = TBL_PART_LAYOUT;
     50       1.6  christos 		active = 1;
     51       1.6  christos 		for (cp = p + pos; *cp != '\0'; cp++) {
     52       1.6  christos 			switch (*cp) {
     53       1.6  christos 			case '(':
     54       1.6  christos 				active = 0;
     55       1.6  christos 				continue;
     56       1.6  christos 			case ')':
     57       1.6  christos 				active = 1;
     58       1.6  christos 				continue;
     59       1.6  christos 			case ';':
     60       1.6  christos 				if (active)
     61       1.6  christos 					break;
     62       1.6  christos 				continue;
     63       1.6  christos 			default:
     64       1.6  christos 				continue;
     65       1.6  christos 			}
     66       1.6  christos 			break;
     67       1.6  christos 		}
     68       1.6  christos 		if (*cp == ';') {
     69       1.6  christos 			tbl_option(tbl, ln, p, &pos);
     70       1.6  christos 			if (p[pos] == '\0')
     71  1.7.14.1  christos 				return;
     72       1.6  christos 		}
     73       1.6  christos 	}
     74       1.1     joerg 
     75       1.6  christos 	/* Process the other section types.  */
     76       1.1     joerg 
     77       1.1     joerg 	switch (tbl->part) {
     78       1.6  christos 	case TBL_PART_LAYOUT:
     79       1.6  christos 		tbl_layout(tbl, ln, p, pos);
     80  1.7.14.1  christos 		break;
     81       1.6  christos 	case TBL_PART_CDATA:
     82  1.7.14.1  christos 		tbl_cdata(tbl, ln, p, pos);
     83  1.7.14.1  christos 		break;
     84       1.1     joerg 	default:
     85  1.7.14.1  christos 		tbl_data(tbl, ln, p, pos);
     86       1.1     joerg 		break;
     87       1.1     joerg 	}
     88       1.1     joerg }
     89       1.1     joerg 
     90       1.1     joerg struct tbl_node *
     91  1.7.14.1  christos tbl_alloc(int pos, int line, struct tbl_node *last_tbl)
     92       1.1     joerg {
     93       1.5     joerg 	struct tbl_node	*tbl;
     94       1.1     joerg 
     95       1.6  christos 	tbl = mandoc_calloc(1, sizeof(*tbl));
     96  1.7.14.1  christos 	if (last_tbl != NULL)
     97  1.7.14.1  christos 		last_tbl->next = tbl;
     98       1.5     joerg 	tbl->line = line;
     99       1.5     joerg 	tbl->pos = pos;
    100       1.5     joerg 	tbl->part = TBL_PART_OPTS;
    101       1.5     joerg 	tbl->opts.tab = '\t';
    102       1.5     joerg 	tbl->opts.decimal = '.';
    103       1.7  christos 	return tbl;
    104       1.1     joerg }
    105       1.1     joerg 
    106       1.1     joerg void
    107       1.5     joerg tbl_free(struct tbl_node *tbl)
    108       1.1     joerg {
    109  1.7.14.1  christos 	struct tbl_node	*old_tbl;
    110       1.1     joerg 	struct tbl_row	*rp;
    111       1.1     joerg 	struct tbl_cell	*cp;
    112       1.1     joerg 	struct tbl_span	*sp;
    113       1.1     joerg 	struct tbl_dat	*dp;
    114       1.1     joerg 
    115  1.7.14.1  christos 	while (tbl != NULL) {
    116  1.7.14.1  christos 		while ((rp = tbl->first_row) != NULL) {
    117  1.7.14.1  christos 			tbl->first_row = rp->next;
    118  1.7.14.1  christos 			while (rp->first != NULL) {
    119  1.7.14.1  christos 				cp = rp->first;
    120  1.7.14.1  christos 				rp->first = cp->next;
    121  1.7.14.1  christos 				free(cp->wstr);
    122  1.7.14.1  christos 				free(cp);
    123  1.7.14.1  christos 			}
    124  1.7.14.1  christos 			free(rp);
    125       1.1     joerg 		}
    126  1.7.14.1  christos 		while ((sp = tbl->first_span) != NULL) {
    127  1.7.14.1  christos 			tbl->first_span = sp->next;
    128  1.7.14.1  christos 			while (sp->first != NULL) {
    129  1.7.14.1  christos 				dp = sp->first;
    130  1.7.14.1  christos 				sp->first = dp->next;
    131  1.7.14.1  christos 				free(dp->string);
    132  1.7.14.1  christos 				free(dp);
    133  1.7.14.1  christos 			}
    134  1.7.14.1  christos 			free(sp);
    135       1.1     joerg 		}
    136  1.7.14.1  christos 		old_tbl = tbl;
    137  1.7.14.1  christos 		tbl = tbl->next;
    138  1.7.14.1  christos 		free(old_tbl);
    139       1.1     joerg 	}
    140       1.1     joerg }
    141       1.1     joerg 
    142       1.1     joerg void
    143       1.1     joerg tbl_restart(int line, int pos, struct tbl_node *tbl)
    144       1.1     joerg {
    145       1.6  christos 	if (tbl->part == TBL_PART_CDATA)
    146  1.7.14.1  christos 		mandoc_msg(MANDOCERR_TBLDATA_BLK, line, pos, "T&");
    147       1.1     joerg 
    148       1.1     joerg 	tbl->part = TBL_PART_LAYOUT;
    149       1.1     joerg 	tbl->line = line;
    150       1.1     joerg 	tbl->pos = pos;
    151       1.1     joerg }
    152       1.1     joerg 
    153  1.7.14.1  christos struct tbl_span *
    154       1.3     joerg tbl_span(struct tbl_node *tbl)
    155       1.1     joerg {
    156       1.3     joerg 	struct tbl_span	 *span;
    157       1.1     joerg 
    158       1.3     joerg 	span = tbl->current_span ? tbl->current_span->next
    159       1.3     joerg 				 : tbl->first_span;
    160  1.7.14.1  christos 	if (span != NULL)
    161       1.3     joerg 		tbl->current_span = span;
    162       1.7  christos 	return span;
    163       1.1     joerg }
    164       1.1     joerg 
    165       1.6  christos int
    166  1.7.14.1  christos tbl_end(struct tbl_node *tbl, int still_open)
    167       1.1     joerg {
    168       1.6  christos 	struct tbl_span *sp;
    169       1.4     joerg 
    170  1.7.14.1  christos 	if (still_open)
    171  1.7.14.1  christos 		mandoc_msg(MANDOCERR_BLK_NOEND, tbl->line, tbl->pos, "TS");
    172  1.7.14.1  christos 	else if (tbl->part == TBL_PART_CDATA)
    173  1.7.14.1  christos 		mandoc_msg(MANDOCERR_TBLDATA_BLK, tbl->line, tbl->pos, "TE");
    174       1.6  christos 
    175       1.6  christos 	sp = tbl->first_span;
    176       1.6  christos 	while (sp != NULL && sp->first == NULL)
    177       1.6  christos 		sp = sp->next;
    178       1.6  christos 	if (sp == NULL) {
    179  1.7.14.1  christos 		mandoc_msg(MANDOCERR_TBLDATA_NONE, tbl->line, tbl->pos, NULL);
    180       1.7  christos 		return 0;
    181       1.6  christos 	}
    182       1.7  christos 	return 1;
    183       1.1     joerg }
    184