gt: Add config load/save commands parsing
authorPawel Szewczyk <p.szewczyk@samsung.com>
Tue, 19 Aug 2014 09:35:29 +0000 (11:35 +0200)
committerPawel Szewczyk <p.szewczyk@samsung.com>
Fri, 29 Aug 2014 12:58:07 +0000 (14:58 +0200)
Change-Id: Ia50c6bb3b6cc4cdeb665def03ac5ed5a82cbca82
Signed-off-by: Pawel Szewczyk <p.szewczyk@samsung.com>
source/config/src/configuration.c

index 92344df..59c9d4b 100644 (file)
@@ -746,6 +746,251 @@ static const Command *gt_config_template_get_children(const Command *cmd)
        return commands;
 }
 
+struct gt_config_load_data {
+       const char *name;
+       const char *gadget;
+       const char *config;
+       const char *file;
+       const char *path;
+       int opts;
+};
+
+static int gt_config_load_func(void *data)
+{
+       struct gt_config_load_data *dt;
+
+       dt = (struct gt_config_load_data *)data;
+       printf("Config load called successfully. Not implemented.\n");
+       if (dt->name)
+               printf("name = %s, ", dt->name);
+       if (dt->gadget)
+               printf("gadget = %s, ", dt->gadget);
+       if (dt->config)
+               printf("config = %s, ", dt->config);
+       if (dt->file)
+               printf("file = %s, ", dt->file);
+       if (dt->path)
+               printf("path = %s, ", dt->path);
+       printf("recursive = %d, force = %d, stdin = %d\n",
+               !!(dt->opts & GT_RECURSIVE), !!(dt->opts & GT_FORCE),
+               !!(dt->opts & GT_STDIN));
+
+       return 0;
+}
+
+static int gt_config_load_help(void *data)
+{
+       printf("Config load help.\n");
+       return -1;
+}
+
+static void gt_parse_config_load(const Command *cmd, int argc,
+               char **argv, ExecutableCommand *exec, void * data)
+{
+       int c;
+       struct gt_config_load_data *dt = NULL;
+       struct option opts[] = {
+                       {"force", no_argument, 0, 'f'},
+                       {"recursive", no_argument, 0, 'r'},
+                       {"file", required_argument, 0, 1},
+                       {"stdin", no_argument, 0, 2},
+                       {"path", required_argument, 0, 3},
+                       {0, 0, 0, 0}
+               };
+
+       dt = zalloc(sizeof(*dt));
+       if (dt == NULL)
+               goto out;
+
+       argv--;
+       argc++;
+       while (1) {
+               int opt_index = 0;
+               c = getopt_long(argc, argv, "fr", opts, &opt_index);
+               if (c == -1)
+                       break;
+               switch (c) {
+               case 'f':
+                       dt->opts |= GT_FORCE;
+                       break;
+               case 'r':
+                       dt->opts |= GT_RECURSIVE;
+                       break;
+               case 1:
+                       if (dt->path || dt->opts & GT_STDIN)
+                               goto out;
+                       dt->file = optarg;
+                       break;
+               case 2:
+                       if (dt->path || dt->file)
+                               goto out;
+                       dt->opts |= GT_STDIN;
+                       break;
+               case 3:
+                       if (dt->file || dt->opts & GT_STDIN)
+                               goto out;
+                       dt->path = optarg;
+                       break;
+               default:
+                       goto out;
+               }
+       }
+
+       if (argc - optind > 3 || argc - optind < 2)
+               goto out;
+
+       dt->name = argv[optind++];
+       dt->gadget = argv[optind++];
+       if (dt->opts & GT_STDIN || dt->file) {
+               dt->config = dt->name;
+               dt->name = NULL;
+               if (optind < argc)
+                       goto out;
+       }
+
+       if (optind < argc)
+               dt->config = argv[optind++];
+
+       executable_command_set(exec, gt_config_load_func, (void *)dt, free);
+
+       return;
+out:
+       free(dt);
+       executable_command_set(exec, cmd->printHelp, data, NULL);
+}
+
+struct gt_config_save_data {
+       const char *gadget;
+       const char *config;
+       const char *name;
+       const char *file;
+       const char *path;
+       int opts;
+       struct gt_setting *attrs;
+};
+
+static void gt_config_save_destructor(void *data)
+{
+       struct gt_config_save_data *dt;
+
+       if (data == NULL)
+               return;
+       dt = (struct gt_config_save_data *)data;
+       gt_setting_list_cleanup(dt->attrs);
+       free(dt);
+}
+
+static int gt_config_save_func(void *data)
+{
+       struct gt_config_save_data *dt;
+       struct gt_setting *ptr;
+
+       dt = (struct gt_config_save_data *)data;
+       printf("Config save called successfully. Not implemented.\n");
+       if (dt->gadget)
+               printf("gadget=%s, ", dt->gadget);
+       if (dt->config)
+               printf("config=%s, ", dt->config);
+       if (dt->name)
+               printf("name=%s, ", dt->name);
+       if (dt->file)
+               printf("file=%s, ", dt->file);
+       if (dt->path)
+               printf("path=%s, ", dt->path);
+       printf("force=%d, stdout=%d",
+               !!(dt->opts & GT_FORCE), !!(dt->opts & GT_STDOUT));
+
+       ptr = dt->attrs;
+       while (ptr->variable) {
+               printf(", %s = %s", ptr->variable, ptr->value);
+               ptr++;
+       }
+
+       putchar('\n');
+
+
+       return 0;
+}
+
+static int gt_config_save_help(void *data)
+{
+       printf("Config save help.\n");
+       return -1;
+}
+
+static void gt_parse_config_save(const Command *cmd, int argc,
+               char **argv, ExecutableCommand *exec, void * data)
+{
+       int c;
+       struct gt_config_save_data *dt = NULL;
+       struct option opts[] = {
+                       {"force", no_argument, 0, 'f'},
+                       {"file", required_argument, 0, 1},
+                       {"stdout", no_argument, 0, 2},
+                       {"path", required_argument, 0, 3},
+                       {0, 0, 0, 0}
+               };
+
+       dt = zalloc(sizeof(*dt));
+       if (dt == NULL)
+               goto out;
+
+       argv--;
+       argc++;
+       while (1) {
+               int opt_index = 0;
+               c = getopt_long(argc, argv, "f", opts, &opt_index);
+               if (c == -1)
+                       break;
+               switch (c) {
+               case 'f':
+                       dt->opts |= GT_FORCE;
+                       break;
+               case 1:
+                       if (dt->path || dt->opts & GT_STDOUT)
+                               goto out;
+                       dt->file = optarg;
+                       break;
+               case 2:
+                       if (dt->path || dt->file)
+                               goto out;
+                       dt->opts |= GT_STDOUT;
+                       break;
+               case 3:
+                       if (dt->file || dt->opts & GT_STDOUT)
+                               goto out;
+                       dt->path = optarg;
+                       break;
+               default:
+                       goto out;
+               }
+       }
+
+       if (argc - optind < 2)
+               goto out;
+
+       dt->gadget = argv[optind++];
+       dt->config = argv[optind++];
+       if (optind < argc
+          && !dt->file
+          && !(dt->opts & GT_STDOUT)
+          && strchr(argv[optind], '=') == NULL)
+               dt->name = argv[optind++];
+
+       c = gt_parse_setting_list(&dt->attrs, argc - optind,
+                       argv + optind);
+       if (c < 0)
+               goto out;
+
+       executable_command_set(exec, gt_config_save_func, (void *)dt,
+                       gt_config_save_destructor);
+
+       return;
+out:
+       gt_config_save_destructor((void *)dt);
+       executable_command_set(exec, cmd->printHelp, data, NULL);
+}
+
 const Command *gt_config_get_children(const Command *cmd)
 {
        static Command commands[] = {
@@ -759,6 +1004,10 @@ const Command *gt_config_get_children(const Command *cmd)
                {"template", NEXT, command_parse,
                        gt_config_template_get_children,
                        gt_config_template_help},
+               {"load", NEXT, gt_parse_config_load, NULL,
+                       gt_config_load_help},
+               {"save", NEXT, gt_parse_config_save, NULL,
+                       gt_config_save_help},
                {NULL, AGAIN, gt_parse_config_config, NULL,
                        gt_config_config_help},
                CMD_LIST_END