GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/mandoc/tbl_opts.c Lines: 52 55 94.5 %
Date: 2017-11-13 Branches: 42 47 89.4 %

Line Branch Exec Source
1
/*	$OpenBSD: tbl_opts.c,v 1.13 2015/09/26 00:53:15 schwarze Exp $ */
2
/*
3
 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4
 * Copyright (c) 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 <ctype.h>
21
#include <stdio.h>
22
#include <stdlib.h>
23
#include <string.h>
24
25
#include "mandoc.h"
26
#include "libmandoc.h"
27
#include "libroff.h"
28
29
#define	KEY_DPOINT	0
30
#define	KEY_DELIM	1
31
#define	KEY_LINESIZE	2
32
#define	KEY_TAB		3
33
34
struct	tbl_phrase {
35
	const char	*name;
36
	int		 key;
37
};
38
39
static	const struct tbl_phrase keys[] = {
40
	{"decimalpoint", 0},
41
	{"delim",	 0},
42
	{"linesize",	 0},
43
	{"tab",		 0},
44
	{"allbox",	 TBL_OPT_ALLBOX | TBL_OPT_BOX},
45
	{"box",		 TBL_OPT_BOX},
46
	{"frame",	 TBL_OPT_BOX},
47
	{"center",	 TBL_OPT_CENTRE},
48
	{"centre",	 TBL_OPT_CENTRE},
49
	{"doublebox",	 TBL_OPT_DBOX},
50
	{"doubleframe",  TBL_OPT_DBOX},
51
	{"expand",	 TBL_OPT_EXPAND},
52
	{"nokeep",	 TBL_OPT_NOKEEP},
53
	{"nospaces",	 TBL_OPT_NOSPACE},
54
	{"nowarn",	 TBL_OPT_NOWARN},
55
};
56
57
#define KEY_MAXKEYS ((int)(sizeof(keys)/sizeof(keys[0])))
58
59
static	void	 arg(struct tbl_node *, int, const char *, int *, int);
60
61
62
static void
63
arg(struct tbl_node *tbl, int ln, const char *p, int *pos, int key)
64
{
65
	int		 len, want;
66
67

8708
	while (p[*pos] == ' ' || p[*pos] == '\t')
68
14
		(*pos)++;
69
70
	/* Arguments are enclosed in parentheses. */
71
72
	len = 0;
73
2170
	if (p[*pos] == '(') {
74
2164
		(*pos)++;
75
8680
		while (p[*pos + len] != ')')
76
2176
			len++;
77
	}
78
79

2170
	switch (key) {
80
	case KEY_DELIM:
81
12
		mandoc_vmsg(MANDOCERR_TBLOPT_EQN, tbl->parse,
82
6
		    ln, *pos, "%.*s", len, p + *pos);
83
		want = 2;
84
6
		break;
85
	case KEY_TAB:
86
		want = 1;
87
2158
		if (len == want)
88
2152
			tbl->opts.tab = p[*pos];
89
		break;
90
	case KEY_LINESIZE:
91
		want = 0;
92
		break;
93
	case KEY_DPOINT:
94
		want = 1;
95
6
		if (len == want)
96
			tbl->opts.decimal = p[*pos];
97
		break;
98
	default:
99
		abort();
100
	}
101
102
2170
	if (len == 0)
103
6
		mandoc_msg(MANDOCERR_TBLOPT_NOARG,
104
6
		    tbl->parse, ln, *pos, keys[key].name);
105

4328
	else if (want && len != want)
106
6
		mandoc_vmsg(MANDOCERR_TBLOPT_ARGSZ,
107
6
		    tbl->parse, ln, *pos, "%s want %d have %d",
108
6
		    keys[key].name, want, len);
109
110
2170
	*pos += len;
111
2170
	if (p[*pos] == ')')
112
2164
		(*pos)++;
113
2170
}
114
115
/*
116
 * Parse one line of options up to the semicolon.
117
 * Each option can be preceded by blanks and/or commas,
118
 * and some options are followed by arguments.
119
 */
120
void
121
tbl_option(struct tbl_node *tbl, int ln, const char *p, int *offs)
122
{
123
4492
	int		 i, pos, len;
124
125
2246
	pos = *offs;
126
6474
	for (;;) {
127

29992
		while (p[pos] == ' ' || p[pos] == '\t' || p[pos] == ',')
128
2009
			pos++;
129
130
6486
		if (p[pos] == ';') {
131
2246
			*offs = pos + 1;
132
			return;
133
		}
134
135
		/* Parse one option name. */
136
137
		len = 0;
138
47936
		while (isalpha((unsigned char)p[pos + len]))
139
19728
			len++;
140
141
4240
		if (len == 0) {
142
6
			mandoc_vmsg(MANDOCERR_TBLOPT_ALPHA,
143
6
			    tbl->parse, ln, pos, "%c", p[pos]);
144
6
			pos++;
145
6
			continue;
146
		}
147
148
		/* Look up the option name. */
149
150
		i = 0;
151

70398
		while (i < KEY_MAXKEYS &&
152
27688
		    (strncasecmp(p + pos, keys[i].name, len) ||
153
4228
		     keys[i].name[len] != '\0'))
154
19232
			i++;
155
156
4234
		if (i == KEY_MAXKEYS) {
157
12
			mandoc_vmsg(MANDOCERR_TBLOPT_BAD, tbl->parse,
158
6
			    ln, pos, "%.*s", len, p + pos);
159
6
			pos += len;
160
6
			continue;
161
		}
162
163
		/* Handle the option. */
164
165
4228
		pos += len;
166
4228
		if (keys[i].key)
167
2058
			tbl->opts.opts |= keys[i].key;
168
		else
169
2170
			arg(tbl, ln, p, &pos, i);
170
	}
171
2246
}