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         /* skip spaces */
145         while (!isblank(str[i]) && str[i] != '\0')
146                 i++;
147
148         return i;
149 }
150
151 static struct ec_strvec *tokenize(const char *str, int completion,
152         int allow_missing_quote, char *missing_quote)
153 {
154         struct ec_strvec *strvec = NULL;
155         size_t off = 0, len, suboff, sublen;
156         char *word = NULL, *concat = NULL, *tmp;
157         int last_is_space = 1;
158
159 //      printf("str=%s\n", str);
160
161         strvec = ec_strvec();
162         if (strvec == NULL)
163                 goto fail;
164
165         while (str[off] != '\0') {
166                 len = eat_spaces(&str[off]);
167                 if (len > 0)
168                         last_is_space = 1;
169 //              printf("space=%zd\n", len);
170                 off += len;
171
172                 len = 0;
173                 suboff = off;
174                 while (str[suboff] != '\0') {
175                         last_is_space = 0;
176                         if (str[suboff] == '"' || str[suboff] == '\'') {
177                                 sublen = eat_quoted_str(&str[suboff]);
178 //                              printf("sublen=%zd\n", sublen);
179                                 word = unquote_str(&str[suboff], sublen,
180                                         allow_missing_quote, missing_quote);
181                         } else {
182                                 sublen = eat_str(&str[suboff]);
183 //                              printf("sublen=%zd\n", sublen);
184                                 if (sublen == 0)
185                                         break;
186                                 word = ec_strndup(&str[suboff], sublen);
187                         }
188
189                         if (word == NULL)
190                                 goto fail;
191 //                      printf("word=%s\n", word);
192
193                         len += sublen;
194                         suboff += sublen;
195
196                         if (concat == NULL) {
197                                 concat = word;
198                                 word = NULL;
199                         } else {
200                                 tmp = ec_realloc(concat, len + 1);
201                                 if (tmp == NULL)
202                                         goto fail;
203                                 concat = tmp;
204                                 strcat(concat, word);
205                                 ec_free(word);
206                                 word = NULL;
207                         }
208                 }
209
210                 if (concat != NULL) {
211                         if (ec_strvec_add(strvec, concat) < 0)
212                                 goto fail;
213                         ec_free(concat);
214                         concat = NULL;
215                 }
216
217                 /* XXX remove all printf comments */
218 //              printf("str off=%zd len=%zd\n", off, len);
219                 off += len;
220         }
221
222         /* in completion mode, append an empty string in the vector if
223          * the input string ends with space */
224         if (completion && last_is_space) {
225                 if (ec_strvec_add(strvec, "") < 0)
226                         goto fail;
227         }
228
229         return strvec;
230
231  fail:
232         ec_free(word);
233         ec_free(concat);
234         ec_strvec_free(strvec);
235         return NULL;
236 }
237
238 static struct ec_parsed *ec_node_sh_lex_parse(const struct ec_node *gen_node,
239         const struct ec_strvec *strvec)
240 {
241         struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
242         struct ec_strvec *new_vec = NULL, *match_strvec;
243         struct ec_parsed *parsed = NULL, *child_parsed;
244         const char *str;
245
246         parsed = ec_parsed();
247         if (parsed == NULL)
248                 return NULL;
249
250         if (ec_strvec_len(strvec) == 0)
251                 return parsed;
252
253         str = ec_strvec_val(strvec, 0);
254         new_vec = tokenize(str, 0, 0, NULL);
255         if (new_vec == NULL)
256                 goto fail;
257
258         child_parsed = ec_node_parse_strvec(node->child, new_vec);
259         if (child_parsed == NULL)
260                 goto fail;
261
262         if (!ec_parsed_matches(child_parsed) ||
263                         ec_parsed_len(child_parsed) !=
264                                 ec_strvec_len(new_vec)) {
265                 ec_strvec_free(new_vec);
266                 ec_parsed_free(child_parsed);
267                 return parsed;
268         }
269         ec_strvec_free(new_vec);
270         new_vec = NULL;
271
272         ec_parsed_add_child(parsed, child_parsed);
273         match_strvec = ec_strvec_ndup(strvec, 0, 1);
274         if (match_strvec == NULL)
275                 goto fail;
276         ec_parsed_set_match(parsed, gen_node, match_strvec);
277
278         return parsed;
279
280  fail:
281         ec_strvec_free(new_vec);
282         ec_parsed_free(parsed);
283
284         return NULL;
285 }
286
287 static struct ec_completed *ec_node_sh_lex_complete(const struct ec_node *gen_node,
288         const struct ec_strvec *strvec)
289 {
290         struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
291         struct ec_completed *completed, *child_completed = NULL;
292         struct ec_strvec *new_vec = NULL;
293         const char *str;
294         char missing_quote;
295
296 //      printf("==================\n");
297         completed = ec_completed();
298         if (completed == NULL)
299                 return NULL;
300
301         if (ec_strvec_len(strvec) != 1)
302                 return completed;
303
304         str = ec_strvec_val(strvec, 0);
305         new_vec = tokenize(str, 1, 1, &missing_quote);
306         if (new_vec == NULL)
307                 goto fail;
308
309 //      ec_strvec_dump(new_vec, stdout);
310
311         child_completed = ec_node_complete_strvec(node->child, new_vec);
312         if (child_completed == NULL)
313                 goto fail;
314
315         ec_strvec_free(new_vec);
316         new_vec = NULL;
317         ec_completed_merge(completed, child_completed);
318
319         return completed;
320
321  fail:
322         ec_strvec_free(new_vec);
323         ec_completed_free(completed);
324         return NULL;
325 }
326
327 static void ec_node_sh_lex_free_priv(struct ec_node *gen_node)
328 {
329         struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
330
331         ec_node_free(node->child);
332 }
333
334 static struct ec_node_type ec_node_sh_lex_type = {
335         .name = "sh_lex",
336         .parse = ec_node_sh_lex_parse,
337         .complete = ec_node_sh_lex_complete,
338         .size = sizeof(struct ec_node_sh_lex),
339         .free_priv = ec_node_sh_lex_free_priv,
340 };
341
342 EC_NODE_TYPE_REGISTER(ec_node_sh_lex_type);
343
344 struct ec_node *ec_node_sh_lex(const char *id, struct ec_node *child)
345 {
346         struct ec_node_sh_lex *node = NULL;
347
348         if (child == NULL)
349                 return NULL;
350
351         node = (struct ec_node_sh_lex *)__ec_node(&ec_node_sh_lex_type, id);
352         if (node == NULL) {
353                 ec_node_free(child);
354                 return NULL;
355         }
356
357         node->child = child;
358
359         return &node->gen;
360 }
361
362 static int ec_node_sh_lex_testcase(void)
363 {
364         struct ec_node *node;
365         int ret = 0;
366
367         node = ec_node_sh_lex(NULL,
368                 EC_NODE_SEQ(NULL,
369                         ec_node_str(NULL, "foo"),
370                         ec_node_option(NULL,
371                                 ec_node_str(NULL, "toto")
372                         ),
373                         ec_node_str(NULL, "bar")
374                 )
375         );
376         if (node == NULL) {
377                 ec_log(EC_LOG_ERR, "cannot create node\n");
378                 return -1;
379         }
380         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo bar");
381         ret |= EC_TEST_CHECK_PARSE(node, 1, "  foo   bar");
382         ret |= EC_TEST_CHECK_PARSE(node, 1, "  'foo' \"bar\"");
383         ret |= EC_TEST_CHECK_PARSE(node, 1, "  'f'oo 'toto' bar");
384         ec_node_free(node);
385
386         /* test completion */
387         node = ec_node_sh_lex(NULL,
388                 EC_NODE_SEQ(NULL,
389                         ec_node_str(NULL, "foo"),
390                         ec_node_option(NULL,
391                                 ec_node_str(NULL, "toto")
392                         ),
393                         ec_node_str(NULL, "bar"),
394                         ec_node_str(NULL, "titi")
395                 )
396         );
397         if (node == NULL) {
398                 ec_log(EC_LOG_ERR, "cannot create node\n");
399                 return -1;
400         }
401         ret |= EC_TEST_CHECK_COMPLETE(node,
402                 "", EC_NODE_ENDLIST,
403                 "foo", EC_NODE_ENDLIST,
404                 "foo");
405         ret |= EC_TEST_CHECK_COMPLETE(node,
406                 " ", EC_NODE_ENDLIST,
407                 "foo", EC_NODE_ENDLIST,
408                 "foo");
409         ret |= EC_TEST_CHECK_COMPLETE(node,
410                 "f", EC_NODE_ENDLIST,
411                 "oo", EC_NODE_ENDLIST,
412                 "oo");
413         ret |= EC_TEST_CHECK_COMPLETE(node,
414                 "foo", EC_NODE_ENDLIST,
415                 "", EC_NODE_ENDLIST,
416                 "");
417         ret |= EC_TEST_CHECK_COMPLETE(node,
418                 "foo ", EC_NODE_ENDLIST,
419                 "bar", "toto", EC_NODE_ENDLIST,
420                 "");
421         ret |= EC_TEST_CHECK_COMPLETE(node,
422                 "foo t", EC_NODE_ENDLIST,
423                 "oto", EC_NODE_ENDLIST,
424                 "oto");
425         ret |= EC_TEST_CHECK_COMPLETE(node,
426                 "foo b", EC_NODE_ENDLIST,
427                 "ar", EC_NODE_ENDLIST,
428                 "ar");
429         ret |= EC_TEST_CHECK_COMPLETE(node,
430                 "foo bar", EC_NODE_ENDLIST,
431                 "", EC_NODE_ENDLIST,
432                 "");
433         ret |= EC_TEST_CHECK_COMPLETE(node,
434                 "foo bar ", EC_NODE_ENDLIST,
435                 "titi", EC_NODE_ENDLIST,
436                 "titi");
437         ret |= EC_TEST_CHECK_COMPLETE(node,
438                 "foo toto bar ", EC_NODE_ENDLIST,
439                 "titi", EC_NODE_ENDLIST,
440                 "titi");
441         ret |= EC_TEST_CHECK_COMPLETE(node,
442                 "x", EC_NODE_ENDLIST,
443                 EC_NODE_ENDLIST,
444                 "");
445         ret |= EC_TEST_CHECK_COMPLETE(node,
446                 "foo barx", EC_NODE_ENDLIST,
447                 EC_NODE_ENDLIST,
448                 "");
449
450         ec_node_free(node);
451         return ret;
452 }
453
454 static struct ec_test ec_node_sh_lex_test = {
455         .name = "node_sh_lex",
456         .test = ec_node_sh_lex_testcase,
457 };
458
459 EC_TEST_REGISTER(ec_node_sh_lex_test);