svn update: 48958 (latest:48959)
[framework/uifw/ecore.git] / src / bin / ecore_config.c
1 #include "config.h"
2 #include "Ecore.h"
3
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdlib.h>
7
8 #ifdef BUILD_ECORE_CONFIG
9 #include <unistd.h>
10 #include <getopt.h>
11 #include <Eet.h>
12 #include "Ecore_Config.h"
13 #include "ecore_config_private.h"
14
15 // strcmp for paths - for sorting folders before files
16 static int
17 pathcmp(const char *s1, const char *s2)
18 {
19    char *s1d, *s2d;
20
21    // strip common part of paths
22    while(*s1 && *s2 && *s1 == *s2)
23      {
24         s1++;
25         s2++;
26      }
27
28    // handle /foo/bar/baz <> /foo/bar_baz properly
29    if (*s1 == '/' && *s2 != '/') return -1;
30    if (*s1 != '/' && *s2 == '/') return  1;
31
32    // skip leading /
33    if (*s1 == '/') s1++;
34    if (*s2 == '/') s2++;
35
36    // order folders before files
37    s1d = strchr(s1, '/');
38    s2d = strchr(s2, '/');
39    if (s1d != NULL && s2d == NULL) return -1;
40    if (s1d == NULL && s2d != NULL) return  1;
41
42    return strcmp(s1, s2);
43 }
44
45 static int
46 del(const char *key)
47 {
48    Ecore_Config_Prop *e;
49    e = ecore_config_get(key);
50    if(e == NULL) return -1;
51
52    ecore_config_dst(e);
53    return 0;
54 }
55
56 static int
57 get(const char *key)
58 {
59    Ecore_Config_Prop *e;
60    char *temp = NULL;
61
62    if (!(e = ecore_config_get(key)))
63      {
64         EINA_LOG_ERR("No such property");
65         return -1;
66      }
67      
68    printf("%-10s", ecore_config_type_get(e));
69
70    switch (e->type)
71      {
72         case ECORE_CONFIG_NIL:
73            printf("\n");
74            break;
75         case ECORE_CONFIG_INT:
76            printf("%ld\n", ecore_config_int_get(key));
77            break;
78         case ECORE_CONFIG_BLN:
79            printf("%d\n",  ecore_config_boolean_get(key));
80            break;
81         case ECORE_CONFIG_FLT:
82            printf("%lf\n", ecore_config_float_get(key));
83            break;
84         case ECORE_CONFIG_STR:
85            temp = ecore_config_string_get(key);
86            break;
87         case ECORE_CONFIG_RGB:
88            temp = ecore_config_argbstr_get(key);
89            break;
90         case ECORE_CONFIG_THM:
91            temp = ecore_config_theme_get(key);
92            break;
93         default:
94            EINA_LOG_ERR("Property has unrecognized type");
95            return -1;
96      }
97    if(temp)
98      {
99         printf("\"%s\"\n", temp);
100         free(temp);
101      }
102    return 0;
103 }
104
105 static int
106 list(const char *file)
107 {
108    Ecore_Config_Prop *e;
109    Eina_List *keys = NULL;
110    char *key;
111
112    e = __ecore_config_bundle_local->data;
113
114    do
115      {
116         // don't show system settings
117         if( !(e->flags & ECORE_CONFIG_FLAG_SYSTEM) )
118            keys = eina_list_append(keys, e->key);
119      }
120    while((e = e->next));
121    keys = eina_list_sort(keys, -1, EINA_COMPARE_CB(pathcmp));
122
123    EINA_LIST_FREE(keys, key)
124      {
125         printf("%-28s\t", key);
126         get(key);
127      }
128
129    return 0;
130 }
131
132 static void
133 usage_and_exit(const char *prog, int ret, const char *msg)
134 {
135    if (msg) fprintf(stderr, "%s\n\n", msg);
136    fprintf(stderr, "Usage: %s <options> <command>\n", prog);
137    fprintf(stderr, "Modify ecore_config files\n\n");
138    fprintf(stderr, "Options:\n");
139    fprintf(stderr, "  -c, --file=FILE config file\n");
140    fprintf(stderr, "  -k, --key=KEY   must be given for all commands except -a\n\n");
141    fprintf(stderr, "Commands:\n");
142    fprintf(stderr, "  -a, --list         get all keys\n");
143    fprintf(stderr, "  -g, --get          get key\n");
144    fprintf(stderr, "  -d, --del          delete key\n");
145    fprintf(stderr, "  -b, --bool=VALUE   set boolean\n");
146    fprintf(stderr, "  -f, --float=VALUE  set float\n");
147    fprintf(stderr, "  -i, --int=VALUE    set integer\n");
148    fprintf(stderr, "  -r, --rgb=VALUE    set RGBA\n");
149    fprintf(stderr, "  -s, --string=VALUE set string\n");
150    fprintf(stderr, "  -t, --theme=VALUE  set theme\n\n");
151    exit(ret);
152 }
153
154 int
155 main(int argc, char * const argv[])
156 {
157    char *prog, *file, *key;
158    void *value = (void *)NULL;
159    char cmd = 's';
160    int type = -1;
161    int ret = 0;
162    long i;
163    float f;
164    
165    file = key = prog = NULL;
166    eina_init();
167    prog = strdup(argv[0]);
168
169    if(argc < 4)
170      usage_and_exit(prog, 2, NULL);
171
172    while(1)
173      {
174         static struct option long_options[] = {
175            {"file",   1, 0, 'c'},
176            {"list",   0, 0, 'a'},
177            {"get",    0, 0, 'g'},
178            {"del",    0, 0, 'd'},
179            {"bool",   1, 0, 'b'},
180            {"float",  1, 0, 'f'},
181            {"int",    1, 0, 'i'},
182            {"rgb",    1, 0, 'r'},
183            {"string", 1, 0, 's'},
184            {"theme",  1, 0, 't'},
185            {"key",    1, 0, 'k'},
186            {0, 0, 0, 0}
187         };
188
189         ret = getopt_long(argc, argv, "c:agdb:f:i:r:s:t:k:", long_options, NULL);
190         if(ret == -1)
191            break;
192
193         switch(ret)
194            {
195              case 'k':
196                 key = strdup(optarg);
197                 break;
198              case 'n':
199                 if(value)
200                    usage_and_exit(prog, 2, "too many commands");
201                 type = ECORE_CONFIG_NIL;
202                 value = NULL;
203                 break;
204              case 'b':
205                 if(value)
206                    usage_and_exit(prog, 2, "too many commands");
207                 type = ECORE_CONFIG_BLN;
208                 i = atoi(optarg);
209                 value = &i;
210                 break;
211              case 'i':
212                 if(value)
213                    usage_and_exit(prog, 2, "too many commands");
214                 type = ECORE_CONFIG_INT;
215                 i = atoi(optarg);
216                 value = &i;
217                 break;
218              case 'f':
219                 if(value)
220                    usage_and_exit(prog, 2, "too many commands");
221                 type = ECORE_CONFIG_FLT;
222                 f = atof(optarg);
223                 value = &f;
224                 break;
225              case 'r':
226                 if(value)
227                    usage_and_exit(prog, 2, "too many commands");
228                 type = ECORE_CONFIG_RGB;
229                 i = (long) strtoul( (*optarg == '#') ? (optarg + 1) : optarg, NULL, 16 );
230                 value = &i;
231                 break;
232              case 's':
233                 if(value)
234                    usage_and_exit(prog, 2, "too many commands");
235                 type = ECORE_CONFIG_STR;
236                 value = strdup(optarg);
237                 break;
238              case 't':
239                 if(value)
240                    usage_and_exit(prog, 2, "too many commands");
241                 type = ECORE_CONFIG_THM;
242                 value = strdup(optarg);
243                 break;
244              case 'c':
245                 if(file)
246                    free(file);
247                 file = strdup(optarg);
248                 break;
249              case '?':
250              case ':':
251                 return 1;
252              default:
253                 cmd = ret;
254                 break;
255            }
256      }
257
258    if(cmd == 's' && type == -1)
259      usage_and_exit(prog, 2, "You need to specify a command!");
260
261    if(cmd != 'a' && key == NULL)
262      usage_and_exit(prog, 2, "You need to specify key!");
263    
264    if(ecore_config_init("econfig") != ECORE_CONFIG_ERR_SUCC)
265      {
266        EINA_LOG_ERR("Couldn't init ecore_config!");
267         return 1;
268      }
269
270    // Load configuration from file
271    ecore_config_file_load(file);
272
273    ret = 0;
274
275    // Execute command
276    switch (cmd)
277      {
278         case 's':
279            if (ecore_config_typed_set(key, value, type) != ECORE_CONFIG_ERR_SUCC)
280              {
281                 fprintf(stderr, "Set failed for %s", key);
282                 ret = 1;
283              } else {
284                 ecore_config_file_save(file);
285              }
286              get(key); // display value after setting it
287            break;
288         case 'd':
289            if(del(key))
290              {
291                 fprintf(stderr, "Delete failed for %s", key);
292                 ret = 1;
293              } else {
294                 ecore_config_file_save(file);
295              }
296            break;
297         case 'g':
298            if (get(key)) ret = 1;
299            break;
300         case 'a':
301            if (list(file)) ret = 1;
302            break;
303         default:
304            EINA_LOG_ERR("Unhandled command '%c'", cmd);
305      }
306
307    ecore_config_shutdown();
308
309    if(type == ECORE_CONFIG_STR || type == ECORE_CONFIG_THM)
310      free(value);
311
312    if(file)
313      free(file);
314    eina_shutdown();
315    return ret;
316 }
317 #else
318 int
319 main(int argc, const char **argv)
320 {
321    printf("Ecore_config module not compiled. This program is empty.\n");
322    return -1;
323 }
324 #endif