add meson support
[protos/libecoli.git] / src / ecoli_node_any.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <errno.h>
9
10 #include <ecoli_malloc.h>
11 #include <ecoli_log.h>
12 #include <ecoli_test.h>
13 #include <ecoli_strvec.h>
14 #include <ecoli_node.h>
15 #include <ecoli_parse.h>
16 #include <ecoli_complete.h>
17 #include <ecoli_keyval.h>
18 #include <ecoli_config.h>
19 #include <ecoli_node_any.h>
20
21 EC_LOG_TYPE_REGISTER(node_any);
22
23 struct ec_node_any {
24         struct ec_node gen;
25         char *attr_name;
26 };
27
28 static int ec_node_any_parse(const struct ec_node *gen_node,
29                         struct ec_parse *state,
30                         const struct ec_strvec *strvec)
31 {
32         struct ec_node_any *node = (struct ec_node_any *)gen_node;
33         const struct ec_keyval *attrs;
34
35         (void)state;
36
37         if (ec_strvec_len(strvec) == 0)
38                 return EC_PARSE_NOMATCH;
39         if (node->attr_name != NULL) {
40                 attrs = ec_strvec_get_attrs(strvec, 0);
41                 if (attrs == NULL || !ec_keyval_has_key(attrs, node->attr_name))
42                         return EC_PARSE_NOMATCH;
43         }
44
45         return 1;
46 }
47
48 static void ec_node_any_free_priv(struct ec_node *gen_node)
49 {
50         struct ec_node_any *node = (struct ec_node_any *)gen_node;
51
52         ec_free(node->attr_name);
53 }
54
55 static const struct ec_config_schema ec_node_any_schema[] = {
56         {
57                 .key = "attr",
58                 .desc = "The optional attribute name to attach.",
59                 .type = EC_CONFIG_TYPE_STRING,
60         },
61         {
62                 .type = EC_CONFIG_TYPE_NONE,
63         },
64 };
65
66 static int ec_node_any_set_config(struct ec_node *gen_node,
67                                 const struct ec_config *config)
68 {
69         struct ec_node_any *node = (struct ec_node_any *)gen_node;
70         const struct ec_config *value = NULL;
71         char *s = NULL;
72
73         value = ec_config_dict_get(config, "attr");
74         if (value != NULL) {
75                 s = ec_strdup(value->string);
76                 if (s == NULL)
77                         goto fail;
78         }
79
80         ec_free(node->attr_name);
81         node->attr_name = s;
82
83         return 0;
84
85 fail:
86         ec_free(s);
87         return -1;
88 }
89
90 static struct ec_node_type ec_node_any_type = {
91         .name = "any",
92         .schema = ec_node_any_schema,
93         .set_config = ec_node_any_set_config,
94         .parse = ec_node_any_parse,
95         .complete = ec_node_complete_unknown,
96         .size = sizeof(struct ec_node_any),
97         .free_priv = ec_node_any_free_priv,
98 };
99
100 EC_NODE_TYPE_REGISTER(ec_node_any_type);
101
102 /* LCOV_EXCL_START */
103 static int ec_node_any_testcase(void)
104 {
105         struct ec_node *node;
106         int testres = 0;
107
108         node = ec_node("any", EC_NO_ID);
109         if (node == NULL) {
110                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
111                 return -1;
112         }
113         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
114         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
115         testres |= EC_TEST_CHECK_PARSE(node, -1);
116         ec_node_free(node);
117
118         /* never completes */
119         node = ec_node("any", EC_NO_ID);
120         if (node == NULL) {
121                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
122                 return -1;
123         }
124         testres |= EC_TEST_CHECK_COMPLETE(node,
125                 "", EC_NODE_ENDLIST,
126                 EC_NODE_ENDLIST);
127         testres |= EC_TEST_CHECK_COMPLETE(node,
128                 "foo", EC_NODE_ENDLIST,
129                 EC_NODE_ENDLIST);
130         ec_node_free(node);
131
132         return testres;
133 }
134 /* LCOV_EXCL_STOP */
135
136 static struct ec_test ec_node_any_test = {
137         .name = "node_any",
138         .test = ec_node_any_testcase,
139 };
140
141 EC_TEST_REGISTER(ec_node_any_test);