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