api documentation for ec_parse
[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_pnode *pstate,
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)pstate;
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         .size = sizeof(struct ec_node_any),
95         .free_priv = ec_node_any_free_priv,
96 };
97
98 EC_NODE_TYPE_REGISTER(ec_node_any_type);
99
100 struct ec_node *
101 ec_node_any(const char *id, const char *attr)
102 {
103         struct ec_config *config = NULL;
104         struct ec_node *node = NULL;
105         int ret;
106
107         node = ec_node_from_type(&ec_node_any_type, id);
108         if (node == NULL)
109                 return NULL;
110
111         config = ec_config_dict();
112         if (config == NULL)
113                 goto fail;
114
115         ret = ec_config_dict_set(config, "attr", ec_config_string(attr));
116         if (ret < 0)
117                 goto fail;
118
119         ret = ec_node_set_config(node, config);
120         config = NULL;
121         if (ret < 0)
122                 goto fail;
123
124         return node;
125
126 fail:
127         ec_config_free(config);
128         ec_node_free(node);
129         return NULL;
130 }
131
132 /* LCOV_EXCL_START */
133 static int ec_node_any_testcase(void)
134 {
135         struct ec_node *node;
136         int testres = 0;
137
138         node = ec_node("any", EC_NO_ID);
139         if (node == NULL) {
140                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
141                 return -1;
142         }
143         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
144         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
145         testres |= EC_TEST_CHECK_PARSE(node, -1);
146         ec_node_free(node);
147
148         /* never completes */
149         node = ec_node("any", EC_NO_ID);
150         if (node == NULL) {
151                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
152                 return -1;
153         }
154         testres |= EC_TEST_CHECK_COMPLETE(node,
155                 "", EC_VA_END,
156                 EC_VA_END);
157         testres |= EC_TEST_CHECK_COMPLETE(node,
158                 "foo", EC_VA_END,
159                 EC_VA_END);
160         ec_node_free(node);
161
162         return testres;
163 }
164 /* LCOV_EXCL_STOP */
165
166 static struct ec_test ec_node_any_test = {
167         .name = "node_any",
168         .test = ec_node_any_testcase,
169 };
170
171 EC_TEST_REGISTER(ec_node_any_test);