1 /* grecs - Gray's Extensible Configuration System
2    Copyright (C) 2007-2019 Sergey Poznyakoff
3 
4    Grecs is free software; you can redistribute it and/or modify it
5    under the terms of the GNU General Public License as published by the
6    Free Software Foundation; either version 3 of the License, or (at your
7    option) any later version.
8 
9    Grecs is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License along
15    with Grecs. If not, see <http://www.gnu.org/licenses/>. */
16 
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20 #include <errno.h>
21 #include <grecs.h>
22 
23 int
grecs_assert_value_type(const grecs_value_t * value,int type,grecs_locus_t * refloc)24 grecs_assert_value_type(const grecs_value_t *value, int type,
25 			grecs_locus_t *refloc)
26 {
27 	if (GRECS_VALUE_EMPTY_P(value)) {
28 		grecs_error(refloc, 0, _("expected %s"),
29 			    gettext(grecs_value_type_string(type)));
30 		return 1;
31 	}
32 	if (value->type != type) {
33 		grecs_error(&value->locus, 0, _("expected %s, but found %s"),
34 			    gettext(grecs_value_type_string(type)),
35 			    gettext(grecs_value_type_string(value->type)));
36 		return 1;
37 	}
38 	return 0;
39 }
40 
41 int
grecs_assert_scalar_stmt(grecs_locus_t * locus,enum grecs_callback_command cmd)42 grecs_assert_scalar_stmt(grecs_locus_t *locus, enum grecs_callback_command cmd)
43 {
44 	if (cmd != grecs_callback_set_value) {
45 		grecs_error(locus, 0, _("unexpected block statement"));
46 		return 1;
47 	}
48 	return 0;
49 }
50 
51 int
grecs_assert_node_value_type(enum grecs_callback_command cmd,grecs_node_t * node,int type)52 grecs_assert_node_value_type(enum grecs_callback_command cmd,
53 			     grecs_node_t *node, int type)
54 {
55 	return grecs_assert_scalar_stmt(&node->locus, cmd)
56 		|| grecs_assert_value_type(node->v.value, type,
57 					   &node->locus);
58 }
59