gt: Add config get/set commands parse
[platform/upstream/gt.git] / source / config / src / configuration.c
1 /*
2  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <getopt.h>
22
23 #include "configuration.h"
24 #include "common.h"
25 #include "parser.h"
26
27 int gt_config_help(void *data)
28 {
29         printf("Configuration help function\n");
30         return -1;
31 }
32
33 struct gt_config_create_data {
34         const char *gadget;
35         const char *config;
36         struct gt_setting *attrs;
37         int opts;
38 };
39
40 static void gt_config_create_destructor(void *data)
41 {
42         struct gt_config_create_data *dt;
43
44         if (data == NULL)
45                 return;
46         dt = (struct gt_config_create_data *)data;
47         gt_setting_list_cleanup(dt->attrs);
48         free(dt);
49 }
50
51 static int  gt_config_create_func(void *data)
52 {
53         struct gt_config_create_data *dt;
54         struct gt_setting *ptr;
55
56         dt = (struct gt_config_create_data *)data;
57         printf("Config create called successfully. Not implemented.\n");
58         printf("gadget = %s, config = %s, force = %d",
59                 dt->gadget, dt->config, !!(dt->opts & GT_FORCE));
60
61         ptr = dt->attrs;
62         while (ptr->variable) {
63                 printf(", %s = %s", ptr->variable, ptr->value);
64                 ptr++;
65         }
66
67         putchar('\n');
68         return 0;
69 }
70
71 static int gt_config_create_help(void *data)
72 {
73         printf("Config create help.\n");
74         return -1;
75 }
76
77 static void gt_parse_config_create(const Command *cmd, int argc, char **argv,
78                 ExecutableCommand *exec, void *data)
79 {
80         int c;
81         int ind;
82         struct gt_config_create_data *dt;
83         int avaible_opts = GT_FORCE;
84
85         dt = zalloc(sizeof(*dt));
86         if (dt == NULL)
87                 goto out;
88         ind = gt_get_options(&dt->opts, avaible_opts, argc, argv);
89         if (ind < 0)
90                 goto out;
91
92         if (argc - ind < 2)
93                 goto out;
94
95         dt->gadget = argv[ind++];
96         dt->config = argv[ind++];
97         c = gt_parse_setting_list(&dt->attrs, argc - ind, argv + ind);
98         if (c < 0)
99                 goto out;
100
101         executable_command_set(exec, gt_config_create_func,
102                                 (void *)dt, gt_config_create_destructor);
103         return;
104
105 out:
106         gt_config_create_destructor((void *)dt);
107         executable_command_set(exec, cmd->printHelp, data, NULL);
108 }
109
110 struct gt_config_rm_data {
111         const char *gadget;
112         const char *config;
113         int opts;
114 };
115
116 static int gt_config_rm_func(void *data)
117 {
118         struct gt_config_rm_data *dt;
119
120         dt = (struct gt_config_rm_data *)data;
121         printf("Config rm called successfully. Not implemented.\n");
122         printf("gadget = %s, config = %s, force = %d, recursive = %d\n",
123                 dt->gadget, dt->config, !!(dt->opts & GT_FORCE),
124                 !!(dt->opts & GT_RECURSIVE));
125
126         return 0;
127 }
128
129 static int gt_config_rm_help(void *data)
130 {
131         printf("Config rm help.\n");
132         return -1;
133 }
134
135 static void gt_parse_config_rm(const Command *cmd, int argc, char **argv,
136                 ExecutableCommand *exec, void *data)
137 {
138         int ind;
139         struct gt_config_rm_data *dt;
140         int avaible_opts = GT_FORCE | GT_RECURSIVE;
141
142         dt = zalloc(sizeof(*dt));
143         if (dt == NULL)
144                 goto out;
145         ind = gt_get_options(&dt->opts, avaible_opts, argc, argv);
146         if (ind < 0)
147                 goto out;
148
149         if (argc - ind != 2)
150                 goto out;
151
152         dt->gadget = argv[ind++];
153         dt->config = argv[ind++];
154         executable_command_set(exec, gt_config_rm_func, (void *)dt, free);
155
156         return;
157 out:
158         free(dt);
159         executable_command_set(exec, cmd->printHelp, data, NULL);
160 }
161
162 struct gt_config_get_data {
163         const char *gadget;
164         const char *config;
165         const char **attrs;
166 };
167
168 static void gt_config_get_destructor(void *data)
169 {
170         struct gt_config_get_data *dt;
171
172         if (data == NULL)
173                 return;
174         dt = (struct gt_config_get_data *)data;
175         free(dt->attrs);
176         free(dt);
177 }
178
179 static int gt_config_get_func(void *data)
180 {
181         struct gt_config_get_data *dt;
182         const char **ptr;
183
184         dt = (struct gt_config_get_data *)data;
185         printf("Config get called successfully. Not implemented.\n");
186         printf("gadget = %s, config = %s, attrs = ",
187                 dt->gadget, dt->config);
188         ptr = dt->attrs;
189         while (*ptr) {
190                 printf("%s, ", *ptr);
191                 ptr++;
192         }
193         putchar('\n');
194         return 0;
195 }
196
197 static int gt_config_get_help(void *data)
198 {
199         printf("Config get help.\n");
200         return -1;
201 }
202
203 static void gt_parse_config_get(const Command *cmd, int argc, char **argv,
204                 ExecutableCommand *exec, void *data)
205 {
206         struct gt_config_get_data *dt = NULL;
207         int i;
208
209         if (argc < 2)
210                 goto out;
211
212         dt = zalloc(sizeof(*dt));
213         if (dt == NULL)
214                 goto out;
215
216         dt->gadget = argv[0];
217         dt->config = argv[1];
218
219         dt->attrs = calloc(argc - 1, sizeof(char *));
220         if (dt->attrs == NULL)
221                 goto out;
222
223         argv += 2;
224         for (i = 0; argv[i]; i++)
225                 dt->attrs[i] = argv[i];
226
227         executable_command_set(exec, gt_config_get_func, (void *)dt,
228                         gt_config_get_destructor);
229         return;
230 out:
231         gt_config_get_destructor((void *)dt);
232         executable_command_set(exec, cmd->printHelp, data, NULL);
233 }
234
235 struct gt_config_set_data {
236         const char *gadget;
237         const char *config;
238         struct gt_setting *attrs;
239 };
240
241 static void gt_config_set_destructor(void *data)
242 {
243         struct gt_config_set_data *dt;
244
245         if (data == NULL)
246                 return;
247         dt = (struct gt_config_set_data *)data;
248         gt_setting_list_cleanup(dt->attrs);
249         free(dt);
250 }
251
252 static int gt_config_set_func(void *data)
253 {
254         struct gt_config_set_data *dt;
255         struct gt_setting *ptr;
256
257         dt = (struct gt_config_set_data *)data;
258         printf("Config set called successfully. Not implemented.\n");
259         printf("gadget = %s, config = %s", dt->gadget, dt->config);
260         ptr = dt->attrs;
261         while (ptr->variable) {
262                 printf(", %s = %s", ptr->variable, ptr->value);
263                 ptr++;
264         }
265
266         putchar('\n');
267         return 0;
268 }
269
270 static int gt_config_set_help(void *data)
271 {
272         printf("Config set help.\n");
273         return -1;
274 }
275
276 static void gt_parse_config_set(const Command *cmd, int argc, char **argv,
277                 ExecutableCommand *exec, void *data)
278 {
279         struct gt_config_set_data *dt = NULL;
280         int tmp;
281
282         if (argc < 3)
283                 goto out;
284         dt = zalloc(sizeof(*dt));
285         if (dt == NULL)
286                 goto out;
287
288         dt->gadget = argv[0];
289         dt->config = argv[1];
290         tmp = gt_parse_setting_list(&dt->attrs, argc - 2, argv + 2);
291         if (tmp < 0)
292                 goto out;
293
294         executable_command_set(exec, gt_config_set_func, (void *)dt,
295                         gt_config_set_destructor);
296         return;
297 out:
298         gt_config_set_destructor((void *)dt);
299         executable_command_set(exec, cmd->printHelp, data, NULL);
300 }
301 const Command *gt_config_get_children(const Command *cmd)
302 {
303         static Command commands[] = {
304                 {"create", NEXT, gt_parse_config_create, NULL,
305                  gt_config_create_help},
306                 {"rm", NEXT, gt_parse_config_rm, NULL, gt_config_rm_help},
307                 {"get", NEXT, gt_parse_config_get, NULL, gt_config_get_help},
308                 {"set", NEXT, gt_parse_config_set, NULL, gt_config_set_help},
309 //              {"add", AGAIN, gt_parse_config_add, NULL, gt_config_add_help},
310 //              {"del", AGAIN, gt_parse_config_del, NULL, gt_config_del_help},
311 //              {"template", NEXT, command_parse,
312 //                      gt_config_template_get_children,
313 //                      gt_config_template_help},
314 //              {NULL, PREV, gt_parse_config_config, NULL,
315 //                      gt_config_config_help},
316                 CMD_LIST_END
317         };
318
319         return commands;
320 }