cont
[protos/libecoli.git] / lib / ecoli_tk_many.c
1 /*
2  * Copyright (c) 2016, 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 <assert.h>
32 #include <stdarg.h>
33
34 #include <ecoli_malloc.h>
35 #include <ecoli_log.h>
36 #include <ecoli_test.h>
37 #include <ecoli_strvec.h>
38 #include <ecoli_tk.h>
39 #include <ecoli_tk_str.h>
40 #include <ecoli_tk_option.h>
41 #include <ecoli_tk_many.h>
42
43 struct ec_tk_many {
44         struct ec_tk gen;
45         unsigned int min;
46         unsigned int max;
47         struct ec_tk *child;
48 };
49
50 static struct ec_parsed_tk *ec_tk_many_parse(const struct ec_tk *gen_tk,
51         const struct ec_strvec *strvec)
52 {
53         struct ec_tk_many *tk = (struct ec_tk_many *)gen_tk;
54         struct ec_parsed_tk *parsed_tk, *child_parsed_tk;
55         struct ec_strvec *match_strvec;
56         struct ec_strvec childvec;
57         size_t off = 0, len, count;
58
59         parsed_tk = ec_parsed_tk_new();
60         if (parsed_tk == NULL)
61                 goto fail;
62
63         for (count = 0; tk->max == 0 || count < tk->max; count++) {
64                 if (ec_strvec_slice(&childvec, strvec, off) < 0)
65                         goto fail;
66
67                 child_parsed_tk = ec_tk_parse_tokens(tk->child, &childvec);
68                 if (child_parsed_tk == NULL)
69                         goto fail;
70
71                 if (!ec_parsed_tk_matches(child_parsed_tk)) {
72                         ec_parsed_tk_free(child_parsed_tk);
73                         break;
74                 }
75
76                 ec_parsed_tk_add_child(parsed_tk, child_parsed_tk);
77
78                 /* it matches "no token", no need to continue */
79                 len = ec_parsed_tk_len(child_parsed_tk);
80                 if (len == 0) {
81                         ec_parsed_tk_free(child_parsed_tk);
82                         break;
83                 }
84
85                 off += len;
86         }
87
88         if (count < tk->min) {
89                 ec_parsed_tk_free_children(parsed_tk);
90                 return parsed_tk;
91         }
92
93         match_strvec = ec_strvec_ndup(strvec, off);
94         if (match_strvec == NULL)
95                 goto fail;
96
97         ec_parsed_tk_set_match(parsed_tk, gen_tk, match_strvec);
98
99         return parsed_tk;
100
101  fail:
102         ec_parsed_tk_free(parsed_tk);
103         return NULL;
104 }
105
106 #if 0
107 static struct ec_completed_tk *ec_tk_many_complete(const struct ec_tk *gen_tk,
108         const struct ec_strvec *strvec)
109 {
110         struct ec_tk_many *tk = (struct ec_tk_many *)gen_tk;
111         struct ec_completed_tk *completed_tk, *child_completed_tk;
112         struct ec_strvec childvec;
113         struct ec_parsed_tk *parsed_tk;
114         size_t len = 0;
115         unsigned int i;
116
117         completed_tk = ec_completed_tk_new();
118         if (completed_tk == NULL)
119                 return NULL;
120
121         if (tk->len == 0)
122                 return completed_tk;
123
124         for (i = 0; i < tk->len; i++) {
125                 if (ec_strvec_slice(&childvec, strvec, len) < 0)
126                         return completed_tk; /* XXX fail ? */
127
128                 child_completed_tk = ec_tk_complete_tokens(tk->table[i],
129                         &childvec);
130                 if (child_completed_tk == NULL) {
131                         ec_completed_tk_free(completed_tk);
132                         return NULL;
133                 }
134                 ec_completed_tk_merge(completed_tk, child_completed_tk);
135
136                 parsed_tk = ec_tk_parse_tokens(tk->table[i], &childvec);
137                 if (parsed_tk == NULL)
138                         goto fail;
139                 if (!ec_parsed_tk_matches(parsed_tk)) {
140                         ec_parsed_tk_free(parsed_tk);
141                         break;
142                 }
143
144                 len += ec_strvec_len(parsed_tk->strvec);
145                 ec_parsed_tk_free(parsed_tk);
146         }
147
148         return completed_tk;
149
150 fail:
151         /* XXX */
152         return NULL;
153 }
154 #endif
155
156 static void ec_tk_many_free_priv(struct ec_tk *gen_tk)
157 {
158         struct ec_tk_many *tk = (struct ec_tk_many *)gen_tk;
159
160         ec_tk_free(tk->child);
161 }
162
163 static struct ec_tk_ops ec_tk_many_ops = {
164         .typename = "many",
165         .parse = ec_tk_many_parse,
166         .complete = ec_tk_default_complete,
167 //XXX   .complete = ec_tk_many_complete,
168         .free_priv = ec_tk_many_free_priv,
169 };
170
171 struct ec_tk *ec_tk_many_new(const char *id, struct ec_tk *child,
172         unsigned int min, unsigned int max)
173 {
174         struct ec_tk_many *tk = NULL;
175
176         if (child == NULL)
177                 return NULL;
178
179         tk = (struct ec_tk_many *)ec_tk_new(id, &ec_tk_many_ops,
180                 sizeof(*tk));
181         if (tk == NULL) {
182                 ec_tk_free(child);
183                 return NULL;
184         }
185
186         tk->child = child;
187         tk->min = min;
188         tk->max = max;
189
190         return &tk->gen;
191 }
192
193 static int ec_tk_many_testcase(void)
194 {
195         struct ec_tk *tk;
196         int ret = 0;
197
198         tk = ec_tk_many_new(NULL, ec_tk_str_new(NULL, "foo"), 0, 0);
199         if (tk == NULL) {
200                 ec_log(EC_LOG_ERR, "cannot create tk\n");
201                 return -1;
202         }
203         ret |= EC_TEST_CHECK_TK_PARSE(tk, 0, "bar", EC_TK_ENDLIST);
204         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "foo", "bar", EC_TK_ENDLIST);
205         ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "foo", "foo", "bar",
206                 EC_TK_ENDLIST);
207         ret |= EC_TEST_CHECK_TK_PARSE(tk, 0, EC_TK_ENDLIST);
208         ec_tk_free(tk);
209
210         tk = ec_tk_many_new(NULL, ec_tk_str_new(NULL, "foo"), 1, 0);
211         if (tk == NULL) {
212                 ec_log(EC_LOG_ERR, "cannot create tk\n");
213                 return -1;
214         }
215         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "bar", EC_TK_ENDLIST);
216         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "foo", "bar", EC_TK_ENDLIST);
217         ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "foo", "foo", "bar",
218                 EC_TK_ENDLIST);
219         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, EC_TK_ENDLIST);
220         ec_tk_free(tk);
221
222         tk = ec_tk_many_new(NULL, ec_tk_str_new(NULL, "foo"), 1, 2);
223         if (tk == NULL) {
224                 ec_log(EC_LOG_ERR, "cannot create tk\n");
225                 return -1;
226         }
227         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "bar", EC_TK_ENDLIST);
228         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "foo", "bar", EC_TK_ENDLIST);
229         ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "foo", "foo", "bar",
230                 EC_TK_ENDLIST);
231         ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "foo", "foo", "foo",
232                 EC_TK_ENDLIST);
233         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, EC_TK_ENDLIST);
234         ec_tk_free(tk);
235
236         /* test completion */
237         /* XXX */
238
239         return ret;
240 }
241
242 static struct ec_test ec_tk_many_test = {
243         .name = "many",
244         .test = ec_tk_many_testcase,
245 };
246
247 EC_REGISTER_TEST(ec_tk_many_test);