save
[protos/libecoli.git] / lib / ecoli_node_sh_lex.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 #include <ctype.h>
34 #include <errno.h>
35
36 #include <ecoli_malloc.h>
37 #include <ecoli_log.h>
38 #include <ecoli_test.h>
39 #include <ecoli_strvec.h>
40 #include <ecoli_node.h>
41 #include <ecoli_parsed.h>
42 #include <ecoli_completed.h>
43 #include <ecoli_node_seq.h>
44 #include <ecoli_node_str.h>
45 #include <ecoli_node_option.h>
46 #include <ecoli_node_sh_lex.h>
47
48 struct ec_node_sh_lex {
49         struct ec_node gen;
50         struct ec_node *child;
51 };
52
53 static size_t eat_spaces(const char *str)
54 {
55         size_t i = 0;
56
57         /* skip spaces */
58         while (isblank(str[i]))
59                 i++;
60
61         return i;
62 }
63
64 /*
65  * Allocate a new string which is a copy of the input string with quotes
66  * removed. If quotes are not closed properly, set missing_quote to the
67  * missing quote char.
68  */
69 static char *unquote_str(const char *str, size_t n, int allow_missing_quote,
70         char *missing_quote)
71 {
72         unsigned s = 1, d = 0;
73         char quote = str[0];
74         char *dst;
75         int closed = 0;
76
77         dst = ec_malloc(n);
78         if (dst == NULL) {
79                 errno = ENOMEM;
80                 return NULL;
81         }
82
83         /* copy string and remove quotes */
84         while (s < n && d < n && str[s] != '\0') {
85                 if (str[s] == '\\' && str[s+1] == quote) {
86                         dst[d++] = quote;
87                         s += 2;
88                         continue;
89                 }
90                 if (str[s] == '\\' && str[s+1] == '\\') {
91                         dst[d++] = '\\';
92                         s += 2;
93                         continue;
94                 }
95                 if (str[s] == quote) {
96                         s++;
97                         closed = 1;
98                         break;
99                 }
100                 dst[d++] = str[s++];
101         }
102
103         /* not enough room in dst buffer (should not happen) */
104         if (d >= n) {
105                 ec_free(dst);
106                 errno = EMSGSIZE;
107                 return NULL;
108         }
109
110         /* quote not closed */
111         if (closed == 0) {
112                 if (missing_quote != NULL)
113                         *missing_quote = str[0];
114                 if (allow_missing_quote == 0) {
115                         ec_free(dst);
116                         errno = EINVAL;
117                         return NULL;
118                 }
119         }
120         dst[d++] = '\0';
121
122         return dst;
123 }
124
125 static size_t eat_quoted_str(const char *str)
126 {
127         size_t i = 0;
128         char quote = str[0];
129
130         while (str[i] != '\0') {
131                 if (str[i] != '\\' && str[i+1] == quote)
132                         return i + 2;
133                 i++;
134         }
135
136         /* unclosed quote, will be detected later */
137         return i;
138 }
139
140 static size_t eat_str(const char *str)
141 {
142         size_t i = 0;
143
144         /* eat chars until we find a quote, space, or end of string  */
145         while (!isblank(str[i]) && str[i] != '\0' &&
146                         str[i] != '"' && str[i] != '\'')
147                 i++;
148
149         return i;
150 }
151
152 static struct ec_strvec *tokenize(const char *str, int completion,
153         int allow_missing_quote, char *missing_quote)
154 {
155         struct ec_strvec *strvec = NULL;
156         size_t off = 0, len, suboff, sublen;
157         char *word = NULL, *concat = NULL, *tmp;
158         int last_is_space = 1;
159
160 //      printf("str=%s\n", str);
161
162         strvec = ec_strvec();
163         if (strvec == NULL)
164                 goto fail;
165
166         while (str[off] != '\0') {
167                 len = eat_spaces(&str[off]);
168                 if (len > 0)
169                         last_is_space = 1;
170 //              printf("space=%zd\n", len);
171                 off += len;
172
173                 len = 0;
174                 suboff = off;
175                 while (str[suboff] != '\0') {
176                         last_is_space = 0;
177                         if (str[suboff] == '"' || str[suboff] == '\'') {
178                                 sublen = eat_quoted_str(&str[suboff]);
179 //                              printf("sublen=%zd\n", sublen);
180                                 word = unquote_str(&str[suboff], sublen,
181                                         allow_missing_quote, missing_quote);
182                         } else {
183                                 sublen = eat_str(&str[suboff]);
184 //                              printf("sublen=%zd\n", sublen);
185                                 if (sublen == 0)
186                                         break;
187                                 word = ec_strndup(&str[suboff], sublen);
188                         }
189
190                         if (word == NULL)
191                                 goto fail;
192 //                      printf("word=%s\n", word);
193
194                         len += sublen;
195                         suboff += sublen;
196
197                         if (concat == NULL) {
198                                 concat = word;
199                                 word = NULL;
200                         } else {
201                                 tmp = ec_realloc(concat, len + 1);
202                                 if (tmp == NULL)
203                                         goto fail;
204                                 concat = tmp;
205                                 strcat(concat, word);
206                                 ec_free(word);
207                                 word = NULL;
208                         }
209                 }
210
211                 if (concat != NULL) {
212                         if (ec_strvec_add(strvec, concat) < 0)
213                                 goto fail;
214                         ec_free(concat);
215                         concat = NULL;
216                 }
217
218                 /* XXX remove all printf comments */
219 //              printf("str off=%zd len=%zd\n", off, len);
220                 off += len;
221         }
222
223         /* in completion mode, append an empty string in the vector if
224          * the input string ends with space */
225         if (completion && last_is_space) {
226                 if (ec_strvec_add(strvec, "") < 0)
227                         goto fail;
228         }
229
230         return strvec;
231
232  fail:
233         ec_free(word);
234         ec_free(concat);
235         ec_strvec_free(strvec);
236         return NULL;
237 }
238
239 static int
240 ec_node_sh_lex_parse(const struct ec_node *gen_node,
241                 struct ec_parsed *state,
242                 const struct ec_strvec *strvec)
243 {
244         struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
245         struct ec_strvec *new_vec = NULL;
246         struct ec_parsed *child_parsed;
247         const char *str;
248         int ret;
249
250         if (ec_strvec_len(strvec) == 0) {
251                 new_vec = ec_strvec();
252         } else {
253                 str = ec_strvec_val(strvec, 0);
254                 new_vec = tokenize(str, 0, 0, NULL);
255         }
256         if (new_vec == NULL) {
257                 ret = -ENOMEM;
258                 goto fail;
259         }
260
261         ret = ec_node_parse_child(node->child, state, new_vec);
262         if (ret >= 0) {
263                 if ((unsigned)ret == ec_strvec_len(new_vec)) {
264                         ret = 1;
265                 } else {
266                         child_parsed = ec_parsed_get_last_child(state);
267                         ec_parsed_del_child(state, child_parsed);
268                         ec_parsed_free(child_parsed);
269                         ret = EC_PARSED_NOMATCH;
270                 }
271         }
272
273         ec_strvec_free(new_vec);
274         new_vec = NULL;
275
276         return ret;
277
278  fail:
279         ec_strvec_free(new_vec);
280         return ret;
281 }
282
283 static int
284 ec_node_sh_lex_complete(const struct ec_node *gen_node,
285                         struct ec_completed *completed,
286                         struct ec_parsed *parsed,
287                         const struct ec_strvec *strvec)
288 {
289         struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
290         struct ec_strvec *new_vec = NULL;
291         const char *str;
292         char missing_quote;
293         int ret;
294
295         if (ec_strvec_len(strvec) != 1)
296                 return 0;
297
298         str = ec_strvec_val(strvec, 0);
299 //      printf("\nold:%s\n", str);
300         new_vec = tokenize(str, 1, 1, &missing_quote);
301         if (new_vec == NULL)
302                 goto fail;
303 //      printf("new:%s\n", ec_strvec_val(new_vec, 0));
304
305         // XXX: complete should add the quotes for !EC_PARTIAL: use another
306         // completed object
307         ret = ec_node_complete_child(node->child, completed, parsed, new_vec);
308         if (ret < 0)
309                 goto fail;
310
311         ec_strvec_free(new_vec);
312
313         return 0;
314
315  fail:
316         ec_strvec_free(new_vec);
317         return -1;
318 }
319
320 static void ec_node_sh_lex_free_priv(struct ec_node *gen_node)
321 {
322         struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
323
324         ec_node_free(node->child);
325 }
326
327 static struct ec_node_type ec_node_sh_lex_type = {
328         .name = "sh_lex",
329         .parse = ec_node_sh_lex_parse,
330         .complete = ec_node_sh_lex_complete,
331         .size = sizeof(struct ec_node_sh_lex),
332         .free_priv = ec_node_sh_lex_free_priv,
333 };
334
335 EC_NODE_TYPE_REGISTER(ec_node_sh_lex_type);
336
337 struct ec_node *ec_node_sh_lex(const char *id, struct ec_node *child)
338 {
339         struct ec_node_sh_lex *node = NULL;
340
341         if (child == NULL)
342                 return NULL;
343
344         node = (struct ec_node_sh_lex *)__ec_node(&ec_node_sh_lex_type, id);
345         if (node == NULL) {
346                 ec_node_free(child);
347                 return NULL;
348         }
349
350         node->child = child;
351
352         return &node->gen;
353 }
354
355 /* LCOV_EXCL_START */
356 static int ec_node_sh_lex_testcase(void)
357 {
358         struct ec_node *node;
359         int ret = 0;
360
361         node = ec_node_sh_lex(NULL,
362                 EC_NODE_SEQ(NULL,
363                         ec_node_str(NULL, "foo"),
364                         ec_node_option(NULL,
365                                 ec_node_str(NULL, "toto")
366                         ),
367                         ec_node_str(NULL, "bar")
368                 )
369         );
370         if (node == NULL) {
371                 ec_log(EC_LOG_ERR, "cannot create node\n");
372                 return -1;
373         }
374         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo bar");
375         ret |= EC_TEST_CHECK_PARSE(node, 1, "  foo   bar");
376         ret |= EC_TEST_CHECK_PARSE(node, 1, "  'foo' \"bar\"");
377         ret |= EC_TEST_CHECK_PARSE(node, 1, "  'f'oo 'toto' bar");
378         ret |= EC_TEST_CHECK_PARSE(node, -1, "  foo toto bar'");
379         ec_node_free(node);
380
381         /* test completion */
382         node = ec_node_sh_lex(NULL,
383                 EC_NODE_SEQ(NULL,
384                         ec_node_str(NULL, "foo"),
385                         ec_node_option(NULL,
386                                 ec_node_str(NULL, "toto")
387                         ),
388                         ec_node_str(NULL, "bar"),
389                         ec_node_str(NULL, "titi")
390                 )
391         );
392         if (node == NULL) {
393                 ec_log(EC_LOG_ERR, "cannot create node\n");
394                 return -1;
395         }
396         ret |= EC_TEST_CHECK_COMPLETE(node,
397                 "", EC_NODE_ENDLIST,
398                 "foo", EC_NODE_ENDLIST,
399                 "foo");
400         ret |= EC_TEST_CHECK_COMPLETE(node,
401                 " ", EC_NODE_ENDLIST,
402                 "foo", EC_NODE_ENDLIST,
403                 "foo");
404         ret |= EC_TEST_CHECK_COMPLETE(node,
405                 "f", EC_NODE_ENDLIST,
406                 "oo", EC_NODE_ENDLIST,
407                 "oo");
408         ret |= EC_TEST_CHECK_COMPLETE(node,
409                 "foo", EC_NODE_ENDLIST,
410                 "", EC_NODE_ENDLIST,
411                 "");
412         ret |= EC_TEST_CHECK_COMPLETE(node,
413                 "foo ", EC_NODE_ENDLIST,
414                 "bar", "toto", EC_NODE_ENDLIST,
415                 "");
416         ret |= EC_TEST_CHECK_COMPLETE(node,
417                 "foo t", EC_NODE_ENDLIST,
418                 "oto", EC_NODE_ENDLIST,
419                 "oto");
420         ret |= EC_TEST_CHECK_COMPLETE(node,
421                 "foo b", EC_NODE_ENDLIST,
422                 "ar", EC_NODE_ENDLIST,
423                 "ar");
424         ret |= EC_TEST_CHECK_COMPLETE(node,
425                 "foo bar", EC_NODE_ENDLIST,
426                 "", EC_NODE_ENDLIST,
427                 "");
428         ret |= EC_TEST_CHECK_COMPLETE(node,
429                 "foo bar ", EC_NODE_ENDLIST,
430                 "titi", EC_NODE_ENDLIST,
431                 "titi");
432         ret |= EC_TEST_CHECK_COMPLETE(node,
433                 "foo toto bar ", EC_NODE_ENDLIST,
434                 "titi", EC_NODE_ENDLIST,
435                 "titi");
436         ret |= EC_TEST_CHECK_COMPLETE(node,
437                 "x", EC_NODE_ENDLIST,
438                 EC_NODE_ENDLIST,
439                 "");
440         ret |= EC_TEST_CHECK_COMPLETE(node,
441                 "foo barx", EC_NODE_ENDLIST,
442                 EC_NODE_ENDLIST,
443                 "");
444
445         ec_node_free(node);
446         return ret;
447 }
448 /* LCOV_EXCL_STOP */
449
450 static struct ec_test ec_node_sh_lex_test = {
451         .name = "node_sh_lex",
452         .test = ec_node_sh_lex_testcase,
453 };
454
455 EC_TEST_REGISTER(ec_node_sh_lex_test);