b4bb391d8c4b2fbd319307881228d5c0b7d756b8
[protos/libecoli.git] / lib / 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 size_t
58 ec_node_option_get_children_count(const struct ec_node *gen_node)
59 {
60         struct ec_node_option *node = (struct ec_node_option *)gen_node;
61
62         if (node->child)
63                 return 1;
64         return 0;
65 }
66
67 static struct ec_node *
68 ec_node_option_get_child(const struct ec_node *gen_node, size_t i)
69 {
70         struct ec_node_option *node = (struct ec_node_option *)gen_node;
71
72         if (i >= 1)
73                 return NULL;
74
75         return node->child;
76 }
77
78 static struct ec_node_type ec_node_option_type = {
79         .name = "option",
80         .parse = ec_node_option_parse,
81         .complete = ec_node_option_complete,
82         .size = sizeof(struct ec_node_option),
83         .get_children_count = ec_node_option_get_children_count,
84         .get_child = ec_node_option_get_child,
85 };
86
87 EC_NODE_TYPE_REGISTER(ec_node_option_type);
88
89 int ec_node_option_set(struct ec_node *gen_node, struct ec_node *child)
90 {
91         struct ec_node_option *node = (struct ec_node_option *)gen_node;
92
93         if (gen_node == NULL || child == NULL) {
94                 errno = EINVAL;
95                 goto fail;
96         }
97
98         if (ec_node_check_type(gen_node, &ec_node_option_type) < 0)
99                 goto fail;
100
101         node->child = child;
102
103         return 0;
104
105 fail:
106         ec_node_free(child);
107         return -1;
108 }
109
110 struct ec_node *ec_node_option(const char *id, struct ec_node *child)
111 {
112         struct ec_node *gen_node = NULL;
113
114         if (child == NULL)
115                 goto fail;
116
117         gen_node = __ec_node(&ec_node_option_type, id);
118         if (gen_node == NULL)
119                 goto fail;
120
121         ec_node_option_set(gen_node, child);
122         child = NULL;
123
124         return gen_node;
125
126 fail:
127         ec_node_free(child);
128         return NULL;
129 }
130
131 /* LCOV_EXCL_START */
132 static int ec_node_option_testcase(void)
133 {
134         struct ec_node *node;
135         int testres = 0;
136
137         node = ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"));
138         if (node == NULL) {
139                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
140                 return -1;
141         }
142         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
143         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
144         testres |= EC_TEST_CHECK_PARSE(node, 0, "bar");
145         testres |= EC_TEST_CHECK_PARSE(node, 0);
146         ec_node_free(node);
147
148         /* test completion */
149         node = ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"));
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_NODE_ENDLIST,
156                 "foo", EC_NODE_ENDLIST);
157         testres |= EC_TEST_CHECK_COMPLETE(node,
158                 "f", EC_NODE_ENDLIST,
159                 "foo", EC_NODE_ENDLIST);
160         testres |= EC_TEST_CHECK_COMPLETE(node,
161                 "b", EC_NODE_ENDLIST,
162                 EC_NODE_ENDLIST);
163         ec_node_free(node);
164
165         return testres;
166 }
167 /* LCOV_EXCL_STOP */
168
169 static struct ec_test ec_node_option_test = {
170         .name = "node_option",
171         .test = ec_node_option_testcase,
172 };
173
174 EC_TEST_REGISTER(ec_node_option_test);