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.
22 #include <sys/types.h>
31 #include <X11/extensions/Xdamage.h>
32 #include <X11/extensions/Xfixes.h>
33 #include <X11/extensions/XShm.h>
34 #include <X11/Xutil.h>
37 #include <glib-object.h>
40 #include <com-core_packet.h>
43 #include <livebox-service.h>
50 #define PROMPT "liveinfo "
100 Ecore_Fd_Handler *fd_handler;
101 Ecore_Fd_Handler *in_handler;
103 struct node *rootdir;
105 struct node *targetdir;
118 struct node *quick_search_node;
121 .fifo_handle = -EINVAL,
129 .input_fd = STDIN_FILENO,
135 .quick_search_node = NULL,
145 static Eina_Bool input_cb(void *data, Ecore_Fd_Handler *fd_handler);
147 static Eina_Bool process_line_cb(void *data)
149 input_cb(NULL, NULL);
150 return ECORE_CALLBACK_CANCEL;
153 static inline void prompt(const char *cmdline)
157 if (s_info.input_fd != STDIN_FILENO) {
158 /* To prevent recursive call, add function to the main loop (idler) */
159 ecore_idler_add(process_line_cb, NULL);
163 path = node_to_abspath(s_info.curdir);
164 printf(PROMPT"%s # %s", path, cmdline ? cmdline : "");
168 static void provider_del_cb(struct node *node)
172 info = node_data(node);
182 static void package_del_cb(struct node *node)
184 struct package *info;
186 info = node_data(node);
191 free(info->slavename);
196 static void inst_del_cb(struct node *node)
198 struct instance *info;
200 info = node_data(node);
206 free(info->category);
211 static inline void ls(void)
218 struct node *next_node;
220 if (!(node_mode(s_info.targetdir) & NODE_READ)) {
221 printf("Access denied\n");
225 is_package = node_name(s_info.targetdir) && !strcmp(node_name(s_info.targetdir), "package");
226 is_provider = !is_package && node_name(s_info.targetdir) && !strcmp(node_name(s_info.targetdir), "provider");
227 is_instance = !is_package && !is_provider && node_parent(s_info.targetdir) && node_name(node_parent(s_info.targetdir)) && !strcmp(node_name(node_parent(s_info.targetdir)), "package");
229 node = node_child(s_info.targetdir);
232 struct package *info;
234 next_node = node_next_sibling(node);
235 if (node_age(node) != s_info.age) {
236 node_delete(node, package_del_cb);
241 info = node_data(node);
242 printf(" %3d %20s %5s ", info->inst_count, info->slavename ? info->slavename : "(none)", info->abi ? info->abi : "?");
243 } else if (is_provider) {
246 next_node = node_next_sibling(node);
247 if (node_age(node) != s_info.age) {
248 node_delete(node, provider_del_cb);
253 info = node_data(node);
254 printf("%6d %3d %5s %5.2f ", info->pid, info->loaded_inst, info->abi ? info->abi : "?", info->ttl);
255 } else if (is_instance) {
256 struct instance *info;
260 next_node = node_next_sibling(node);
262 if (node_age(node) != s_info.age) {
263 node_delete(node, inst_del_cb);
268 info = node_data(node);
270 printf(" %5.2f %6s %10s %10s %4dx%-4d ", info->period, info->state, info->cluster, info->category, info->width, info->height);
271 snprintf(buf, sizeof(buf), "/opt/usr/share/live_magazine/reader/%s", node_name(node));
272 if (lstat(buf, &stat) < 0)
273 printf("%3d ERR ", errno);
275 printf("%2.2lf KB ", (double)stat.st_size / 1024.0f);
278 if (node_type(node) == NODE_DIR)
279 printf("%s/", node_name(node));
280 else if (node_type(node) == NODE_FILE)
281 printf("%s", node_name(node));
284 node = node_next_sibling(node);
288 printf("Total: %d\n", cnt);
291 static void send_slave_list(void)
293 struct packet *packet;
295 if (s_info.cmd != NOP) {
296 printf("Previous command is not finished\n");
300 packet = packet_create_noack("slave_list", "d", 0.0f);
302 printf("Failed to create a packet\n");
306 com_core_packet_send_only(s_info.fd, packet);
307 packet_destroy(packet);
308 s_info.cmd = SLAVE_LIST;
313 * var = debug, slave_max_load
316 static void send_command(const char *cmd, const char *var, const char *val)
318 struct packet *packet;
320 if (s_info.cmd != NOP) {
321 printf("Previous command is not finished\n");
325 packet = packet_create_noack("master_ctrl", "sss", cmd, var, val);
326 com_core_packet_send_only(s_info.fd, packet);
327 packet_destroy(packet);
328 s_info.cmd = MASTER_CTRL;
332 static int pkglist_cb(const char *appid, const char *lbid, int is_prime, void *data)
334 struct node *parent = data;
336 struct package *info;
338 node = node_find(parent, lbid);
340 info = node_data(node);
342 printf("Invalid node\n");
347 info->pkgid = strdup(appid);
349 printf("Error: %s\n", strerror(errno));
353 node_set_age(node, s_info.age);
357 info = calloc(1, sizeof(*info));
359 printf("Error: %s\n", strerror(errno));
363 info->pkgid = strdup(appid);
365 printf("Error: %s\n", strerror(errno));
370 info->primary = is_prime;
372 node = node_create(parent, lbid, NODE_DIR);
379 node_set_mode(node, NODE_READ | NODE_EXEC);
380 node_set_data(node, info);
381 node_set_age(node, s_info.age);
385 static void send_pkg_list(void)
387 struct packet *packet;
389 if (s_info.cmd != NOP) {
390 printf("Previous command is not finished\n");
394 packet = packet_create_noack("pkg_list", "d", 0.0f);
396 printf("Failed to create a packet\n");
400 com_core_packet_send_only(s_info.fd, packet);
401 packet_destroy(packet);
402 s_info.cmd = PKG_LIST;
405 livebox_service_get_pkglist(pkglist_cb, s_info.targetdir);
408 static void send_inst_delete(void)
410 struct packet *packet;
413 struct instance *inst;
415 if (s_info.cmd != NOP) {
416 printf("Previous command is not finished\n");
420 parent = node_parent(s_info.targetdir);
422 printf("Invalid argument\n");
426 if (!node_parent(parent)) {
427 printf("Invalid argument\n");
431 name = node_name(node_parent(parent));
432 if (!name || strcmp(name, "package")) {
433 printf("Invalid argument\n");
437 inst = node_data(s_info.targetdir);
438 name = node_name(parent);
440 packet = packet_create_noack("pkg_ctrl", "sss", "rminst", name, inst->id);
442 printf("Failed to create a packet\n");
446 com_core_packet_send_only(s_info.fd, packet);
447 packet_destroy(packet);
448 s_info.cmd = INST_CTRL;
452 static void send_inst_list(const char *pkgname)
454 struct packet *packet;
456 if (s_info.cmd != NOP) {
457 printf("Previous command is not finished\n");
461 packet = packet_create_noack("inst_list", "s", pkgname);
463 printf("Failed to create a packet\n");
467 com_core_packet_send_only(s_info.fd, packet);
468 packet_destroy(packet);
469 s_info.cmd = INST_LIST;
473 static inline void help(void)
475 printf("liveinfo - Livebox utility\n");
476 printf("------------------------------ [Option] ------------------------------\n");
477 printf("-b Batch mode\n");
478 printf("------------------------------ [Command list] ------------------------------\n");
479 printf("
\e[32mcd [PATH] - Change directory
\e[0m\n");
480 printf("
\e[32mls [ | PATH] - List up content as a file
\e[0m\n");
481 printf("
\e[32mrm [PKG_ID|INST_ID] - Delete package or instance
\e[0m\n");
482 printf("
\e[32mstat [path] - Display the information of given path
\e[0m\n");
483 printf("
\e[32mset [debug] [on|off] Set the control variable of master provider
\e[0m\n");
484 printf("
\e[32mx damage Pix x y w h - Create damage event for given pixmap
\e[0m\n");
485 printf("
\e[32mx move Pix x y - Move the window
\e[0m\n");
486 printf("
\e[32mx resize Pix w h - Resize the window
\e[0m\n");
487 printf("
\e[32mx map Pix - Show the window
\e[0m\n");
488 printf("
\e[32mx unmap Pix - Hide the window
\e[0m\n");
489 printf("
\e[32msh [command] Execute shell command, [command] should be abspath
\e[0m\n");
490 printf("
\e[32mexit -
\e[0m\n");
491 printf("
\e[32mquit -
\e[0m\n");
492 printf("----------------------------------------------------------------------------\n");
495 static inline void init_directory(void)
498 s_info.rootdir = node_create(NULL, NULL, NODE_DIR);
501 node_set_mode(s_info.rootdir, NODE_READ | NODE_EXEC);
503 node = node_create(s_info.rootdir, "provider", NODE_DIR);
505 node_destroy(s_info.rootdir);
506 s_info.rootdir = NULL;
509 node_set_mode(node, NODE_READ | NODE_EXEC);
511 node = node_create(s_info.rootdir, "package", NODE_DIR);
513 node_destroy(node_child(s_info.rootdir));
514 node_destroy(s_info.rootdir);
515 s_info.rootdir = NULL;
518 node_set_mode(node, NODE_READ | NODE_EXEC);
520 s_info.curdir = s_info.rootdir;
524 static inline void fini_directory(void)
528 static inline struct node *update_target_dir(const char *cmd)
532 node = (*cmd == '/') ? s_info.rootdir : s_info.curdir;
533 node = node_find(node, cmd);
538 static int get_token(const char *src, char *out)
541 while (*src && *src == ' ') src++;
546 while (*src && *src != ' ') {
555 static inline int do_stat(const char *cmd)
570 while (*cmd && *cmd == ' ') cmd++;
573 printf("Invalid argument\n");
577 node = node_find(*cmd == '/' ? s_info.rootdir : s_info.curdir, cmd);
579 printf("Invalid path\n");
585 parent = node_parent(node);
587 if (!node_name(parent)) {
588 printf("%s has no info\n", node_name(node));
590 } else if (!strcmp(node_name(parent), "package")) {
591 type = (i == 0) ? PKG : PKG_INSTANCE;
593 } else if (!strcmp(node_name(parent), "provider")){
594 type = (i == 0) ? PROVIDER : PROVIDER_INSTANCE;
598 parent = node_parent(parent);
601 printf("%s is invalid path\n", node_name(node));
608 tmp = livebox_service_i18n_name(node_name(node), NULL);
609 printf("Name: %s (", tmp);
612 i = livebox_service_is_enabled(node_name(node));
613 printf("%s)\n", i ? "enabled" : "disabled");
615 tmp = livebox_service_i18n_icon(node_name(node), NULL);
616 printf("Icon: %s\n", tmp);
619 tmp = livebox_service_provider_name(node_name(node));
620 printf("Provider: %s (content:", tmp);
623 tmp = livebox_service_content(node_name(node));
624 printf("%s)\n", tmp);
627 tmp = livebox_service_lb_script_path(node_name(node));
628 printf("LB Script: %s (", tmp);
631 tmp = livebox_service_lb_script_group(node_name(node));
632 printf("%s)\n", tmp);
635 tmp = livebox_service_pd_script_path(node_name(node));
636 printf("PD Script: %s (", tmp);
639 tmp = livebox_service_pd_script_group(node_name(node));
640 printf("%s)\n", tmp);
643 i = livebox_service_mouse_event(node_name(node));
644 printf("Mouse event: %s\n", i ? "enabled" : "disabled");
648 printf("Not supported yet\n");
651 printf("Not supported yet\n");
653 case PROVIDER_INSTANCE:
654 printf("Not supported yet\n");
657 printf("Not supported yet\n");
664 static inline int do_set(const char *cmd)
667 char variable[4096] = { '0', };
670 i = get_token(cmd, variable);
673 while (*cmd && *cmd == ' ') cmd++;
676 printf("Invalid argument(%s): set [VAR] [VAL]\n", cmd);
680 send_command("set", variable, cmd);
684 static inline int do_get(const char *cmd)
688 while (*cmd && *cmd == ' ') cmd++;
690 printf("Invalid argument(%s): get [VAR]\n", cmd);
694 send_command("get", cmd, "");
698 static inline int do_ls(const char *cmd)
705 while (*cmd && *cmd == ' ')
708 s_info.targetdir = *cmd ? update_target_dir(cmd) : s_info.curdir;
709 if (!s_info.targetdir) {
710 printf("%s is not exists\n", cmd);
714 name = node_name(s_info.targetdir);
716 if (!strcmp(name, "package")) {
717 if (s_info.cmd == NOP) {
722 printf("Waiting the server response\n");
724 } else if (!strcmp(name, "provider")) {
725 if (s_info.cmd == NOP) {
730 printf("Waiting the server response\n");
735 parent = node_parent(s_info.targetdir);
736 if (parent && node_name(parent)) {
737 if (!strcmp(node_name(parent), "package")) {
738 if (s_info.cmd != NOP) {
739 printf("Waiting the server response\n");
743 send_inst_list(name);
752 static inline int do_cd(const char *cmd)
756 while (*cmd && *cmd == ' ')
762 if (s_info.cmd != NOP) {
763 printf("Waiting the server response\n");
767 s_info.targetdir = update_target_dir(cmd);
768 if (!s_info.targetdir) {
769 printf("%s is not exists\n", cmd);
773 if (node_type(s_info.targetdir) != NODE_DIR) {
774 printf("Unable change directory to %s\n", cmd);
778 if (!(node_mode(s_info.targetdir) & NODE_EXEC)) {
779 printf("Access denied %s\n", cmd);
783 s_info.curdir = s_info.targetdir;
787 static inline int do_rm(const char *cmd)
790 while (*cmd && *cmd == ' ') cmd++;
794 if (s_info.cmd != NOP) {
795 printf("Waiting the server response\n");
799 s_info.targetdir = update_target_dir(cmd);
800 if (!s_info.targetdir) {
801 printf("%s is not exists\n", cmd);
805 if (!(node_mode(s_info.targetdir) & NODE_WRITE)) {
806 printf("Access denied %s\n", cmd);
814 #if !defined(WCOREDUMP)
815 #define WCOREDUMP(a) 0
818 static inline void do_sh(const char *cmd)
824 while (*cmd && *cmd == ' ') cmd++;
834 while (idx < (sizeof(command) - 1) && *cmd && *cmd != ' ')
835 command[idx++] = *cmd++;
838 if (execl(command, cmd, NULL) < 0)
839 printf("Failed to execute: %s\n", strerror(errno));
842 } else if (pid < 0) {
843 printf("Failed to create a new process: %s\n", strerror(errno));
846 if (waitpid(pid, &status, 0) < 0) {
847 printf("error: %s\n", strerror(errno));
849 if (WIFEXITED(status)) {
850 printf("Exit: %d\n", WEXITSTATUS(status));
851 } else if (WIFSIGNALED(status)) {
852 printf("Terminated by %d %s\n", WTERMSIG(status), WCOREDUMP(status) ? " - core generated" : "");
853 } else if (WIFSTOPPED(status)) {
854 printf("Stopped by %d\n", WSTOPSIG(status));
855 } else if (WIFCONTINUED(status)) {
856 printf("Child is resumed\n");
862 static inline int get_pixmap_size(Display *disp, Pixmap id, int *x, int *y, unsigned int *w, unsigned int *h)
865 unsigned int dummy_border, dummy_depth;
874 if (!XGetGeometry(disp, id, &dummy_win, x, y, w, h, &dummy_border, &dummy_depth))
880 static inline int do_capture(Display *disp, Pixmap id, const char *filename)
891 screen = DefaultScreenOfDisplay(disp);
892 visual = DefaultVisualOfScreen(screen);
894 if (get_pixmap_size(disp, id, NULL, NULL, &w, &h) < 0) {
895 printf("Failed to get size of a pixmap\n");
899 printf("Pixmap size: %dx%d\n", w, h);
900 bufsz = w * h * sizeof(int);
902 si.shmid = shmget(IPC_PRIVATE, bufsz, IPC_CREAT | 0666);
904 printf("shmget: %s\n", strerror(errno));
909 si.shmaddr = shmat(si.shmid, NULL, 0);
910 if (si.shmaddr == (void *)-1) {
912 if (shmctl(si.shmid, IPC_RMID, 0) < 0)
913 printf("shmctl: %s\n", strerror(errno));
920 * Use the 24 bits Pixmap for Video player
922 xim = XShmCreateImage(disp, visual, 24 /* (depth << 3) */, ZPixmap, NULL, &si, w, h);
924 if (shmdt(si.shmaddr) < 0)
925 printf("shmdt: %s\n", strerror(errno));
927 if (shmctl(si.shmid, IPC_RMID, 0) < 0)
928 printf("shmctl: %s\n", strerror(errno));
933 xim->data = si.shmaddr;
934 XShmAttach(disp, &si);
936 XShmGetImage(disp, id, xim, 0, 0, 0xFFFFFFFF);
939 fd = open(filename, O_CREAT | O_RDWR, 0644);
941 if (write(fd, xim->data, bufsz) != bufsz)
942 printf("Data is not fully written\n");
946 printf("Error: %sn\n", strerror(errno));
949 XShmDetach(disp, &si);
952 if (shmdt(si.shmaddr) < 0)
953 printf("shmdt: %s\n", strerror(errno));
955 if (shmctl(si.shmid, IPC_RMID, 0) < 0)
956 printf("shmctl: %s\n", strerror(errno));
961 static inline void do_x(const char *cmd)
967 while (*cmd && *cmd == ' ') cmd++;
971 disp = XOpenDisplay(NULL);
973 printf("Failed to connect to the X\n");
977 if (!strncasecmp(cmd, "damage ", 7)) {
980 XserverRegion region;
985 if (sscanf(cmd, "%u %d %d %d %d", &winId, &x, &y, &w, &h) != 5) {
986 printf("Invalid argument\nx damage WINID_DEC X Y W H\n");
993 region = XFixesCreateRegion(disp, &rect, 1);
994 XDamageAdd(disp, winId, region);
995 XFixesDestroyRegion(disp, region);
998 printf("Damage: %u %d %d %d %d\n", winId, x, y, w, h);
999 } else if (!strncasecmp(cmd, "capture ", 8)) {
1005 if (sscanf(cmd, "%u %255[^ ]", &winId, filename) != 2) {
1006 printf("Invalid argument\nx capture WINID_DEC FILENAME (%s)\n", cmd);
1009 if (do_capture(disp, winId, filename) == 0)
1010 printf("Captured: %s\n", filename);
1011 } else if (!strncasecmp(cmd, "resize ", 7)) {
1018 if (sscanf(cmd, "%u %d %d", &winId, &w, &h) != 3) {
1019 printf("Invalid argument\nx resize WINID_DEC W H\n");
1023 XResizeWindow(disp, winId, w, h);
1024 printf("Resize: %u %d %d\n", winId, w, h);
1025 } else if (!strncasecmp(cmd, "move ", 5)) {
1031 if (sscanf(cmd, "%u %d %d", &winId, &x, &y) != 3) {
1032 printf("Invalid argument\nx move WINID_DEC X Y\n");
1036 XMoveWindow(disp, winId, x, y);
1037 printf("Move: %u %d %d\n", winId, x, y);
1038 } else if (!strncasecmp(cmd, "map ", 4)) {
1041 if (sscanf(cmd, "%u", &winId) != 1) {
1042 printf("Invalid argument\nx map WINID_DEC\n");
1045 XMapRaised(disp, winId);
1046 printf("Map: %u\n", winId);
1047 } else if (!strncasecmp(cmd, "unmap ", 6)) {
1050 if (sscanf(cmd, "%u", &winId) != 1) {
1051 printf("Invalid argument\nx unmap WINID_DEC\n");
1054 XUnmapWindow(disp, winId);
1055 printf("Unmap: %u\n", winId);
1057 printf("Unknown command\n");
1060 XCloseDisplay(disp);
1063 static inline void put_command(const char *cmd)
1065 if (s_info.history[s_info.history_top]) {
1066 free(s_info.history[s_info.history_top]);
1067 s_info.history[s_info.history_top] = NULL;
1070 s_info.history[s_info.history_top] = strdup(cmd);
1071 s_info.history_top = (s_info.history_top + !!s_info.history[s_info.history_top]) % (sizeof(s_info.history) / sizeof(s_info.history[0]));
1074 static inline const char *get_command(int idx)
1076 idx = s_info.history_top + idx;
1078 idx += (sizeof(s_info.history) / sizeof(s_info.history[0]));
1080 return s_info.history[idx];
1083 static inline void do_command(const char *cmd)
1085 /* Skip the first spaces */
1086 while (*cmd && *cmd == ' ') cmd++;
1088 if (strlen(cmd) && *cmd != '#') {
1089 if (!strncasecmp(cmd, "exit", 4) || !strncasecmp(cmd, "quit", 4)) {
1090 ecore_main_loop_quit();
1091 } else if (!strncasecmp(cmd, "set ", 4)) {
1092 if (do_set(cmd) == 0)
1094 } else if (!strncasecmp(cmd, "stat ", 5)) {
1096 } else if (!strncasecmp(cmd, "get ", 4)) {
1097 if (do_get(cmd) == 0)
1099 } else if (!strncasecmp(cmd, "ls", 2)) {
1100 if (do_ls(cmd) == 0)
1102 } else if (!strncasecmp(cmd, "cd", 2)) {
1103 if (do_cd(cmd) == 0)
1105 } else if (!strncasecmp(cmd, "rm", 2)) {
1106 if (do_rm(cmd) == 0)
1108 } else if (!strncasecmp(cmd, "sh ", 3)) {
1110 } else if (!strncasecmp(cmd, "x ", 2)) {
1121 static Eina_Bool input_cb(void *data, Ecore_Fd_Handler *fd_handler)
1124 static char cmd_buffer[256];
1128 const char escape_str[] = { 0x1b, 0x5b, 0x0 };
1129 const char *escape_ptr = escape_str;
1133 fd = ecore_main_fd_handler_fd_get(fd_handler);
1135 printf("FD is not valid: %d\n", fd);
1136 return ECORE_CALLBACK_CANCEL;
1139 fd = s_info.input_fd;
1144 * Using this routine, we can implement the command recommend algorithm.
1145 * When a few more characters are matched with history of command, we can show it to user
1146 * Then the user will choose one or write new command
1150 while ((ret = read(fd, &ch, sizeof(ch))) == sizeof(ch)) {
1151 if (*escape_ptr == '\0') {
1155 printf("%s2K%s1G", escape_str, escape_str);
1156 tmp = get_command(--s_info.history_idx);
1158 s_info.history_idx = 0;
1159 cmd_buffer[0] = '\0';
1162 strcpy(cmd_buffer, tmp);
1163 idx = strlen(cmd_buffer);
1167 case 0x42: /* DOWN */
1168 if (s_info.history_idx >= 0)
1171 printf("%s2K%s1G", escape_str, escape_str);
1172 tmp = get_command(++s_info.history_idx);
1173 if (s_info.history_idx == 0) {
1174 s_info.history_idx = 0;
1175 cmd_buffer[0] = '\0';
1178 strcpy(cmd_buffer, tmp);
1179 idx = strlen(cmd_buffer);
1183 case 0x43: /* RIGHT */
1185 case 0x44: /* LEFT */
1191 escape_ptr = escape_str;
1193 } else if (ch == *escape_ptr) {
1199 case 0x08: /* BKSP */
1200 cmd_buffer[idx] = '\0';
1203 cmd_buffer[idx] = ' ';
1208 cmd_buffer[idx] = '\0';
1212 case 0x09: /* TAB */
1213 if (!s_info.quick_search_node) {
1214 s_info.quick_search_node = node_child(s_info.curdir);
1215 s_info.quick_idx = idx;
1217 s_info.quick_search_node = node_next_sibling(s_info.quick_search_node);
1218 idx = s_info.quick_idx;
1221 if (!s_info.quick_search_node)
1224 printf("%s2K%s1G", escape_str, escape_str);
1225 strcpy(cmd_buffer + idx, node_name(s_info.quick_search_node));
1226 idx += strlen(node_name(s_info.quick_search_node));
1231 cmd_buffer[idx] = '\0';
1233 if (s_info.input_fd == STDIN_FILENO || s_info.verbose)
1234 putc((int)'\n', stdout);
1235 do_command(cmd_buffer);
1236 put_command(cmd_buffer);
1237 memset(cmd_buffer, 0, sizeof(cmd_buffer));
1238 s_info.history_idx = 0;
1239 s_info.quick_search_node = NULL;
1241 /* Make a main loop processing for command handling */
1242 return ECORE_CALLBACK_RENEW;
1244 cmd_buffer[idx++] = ch;
1246 if (s_info.input_fd == STDIN_FILENO || s_info.verbose)
1247 putc((int)ch, stdout);
1249 if (idx == sizeof(cmd_buffer) - 1) {
1250 cmd_buffer[idx] = '\0';
1251 printf("\nCommand buffer is overflow: %s\n", cmd_buffer);
1258 if (ret < 0 && !fd_handler)
1259 ecore_main_loop_quit();
1261 return ECORE_CALLBACK_RENEW;
1264 static void processing_line_buffer(const char *buffer)
1267 char slavename[256];
1285 struct package *pkginfo;
1286 struct instance *instinfo;
1287 struct slave *slaveinfo;
1290 switch (s_info.cmd) {
1292 if (sscanf(buffer, "%d %255[^ ] %255[^ ] %255[^ ] %d %d %d", &pid, slavename, pkgname, abi, &refcnt, &fault_count, &list_count) != 7) {
1293 printf("Invalid format : [%s]\n", buffer);
1297 node = node_find(s_info.targetdir, pkgname);
1299 pkginfo = calloc(1, sizeof(*pkginfo));
1301 printf("Error: %s\n", strerror(errno));
1305 pkginfo->pkgid = strdup("conf.file");
1306 if (!pkginfo->pkgid)
1307 printf("Error: %s\n", strerror(errno));
1309 pkginfo->primary = 1;
1311 node = node_create(s_info.targetdir, pkgname, NODE_DIR);
1313 free(pkginfo->pkgid);
1315 printf("Failed to create a new node (%s)\n", pkgname);
1319 node_set_mode(node, NODE_READ | NODE_EXEC);
1320 node_set_data(node, pkginfo);
1322 pkginfo = node_data(node);
1324 printf("Package info is inavlid\n");
1328 free(pkginfo->slavename);
1331 pkginfo->slavename = NULL;
1332 pkginfo->abi = NULL;
1335 node_set_age(node, s_info.age);
1337 pkginfo->slavename = strdup(slavename);
1338 if (!pkginfo->slavename)
1339 printf("Error: %s\n", strerror(errno));
1341 pkginfo->abi = strdup(abi);
1343 printf("Error: %s\n", strerror(errno));
1346 pkginfo->refcnt = refcnt;
1347 pkginfo->fault_count = fault_count;
1348 pkginfo->inst_count = list_count;
1351 if (sscanf(buffer, "%d %[^ ] %[^ ] %[^ ] %d %d %d %[^ ] %d %d %lf", &pid, slavename, pkgname, abi, &secured, &refcnt, &fault_count, state, &loaded_inst, &loaded_pkg, &ttl) != 11) {
1352 printf("Invalid format : [%s]\n", buffer);
1355 node = node_find(s_info.targetdir, slavename);
1357 slaveinfo = calloc(1, sizeof(*slaveinfo));
1359 printf("Error: %s\n", strerror(errno));
1363 node = node_create(s_info.targetdir, slavename, NODE_DIR);
1369 node_set_mode(node, NODE_READ | NODE_EXEC);
1370 node_set_data(node, slaveinfo);
1372 slaveinfo = node_data(node);
1375 node_set_age(node, s_info.age);
1377 free(slaveinfo->pkgname);
1378 free(slaveinfo->abi);
1379 free(slaveinfo->state);
1381 slaveinfo->pkgname = strdup(pkgname);
1382 if (!slaveinfo->pkgname)
1383 printf("Error: %s\n", strerror(errno));
1385 slaveinfo->abi = strdup(abi);
1386 if (!slaveinfo->abi)
1387 printf("Error: %s\n", strerror(errno));
1389 slaveinfo->state = strdup(state);
1390 if (!slaveinfo->state)
1391 printf("Error: %s\n", strerror(errno));
1393 slaveinfo->pid = pid;
1394 slaveinfo->secured = secured;
1395 slaveinfo->refcnt = refcnt;
1396 slaveinfo->fault_count = fault_count;
1397 slaveinfo->loaded_inst = loaded_inst;
1398 slaveinfo->loaded_pkg = loaded_pkg;
1399 slaveinfo->ttl = ttl;
1402 if (sscanf(buffer, "%[^ ] %[^ ] %[^ ] %lf %[^ ] %d %d", inst_id, cluster, category, &period, state, &width, &height) != 7) {
1403 printf("Invalid format : [%s]\n", buffer);
1407 for (i = strlen(inst_id); i > 0 && inst_id[i] != '/'; i--);
1408 i += (inst_id[i] == '/');
1410 node = node_find(s_info.targetdir, inst_id + i);
1412 instinfo = calloc(1, sizeof(*instinfo));
1414 printf("Error: %s\n", strerror(errno));
1418 node = node_create(s_info.targetdir, inst_id + i, NODE_FILE);
1424 node_set_mode(node, NODE_READ | NODE_WRITE);
1425 node_set_data(node, instinfo);
1427 instinfo = node_data(node);
1430 node_set_age(node, s_info.age);
1433 free(instinfo->cluster);
1434 free(instinfo->category);
1435 free(instinfo->state);
1437 instinfo->id = strdup(inst_id);
1439 printf("Error: %s\n", strerror(errno));
1441 instinfo->cluster = strdup(cluster);
1442 if (!instinfo->cluster)
1443 printf("Error: %s\n", strerror(errno));
1445 instinfo->category = strdup(category);
1446 if (!instinfo->category)
1447 printf("Error: %s\n", strerror(errno));
1449 instinfo->state = strdup(state);
1450 if (!instinfo->state)
1451 printf("Error: %s\n", strerror(errno));
1453 instinfo->period = period;
1454 instinfo->width = width;
1455 instinfo->height = height;
1458 sscanf(buffer, "%d", &i);
1459 printf("%s\n", strerror(i));
1460 printf("Result: %d\n", i);
1463 sscanf(buffer, "%d", &i);
1464 printf("Result: %d\n", i);
1467 sscanf(buffer, "%d", &i);
1468 printf("Result: %d\n", i);
1475 static inline void do_line_command(void)
1477 switch (s_info.cmd) {
1499 static Eina_Bool read_cb(void *data, Ecore_Fd_Handler *fd_handler)
1502 static char *line_buffer = NULL;
1503 static int line_index = 0;
1504 static int bufsz = 256;
1507 fd = ecore_main_fd_handler_fd_get(fd_handler);
1509 printf("FD is not valid: %d\n", fd);
1510 return ECORE_CALLBACK_CANCEL;
1513 if (read(fd, &ch, sizeof(ch)) != sizeof(ch)) {
1514 printf("Error: %s\n", strerror(errno));
1515 return ECORE_CALLBACK_CANCEL;
1520 line_buffer = malloc(bufsz);
1522 printf("Error: %s\n", strerror(errno));
1523 return ECORE_CALLBACK_CANCEL;
1527 if (ch == '\n') { /* End of a line */
1528 if (line_index == bufsz - 1) {
1530 new_buf = realloc(line_buffer, bufsz + 2);
1532 printf("Error: %s\n", strerror(errno));
1537 return ECORE_CALLBACK_CANCEL;
1540 line_buffer = new_buf;
1543 line_buffer[line_index] = '\0';
1545 if (!strcmp(line_buffer, "EOD")) {
1549 processing_line_buffer(line_buffer);
1559 line_buffer[line_index++] = ch;
1560 if (line_index == bufsz - 1) {
1562 new_buf = realloc(line_buffer, bufsz);
1564 printf("Error: %s\n", strerror(errno));
1569 return ECORE_CALLBACK_CANCEL;
1572 line_buffer = new_buf;
1576 return ECORE_CALLBACK_RENEW;
1579 static int ret_cb(pid_t pid, int handle, const struct packet *packet, void *data)
1581 const char *fifo_name;
1584 if (packet_get(packet, "si", &fifo_name, &ret) != 2) {
1585 printf("Invalid packet\n");
1590 printf("Returns %d\n", ret);
1594 printf("FIFO: %s\n", fifo_name);
1596 s_info.fifo_handle = open(fifo_name, O_RDONLY | O_NONBLOCK);
1597 if (s_info.fifo_handle < 0) {
1598 printf("Error: %s\n", strerror(errno));
1599 s_info.fifo_handle = -EINVAL;
1600 ecore_main_loop_quit();
1604 s_info.fd_handler = ecore_main_fd_handler_add(s_info.fifo_handle, ECORE_FD_READ, read_cb, NULL, NULL, NULL);
1605 if (!s_info.fd_handler) {
1606 printf("Failed to add a fd handler\n");
1607 close(s_info.fifo_handle);
1608 s_info.fifo_handle = -EINVAL;
1609 ecore_main_loop_quit();
1615 if (s_info.input_fd == STDIN_FILENO) {
1616 if (fcntl(s_info.input_fd, F_SETFL, O_NONBLOCK) < 0)
1617 printf("Error: %s\n", strerror(errno));
1619 s_info.in_handler = ecore_main_fd_handler_add(s_info.input_fd, ECORE_FD_READ, input_cb, NULL, NULL, NULL);
1620 if (!s_info.in_handler) {
1621 printf("Failed to add a input handler\n");
1622 ecore_main_loop_quit();
1630 static int disconnected_cb(int handle, void *data)
1632 printf("Disconnected\n");
1633 ecore_main_loop_quit();
1637 static int connected_cb(int handle, void *data)
1639 struct packet *packet;
1641 printf("Connected\n");
1643 packet = packet_create("liveinfo_hello", "d", 0.0f);
1645 printf("Failed to build a packet for hello\n");
1646 com_core_packet_client_fini(s_info.fd);
1647 s_info.fd = -EINVAL;
1653 if (com_core_packet_async_send(s_info.fd, packet, 0.0f, ret_cb, NULL) < 0) {
1654 printf("Failed to send a packet hello\n");
1655 packet_destroy(packet);
1656 com_core_packet_client_fini(s_info.fd);
1657 s_info.fd = -EINVAL;
1661 packet_destroy(packet);
1665 int main(int argc, char *argv[])
1667 struct termios ttystate;
1668 static struct method s_table[] = {
1674 static struct option long_options[] = {
1675 { "batchmode", required_argument, 0, 'b' },
1676 { "help", no_argument, 0, 'h' },
1677 { "verbose", required_argument, 0, 'v' },
1684 c = getopt_long(argc, argv, "b:hv:", long_options, &option_index);
1687 if (!optarg || !*optarg) {
1688 printf("Invalid argument\n");
1693 if (s_info.input_fd != STDIN_FILENO) {
1694 /* Close the previously, opened file */
1695 close(s_info.input_fd);
1698 s_info.input_fd = open(optarg, O_RDONLY);
1699 if (s_info.input_fd < 0) {
1700 printf("Unable to access %s (%s)\n", optarg, strerror(errno));
1708 if (!optarg || !*optarg) {
1709 printf("Invalid argument\n");
1714 s_info.verbose = !strcmp(optarg, "true");
1724 com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1725 com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1726 livebox_service_init();
1728 s_info.fd = com_core_packet_client_init(SOCKET_FILE, 0, s_table);
1729 if (s_info.fd < 0) {
1730 printf("Failed to make a connection\n");
1734 if (s_info.input_fd == STDIN_FILENO) {
1735 printf("Type your command on below empty line\n");
1737 if (tcgetattr(s_info.input_fd, &ttystate) < 0) {
1738 printf("Error: %s\n", strerror(errno));
1740 ttystate.c_lflag &= ~(ICANON | ECHO);
1741 ttystate.c_cc[VMIN] = 1;
1743 if (tcsetattr(s_info.input_fd, TCSANOW, &ttystate) < 0)
1744 printf("Error: %s\n", strerror(errno));
1747 printf("Batch mode enabled\n");
1750 if (setvbuf(stdout, (char *)NULL, _IONBF, 0) != 0)
1751 printf("Error: %s\n", strerror(errno));
1755 ecore_main_loop_begin();
1758 livebox_service_fini();
1760 if (s_info.fd > 0) {
1761 com_core_packet_client_fini(s_info.fd);
1762 s_info.fd = -EINVAL;
1765 if (s_info.fd_handler) {
1766 ecore_main_fd_handler_del(s_info.fd_handler);
1767 s_info.fd_handler = NULL;
1770 if (s_info.input_fd == STDIN_FILENO) {
1771 ttystate.c_lflag |= ICANON | ECHO;
1772 if (tcsetattr(s_info.input_fd, TCSANOW, &ttystate) < 0)
1773 printf("Error: %s\n", strerror(errno));
1775 close(s_info.input_fd);
1778 if (s_info.fifo_handle > 0) {
1779 close(s_info.fifo_handle);
1780 s_info.fifo_handle = -EINVAL;
1783 if (s_info.in_handler) {
1784 ecore_main_fd_handler_del(s_info.in_handler);
1785 s_info.in_handler = NULL;
1789 putc((int)'\n', stdout);