GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/mandoc/tbl.c Lines: 67 67 100.0 %
Date: 2017-11-07 Branches: 38 41 92.7 %

Line Branch Exec Source
1
/*	$OpenBSD: tbl.c,v 1.23 2017/07/08 17:52:42 schwarze Exp $ */
2
/*
3
 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4
 * Copyright (c) 2011, 2015 Ingo Schwarze <schwarze@openbsd.org>
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
 */
18
#include <sys/types.h>
19
20
#include <assert.h>
21
#include <stdio.h>
22
#include <stdlib.h>
23
#include <string.h>
24
#include <time.h>
25
26
#include "mandoc.h"
27
#include "mandoc_aux.h"
28
#include "libmandoc.h"
29
#include "libroff.h"
30
31
32
void
33
tbl_read(struct tbl_node *tbl, int ln, const char *p, int pos)
34
{
35
	const char	*cp;
36
	int		 active;
37
38
	/*
39
	 * In the options section, proceed to the layout section
40
	 * after a semicolon, or right away if there is no semicolon.
41
	 * Ignore semicolons in arguments.
42
	 */
43
44
57050
	if (tbl->part == TBL_PART_OPTS) {
45
6919
		tbl->part = TBL_PART_LAYOUT;
46
		active = 1;
47
184184
		for (cp = p + pos; *cp != '\0'; cp++) {
48

104826
			switch (*cp) {
49
			case '(':
50
				active = 0;
51
6469
				continue;
52
			case ')':
53
				active = 1;
54
6469
				continue;
55
			case ';':
56
6715
				if (active)
57
					break;
58
				continue;
59
			default:
60
				continue;
61
			}
62
			break;
63
		}
64
6919
		if (*cp == ';') {
65
6715
			tbl_option(tbl, ln, p, &pos);
66
6715
			if (p[pos] == '\0')
67
6499
				return;
68
		}
69
	}
70
71
	/* Process the other section types.  */
72
73
50551
	switch (tbl->part) {
74
	case TBL_PART_LAYOUT:
75
20594
		tbl_layout(tbl, ln, p, pos);
76
20594
		break;
77
	case TBL_PART_CDATA:
78
3420
		tbl_cdata(tbl, ln, p, pos);
79
3420
		break;
80
	default:
81
26537
		tbl_data(tbl, ln, p, pos);
82
26537
		break;
83
	}
84
107601
}
85
86
struct tbl_node *
87
tbl_alloc(int pos, int line, struct mparse *parse)
88
{
89
	struct tbl_node	*tbl;
90
91
13838
	tbl = mandoc_calloc(1, sizeof(*tbl));
92
6919
	tbl->line = line;
93
6919
	tbl->pos = pos;
94
6919
	tbl->parse = parse;
95
6919
	tbl->part = TBL_PART_OPTS;
96
6919
	tbl->opts.tab = '\t';
97
6919
	tbl->opts.decimal = '.';
98
6919
	return tbl;
99
}
100
101
void
102
tbl_free(struct tbl_node *tbl)
103
{
104
	struct tbl_row	*rp;
105
	struct tbl_cell	*cp;
106
	struct tbl_span	*sp;
107
	struct tbl_dat	*dp;
108
109
61963
	while ((rp = tbl->first_row) != NULL) {
110
20603
		tbl->first_row = rp->next;
111
147628
		while (rp->first != NULL) {
112
			cp = rp->first;
113
53211
			rp->first = cp->next;
114
53211
			free(cp->wstr);
115
53211
			free(cp);
116
		}
117
20603
		free(rp);
118
	}
119
120
60077
	while ((sp = tbl->first_span) != NULL) {
121
26579
		tbl->first_span = sp->next;
122
190252
		while (sp->first != NULL) {
123
			dp = sp->first;
124
68547
			sp->first = dp->next;
125
68547
			free(dp->string);
126
68547
			free(dp);
127
		}
128
26579
		free(sp);
129
	}
130
131
6919
	free(tbl);
132
6919
}
133
134
void
135
tbl_restart(int line, int pos, struct tbl_node *tbl)
136
{
137
36
	if (tbl->part == TBL_PART_CDATA)
138
18
		mandoc_msg(MANDOCERR_TBLDATA_BLK, tbl->parse,
139
		    line, pos, "T&");
140
141
18
	tbl->part = TBL_PART_LAYOUT;
142
18
	tbl->line = line;
143
18
	tbl->pos = pos;
144
18
}
145
146
const struct tbl_span *
147
tbl_span(struct tbl_node *tbl)
148
{
149
	struct tbl_span	 *span;
150
151
167258
	assert(tbl);
152
167258
	span = tbl->current_span ? tbl->current_span->next
153
33976
				 : tbl->first_span;
154
83629
	if (span)
155
26579
		tbl->current_span = span;
156
83629
	return span;
157
}
158
159
int
160
tbl_end(struct tbl_node *tbl)
161
{
162
	struct tbl_span *sp;
163
164
13838
	if (tbl->part == TBL_PART_CDATA)
165
36
		mandoc_msg(MANDOCERR_TBLDATA_BLK, tbl->parse,
166
18
		    tbl->line, tbl->pos, "TE");
167
168
6919
	sp = tbl->first_span;
169

28126
	while (sp != NULL && sp->first == NULL)
170
117
		sp = sp->next;
171
6919
	if (sp == NULL) {
172
36
		mandoc_msg(MANDOCERR_TBLDATA_NONE, tbl->parse,
173
18
		    tbl->line, tbl->pos, NULL);
174
18
		return 0;
175
	}
176
6901
	return 1;
177
6919
}