ab4f35265e43fe0190c2e8b009509d9173bad29a
[protos/libecoli.git] / libecoli / ecoli_node_option.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 <assert.h>
9 #include <stdarg.h>
10 #include <errno.h>
11
12 #include <ecoli_malloc.h>
13 #include <ecoli_log.h>
14 #include <ecoli_strvec.h>
15 #include <ecoli_node.h>
16 #include <ecoli_parse.h>
17 #include <ecoli_complete.h>
18 #include <ecoli_node_str.h>
19 #include <ecoli_test.h>
20 #include <ecoli_node_option.h>
21
22 EC_LOG_TYPE_REGISTER(node_option);
23
24 struct ec_node_option {
25         struct ec_node gen;
26         struct ec_node *child;
27 };
28
29 static int
30 ec_node_option_parse(const struct ec_node *gen_node,
31                 struct ec_parse *state,
32                 const struct ec_strvec *strvec)
33 {
34         struct ec_node_option *node = (struct ec_node_option *)gen_node;
35         int ret;
36
37         ret = ec_node_parse_child(node->child, state, strvec);
38         if (ret < 0)
39                 return ret;
40
41         if (ret == EC_PARSE_NOMATCH)
42                 return 0;
43
44         return ret;
45 }
46
47 static int
48 ec_node_option_complete(const struct ec_node *gen_node,
49                         struct ec_comp *comp,
50                         const struct ec_strvec *strvec)
51 {
52         struct ec_node_option *node = (struct ec_node_option *)gen_node;
53
54         return ec_node_complete_child(node->child, comp, strvec);
55 }
56
57 static void ec_node_option_free_priv(struct ec_node *gen_node)
58 {
59         struct ec_node_option *node = (struct ec_node_option *)gen_node;
60
61         ec_node_free(node->child);
62 }
63
64 static size_t
65 ec_node_option_get_children_count(const struct ec_node *gen_node)
66 {
67         struct ec_node_option *node = (struct ec_node_option *)gen_node;
68
69         if (node->child)
70                 return 1;
71         return 0;
72 }
73
74 static int
75 ec_node_option_get_child(const struct ec_node *gen_node, size_t i,
76         struct ec_node **child, unsigned int *refs)
77 {
78         struct ec_node_option *node = (struct ec_node_option *)gen_node;
79
80         if (i >= 1)
81                 return -1;
82
83         *child = node->child;
84         *refs = 1;
85         return 0;
86 }
87
88 static struct ec_node_type ec_node_option_type = {
89         .name = "option",
90         .parse = ec_node_option_parse,
91         .complete = ec_node_option_complete,
92         .size = sizeof(struct ec_node_option),
93         .free_priv = ec_node_option_free_priv,
94         .get_children_count = ec_node_option_get_children_count,
95         .get_child = ec_node_option_get_child,
96 };
97
98 EC_NODE_TYPE_REGISTER(ec_node_option_type);
99
100 int ec_node_option_set(struct ec_node *gen_node, struct ec_node *child)
101 {
102         struct ec_node_option *node = (struct ec_node_option *)gen_node;
103
104         if (gen_node == NULL || child == NULL) {
105                 errno = EINVAL;
106                 goto fail;
107         }
108
109         if (ec_node_check_type(gen_node, &ec_node_option_type) < 0)
110                 goto fail;
111
112         node->child = child;
113
114         return 0;
115
116 fail:
117         ec_node_free(child);
118         return -1;
119 }
120
121 struct ec_node *ec_node_option(const char *id, struct ec_node *child)
122 {
123         struct ec_node *gen_node = NULL;
124
125         if (child == NULL)
126                 goto fail;
127
128         gen_node = ec_node_from_type(&ec_node_option_type, id);
129         if (gen_node == NULL)
130                 goto fail;
131
132         ec_node_option_set(gen_node, child);
133         child = NULL;
134
135         return gen_node;
136
137 fail:
138         ec_node_free(child);
139         return NULL;
140 }
141
142 /* LCOV_EXCL_START */
143 static int ec_node_option_testcase(void)
144 {
145         struct ec_node *node;
146         int testres = 0;
147
148         node = ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"));
149         if (node == NULL) {
150                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
151                 return -1;
152         }
153         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
154         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
155         testres |= EC_TEST_CHECK_PARSE(node, 0, "bar");
156         testres |= EC_TEST_CHECK_PARSE(node, 0);
157         ec_node_free(node);
158
159         /* test completion */
160         node = ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"));
161         if (node == NULL) {
162                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
163                 return -1;
164         }
165         testres |= EC_TEST_CHECK_COMPLETE(node,
166                 "", EC_NODE_ENDLIST,
167                 "foo", EC_NODE_ENDLIST);
168         testres |= EC_TEST_CHECK_COMPLETE(node,
169                 "f", EC_NODE_ENDLIST,
170                 "foo", EC_NODE_ENDLIST);
171         testres |= EC_TEST_CHECK_COMPLETE(node,
172                 "b", EC_NODE_ENDLIST,
173                 EC_NODE_ENDLIST);
174         ec_node_free(node);
175
176         return testres;
177 }
178 /* LCOV_EXCL_STOP */
179
180 static struct ec_test ec_node_option_test = {
181         .name = "node_option",
182         .test = ec_node_option_testcase,
183 };
184
185 EC_TEST_REGISTER(ec_node_option_test);