gt: Add config template parse
authorPawel Szewczyk <p.szewczyk@samsung.com>
Thu, 21 Aug 2014 11:38:18 +0000 (13:38 +0200)
committerPawel Szewczyk <p.szewczyk@samsung.com>
Fri, 29 Aug 2014 10:41:38 +0000 (12:41 +0200)
Change-Id: I10493ff59cb52b061abe12ce9135602dc01731cf
Signed-off-by: Pawel Szewczyk <p.szewczyk@samsung.com>
source/config/src/configuration.c

index 6cb7d85..92344df 100644 (file)
@@ -506,6 +506,246 @@ out:
        executable_command_set(exec, cmd->printHelp, data, NULL);
 }
 
+struct gt_config_template_data {
+       const char *name;
+       int opts;
+};
+
+static int gt_config_template_func(void *data)
+{
+       struct gt_config_template_data *dt;
+
+       dt = (struct gt_config_template_data *)data;
+       printf("Config template called successfully. Not implemented.\n");
+       if (dt->name)
+               printf("name = %s, ", dt->name);
+       printf("verbose = %d, recursive = %d\n",
+               !!(dt->opts & GT_VERBOSE), !!(dt->opts & GT_RECURSIVE));
+       return 0;
+}
+
+static int gt_config_template_help(void *data)
+{
+       printf("Config template help.\n");
+       return -1;
+}
+
+static void gt_parse_config_template(const Command *cmd, int argc, char **argv,
+               ExecutableCommand *exec, void *data)
+{
+       int ind;
+       struct gt_config_template_data *dt = NULL;
+       int avaible_opts = GT_VERBOSE | GT_RECURSIVE;
+
+       dt = zalloc(sizeof(*dt));
+       if (dt == NULL)
+               goto out;
+
+       ind = gt_get_options(&dt->opts, avaible_opts, argc, argv);
+       if (ind < 0)
+               goto out;
+
+       if (argc - ind > 1)
+               goto out;
+
+       dt->name = argv[ind];
+
+       executable_command_set(exec, gt_config_template_func, (void *)dt, free);
+
+       return;
+out:
+       free(dt);
+       executable_command_set(exec, cmd->printHelp, data, NULL);
+}
+
+struct gt_config_template_get_data {
+       const char *name;
+       const char **attr;
+};
+
+static void gt_config_template_get_destructor(void *data)
+{
+       struct gt_config_template_get_data *dt;
+
+       if (data == NULL)
+               return;
+       dt = (struct gt_config_template_get_data *)data;
+       free(dt->attr);
+       free(dt);
+}
+
+static int gt_config_template_get_func(void *data)
+{
+       struct gt_config_template_get_data *dt;
+       const char **ptr;
+
+       dt = (struct gt_config_template_get_data *)data;
+       printf("Config template get called successfully. Not implemented.\n");
+       printf("name = %s, attr = ", dt->name);
+       ptr = dt->attr;
+       while (*ptr) {
+               printf("%s, ", *ptr);
+               ptr++;
+       }
+
+       putchar('\n');
+       return 0;
+}
+
+static int gt_config_template_get_help(void *data)
+{
+       printf("Config template get help.\n");
+       return -1;
+}
+
+static void gt_parse_config_template_get(const Command *cmd, int argc,
+               char **argv, ExecutableCommand *exec, void * data)
+{
+       int i;
+       struct gt_config_template_get_data *dt = NULL;
+
+       if (argc < 1)
+               goto out;
+
+       dt = zalloc(sizeof(*dt));
+       if (dt == NULL)
+               goto out;
+
+       dt->name = argv[0];
+       dt->attr = calloc(argc, sizeof(char *));
+       if(dt->attr == NULL)
+               goto out;
+
+       argv++;
+       for (i = 0; argv[i]; i++)
+               dt->attr[i] = argv[i];
+
+       executable_command_set(exec, gt_config_template_get_func, (void *)dt,
+               gt_config_template_get_destructor);
+
+       return;
+out:
+       gt_config_template_get_destructor((void *)dt);
+       executable_command_set(exec, cmd->printHelp, data, NULL);
+}
+
+struct gt_config_template_set_data {
+       const char *name;
+       struct gt_setting *attr;
+};
+
+static void gt_config_template_set_destructor(void *data)
+{
+       struct gt_config_template_set_data *dt;
+
+       if (data == NULL)
+               return;
+       dt = (struct gt_config_template_set_data *)data;
+       gt_setting_list_cleanup(dt->attr);
+       free(dt);
+}
+
+static int gt_config_template_set_func(void *data)
+{
+       struct gt_config_template_set_data *dt;
+       struct gt_setting *ptr;
+
+       dt = (struct gt_config_template_set_data *)data;
+       printf("Config template set called successfully. Not implemened.\n");
+       printf("name = %s", dt->name);
+       ptr = dt->attr;
+       while (ptr->variable) {
+               printf(", %s = %s", ptr->variable, ptr->value);
+               ptr++;
+       }
+
+       putchar('\n');
+       return 0;
+}
+
+static int gt_config_template_set_help(void *data)
+{
+       printf("Config template set help.\n");
+       return -1;
+}
+
+static void gt_parse_config_template_set(const Command *cmd, int argc,
+               char **argv, ExecutableCommand *exec, void * data)
+{
+       int tmp;
+       struct gt_config_template_set_data *dt = NULL;
+
+       if (argc < 2)
+               goto out;
+
+       dt = zalloc(sizeof(*dt));
+       if (dt == NULL)
+               goto out;
+
+       dt->name = argv[0];
+       tmp = gt_parse_setting_list(&dt->attr, argc - 1, argv + 1);
+       if (tmp < 0)
+               goto out;
+
+       executable_command_set(exec, gt_config_template_set_func, (void *)dt,
+               gt_config_template_set_destructor);
+
+       return;
+out:
+       gt_config_template_set_destructor((void *)dt);
+       executable_command_set(exec, cmd->printHelp, data, NULL);
+}
+
+static int gt_config_template_rm_func(void *data)
+{
+       const char *dt;
+
+       dt = (const char *)data;
+       printf("Config template rm called successfully. Not implemented.\n");
+       printf("name = %s\n", dt);
+       return 0;
+}
+
+static int gt_config_template_rm_help(void *data)
+{
+       printf("Config template rm help.\n");
+       return -1;
+}
+
+static void gt_parse_config_template_rm(const Command *cmd, int argc,
+               char **argv, ExecutableCommand *exec, void * data)
+{
+       const char *dt = NULL;
+
+       if (argc != 1)
+               goto out;
+
+       dt = argv[0];
+       executable_command_set(exec, gt_config_template_rm_func, (void *)dt,
+                       NULL);
+
+       return;
+out:
+       executable_command_set(exec, cmd->printHelp, data, NULL);
+}
+
+static const Command *gt_config_template_get_children(const Command *cmd)
+{
+       static Command commands[] = {
+               {"rm", NEXT, gt_parse_config_template_rm, NULL,
+                       gt_config_template_rm_help},
+               {"set", NEXT, gt_parse_config_template_set, NULL,
+                       gt_config_template_set_help},
+               {"get", NEXT, gt_parse_config_template_get, NULL,
+                       gt_config_template_get_help},
+               {NULL, AGAIN, gt_parse_config_template, NULL,
+                       gt_config_template_help},
+               CMD_LIST_END
+       };
+
+       return commands;
+}
+
 const Command *gt_config_get_children(const Command *cmd)
 {
        static Command commands[] = {
@@ -516,9 +756,9 @@ const Command *gt_config_get_children(const Command *cmd)
                {"set", NEXT, gt_parse_config_set, NULL, gt_config_set_help},
                {"add", NEXT, gt_parse_config_add, NULL, gt_config_add_help},
                {"del", NEXT, gt_parse_config_del, NULL, gt_config_del_help},
-//             {"template", NEXT, command_parse,
-//                     gt_config_template_get_children,
-//                     gt_config_template_help},
+               {"template", NEXT, command_parse,
+                       gt_config_template_get_children,
+                       gt_config_template_help},
                {NULL, AGAIN, gt_parse_config_config, NULL,
                        gt_config_config_help},
                CMD_LIST_END