8efbec4836ca94d550ebdf30f7ab99092398e5a0
[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_dict.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         char *attr_name;
25 };
26
27 static int ec_node_any_parse(const struct ec_node *node,
28                         struct ec_parse *state,
29                         const struct ec_strvec *strvec)
30 {
31         struct ec_node_any *priv = ec_node_priv(node);
32         const struct ec_dict *attrs;
33
34         (void)state;
35
36         if (ec_strvec_len(strvec) == 0)
37                 return EC_PARSE_NOMATCH;
38         if (priv->attr_name != NULL) {
39                 attrs = ec_strvec_get_attrs(strvec, 0);
40                 if (attrs == NULL || !ec_dict_has_key(attrs, priv->attr_name))
41                         return EC_PARSE_NOMATCH;
42         }
43
44         return 1;
45 }
46
47 static void ec_node_any_free_priv(struct ec_node *node)
48 {
49         struct ec_node_any *priv = ec_node_priv(node);
50
51         ec_free(priv->attr_name);
52 }
53
54 static const struct ec_config_schema ec_node_any_schema[] = {
55         {
56                 .key = "attr",
57                 .desc = "The optional attribute name to attach.",
58                 .type = EC_CONFIG_TYPE_STRING,
59         },
60         {
61                 .type = EC_CONFIG_TYPE_NONE,
62         },
63 };
64
65 static int ec_node_any_set_config(struct ec_node *node,
66                                 const struct ec_config *config)
67 {
68         struct ec_node_any *priv = ec_node_priv(node);
69         const struct ec_config *value = NULL;
70         char *s = NULL;
71
72         value = ec_config_dict_get(config, "attr");
73         if (value != NULL) {
74                 s = ec_strdup(value->string);
75                 if (s == NULL)
76                         goto fail;
77         }
78
79         ec_free(priv->attr_name);
80         priv->attr_name = s;
81
82         return 0;
83
84 fail:
85         ec_free(s);
86         return -1;
87 }
88
89 static struct ec_node_type ec_node_any_type = {
90         .name = "any",
91         .schema = ec_node_any_schema,
92         .set_config = ec_node_any_set_config,
93         .parse = ec_node_any_parse,
94         .complete = ec_node_complete_unknown,
95         .size = sizeof(struct ec_node_any),
96         .free_priv = ec_node_any_free_priv,
97 };
98
99 EC_NODE_TYPE_REGISTER(ec_node_any_type);
100
101 struct ec_node *
102 ec_node_any(const char *id, const char *attr)
103 {
104         struct ec_config *config = NULL;
105         struct ec_node *node = NULL;
106         int ret;
107
108         node = ec_node_from_type(&ec_node_any_type, id);
109         if (node == NULL)
110                 return NULL;
111
112         config = ec_config_dict();
113         if (config == NULL)
114                 goto fail;
115
116         ret = ec_config_dict_set(config, "attr", ec_config_string(attr));
117         if (ret < 0)
118                 goto fail;
119
120         ret = ec_node_set_config(node, config);
121         config = NULL;
122         if (ret < 0)
123                 goto fail;
124
125         return node;
126
127 fail:
128         ec_config_free(config);
129         ec_node_free(node);
130         return NULL;
131 }
132
133 /* LCOV_EXCL_START */
134 static int ec_node_any_testcase(void)
135 {
136         struct ec_node *node;
137         int testres = 0;
138
139         node = ec_node("any", EC_NO_ID);
140         if (node == NULL) {
141                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
142                 return -1;
143         }
144         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
145         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
146         testres |= EC_TEST_CHECK_PARSE(node, -1);
147         ec_node_free(node);
148
149         /* never completes */
150         node = ec_node("any", EC_NO_ID);
151         if (node == NULL) {
152                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
153                 return -1;
154         }
155         testres |= EC_TEST_CHECK_COMPLETE(node,
156                 "", EC_NODE_ENDLIST,
157                 EC_NODE_ENDLIST);
158         testres |= EC_TEST_CHECK_COMPLETE(node,
159                 "foo", EC_NODE_ENDLIST,
160                 EC_NODE_ENDLIST);
161         ec_node_free(node);
162
163         return testres;
164 }
165 /* LCOV_EXCL_STOP */
166
167 static struct ec_test ec_node_any_test = {
168         .name = "node_any",
169         .test = ec_node_any_testcase,
170 };
171
172 EC_TEST_REGISTER(ec_node_any_test);