37static char *input_file;
38static char *output_file;
41static const char short_options[] =
"h"
47#define OPT_HELP "help"
48#define OPT_INPUT_FILE "input-file"
49#define OPT_OUTPUT_FILE "output-file"
50#define OPT_COMPLETE "complete"
52static const struct option long_options[] = {
53 {OPT_HELP, 0, NULL,
'h'},
54 {OPT_INPUT_FILE, 1, NULL,
'i'},
55 {OPT_OUTPUT_FILE, 1, NULL,
'o'},
56 {OPT_COMPLETE, 0, NULL,
'c'},
60static void usage(
const char *prgname)
63 "%s -o <file.sh> -i <file.yaml>\n"
68 " --" OPT_INPUT_FILE
"=<file>\n"
69 " Set the yaml input file describing the grammar.\n"
71 " --" OPT_OUTPUT_FILE
"=<file>\n"
72 " Set the output file.\n"
74 " --" OPT_COMPLETE
"\n"
75 " Output the completion list.\n",
79static int parse_args(
int argc,
char **argv)
83 while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) != EOF) {
90 input_file = strdup(optarg);
94 output_file = strdup(optarg);
107 if (input_file == NULL) {
108 fprintf(stderr,
"No input file\n");
112 if (output_file == NULL) {
113 fprintf(stderr,
"No output file\n");
124static int __dump_as_shell(FILE *f,
const struct ec_pnode *parse,
size_t *seq)
128 size_t cur_seq, i, len;
135 fprintf(f,
"ec_node%zu_id=%s\n", cur_seq, quoted);
139 fprintf(f,
"ec_node%zu_type=%s\n", cur_seq, quoted);
143 fprintf(f,
"ec_node%zu_strvec_len=%zu\n", cur_seq, len);
144 for (i = 0; i < len; i++) {
146 fprintf(f,
"ec_node%zu_str%zu=%s\n", cur_seq, i, quoted);
151 fprintf(f,
"ec_node%zu_first_child='ec_node%zu'\n", cur_seq, cur_seq + 1);
155 fprintf(f,
"ec_node%zu_parent='ec_node%zu'\n", *seq + 1, cur_seq);
156 __dump_as_shell(f, child, seq);
160 fprintf(f,
"ec_node%zu_next='ec_node%zu'\n", cur_seq, *seq + 1);
166static int dump_as_shell(
const struct ec_pnode *parse)
172 f = fopen(output_file,
"w");
176 ret = __dump_as_shell(f, parse, &seq);
183static int interact(
struct ec_node *node)
191 fprintf(stderr,
"Failed to add lexer node\n");
195 editline =
ec_editline(
"parse-yaml", stdin, stdout, stderr, 0);
196 if (editline == NULL) {
197 fprintf(stderr,
"Failed to initialize editline\n");
202 fprintf(stderr,
"Failed to set editline ec_node\n");
213 if (dump_as_shell(parse) < 0) {
214 fprintf(stderr,
"Failed to dump the parsed result\n");
230static int complete_words(
const struct ec_node *node,
int argc,
char *argv[])
234 struct ec_comp_item *item = NULL;
270int main(
int argc,
char *argv[])
275 ret = parse_args(argc, argv);
283 fprintf(stderr,
"cannot init ecoli: %s\n", strerror(errno));
289 fprintf(stderr,
"Failed to parse file\n");
294 if (complete_words(node, argc, argv) < 0)
297 if (interact(node) < 0)
struct ec_comp * ec_comp(void)
const char * ec_comp_item_get_display(const struct ec_comp_item *item)
struct ec_comp * ec_complete_strvec(const struct ec_node *node, const struct ec_strvec *strvec)
size_t ec_comp_count(const struct ec_comp *comp, enum ec_comp_type type)
void ec_comp_free(struct ec_comp *comp)
const char * ec_comp_item_get_str(const struct ec_comp_item *item)
#define EC_COMP_FOREACH(item, comp, type)
struct ec_editline * ec_editline(const char *prog, FILE *f_in, FILE *f_out, FILE *f_err, enum ec_editline_init_flags flags)
void ec_editline_free(struct ec_editline *editline)
struct ec_pnode * ec_editline_parse(struct ec_editline *editline)
int ec_editline_set_node(struct ec_editline *editline, const struct ec_node *node)
struct ec_node * ec_node_sh_lex(const char *id, struct ec_node *child)
struct ec_node * ec_node_clone(struct ec_node *node)
const char * ec_node_id(const struct ec_node *node)
struct ec_node * ec_node(const char *typename, const char *id)
const struct ec_node_type * ec_node_type(const struct ec_node *node)
const char * ec_node_type_name(const struct ec_node_type *type)
void ec_node_free(struct ec_node *node)
bool ec_pnode_matches(const struct ec_pnode *pnode)
struct ec_pnode * ec_pnode_next(const struct ec_pnode *pnode)
void ec_pnode_free(struct ec_pnode *pnode)
const struct ec_strvec * ec_pnode_get_strvec(const struct ec_pnode *pnode)
#define EC_PNODE_FOREACH_CHILD(child, pnode)
struct ec_pnode * ec_pnode_get_first_child(const struct ec_pnode *pnode)
const struct ec_node * ec_pnode_get_node(const struct ec_pnode *pnode)
struct ec_pnode * ec_pnode(const struct ec_node *node)
char * ec_str_quote(const char *str, char quote, bool force)
struct ec_strvec * ec_strvec_from_array(const char *const *strarr, size_t n)
void ec_strvec_free(struct ec_strvec *strvec)
const char * ec_strvec_val(const struct ec_strvec *strvec, size_t idx)
struct ec_strvec * ec_strvec(void)
size_t ec_strvec_len(const struct ec_strvec *strvec)
struct ec_node * ec_yaml_import(const char *filename)