cmdline: big rework and clean of cmdline library
[libcmdline.git] / src / genconf / commands.c
index 260502c..957bbef 100644 (file)
@@ -31,6 +31,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <unistd.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
@@ -407,22 +408,24 @@ struct cmd_set_str_result {
 static int get_str(struct cmdline *cl, char *buf, int len)
 {
        struct rdline rl;
-       int ret = 0, n;
-       char c;
+       int ret = 0;
+       int c;
        char *s;
 
        buf[0] = '\0';
 
-       rdline_init(&rl, cmdline_write_char, NULL, NULL);
+       rdline_init(&rl, cl->s_in, cl->s_out, NULL, NULL, NULL);
        rl.opaque = cl;
+
+       /* XXX use rdline() */
        rdline_newline(&rl, "edit> ");
 
-       while (ret == 0 || ret == 2) {
-               n = read(cl->s_in, &c, 1);
-               if (n <= 0)
+       while (ret == RDLINE_RES_SUCCESS) {
+               read(cl->s_in, &c, 1);
+               if (c <= 0)
                        return -1;
                ret = rdline_char_in(&rl, c);
-               if (ret == 1) {
+               if (ret == RDLINE_RES_VALIDATED) {
                        snprintf(buf, len, "%s", rdline_get_buffer(&rl));
                        s = strchr(buf, '\n');
                        if (s)
@@ -665,22 +668,25 @@ cmdline_parse_inst_t cmd_savefile = {
 /****** CONTEXT (list of instruction) */
 
 /* in progmem */
-cmdline_parse_ctx_t main_ctx[] = {
-       (cmdline_parse_inst_t *)&cmd_ls,
-       (cmdline_parse_inst_t *)&cmd_ls_node,
-       (cmdline_parse_inst_t *)&cmd_pwd,
-       (cmdline_parse_inst_t *)&cmd_cd_prev,
-       (cmdline_parse_inst_t *)&cmd_cd_root,
-       (cmdline_parse_inst_t *)&cmd_cd_node,
-       (cmdline_parse_inst_t *)&cmd_set_bool,
-       (cmdline_parse_inst_t *)&cmd_set_int,
-       (cmdline_parse_inst_t *)&cmd_set_str,
-       (cmdline_parse_inst_t *)&cmd_show_node,
-       (cmdline_parse_inst_t *)&cmd_show_node_a,
-       (cmdline_parse_inst_t *)&cmd_load,
-       (cmdline_parse_inst_t *)&cmd_loadfile,
-       (cmdline_parse_inst_t *)&cmd_save,
-       (cmdline_parse_inst_t *)&cmd_savefile,
-       NULL,
+cmdline_parse_ctx_t main_ctx = {
+       .name = "main",
+       .insts = {
+               &cmd_ls,
+               &cmd_ls_node,
+               &cmd_pwd,
+               &cmd_cd_prev,
+               &cmd_cd_root,
+               &cmd_cd_node,
+               &cmd_set_bool,
+               &cmd_set_int,
+               &cmd_set_str,
+               &cmd_show_node,
+               &cmd_show_node_a,
+               &cmd_load,
+               &cmd_loadfile,
+               &cmd_save,
+               &cmd_savefile,
+               NULL,
+       },
 };