1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018, Olivier MATZ <zer0@droids-corp.org>
10 #include <ecoli_strvec.h>
11 #include <ecoli_node.h>
12 #include <ecoli_parse.h>
13 #include <ecoli_complete.h>
14 #include <ecoli_yaml.h>
15 #include <ecoli_editline.h>
16 #include <ecoli_node_sh_lex.h>
18 static char *input_file;
19 static char *output_file;
22 static const char short_options[] =
25 "o:" /* output-file */
29 #define OPT_HELP "help"
30 #define OPT_INPUT_FILE "input-file"
31 #define OPT_OUTPUT_FILE "output-file"
32 #define OPT_COMPLETE "complete"
34 static const struct option long_options[] = {
35 {OPT_HELP, 0, NULL, 'h'},
36 {OPT_INPUT_FILE, 1, NULL, 'i'},
37 {OPT_OUTPUT_FILE, 1, NULL, 'o'},
38 {OPT_COMPLETE, 0, NULL, 'c'},
42 static void usage(const char *prgname)
44 fprintf(stderr, "%s -o <file.sh> -i <file.yaml>\n"
49 " --"OPT_INPUT_FILE"=<file>\n"
50 " Set the yaml input file describing the grammar.\n"
52 " --"OPT_OUTPUT_FILE"=<file>\n"
53 " Set the output file.\n"
56 " Output the completion list."
60 static int parse_args(int argc, char **argv)
64 while ((opt = getopt_long(argc, argv, short_options,
65 long_options, NULL)) != EOF) {
72 case 'i': /* input-file */
73 input_file = strdup(optarg);
76 case 'o': /* output-file */
77 output_file = strdup(optarg);
80 case 'c': /* complete */
91 if (input_file == NULL) {
92 fprintf(stderr, "No input file\n");
96 if (output_file == NULL) {
97 fprintf(stderr, "No output file\n");
109 __dump_as_shell(FILE *f, const struct ec_parse *parse, size_t *seq)
111 const struct ec_node *node = ec_parse_get_node(parse);
112 struct ec_parse *child;
113 size_t cur_seq, i, len;
119 // XXX protect strings
122 fprintf(f, "ec_node%zu_id='%s'\n", cur_seq, ec_node_id(node));
123 fprintf(f, "ec_node%zu_type='%s'\n", cur_seq,
124 ec_node_type_name(ec_node_type(node)));
126 len = ec_strvec_len(ec_parse_strvec(parse));
127 fprintf(f, "ec_node%zu_strvec_len=%zu\n", cur_seq, len);
128 for (i = 0; i < len; i++) {
129 s = ec_strvec_val(ec_parse_strvec(parse), i);
130 fprintf(f, "ec_node%zu_str%zu='%s'\n", cur_seq, i, s);
133 if (ec_parse_get_first_child(parse) != NULL) {
134 fprintf(f, "ec_node%zu_first_child='ec_node%zu'\n",
135 cur_seq, cur_seq + 1);
138 EC_PARSE_FOREACH_CHILD(child, parse) {
139 fprintf(f, "ec_node%zu_parent='ec_node%zu'\n",
141 __dump_as_shell(f, child, seq);
144 if (ec_parse_get_next(parse) != NULL) {
145 fprintf(f, "ec_node%zu_next='ec_node%zu'\n",
153 dump_as_shell(const struct ec_parse *parse)
159 f = fopen(output_file, "w");
163 ret = __dump_as_shell(f, parse, &seq);
171 interact(struct ec_node *node)
173 struct ec_editline *editline = NULL;
174 struct ec_parse *parse = NULL;
175 struct ec_node *shlex = NULL;
178 shlex = ec_node_sh_lex(EC_NO_ID, ec_node_clone(node)); //XXX
180 fprintf(stderr, "Failed to add lexer node\n");
184 editline = ec_editline("ecoli", stdin, stdout, stderr, 0);
185 if (editline == NULL) {
186 fprintf(stderr, "Failed to initialize editline\n");
190 parse = ec_editline_parse(editline, shlex);
194 if (!ec_parse_matches(parse))
197 //ec_parse_dump(stdout, parse);
199 if (dump_as_shell(parse) < 0) {
200 fprintf(stderr, "Failed to dump the parsed result\n");
204 ec_parse_free(parse);
205 ec_editline_free(editline);
210 ec_parse_free(parse);
211 ec_editline_free(editline);
218 complete_words(const struct ec_node *node, int argc, char *argv[])
220 struct ec_comp *comp = NULL;
221 struct ec_strvec *strvec = NULL;
222 struct ec_comp_iter *iter = NULL;
223 struct ec_comp_item *item = NULL;
228 strvec = ec_strvec_from_array((const char * const *)&argv[1],
233 comp = ec_node_complete_strvec(node, strvec);
237 count = ec_comp_count(comp, EC_COMP_UNKNOWN | EC_COMP_FULL |
240 iter = ec_comp_iter(comp,
241 EC_COMP_UNKNOWN | EC_COMP_FULL | EC_COMP_PARTIAL);
245 while ((item = ec_comp_iter_next(iter)) != NULL) {
247 /* only one match, display it fully */
249 printf("%s\n", ec_comp_item_get_str(item));
253 /* else show the 'display' part only */
254 printf("%s\n", ec_comp_item_get_display(item));
257 ec_comp_iter_free(iter);
259 ec_strvec_free(strvec);
264 ec_strvec_free(strvec);
269 main(int argc, char *argv[])
271 struct ec_node *node = NULL;
274 ret = parse_args(argc, argv);
282 fprintf(stderr, "cannot init ecoli: %s\n", strerror(errno));
286 node = ec_yaml_import(input_file);
288 fprintf(stderr, "Failed to parse file\n");
291 //ec_node_dump(stdout, node);
294 if (complete_words(node, argc, argv) < 0)
297 if (interact(node) < 0)