genconf: use the new file token for load/save
[libcmdline.git] / src / genconf / commands.c
index 260502c..1db4595 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>
@@ -40,6 +41,7 @@
 #include <cmdline_parse_ipaddr.h>
 #include <cmdline_parse_num.h>
 #include <cmdline_parse_string.h>
+#include <cmdline_parse_file.h>
 #include <cmdline_rdline.h>
 #include <cmdline.h>
 
@@ -407,22 +409,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)
@@ -547,7 +551,7 @@ cmdline_parse_inst_t cmd_show_node_a = {
 /**********************************************************/
 
 struct cmd_load_result {
-       cmdline_fixed_string_t load;
+       cmdline_filename_t load;
        cmdline_fixed_string_t file;
 };
 
@@ -588,8 +592,8 @@ cmdline_parse_inst_t cmd_load = {
        },
 };
 
-cmdline_parse_token_string_t cmd_load_file =
-       TOKEN_STRING_INITIALIZER(struct cmd_load_result, file, NULL);
+cmdline_parse_token_file_t cmd_load_file =
+       TOKEN_FILE_INITIALIZER(struct cmd_load_result, file, 0);
 
 cmdline_parse_inst_t cmd_loadfile = {
        .f = cmd_load_parsed,  /* function to call */
@@ -608,7 +612,7 @@ cmdline_parse_inst_t cmd_loadfile = {
 
 struct cmd_save_result {
        cmdline_fixed_string_t save;
-       cmdline_fixed_string_t file;
+       cmdline_filename_t file;
 };
 
 static void cmd_save_parsed(void *parsed_result,
@@ -643,8 +647,9 @@ cmdline_parse_inst_t cmd_save = {
        },
 };
 
-cmdline_parse_token_string_t cmd_save_file =
-       TOKEN_STRING_INITIALIZER(struct cmd_save_result, file, NULL);
+cmdline_parse_token_file_t cmd_save_file =
+       TOKEN_FILE_INITIALIZER(struct cmd_save_result, file,
+                              PARSE_FILE_F_CREATE);
 
 cmdline_parse_inst_t cmd_savefile = {
        .f = cmd_save_parsed,  /* function to call */
@@ -665,22 +670,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,
+       },
 };