save
[protos/libecoli.git] / lib / ecoli_node_dynamic.c
1 /*
2  * Copyright (c) 2017, Olivier MATZ <zer0@droids-corp.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the University of California, Berkeley nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32
33 #include <ecoli_log.h>
34 #include <ecoli_malloc.h>
35 #include <ecoli_test.h>
36 #include <ecoli_strvec.h>
37 #include <ecoli_keyval.h>
38 #include <ecoli_node.h>
39 #include <ecoli_parsed.h>
40 #include <ecoli_completed.h>
41 #include <ecoli_string.h>
42 #include <ecoli_node_str.h>
43 #include <ecoli_node_many.h>
44
45 #include "ecoli_node_dynamic.h"
46
47 EC_LOG_TYPE_REGISTER(node_dynamic);
48
49 struct ec_node_dynamic {
50         struct ec_node gen;
51         ec_node_dynamic_build_t build;
52         void *opaque;
53 };
54
55 static int
56 ec_node_dynamic_parse(const struct ec_node *gen_node,
57                 struct ec_parsed *parsed,
58                 const struct ec_strvec *strvec)
59 {
60         struct ec_node_dynamic *node = (struct ec_node_dynamic *)gen_node;
61         struct ec_node *child = NULL;
62         void (*node_free)(struct ec_node *) = ec_node_free;
63         char key[64];
64         int ret = -1;
65
66         child = node->build(parsed, node->opaque);
67         if (child == NULL)
68                 goto fail;
69
70         /* add the node pointer in the attributes, so it will be freed
71          * when parsed is freed */
72         snprintf(key, sizeof(key), "_dyn_%p", child);
73         ret = ec_keyval_set(ec_parsed_get_attrs(parsed), key, child,
74                         (void *)node_free);
75         if (ret < 0) {
76                 child = NULL; /* already freed */
77                 goto fail;
78         }
79
80         return ec_node_parse_child(child, parsed, strvec);
81
82 fail:
83         ec_node_free(child);
84         return ret;
85 }
86
87 static int
88 ec_node_dynamic_complete(const struct ec_node *gen_node,
89                 struct ec_completed *completed,
90                 const struct ec_strvec *strvec)
91 {
92         struct ec_node_dynamic *node = (struct ec_node_dynamic *)gen_node;
93         struct ec_parsed *parsed;
94         struct ec_node *child = NULL;
95         void (*node_free)(struct ec_node *) = ec_node_free;
96         char key[64];
97         int ret = -1;
98
99         parsed = ec_completed_get_state(completed);
100         child = node->build(parsed, node->opaque);
101         if (child == NULL)
102                 goto fail;
103
104         /* add the node pointer in the attributes, so it will be freed
105          * when parsed is freed */
106         snprintf(key, sizeof(key), "_dyn_%p", child);
107         ret = ec_keyval_set(completed->attrs, key, child,
108                         (void *)node_free);
109         if (ret < 0) {
110                 child = NULL; /* already freed */
111                 goto fail;
112         }
113
114         return ec_node_complete_child(child, completed, strvec);
115
116 fail:
117         ec_node_free(child);
118         return ret;
119 }
120
121 static struct ec_node_type ec_node_dynamic_type = {
122         .name = "dynamic",
123         .parse = ec_node_dynamic_parse,
124         .complete = ec_node_dynamic_complete,
125         .size = sizeof(struct ec_node_dynamic),
126 };
127
128 struct ec_node *
129 ec_node_dynamic(const char *id, ec_node_dynamic_build_t build, void *opaque)
130 {
131         struct ec_node *gen_node = NULL;
132         struct ec_node_dynamic *node;
133
134         if (build == NULL) {
135                 errno = EINVAL;
136                 goto fail;
137         }
138
139         gen_node = __ec_node(&ec_node_dynamic_type, id);
140         if (gen_node == NULL)
141                 goto fail;
142
143         node = (struct ec_node_dynamic *)gen_node;
144         node->build = build;
145         node->opaque = opaque;
146
147         return gen_node;
148
149 fail:
150         ec_node_free(gen_node);
151         return NULL;
152
153 }
154
155 EC_NODE_TYPE_REGISTER(ec_node_dynamic_type);
156
157 static struct ec_node *
158 build_counter(struct ec_parsed *parsed, void *opaque)
159 {
160         const struct ec_node *node;
161         struct ec_parsed *iter;
162         unsigned int count = 0;
163         char buf[32];
164
165         (void)opaque;
166         for (iter = ec_parsed_get_root(parsed); iter != NULL;
167              iter = ec_parsed_iter_next(iter)) {
168                 node = ec_parsed_get_node(iter);
169                 if (node->id && !strcmp(node->id, "my-id"))
170                         count++;
171         }
172         snprintf(buf, sizeof(buf), "count-%u", count);
173
174         return ec_node_str("my-id", buf);
175 }
176
177 /* LCOV_EXCL_START */
178 static int ec_node_dynamic_testcase(void)
179 {
180         struct ec_node *node;
181         int ret = 0;
182
183         node = ec_node_many(EC_NO_ID,
184                         ec_node_dynamic(EC_NO_ID, build_counter, NULL),
185                         1, 3);
186         if (node == NULL) {
187                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
188                 return -1;
189         }
190         ret |= EC_TEST_CHECK_PARSE(node, 1, "count-0");
191         ret |= EC_TEST_CHECK_PARSE(node, 3, "count-0", "count-1", "count-2");
192         ret |= EC_TEST_CHECK_PARSE(node, 1, "count-0", "count-0");
193
194         /* test completion */
195
196         ret |= EC_TEST_CHECK_COMPLETE(node,
197                 "c", EC_NODE_ENDLIST,
198                 "count-0", EC_NODE_ENDLIST);
199         ret |= EC_TEST_CHECK_COMPLETE(node,
200                 "count-0", "", EC_NODE_ENDLIST,
201                 "count-1", EC_NODE_ENDLIST,
202                 "count-1");
203         ec_node_free(node);
204
205         return ret;
206 }
207 /* LCOV_EXCL_STOP */
208
209 static struct ec_test ec_node_dynamic_test = {
210         .name = "node_dynamic",
211         .test = ec_node_dynamic_testcase,
212 };
213
214 EC_TEST_REGISTER(ec_node_dynamic_test);