Remove code of the deprecated feature
[platform/framework/web/data-provider-slave.git] / src / conf.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 <Ecore_X.h>
18 #include <ctype.h>
19
20 #include <dlog.h>
21 #include <Eina.h>
22
23 #include "conf.h"
24 #include "util.h"
25 #include "debug.h"
26
27 HAPI struct conf g_conf = {
28         .width = 0,
29         .height = 0,
30
31         .base_width = 720,
32         .base_height = 1280,
33
34         .minimum_period = 1.0f,
35
36         .default_conf.script = "edje",
37         .default_conf.abi = "c",
38         .default_conf.pd_group = "disclosure",
39         .default_conf.period = -1.0f,
40
41         .launch_key.name = "name",
42         .launch_key.secured = "secured",
43         .launch_key.abi = "abi",
44
45         .default_packet_time = 0.0001f,
46
47         .empty_content = "",
48         .empty_title = "",
49
50         .default_content = "default",
51         .default_title = "",
52
53         .minimum_space = 5242880,
54
55         .replace_tag = "/APPID/",
56
57         .slave_ttl = 30.0f,
58
59         .max_log_line = 1000,
60         .max_log_file = 3,
61
62         .sqlite_flush_max = 1048576,
63
64         .path = {
65                 .image = "/opt/usr/share/live_magazine/",
66                 .slave_log = "/opt/usr/share/live_magazine/log",
67                 .root = "/opt/usr/live/",
68                 .script_port = "/opt/usr/live/script_port/",
69                 .db = "/opt/dbspace/.livebox.db",
70
71                 /*!
72                  * This is not loaded from the conf file
73                  */
74                 .conf = "/opt/usr/live/%s/etc/%s.conf",
75                 .script = "/opt/usr/live/%s/res/script/%s.edj",
76                 .module = "/opt/usr/live/%s/libexec/liblive-%s.so",
77         },
78
79         .ping_time = 240.0f,
80         .slave_max_load = 30,
81
82         .use_sw_backend = 0,
83         .provider_method = "pixmap",
84         .debug_mode = 0,
85         .overwrite_content = 0,
86         .com_core_thread = 1,
87 };
88
89 static void conf_update_size(void)
90 {
91         ecore_x_window_size_get(0, &g_conf.width, &g_conf.height);
92 }
93
94 static void use_sw_backend_handler(char *buffer)
95 {
96         g_conf.use_sw_backend = !strcasecmp(buffer, "true");
97         DbgPrint("SW Backend: %d\n", g_conf.use_sw_backend);
98 }
99
100 static void provider_method_handler(char *buffer)
101 {
102         g_conf.provider_method = strdup(buffer);
103         if (!g_conf.provider_method)
104                 ErrPrint("Heap: %s\n", strerror(errno));
105
106         DbgPrint("Method: %s\n", g_conf.provider_method);
107 }
108
109 static void debug_mode_handler(char *buffer)
110 {
111         g_conf.debug_mode = !strcasecmp(buffer, "true");
112         DbgPrint("Debug mode: %d\n", g_conf.debug_mode);
113 }
114
115 static void overwrite_content_handler(char *buffer)
116 {
117         g_conf.overwrite_content = !strcasecmp(buffer, "true");
118         DbgPrint("Overwrite Content: %d\n", g_conf.overwrite_content);
119 }
120
121 static void com_core_thread_handler(char *buffer)
122 {
123         g_conf.com_core_thread = !strcasecmp(buffer, "true");
124         DbgPrint("Com core thread: %d\n", g_conf.com_core_thread);
125 }
126
127 static void base_width_handler(char *buffer)
128 {
129         if (sscanf(buffer, "%d", &g_conf.base_width) != 1)
130                 ErrPrint("Failed to parse the base_width\n");
131
132         DbgPrint("Base width: %d\n", g_conf.base_width);
133 }
134
135 static void base_height_handler(char *buffer)
136 {
137         if (sscanf(buffer, "%d", &g_conf.base_height) != 1)
138                 ErrPrint("Failed to parse the base_height\n");
139         DbgPrint("Base height: %d\n", g_conf.base_height);
140 }
141
142 static void minimum_period_handler(char *buffer)
143 {
144         if (sscanf(buffer, "%lf", &g_conf.minimum_period) != 1)
145                 ErrPrint("Failed to parse the minimum_period\n");
146         DbgPrint("Minimum period: %lf\n", g_conf.minimum_period);
147 }
148
149 static void script_handler(char *buffer)
150 {
151         g_conf.default_conf.script = strdup(buffer);
152         if (!g_conf.default_conf.script)
153                 ErrPrint("Heap: %s\n", strerror(errno));
154         DbgPrint("Default script: %s\n", g_conf.default_conf.script);
155 }
156
157 static void default_abi_handler(char *buffer)
158 {
159         g_conf.default_conf.abi = strdup(buffer);
160         if (!g_conf.default_conf.abi)
161                 ErrPrint("Heap: %s\n", strerror(errno));
162         DbgPrint("Default ABI: %s\n", g_conf.default_conf.abi);
163 }
164
165 static void default_group_handler(char *buffer)
166 {
167         g_conf.default_conf.pd_group = strdup(buffer);
168         if (!g_conf.default_conf.pd_group)
169                 ErrPrint("Heap: %s\n", strerror(errno));
170         DbgPrint("Default PD Group: %s\n", g_conf.default_conf.pd_group);
171 }
172
173 static void default_period_handler(char *buffer)
174 {
175         if (sscanf(buffer, "%lf", &g_conf.default_conf.period) != 1)
176                 ErrPrint("Failed to parse the default_period\n");
177         DbgPrint("Default Period: %lf\n", g_conf.default_conf.period);
178 }
179
180 static void default_packet_time_handler(char *buffer)
181 {
182         if (sscanf(buffer, "%lf", &g_conf.default_packet_time) != 1)
183                 ErrPrint("Failed to parse the default_packet_time\n");
184         DbgPrint("Default packet time: %lf\n", g_conf.default_packet_time);
185 }
186
187 static void default_content_handler(char *buffer)
188 {
189         g_conf.default_content = strdup(buffer);
190         if (!g_conf.default_content)
191                 ErrPrint("Heap: %s\n", strerror(errno));
192         DbgPrint("Default content: %s\n", g_conf.default_content);
193 }
194
195 static void default_title_handler(char *buffer)
196 {
197         g_conf.default_title = strdup(buffer);
198         if (!g_conf.default_title)
199                 ErrPrint("Heap: %s\n", strerror(errno));
200         DbgPrint("Default title: %s\n", g_conf.default_title);
201 }
202
203 static void minimum_space_handler(char *buffer)
204 {
205         if (sscanf(buffer, "%lu", &g_conf.minimum_space) != 1)
206                 ErrPrint("Failed to parse the minimum_space\n");
207         DbgPrint("Minimum space: %lu\n", g_conf.minimum_space);
208 }
209
210 static void replace_tag_handler(char *buffer)
211 {
212         g_conf.replace_tag = strdup(buffer);
213         if (!g_conf.replace_tag)
214                 ErrPrint("Heap: %s\n", strerror(errno));
215         DbgPrint("Replace Tag: %s\n", g_conf.replace_tag);
216 }
217
218 static void slave_ttl_handler(char *buffer)
219 {
220         if (sscanf(buffer, "%lf", &g_conf.slave_ttl) != 1)
221                 ErrPrint("Failed to parse the slave_ttl\n");
222         DbgPrint("Slave TTL: %s\n", g_conf.slave_ttl);
223 }
224
225 static void max_log_line_handler(char *buffer)
226 {
227         if (sscanf(buffer, "%d", &g_conf.max_log_line) != 1)
228                 ErrPrint("Failed to parse the max_log_line\n");
229         DbgPrint("Max log line: %d\n", g_conf.max_log_line);
230 }
231
232 static void max_log_file_handler(char *buffer)
233 {
234         if (sscanf(buffer, "%d", &g_conf.max_log_file) != 1)
235                 ErrPrint("Failed to parse the max_log_file\n");
236         DbgPrint("Max log file: %d\n", g_conf.max_log_file);
237 }
238
239 static void sqlite_flush_max_handler(char *buffer)
240 {
241         if (sscanf(buffer, "%lu", &g_conf.sqlite_flush_max) != 1)
242                 ErrPrint("Failed to parse the sqlite_flush_max\n");
243         DbgPrint("Flush size: %lu\n", g_conf.sqlite_flush_max);
244 }
245
246 static void db_path_handler(char *buffer)
247 {
248         g_conf.path.db = strdup(buffer);
249         if (!g_conf.path.db)
250                 ErrPrint("Heap: %s\n", strerror(errno));
251         DbgPrint("DB Path: %s\n", g_conf.path.db);
252 }
253
254 static void log_path_handler(char *buffer)
255 {
256         g_conf.path.slave_log = strdup(buffer);
257         if (!g_conf.path.slave_log)
258                 ErrPrint("Heap: %s\n", strerror(errno));
259         DbgPrint("LOG Path: %s\n", g_conf.path.slave_log);
260 }
261
262 static void script_port_path_handler(char *buffer)
263 {
264         g_conf.path.script_port = strdup(buffer);
265         if (!g_conf.path.script_port)
266                 ErrPrint("Heap: %s\n", strerror(errno));
267         DbgPrint("Script Port PATH: %s\n", g_conf.path.script_port);
268 }
269
270 static void share_path_handler(char *buffer)
271 {
272         g_conf.path.image = strdup(buffer);
273         if (!g_conf.path.image)
274                 ErrPrint("Heap: %s\n", strerror(errno));
275         DbgPrint("Shared folder: %s\n", g_conf.path.image);
276 }
277
278 static void ping_time_handler(char *buffer)
279 {
280         if (sscanf(buffer, "%lf", &g_conf.ping_time) != 1)
281                 ErrPrint("Failed to parse the ping_time\n");
282         g_conf.ping_time /= 2.0f; /*!< Half */
283         DbgPrint("Default ping time: %lf\n", g_conf.ping_time);
284 }
285
286 static void slave_max_loader(char *buffer)
287 {
288         if (sscanf(buffer, "%d", &g_conf.slave_max_load) != 1)
289                 ErrPrint("Failed to parse the slave_max_load\n");
290         DbgPrint("Max load: %d\n", g_conf.slave_max_load);
291 }
292
293 HAPI int conf_loader(void)
294 {
295         FILE *fp;
296         int c;
297         enum state {
298                 START,
299                 SPACE,
300                 TOKEN,
301                 VALUE,
302                 ERROR,
303                 COMMENT,
304                 END,
305         } state;
306         int ch_idx;
307         int token_idx;
308         int buffer_idx;
309         int quote;
310         int linelen;
311         char buffer[256];
312         static const struct token_parser {
313                 const char *name;
314                 void (*handler)(char *buffer);
315         } token_handler[] = {
316                 {
317                         .name = "base_width",
318                         .handler = base_width_handler,
319                 },
320                 {
321                         .name = "base_height",
322                         .handler = base_height_handler,
323                 },
324                 {
325                         .name = "minimum_period",
326                         .handler = minimum_period_handler,
327                 },
328                 {
329                         .name = "script",
330                         .handler = script_handler,
331                 },
332                 {
333                         .name = "default_abi",
334                         .handler = default_abi_handler,
335                 },
336                 {
337                         .name = "default_group",
338                         .handler = default_group_handler,
339                 },
340                 {
341                         .name = "default_period",
342                         .handler = default_period_handler,
343                 },
344                 {
345                         .name = "default_packet_time",
346                         .handler = default_packet_time_handler,
347                 },
348                 {
349                         .name = "default_content",
350                         .handler = default_content_handler,
351                 },
352                 {
353                         .name = "default_title",
354                         .handler = default_title_handler,
355                 },
356                 {
357                         .name = "minimum_space",
358                         .handler = minimum_space_handler,
359                 },
360                 {
361                         .name = "replace_tag",
362                         .handler = replace_tag_handler,
363                 },
364                 {
365                         .name = "slave_ttl",
366                         .handler = slave_ttl_handler,
367                 },
368                 {
369                         .name = "max_log_line",
370                         .handler = max_log_line_handler,
371                 },
372                 {
373                         .name = "max_log_file",
374                         .handler = max_log_file_handler,
375                 },
376                 {
377                         .name = "sqilte_flush_max",
378                         .handler = sqlite_flush_max_handler,
379                 },
380                 {
381                         .name = "db_path",
382                         .handler = db_path_handler,
383                 },
384                 {
385                         .name = "log_path",
386                         .handler = log_path_handler,
387                 },
388                 {
389                         .name = "share_path",
390                         .handler = share_path_handler,
391                 },
392                 {
393                         .name = "script_port_path",
394                         .handler = script_port_path_handler,
395                 },
396                 {
397                         .name = "ping_interval",
398                         .handler = ping_time_handler,
399                 },
400                 {
401                         .name = "slave_max_load",
402                         .handler = slave_max_loader,
403                 },
404                 {
405                         .name = "use_sw_backend",
406                         .handler = use_sw_backend_handler,
407                 },
408                 {
409                         .name = "provider_method",
410                         .handler = provider_method_handler,
411                 },
412                 {
413                         .name = "debug_mode",
414                         .handler = debug_mode_handler,
415                 },
416                 {
417                         .name = "overwrite_content",
418                         .handler = overwrite_content_handler,
419                 },
420                 {
421                         .name = "com_core_thread",
422                         .handler = com_core_thread_handler,
423                 },
424                 {
425                         .name = NULL,
426                         .handler = NULL,
427                 },
428         };
429
430         conf_update_size();
431
432         fp = fopen("/usr/share/data-provider-master/conf.ini", "rt");
433         if (!fp) {
434                 ErrPrint("Error: %s\n", strerror(errno));
435                 return -EIO;
436         }
437
438         state = START;
439         ch_idx = 0;
440         token_idx = -1;
441         buffer_idx = 0;
442         quote = 0;
443         linelen = 0;
444         do {
445                 c = getc(fp);
446                 if ((c == EOF) && (state == VALUE)) {
447                         LOGD("[%s:%d] VALUE state EOF\n", __func__, __LINE__);
448                         state = END;
449                 }
450
451                 switch (state) {
452                 case COMMENT:
453                         if (c == CR || c == LF || c == EOF) {
454                                 buffer[buffer_idx] = '\0';
455
456                                 state = START;
457                                 token_idx = -1;
458                                 ch_idx = 0;
459                                 buffer_idx = 0;
460                                 linelen = -1; /* Will be ZERO by follwing increment code */
461                                 quote = 0;
462                         } else {
463                                 buffer[buffer_idx++] = c;
464                                 if (buffer_idx == (sizeof(buffer) - 1)) {
465                                         buffer[buffer_idx] = '\0';
466                                         buffer_idx = 0;
467                                 }
468                         }
469                         break;
470                 case START:
471                         if (linelen == 0 && c == '#') {
472                                 state = COMMENT;
473                         } else if (isspace(c)) {
474                                 /* Ignore empty space */
475                         } else {
476                                 state = TOKEN;
477                                 ungetc(c, fp);
478                         }
479                         break;
480                 case SPACE:
481                         if (c == '=')
482                                 state = VALUE;
483                         else if (!isspace(c))
484                                 state = ERROR;
485                         break;
486                 case VALUE:
487                         if (c == '"') {
488                                 if (quote == 1) {
489                                         buffer[buffer_idx] = '\0';
490                                         state = END;
491                                 } else if (buffer_idx != 0) {
492                                         buffer[buffer_idx++] = c;
493                                         if (buffer_idx >= sizeof(buffer))
494                                                 state = ERROR;
495                                 } else {
496                                         quote = 1;
497                                 }
498                         } else if (isspace(c)) {
499                                 if (buffer_idx == 0) {
500                                         /* Ignore */
501                                 } else if (quote == 1) {
502                                         buffer[buffer_idx++] = c;
503                                         if (buffer_idx >= sizeof(buffer))
504                                                 state = ERROR;
505                                 } else {
506                                         buffer[buffer_idx] = '\0';
507                                         ungetc(c, fp);
508                                         state = END;
509                                 }
510                         } else {
511                                 buffer[buffer_idx++] = c;
512                                 if (buffer_idx >= sizeof(buffer))
513                                         state = ERROR;
514                         }
515                         break;
516                 case TOKEN:
517                         if (c == '=') {
518                                 if (token_idx < 0)
519                                         state = ERROR;
520                                 else
521                                         state = VALUE;
522                         } else if (isspace(c)) {
523                                 if (token_idx < 0)
524                                         break;
525
526                                 if (token_handler[token_idx].name[ch_idx] != '\0')
527                                         state = ERROR;
528                                 else
529                                         state = SPACE;
530                         } else  {
531                                 if (token_idx < 0) {
532                                         /* Now start to find a token! */
533                                         token_idx = 0;
534                                 }
535
536                                 if (token_handler[token_idx].name[ch_idx] == c) {
537                                         ch_idx++;
538                                 } else {
539                                         ungetc(c, fp);
540                                         while (ch_idx-- > 0)
541                                                 ungetc(token_handler[token_idx].name[ch_idx], fp);
542
543                                         token_idx++;
544
545                                         if (token_handler[token_idx].name == NULL)
546                                                 state = ERROR;
547                                         else
548                                                 ch_idx = 0;
549                                 }
550                         }
551                         break;
552                 case ERROR:
553                         if (c == CR || c == LF || c == EOF) {
554                                 state = START;
555                                 token_idx = -1;
556                                 buffer_idx = 0;
557                                 ch_idx = 0;
558                                 linelen = -1;
559                                 quote = 0;
560                         }
561                         break;
562                 case END:
563                         if (c == LF || c == CR || c == EOF) {
564                                 state = START;
565
566                                 if (token_idx >= 0 && token_handler[token_idx].handler) {
567                                         buffer[buffer_idx] = '\0';
568                                         DbgPrint("BUFFER: [%s]\n", buffer);
569                                         token_handler[token_idx].handler(buffer);
570                                 }
571
572                                 token_idx = -1;
573                                 ch_idx = 0;
574                                 buffer_idx = 0;
575                                 linelen = -1;
576                                 quote = 0;
577                                 /* Finish */
578                         } else if (isspace(c)) {
579                                 /* ignore */
580                         } else {
581                                 state = ERROR;
582                         }
583                         break;
584                 default:
585                         /* ?? */
586                         break;
587                 }
588
589                 linelen++;
590          } while (c != EOF);
591
592         fclose(fp);
593         return 0;
594 }
595
596 /* End of a file */