aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ecore/src/bin/ecore_config.c
blob: b4973d91d10dbc9356075539029c48802545c4cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "config.h"
#include "Ecore.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#ifdef BUILD_ECORE_CONFIG
#include <unistd.h>
#include <getopt.h>
#include <Eet.h>
#include "Ecore_Config.h"
#include "ecore_config_private.h"

// strcmp for paths - for sorting folders before files
static int
pathcmp(const char *s1, const char *s2)
{
   char *s1d, *s2d;

   // strip common part of paths
   while(*s1 && *s2 && *s1 == *s2)
     {
	s1++;
	s2++;
     }

   // handle /foo/bar/baz <> /foo/bar_baz properly
   if (*s1 == '/' && *s2 != '/') return -1;
   if (*s1 != '/' && *s2 == '/') return  1;

   // skip leading /
   if (*s1 == '/') s1++;
   if (*s2 == '/') s2++;

   // order folders before files
   s1d = strchr(s1, '/');
   s2d = strchr(s2, '/');
   if (s1d && !s2d) return -1;
   if (!s1d && s2d) return  1;

   return strcmp(s1, s2);
}

static int
del(const char *key)
{
   Ecore_Config_Prop *e;
   e = ecore_config_get(key);
   if(!e) return -1;

   ecore_config_dst(e);
   return 0;
}

static int
get(const char *key)
{
   Ecore_Config_Prop *e;
   char *temp = NULL;

   if (!(e = ecore_config_get(key)))
     {
	EINA_LOG_ERR("No such property");
	return -1;
     }
     
   printf("%-10s", ecore_config_type_get(e));

   switch (e->type)
     {
	case ECORE_CONFIG_NIL:
	   printf("\n");
	   break;
	case ECORE_CONFIG_INT:
	   printf("%ld\n", ecore_config_int_get(key));
	   break;
	case ECORE_CONFIG_BLN:
	   printf("%d\n",  ecore_config_boolean_get(key));
	   break;
	case ECORE_CONFIG_FLT:
	   printf("%lf\n", ecore_config_float_get(key));
	   break;
	case ECORE_CONFIG_STR:
	   temp = ecore_config_string_get(key);
	   break;
	case ECORE_CONFIG_RGB:
	   temp = ecore_config_argbstr_get(key);
	   break;
	case ECORE_CONFIG_THM:
	   temp = ecore_config_theme_get(key);
	   break;
	default:
	   EINA_LOG_ERR("Property has unrecognized type");
	   return -1;
     }
   if(temp)
     {
	printf("\"%s\"\n", temp);
	free(temp);
     }
   return 0;
}

static int
list(const char *file)
{
   Ecore_Config_Prop *e;
   Eina_List *keys = NULL;
   char *key;

   e = __ecore_config_bundle_local->data;

   do
     {
	// don't show system settings
	if( !(e->flags & ECORE_CONFIG_FLAG_SYSTEM) )
	   keys = eina_list_append(keys, e->key);
     }
   while((e = e->next));
   keys = eina_list_sort(keys, -1, EINA_COMPARE_CB(pathcmp));

   EINA_LIST_FREE(keys, key)
     {
	printf("%-28s\t", key);
	get(key);
     }

   return 0;
}

static void
usage_and_exit(const char *prog, int ret, const char *msg)
{
   if (msg) fprintf(stderr, "%s\n\n", msg);
   fprintf(stderr, "Usage: %s <options> <command>\n", prog);
   fprintf(stderr, "Modify ecore_config files\n\n");
   fprintf(stderr, "Options:\n");
   fprintf(stderr, "  -c, --file=FILE config file\n");
   fprintf(stderr, "  -k, --key=KEY   must be given for all commands except -a\n\n");
   fprintf(stderr, "Commands:\n");
   fprintf(stderr, "  -a, --list         get all keys\n");
   fprintf(stderr, "  -g, --get          get key\n");
   fprintf(stderr, "  -d, --del          delete key\n");
   fprintf(stderr, "  -b, --bool=VALUE   set boolean\n");
   fprintf(stderr, "  -f, --float=VALUE  set float\n");
   fprintf(stderr, "  -i, --int=VALUE    set integer\n");
   fprintf(stderr, "  -r, --rgb=VALUE    set RGBA\n");
   fprintf(stderr, "  -s, --string=VALUE set string\n");
   fprintf(stderr, "  -t, --theme=VALUE  set theme\n\n");
   exit(ret);
}

