2 * Copyright 2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (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
8 * http://floralicense.org/license/
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.
18 #include <stdlib.h> /* strtod */
20 #include <ctype.h> /* isspace */
25 #include <dynamicbox_service.h>
26 #include <dynamicbox_errno.h>
27 #include <dynamicbox_conf.h>
34 static Eina_List *s_list;
38 #define RETURN_TYPE long long
40 #define RETURN_TYPE int
50 unsigned int gbar_width;
51 unsigned int gbar_height;
71 HAPI double parser_period(struct parser *handle)
73 return handle->period;
76 HAPI int parser_network(struct parser *handle)
78 return handle->network;
81 HAPI int parser_timeout(struct parser *handle)
83 return handle->timeout;
86 HAPI const char *parser_auto_launch(struct parser *handle)
88 return handle->auto_launch;
91 HAPI const char *parser_script(struct parser *handle)
93 return handle->script;
96 HAPI const char *parser_abi(struct parser *handle)
101 HAPI unsigned int parser_size(struct parser *handle)
106 HAPI const char *parser_dbox_path(struct parser *handle)
108 return handle->dbox_path;
111 HAPI const char *parser_dbox_group(struct parser *handle)
113 return handle->dbox_group;
116 HAPI const char *parser_gbar_path(struct parser *handle)
118 return handle->gbar_path;
121 HAPI const char *parser_gbar_group(struct parser *handle)
123 return handle->gbar_group;
126 HAPI const char *parser_group_str(struct parser *handle)
128 return handle->group;
131 HAPI int parser_secured(struct parser *handle)
133 return handle->secured;
136 HAPI void parser_get_gbar_size(struct parser *handle, unsigned int *width, unsigned int *height)
138 *width = handle->gbar_width;
139 *height = handle->gbar_height;
142 HAPI int parser_pinup(struct parser *handle)
144 return handle->pinup;
147 HAPI int parser_text_dbox(struct parser *handle)
149 return handle->text_lb;
152 HAPI int parser_text_gbar(struct parser *handle)
154 return handle->text_gbar;
157 HAPI int parser_buffer_dbox(struct parser *handle)
159 return handle->buffer_lb;
162 HAPI int parser_buffer_gbar(struct parser *handle)
164 return handle->buffer_gbar;
167 HAPI RETURN_TYPE parser_find(const char *pkgname)
175 len = strlen(pkgname) * 2 + strlen(DYNAMICBOX_CONF_CONF_PATH);
177 filename = malloc(len);
179 return (RETURN_TYPE)0;
182 ret = snprintf(filename, len, DYNAMICBOX_CONF_CONF_PATH, pkgname, pkgname);
185 return (RETURN_TYPE)0;
188 DbgPrint("Conf file %s for package %s\n", filename, pkgname);
190 EINA_LIST_FOREACH(s_list, l, item) {
191 if (!strcmp(item->filename, filename)) {
193 return (RETURN_TYPE)item;
198 return (RETURN_TYPE)0;
201 static inline int parse_size(const char *buffer, unsigned int *size)
223 while (state != ERROR && state != END) {
246 w = (w * 10) + (buffer[i] - '0');
276 h = (h * 10) + (buffer[i] - '0');
289 if (w == 1 && h == 1) {
290 *size |= DBOX_SIZE_TYPE_1x1;
291 } else if (w == 2 && h == 1) {
292 *size |= DBOX_SIZE_TYPE_2x1;
293 } else if (w == 2 && h == 2) {
294 *size |= DBOX_SIZE_TYPE_2x2;
295 } else if (w == 4 && h == 1) {
296 *size |= DBOX_SIZE_TYPE_4x1;
297 } else if (w == 4 && h == 2) {
298 *size |= DBOX_SIZE_TYPE_4x2;
299 } else if (w == 4 && h == 3) {
300 *size |= DBOX_SIZE_TYPE_4x3;
301 } else if (w == 4 && h == 4) {
302 *size |= DBOX_SIZE_TYPE_4x4;
303 } else if (w == 21 && h == 21) {
304 *size |= DBOX_SIZE_TYPE_EASY_1x1;
305 } else if (w == 23 && h == 21) {
306 *size |= DBOX_SIZE_TYPE_EASY_3x1;
307 } else if (w == 23 && h == 23) {
308 *size |= DBOX_SIZE_TYPE_EASY_3x3;
310 ErrPrint("Invalid size type: %dx%d\n", w, h);
313 if (buffer[i] == ';') {
315 } else if (buffer[i] == '\0') {
327 return *size ? 0 : -1;
332 * This will change the value of "buffer"
334 static inline const char *rtrim(char *buffer)
338 len = strlen(buffer);
339 while (len > 0 && isspace(buffer[len - 1])) {
354 * This will change the value of "buffer"
356 static inline char *dup_rtrim(char *buffer)
360 if (!rtrim(buffer)) {
364 ret = strdup(buffer);
366 ErrPrint("Heap: %s\n", strerror(errno));
373 static void period_handler(struct parser *item, char *buffer)
377 if (!rtrim(buffer)) {
381 item->period = strtod(buffer, &tmp);
384 static void timeout_handler(struct parser *item, char *buffer)
386 if (!rtrim(buffer)) {
390 item->timeout = atoi(buffer);
393 static void network_handler(struct parser *item, char *buffer)
395 if (!rtrim(buffer)) {
399 item->network = !!atoi(buffer);
402 static void auto_launch_handler(struct parser *item, char *buffer)
404 if (!rtrim(buffer)) {
408 item->auto_launch = strdup(buffer);
409 if (!item->auto_launch) {
410 ErrPrint("Heap: %s\n", strerror(errno));
415 static void size_handler(struct parser *item, char *buffer)
417 if (parse_size(buffer, &item->size) == -1) {
418 ErrPrint("Failed to get size\n");
419 item->size = 0x00000001;
423 static void gbar_size_handler(struct parser *item, char *buffer)
425 if (sscanf(buffer, "%ux%u", &item->gbar_width, &item->gbar_height) != 2) {
426 ErrPrint("parse pd size\n");
430 static void text_dbox_handler(struct parser *item, char *buffer)
432 if (!rtrim(buffer)) {
436 item->text_lb = !!atoi(buffer);
439 static void abi_handler(struct parser *item, char *buffer)
441 item->abi = dup_rtrim(buffer);
444 static void script_handler(struct parser *item, char *buffer)
446 item->script = dup_rtrim(buffer);
449 static void buffer_gbar_handler(struct parser *item, char *buffer)
451 if (!rtrim(buffer)) {
455 item->buffer_gbar = !!atoi(buffer);
458 static void buffer_dbox_handler(struct parser *item, char *buffer)
460 if (!rtrim(buffer)) {
464 item->buffer_lb = !!atoi(buffer);
467 static void text_gbar_handler(struct parser *item, char *buffer)
469 if (!rtrim(buffer)) {
473 item->text_gbar = !!atoi(buffer);
476 static void pinup_handler(struct parser *item, char *buffer)
478 if (!rtrim(buffer)) {
482 item->pinup = !!atoi(buffer);
485 static void dbox_path_handler(struct parser *item, char *buffer)
487 if (item->dbox_path) {
488 DbgFree(item->dbox_path);
491 item->dbox_path = dup_rtrim(buffer);
492 if (!item->dbox_path) {
493 ErrPrint("Error: %s\n", strerror(errno));
497 static void group_handler(struct parser *item, char *buffer)
500 DbgFree(item->group);
503 item->group = dup_rtrim(buffer);
505 ErrPrint("Error: %s\n", strerror(errno));
509 static void secured_handler(struct parser *item, char *buffer)
511 if (!rtrim(buffer)) {
515 item->secured = !!atoi(buffer);
518 static void dbox_group_handler(struct parser *item, char *buffer)
520 if (item->dbox_group) {
521 DbgFree(item->dbox_group);
524 item->dbox_group = dup_rtrim(buffer);
525 if (!item->dbox_group) {
526 ErrPrint("Error: %s\n", strerror(errno));
530 static void gbar_path_handler(struct parser *item, char *buffer)
532 if (item->gbar_path) {
533 DbgFree(item->gbar_path);
536 item->gbar_path = dup_rtrim(buffer);
537 if (!item->gbar_path) {
538 ErrPrint("Error: %s\n", strerror(errno));
542 static void gbar_group_handler(struct parser *item, char *buffer)
544 if (item->gbar_group) {
545 DbgFree(item->gbar_group);
548 item->gbar_group = dup_rtrim(buffer);
549 if (!item->gbar_group) {
550 ErrPrint("Error: %s\n", strerror(errno));
554 HAPI struct parser *parser_load(const char *pkgname)
575 static const struct token_parser {
577 void (*handler)(struct parser *, char *buffer);
578 } token_handler[] = {
581 .handler = period_handler,
585 .handler = timeout_handler,
589 .handler = network_handler,
592 .name = "auto_launch",
593 .handler = auto_launch_handler,
597 .handler = size_handler,
601 .handler = group_handler,
605 .handler = secured_handler,
608 .name = "dynamicbox_path",
609 .handler = dbox_path_handler,
612 .name = "dynamicbox_group",
613 .handler = dbox_group_handler,
617 .handler = gbar_path_handler,
620 .name = "gbar_group",
621 .handler = gbar_group_handler,
625 .handler = gbar_size_handler,
629 .handler = pinup_handler,
632 .name = "text_dynamicbox",
633 .handler = text_dbox_handler,
637 .handler = text_gbar_handler,
640 .name = "buffer_dynamicbox",
641 .handler = buffer_dbox_handler,
644 .name = "buffer_gbar",
645 .handler = buffer_gbar_handler,
649 .handler = script_handler,
653 .handler = abi_handler,
662 item = calloc(1, sizeof(*item));
668 len = strlen(DYNAMICBOX_CONF_CONF_PATH) + strlen(pkgname) * 2;
669 item->filename = malloc(len);
670 if (!item->filename) {
671 ErrPrint("Error: %s\n", strerror(errno));
676 ret = snprintf(item->filename, len, DYNAMICBOX_CONF_CONF_PATH, pkgname, pkgname);
678 ErrPrint("Error: %s\n", strerror(errno));
679 DbgFree(item->filename);
684 item->dbox_path = NULL;
685 item->dbox_group = NULL;
686 item->gbar_width = 0;
687 item->gbar_height = 0;
688 item->auto_launch = NULL;
689 item->size = 0x00000001;
694 fp = fopen(item->filename, "rt");
696 DbgFree(item->filename);
709 if ((c == EOF) && (state == VALUE)) {
715 if (c == CR || c == LF || c == EOF) {
716 buffer[buffer_idx] = '\0';
722 linelen = -1; /* Will be ZERO by follwing increment code */
725 buffer[buffer_idx++] = c;
726 if (buffer_idx == (sizeof(buffer) - 1)) {
727 buffer[buffer_idx] = '\0';
733 if (linelen == 0 && c == '#') {
735 } else if (isspace(c)) {
736 /* Ignore empty space */
745 } else if (!isspace(c)) {
752 buffer[buffer_idx] = '\0';
754 } else if (buffer_idx != 0) {
755 buffer[buffer_idx++] = c;
756 if (buffer_idx >= sizeof(buffer)) {
762 } else if (isspace(c)) {
763 if (buffer_idx == 0) {
765 } else if (quote == 1) {
766 buffer[buffer_idx++] = c;
767 if (buffer_idx >= sizeof(buffer)) {
771 buffer[buffer_idx] = '\0';
776 buffer[buffer_idx++] = c;
777 if (buffer_idx >= sizeof(buffer)) {
789 } else if (isspace(c)) {
794 if (token_handler[token_idx].name[ch_idx] != '\0') {
801 /* Now start to find a token! */
805 if (token_handler[token_idx].name[ch_idx] == c) {
809 while (ch_idx-- > 0) {
810 ungetc(token_handler[token_idx].name[ch_idx], fp);
815 if (token_handler[token_idx].name == NULL) {
824 if (c == CR || c == LF || c == EOF) {
834 if (c == LF || c == CR || c == EOF) {
839 * Make the string terminator
841 buffer[buffer_idx] = '\0';
843 if (token_idx >= 0 && token_handler[token_idx].handler) {
844 token_handler[token_idx].handler(item, buffer);
853 } else if (isspace(c)) {
867 if (fclose(fp) != 0) {
868 ErrPrint("fclose: %s\n", strerror(errno));
871 s_list = eina_list_append(s_list, item);
875 HAPI int parser_unload(struct parser *item)
877 s_list = eina_list_remove(s_list, item);
879 DbgFree(item->auto_launch);
881 DbgFree(item->script);
882 DbgFree(item->group);
883 DbgFree(item->gbar_group);
884 DbgFree(item->gbar_path);
885 DbgFree(item->dbox_group);
886 DbgFree(item->dbox_path);
887 DbgFree(item->filename);
889 return DBOX_STATUS_ERROR_NONE;