diff options
-rw-r--r-- | include/grecs/node.h | 2 | ||||
-rw-r--r-- | include/grecs/tree.h | 8 | ||||
-rw-r--r-- | include/grecs/value.h | 1 | ||||
-rw-r--r-- | src/Make.am | 1 | ||||
-rw-r--r-- | src/assert.c | 58 | ||||
-rw-r--r-- | src/format.c | 22 | ||||
-rw-r--r-- | src/tree.c | 63 | ||||
-rw-r--r-- | tests/set.at | 4 |
8 files changed, 125 insertions, 34 deletions
diff --git a/include/grecs/node.h b/include/grecs/node.h index 2d879ad..752b002 100644 --- a/include/grecs/node.h +++ b/include/grecs/node.h | |||
@@ -32,13 +32,13 @@ typedef struct grecs_node { | |||
32 | grecs_locus_t locus; | 32 | grecs_locus_t locus; |
33 | struct grecs_node *up; | 33 | struct grecs_node *up; |
34 | struct grecs_node *down; | 34 | struct grecs_node *down; |
35 | struct grecs_node *next; | 35 | struct grecs_node *next; |
36 | struct grecs_node *prev; | 36 | struct grecs_node *prev; |
37 | char *ident; | 37 | char *ident; |
38 | grecs_locus_t idloc; | 38 | grecs_locus_t idloc; /* Location of the identifier */ |
39 | union { | 39 | union { |
40 | grecs_value_t *value; | 40 | grecs_value_t *value; |
41 | grecs_symtab_ptr_t texttab; | 41 | grecs_symtab_ptr_t texttab; |
42 | } v; | 42 | } v; |
43 | } grecs_node_t; | 43 | } grecs_node_t; |
44 | 44 | ||
diff --git a/include/grecs/tree.h b/include/grecs/tree.h index 2fb26dd..f13a74b 100644 --- a/include/grecs/tree.h +++ b/include/grecs/tree.h | |||
@@ -64,8 +64,16 @@ grecs_match_buf_t grecs_match_buf_create(int argc, char **argv, | |||
64 | struct grecs_node *grecs_match_buf_first(struct grecs_match_buf *buf, | 64 | struct grecs_node *grecs_match_buf_first(struct grecs_match_buf *buf, |
65 | struct grecs_node *tree); | 65 | struct grecs_node *tree); |
66 | struct grecs_node *grecs_match_buf_get_node(grecs_match_buf_t buf); | 66 | struct grecs_node *grecs_match_buf_get_node(grecs_match_buf_t buf); |
67 | size_t grecs_match_buf_get_args(grecs_match_buf_t buf, char ***argv); | 67 | size_t grecs_match_buf_get_args(grecs_match_buf_t buf, char ***argv); |
68 | struct grecs_node *grecs_match_buf_get_root(grecs_match_buf_t buf); | 68 | struct grecs_node *grecs_match_buf_get_root(grecs_match_buf_t buf); |
69 | void grecs_match_buf_set_root(grecs_match_buf_t buf, struct grecs_node *root); | 69 | void grecs_match_buf_set_root(grecs_match_buf_t buf, struct grecs_node *root); |
70 | |||
71 | /* Assertions */ | ||
72 | int grecs_assert_value_type(const grecs_value_t *value, int type, | ||
73 | grecs_locus_t *refloc); | ||
74 | int grecs_assert_scalar_stmt(grecs_locus_t *locus, | ||
75 | enum grecs_callback_command cmd); | ||
76 | int grecs_assert_node_value_type(enum grecs_callback_command cmd, | ||
77 | grecs_node_t *node, int type); | ||
70 | 78 | ||
71 | #endif | 79 | #endif |
diff --git a/include/grecs/value.h b/include/grecs/value.h index 8cd2800..480db9d 100644 --- a/include/grecs/value.h +++ b/include/grecs/value.h | |||
@@ -41,12 +41,13 @@ typedef struct grecs_value { | |||
41 | (!(val) || \ | 41 | (!(val) || \ |
42 | ((val)->type == GRECS_TYPE_STRING && (val)->v.string == NULL)) | 42 | ((val)->type == GRECS_TYPE_STRING && (val)->v.string == NULL)) |
43 | 43 | ||
44 | void grecs_value_free(struct grecs_value *val); | 44 | void grecs_value_free(struct grecs_value *val); |
45 | void grecs_value_free_content(struct grecs_value *val); | 45 | void grecs_value_free_content(struct grecs_value *val); |
46 | grecs_value_t *grecs_value_ptr_from_static(grecs_value_t *input); | 46 | grecs_value_t *grecs_value_ptr_from_static(grecs_value_t *input); |
47 | const char *grecs_value_type_string(int t); | ||
47 | 48 | ||
48 | int grecs_value_eq(struct grecs_value *a, struct grecs_value *b); | 49 | int grecs_value_eq(struct grecs_value *a, struct grecs_value *b); |
49 | int grecs_value_match(struct grecs_value *pat, struct grecs_value *b, | 50 | int grecs_value_match(struct grecs_value *pat, struct grecs_value *b, |
50 | int flags); | 51 | int flags); |
51 | 52 | ||
52 | #endif | 53 | #endif |
diff --git a/src/Make.am b/src/Make.am index 227973e..9fa7688 100644 --- a/src/Make.am +++ b/src/Make.am | |||
@@ -43,12 +43,13 @@ if GRECS_COND_JSON | |||
43 | GRECS_JSON = json-gram.y json-lex.l jsonfmt.c | 43 | GRECS_JSON = json-gram.y json-lex.l jsonfmt.c |
44 | GRECS_EXTRA_JSON = json-gram.h | 44 | GRECS_EXTRA_JSON = json-gram.h |
45 | endif | 45 | endif |
46 | 46 | ||
47 | GRECS_SRC = \ | 47 | GRECS_SRC = \ |
48 | asprintf.c\ | 48 | asprintf.c\ |
49 | assert.c\ | ||
49 | cidr.c\ | 50 | cidr.c\ |
50 | diag.c\ | 51 | diag.c\ |
51 | format.c\ | 52 | format.c\ |
52 | grecs-gram.y\ | 53 | grecs-gram.y\ |
53 | grecs-lex.l\ | 54 | grecs-lex.l\ |
54 | ipstr.c\ | 55 | ipstr.c\ |
diff --git a/src/assert.c b/src/assert.c new file mode 100644 index 0000000..0577768 --- /dev/null +++ b/src/assert.c | |||
@@ -0,0 +1,58 @@ | |||
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 | ||
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 | ||
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 | ||
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 | } | ||
diff --git a/src/format.c b/src/format.c index af87329..d7cdb0f 100644 --- a/src/format.c +++ b/src/format.c | |||
@@ -27,50 +27,50 @@ | |||
27 | 27 | ||
28 | const char * | 28 | const char * |
29 | grecs_data_type_string(enum grecs_data_type type) | 29 | grecs_data_type_string(enum grecs_data_type type) |
30 | { | 30 | { |
31 | switch (type) { | 31 | switch (type) { |
32 | case grecs_type_void: | 32 | case grecs_type_void: |
33 | return "void"; | 33 | return N_("void"); |
34 | 34 | ||
35 | case grecs_type_string: | 35 | case grecs_type_string: |
36 | return "string"; | 36 | return N_("string"); |
37 | 37 | ||
38 | case grecs_type_short: | 38 | case grecs_type_short: |
39 | case grecs_type_ushort: | 39 | case grecs_type_ushort: |
40 | case grecs_type_int: | 40 | case grecs_type_int: |
41 | case grecs_type_uint: | 41 | case grecs_type_uint: |
42 | case grecs_type_long: | 42 | case grecs_type_long: |
43 | case grecs_type_ulong: | 43 | case grecs_type_ulong: |
44 | case grecs_type_size: | 44 | case grecs_type_size: |
45 | /*FIXME case grecs_type_off:*/ | 45 | /*FIXME case grecs_type_off:*/ |
46 | return "number"; | 46 | return N_("number"); |
47 | 47 | ||
48 | case grecs_type_time: | 48 | case grecs_type_time: |
49 | return "time"; | 49 | return N_("time"); |
50 | 50 | ||
51 | case grecs_type_bool: | 51 | case grecs_type_bool: |
52 | return "boolean"; | 52 | return N_("boolean"); |
53 | 53 | ||
54 | case grecs_type_ipv4: | 54 | case grecs_type_ipv4: |
55 | return "IPv4"; | 55 | return N_("IPv4"); |
56 | 56 | ||
57 | case grecs_type_cidr: | 57 | case grecs_type_cidr: |
58 | return "CIDR"; | 58 | return N_("CIDR"); |
59 | 59 | ||
60 | case grecs_type_host: | 60 | case grecs_type_host: |
61 | return "hostname"; | 61 | return N_("hostname"); |
62 | 62 | ||
63 | case grecs_type_sockaddr: | 63 | case grecs_type_sockaddr: |
64 | return "sockaddr"; | 64 | return N_("sockaddr"); |
65 | 65 | ||
66 | case grecs_type_section: | 66 | case grecs_type_section: |
67 | return "section"; | 67 | return N_("section"); |
68 | 68 | ||
69 | case grecs_type_null: | 69 | case grecs_type_null: |
70 | return "null"; | 70 | return N_("null"); |
71 | } | 71 | } |
72 | return "UNKNOWN?"; | 72 | return "UNKNOWN?"; |
73 | } | 73 | } |
74 | 74 | ||
75 | static void | 75 | static void |
76 | format_level(unsigned level, FILE *stream) | 76 | format_level(unsigned level, FILE *stream) |
@@ -59,12 +59,25 @@ void | |||
59 | grecs_value_free(struct grecs_value *val) | 59 | grecs_value_free(struct grecs_value *val) |
60 | { | 60 | { |
61 | grecs_value_free_content(val); | 61 | grecs_value_free_content(val); |
62 | grecs_free(val); | 62 | grecs_free(val); |
63 | } | 63 | } |
64 | 64 | ||
65 | const char * | ||