int
main(int argc, char * const argv[])
{
   char *prog, *file, *key;
   void *value = (void *)NULL;
   char cmd = 's';
   int type = -1;
   int ret = 0;
   long i;
   float f;
   
   file = key = prog = NULL;
   eina_init();
   prog = strdup(argv[0]);

   if(argc < 4)
     usage_and_exit(prog, 2, NULL);

   while(1)
     {
	static struct option long_options[] = {
	   {"file",   1, 0, 'c'},
	   {"list",   0, 0, 'a'},
	   {"get",    0, 0, 'g'},
	   {"del",    0, 0, 'd'},
	   {"bool",   1, 0, 'b'},
	   {"float",  1, 0, 'f'},
	   {"int",    1, 0, 'i'},
	   {"rgb",    1, 0, 'r'},
	   {"string", 1, 0, 's'},
	   {"theme",  1, 0, 't'},
	   {"key",    1, 0, 'k'},
	   {0, 0, 0, 0}
	};

	ret = getopt_long(argc, argv, "c:agdb:f:i:r:s:t:k:", long_options, NULL);
	if(ret == -1)
	   break;

	switch(ret)
	   {
	     case 'k':
		key = strdup(optarg);
		break;
	     case 'n':
		if(value)
		   usage_and_exit(prog, 2, "too many commands");
		type = ECORE_CONFIG_NIL;
		value = NULL;
		break;
	     case 'b':
		if(value)
		   usage_and_exit(prog, 2, "too many commands");
		type = ECORE_CONFIG_BLN;
		i = atoi(optarg);
		value = &i;
		break;
	     case 'i':
		if(value)
		   usage_and_exit(prog, 2, "too many commands");
		type = ECORE_CONFIG_INT;
		i = atoi(optarg);
		value = &i;
		break;
	     case 'f':
		if(value)
		   usage_and_exit(prog, 2, "too many commands");
		type = ECORE_CONFIG_FLT;
		f = atof(optarg);
		value = &f;
		break;
	     case 'r':
		if(value)
		   usage_and_exit(prog, 2, "too many commands");
		type = ECORE_CONFIG_RGB;
		i = (long) strtoul( (*optarg == '#') ? (optarg + 1) : optarg, NULL, 16 );
		value = &i;
		break;
	     case 's':
		if(value)
		   usage_and_exit(prog, 2, "too many commands");
		type = ECORE_CONFIG_STR;
		value = strdup(optarg);
		break;
	     case 't':
		if(value)
		   usage_and_exit(prog, 2, "too many commands");
		type = ECORE_CONFIG_THM;
		value = strdup(optarg);
		break;
	     case 'c':
		if(file)
		   free(file);
		file = strdup(optarg);
		break;
	     case '?':
	     case ':':
		return 1;
	     default:
		cmd = ret;
		break;
	   }
     }

   if(cmd == 's' && type == -1)
     usage_and_exit(prog, 2, "You need to specify a command!");

   if(cmd != 'a' && !key)
     usage_and_exit(prog, 2, "You need to specify key!");
   
   if(ecore_config_init("econfig") != ECORE_CONFIG_ERR_SUCC)
     {
       EINA_LOG_ERR("Couldn't init ecore_config!");
	return 1;
     }

   // Load configuration from file
   ecore_config_file_load(file);

   ret = 0;

   // Execute command
   switch (cmd)
     {
	case 's':
	   if (ecore_config_typed_set(key, value, type) != ECORE_CONFIG_ERR_SUCC)
	     {
		fprintf(stderr, "Set failed for %s", key);
		ret = 1;
	     } else {
		ecore_config_file_save(file);
	     }
	     get(key); // display value after setting it
	   break;
	case 'd':
	   if(del(key))
	     {
		fprintf(stderr, "Delete failed for %s", key);
		ret = 1;
	     } else {
		ecore_config_file_save(file);
	     }
	   break;
	case 'g':
	   if (get(key)) ret = 1;
	   break;
	case 'a':
	   if (list(file)) ret = 1;
	   break;
	default:
	   EINA_LOG_ERR("Unhandled command '%c'", cmd);
     }

   ecore_config_shutdown();

   if(type == ECORE_CONFIG_STR || type == ECORE_CONFIG_THM)
     free(value);

   if(file)
     free(file);
   eina_shutdown();
   return ret;
}
#else
int
main(int argc, const char **argv)
{
   printf("Ecore_config module not compiled. This program is empty.\n");
   return -1;
}
#endif