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 <livebox-service.h>
26 #include <livebox-errno.h>
33 static Eina_List *s_list;
43 unsigned int pd_width;
44 unsigned int pd_height;
64 HAPI double parser_period(struct parser *handle)
66 return handle->period;
69 HAPI int parser_network(struct parser *handle)
71 return handle->network;
74 HAPI int parser_timeout(struct parser *handle)
76 return handle->timeout;
79 HAPI const char *parser_auto_launch(struct parser *handle)
81 return handle->auto_launch;
84 HAPI const char *parser_script(struct parser *handle)
86 return handle->script;
89 HAPI const char *parser_abi(struct parser *handle)
94 HAPI unsigned int parser_size(struct parser *handle)
99 HAPI const char *parser_lb_path(struct parser *handle)
101 return handle->lb_path;
104 HAPI const char *parser_lb_group(struct parser *handle)
106 return handle->lb_group;
109 HAPI const char *parser_pd_path(struct parser *handle)
111 return handle->pd_path;
114 HAPI const char *parser_pd_group(struct parser *handle)
116 return handle->pd_group;
119 HAPI const char *parser_group_str(struct parser *handle)
121 return handle->group;
124 HAPI int parser_secured(struct parser *handle)
126 return handle->secured;
129 HAPI void parser_get_pdsize(struct parser *handle, unsigned int *width, unsigned int *height)
131 *width = handle->pd_width;
132 *height = handle->pd_height;
135 HAPI int parser_pinup(struct parser *handle)
137 return handle->pinup;
140 HAPI int parser_text_lb(struct parser *handle)
142 return handle->text_lb;
145 HAPI int parser_text_pd(struct parser *handle)
147 return handle->text_pd;
150 HAPI int parser_buffer_lb(struct parser *handle)
152 return handle->buffer_lb;
155 HAPI int parser_buffer_pd(struct parser *handle)
157 return handle->buffer_pd;
160 HAPI int parser_find(const char *pkgname)
168 len = strlen(pkgname) * 2 + strlen(CONF_PATH);
170 filename = malloc(len);
172 return LB_STATUS_SUCCESS;
174 ret = snprintf(filename, len, CONF_PATH, pkgname, pkgname);
177 return LB_STATUS_ERROR_FAULT;
180 DbgPrint("Conf file is %s for package %s\n", filename, pkgname);
182 EINA_LIST_FOREACH(s_list, l, item) {
183 if (!strcmp(item->filename, filename)) {
190 return LB_STATUS_SUCCESS;
193 static inline int parse_size(const char *buffer, unsigned int *size)
215 while (state != ERROR && state != END) {
238 w = (w * 10) + (buffer[i] - '0');
268 h = (h * 10) + (buffer[i] - '0');
281 if (w == 1 && h == 1) {
282 *size |= LB_SIZE_TYPE_1x1;
283 } else if (w == 2 && h == 1) {
284 *size |= LB_SIZE_TYPE_2x1;
285 } else if (w == 2 && h == 2) {
286 *size |= LB_SIZE_TYPE_2x2;
287 } else if (w == 4 && h == 1) {
288 *size |= LB_SIZE_TYPE_4x1;
289 } else if (w == 4 && h == 2) {
290 *size |= LB_SIZE_TYPE_4x2;
291 } else if (w == 4 && h == 3) {
292 *size |= LB_SIZE_TYPE_4x3;
293 } else if (w == 4 && h == 4) {
294 *size |= LB_SIZE_TYPE_4x4;
295 } else if (w == 21 && h == 21) {
296 *size |= LB_SIZE_TYPE_EASY_1x1;
297 } else if (w == 23 && h == 21) {
298 *size |= LB_SIZE_TYPE_EASY_3x1;
299 } else if (w == 23 && h == 23) {
300 *size |= LB_SIZE_TYPE_EASY_3x3;
302 ErrPrint("Invalid size type: %dx%d\n", w, h);
305 if (buffer[i] == ';')
307 else if (buffer[i] == '\0')
318 return *size ? 0 : -1;
323 * This will change the value of "buffer"
325 static inline const char *rtrim(char *buffer)
329 len = strlen(buffer);
330 while (len > 0 && isspace(buffer[len - 1]))
343 * This will change the value of "buffer"
345 static inline char *dup_rtrim(char *buffer)
352 ret = strdup(buffer);
354 ErrPrint("Heap: %s\n", strerror(errno));
361 static void period_handler(struct parser *item, char *buffer)
368 item->period = strtod(buffer, &tmp);
371 static void timeout_handler(struct parser *item, char *buffer)
376 item->timeout = atoi(buffer);
379 static void network_handler(struct parser *item, char *buffer)
384 item->network = !!atoi(buffer);
387 static void auto_launch_handler(struct parser *item, char *buffer)
392 item->auto_launch = strdup(buffer);
393 if (!item->auto_launch) {
394 ErrPrint("Heap: %s\n", strerror(errno));
399 static void size_handler(struct parser *item, char *buffer)
401 if (parse_size(buffer, &item->size) == -1) {
402 ErrPrint("Failed to get size\n");
403 item->size = 0x00000001;
407 static void pd_size_handler(struct parser *item, char *buffer)
409 if (sscanf(buffer, "%ux%u", &item->pd_width, &item->pd_height) != 2)
410 ErrPrint("parse pd size\n");
413 static void text_lb_handler(struct parser *item, char *buffer)
418 item->text_lb = !!atoi(buffer);
421 static void abi_handler(struct parser *item, char *buffer)
423 item->abi = dup_rtrim(buffer);
426 static void script_handler(struct parser *item, char *buffer)
428 item->script = dup_rtrim(buffer);
431 static void buffer_pd_handler(struct parser *item, char *buffer)
436 item->buffer_pd = !!atoi(buffer);
439 static void buffer_lb_handler(struct parser *item, char *buffer)
443 item->buffer_lb = !!atoi(buffer);
446 static void text_pd_handler(struct parser *item, char *buffer)
451 item->text_pd = !!atoi(buffer);
454 static void pinup_handler(struct parser *item, char *buffer)
459 item->pinup = !!atoi(buffer);
462 static void lb_path_handler(struct parser *item, char *buffer)
465 DbgFree(item->lb_path);
467 item->lb_path = dup_rtrim(buffer);
469 ErrPrint("Error: %s\n", strerror(errno));
472 static void group_handler(struct parser *item, char *buffer)
475 DbgFree(item->group);
477 item->group = dup_rtrim(buffer);
479 ErrPrint("Error: %s\n", strerror(errno));
482 static void secured_handler(struct parser *item, char *buffer)
487 item->secured = !!atoi(buffer);
490 static void lb_group_handler(struct parser *item, char *buffer)
493 DbgFree(item->lb_group);
495 item->lb_group = dup_rtrim(buffer);
497 ErrPrint("Error: %s\n", strerror(errno));
500 static void pd_path_handler(struct parser *item, char *buffer)
503 DbgFree(item->pd_path);
505 item->pd_path = dup_rtrim(buffer);
507 ErrPrint("Error: %s\n", strerror(errno));
510 static void pd_group_handler(struct parser *item, char *buffer)
513 DbgFree(item->pd_group);
515 item->pd_group = dup_rtrim(buffer);
517 ErrPrint("Error: %s\n", strerror(errno));
520 HAPI struct parser *parser_load(const char *pkgname)
541 static const struct token_parser {
543 void (*handler)(struct parser *, char *buffer);
544 } token_handler[] = {
547 .handler = period_handler,
551 .handler = timeout_handler,
555 .handler = network_handler,
558 .name = "auto_launch",
559 .handler = auto_launch_handler,
563 .handler = size_handler,
567 .handler = group_handler,
571 .handler = secured_handler,
574 .name = "livebox_path",
575 .handler = lb_path_handler,
578 .name = "livebox_group",
579 .handler = lb_group_handler,
583 .handler = pd_path_handler,
587 .handler = pd_group_handler,
591 .handler = pd_size_handler,
595 .handler = pinup_handler,
598 .name = "text_livebox",
599 .handler = text_lb_handler,
603 .handler = text_pd_handler,
606 .name = "buffer_livebox",
607 .handler = buffer_lb_handler,
611 .handler = buffer_pd_handler,
615 .handler = script_handler,
619 .handler = abi_handler,
628 item = calloc(1, sizeof(*item));
633 len = strlen(CONF_PATH) + strlen(pkgname) * 2;
634 item->filename = malloc(len);
635 if (!item->filename) {
636 ErrPrint("Error: %s\n", strerror(errno));
641 ret = snprintf(item->filename, len, CONF_PATH, pkgname, pkgname);
643 ErrPrint("Error: %s\n", strerror(errno));
644 DbgFree(item->filename);
649 item->lb_path = NULL;
650 item->lb_group = NULL;
653 item->auto_launch = NULL;
654 item->size = 0x00000001;
659 fp = fopen(item->filename, "rt");
661 DbgFree(item->filename);
674 if ((c == EOF) && (state == VALUE)) {
675 DbgPrint("[%s:%d] VALUE state EOF\n", __func__, __LINE__);
681 if (c == CR || c == LF || c == EOF) {
682 buffer[buffer_idx] = '\0';
688 linelen = -1; /* Will be ZERO by follwing increment code */
691 buffer[buffer_idx++] = c;
692 if (buffer_idx == (sizeof(buffer) - 1)) {
693 buffer[buffer_idx] = '\0';
699 if (linelen == 0 && c == '#') {
701 } else if (isspace(c)) {
702 /* Ignore empty space */
711 else if (!isspace(c))
717 buffer[buffer_idx] = '\0';
719 } else if (buffer_idx != 0) {
720 buffer[buffer_idx++] = c;
721 if (buffer_idx >= sizeof(buffer))
726 } else if (isspace(c)) {
727 if (buffer_idx == 0) {
729 } else if (quote == 1) {
730 buffer[buffer_idx++] = c;
731 if (buffer_idx >= sizeof(buffer))
734 buffer[buffer_idx] = '\0';
739 buffer[buffer_idx++] = c;
740 if (buffer_idx >= sizeof(buffer))
750 } else if (isspace(c)) {
754 if (token_handler[token_idx].name[ch_idx] != '\0')
760 /* Now start to find a token! */
764 if (token_handler[token_idx].name[ch_idx] == c) {
769 ungetc(token_handler[token_idx].name[ch_idx], fp);
773 if (token_handler[token_idx].name == NULL)
781 if (c == CR || c == LF || c == EOF) {
791 if (c == LF || c == CR || c == EOF) {
796 * Make the string terminator
798 buffer[buffer_idx] = '\0';
800 if (token_idx >= 0 && token_handler[token_idx].handler)
801 token_handler[token_idx].handler(item, buffer);
809 } else if (isspace(c)) {
824 ErrPrint("fclose: %s\n", strerror(errno));
826 s_list = eina_list_append(s_list, item);
830 HAPI int parser_unload(struct parser *item)
832 s_list = eina_list_remove(s_list, item);
834 DbgFree(item->auto_launch);
836 DbgFree(item->script);
837 DbgFree(item->group);
838 DbgFree(item->pd_group);
839 DbgFree(item->pd_path);
840 DbgFree(item->lb_group);
841 DbgFree(item->lb_path);
842 DbgFree(item->filename);
844 return LB_STATUS_SUCCESS;