2 * Copyright 2012 Samsung Electronics Co., Ltd
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
8 * http://www.tizenopensource.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);
441 com_core_packet_send_only(s_info.fd, packet);
442 packet_destroy(packet);
443 s_info.cmd = INST_CTRL;
447 static void send_inst_list(const char *pkgname)
449 struct packet *packet;
451 if (s_info.cmd != NOP) {
452 printf("Previous command is not finished\n");
456 packet = packet_create_noack("inst_list", "s", pkgname);
458 printf("Failed to create a packet\n");
462 com_core_packet_send_only(s_info.fd, packet);
463 packet_destroy(packet);
464 s_info.cmd = INST_LIST;
468 static inline void help(void)
470 printf("liveinfo - Livebox utility\n");
471 printf("------------------------------ [Option] ------------------------------\n");
472 printf("-b Batch mode\n");
473 printf("------------------------------ [Command list] ------------------------------\n");
474 printf("
\e[32mcd [PATH] - Change directory
\e[0m\n");
475 printf("
\e[32mls [ | PATH] - List up content as a file
\e[0m\n");
476 printf("
\e[32mrm [PKG_ID|INST_ID] - Delete package or instance
\e[0m\n");
477 printf("
\e[32mstat [path] - Display the information of given path
\e[0m\n");
478 printf("
\e[32mset [debug] [on|off] Set the control variable of master provider
\e[0m\n");
479 printf("
\e[32mx damage Pix x y w h - Create damage event for given pixmap
\e[0m\n");
480 printf("
\e[32mx move Pix x y - Move the window
\e[0m\n");
481 printf("
\e[32mx resize Pix w h - Resize the window
\e[0m\n");
482 printf("
\e[32mx map Pix - Show the window
\e[0m\n");
483 printf("
\e[32mx unmap Pix - Hide the window
\e[0m\n");
484 printf("
\e[32msh [command] Execute shell command, [command] should be abspath
\e[0m\n");
485 printf("
\e[32mexit -
\e[0m\n");
486 printf("
\e[32mquit -
\e[0m\n");
487 printf("----------------------------------------------------------------------------\n");
490 static inline void init_directory(void)
493 s_info.rootdir = node_create(NULL, NULL, NODE_DIR);
496 node_set_mode(s_info.rootdir, NODE_READ | NODE_EXEC);
498 node = node_create(s_info.rootdir, "provider", NODE_DIR);
500 node_destroy(s_info.rootdir);
501 s_info.rootdir = NULL;
504 node_set_mode(node, NODE_READ | NODE_EXEC);
506 node = node_create(s_info.rootdir, "package", NODE_DIR);
508 node_destroy(node_child(s_info.rootdir));
509 node_destroy(s_info.rootdir);
510 s_info.rootdir = NULL;
513 node_set_mode(node, NODE_READ | NODE_EXEC);
515 s_info.curdir = s_info.rootdir;
519 static inline void fini_directory(void)
523 static inline struct node *update_target_dir(const char *cmd)
527 node = (*cmd == '/') ? s_info.rootdir : s_info.curdir;
528 node = node_find(node, cmd);
533 static int get_token(const char *src, char *out)
536 while (*src && *src == ' ') src++;
541 while (*src && *src != ' ') {
550 static inline int do_stat(const char *cmd)
565 while (*cmd && *cmd == ' ') cmd++;
568 printf("Invalid argument\n");
572 node = node_find(*cmd == '/' ? s_info.rootdir : s_info.curdir, cmd);
574 printf("Invalid path\n");
580 parent = node_parent(node);
582 if (!node_name(parent)) {
583 printf("%s has no info\n", node_name(node));
585 } else if (!strcmp(node_name(parent), "package")) {
586 type = (i == 0) ? PKG : PKG_INSTANCE;
588 } else if (!strcmp(node_name(parent), "provider")){
589 type = (i == 0) ? PROVIDER : PROVIDER_INSTANCE;
593 parent = node_parent(parent);
596 printf("%s is invalid path\n", node_name(node));
603 tmp = livebox_service_i18n_name(node_name(node), NULL);
604 printf("Name: %s (", tmp);
607 i = livebox_service_is_enabled(node_name(node));
608 printf("%s)\n", i ? "enabled" : "disabled");
610 tmp = livebox_service_i18n_icon(node_name(node), NULL);
611 printf("Icon: %s\n", tmp);
614 tmp = livebox_service_provider_name(node_name(node));
615 printf("Provider: %s (content:", tmp);
618 tmp = livebox_service_content(node_name(node));
619 printf("%s)\n", tmp);
622 tmp = livebox_service_lb_script_path(node_name(node));
623 printf("LB Script: %s (", tmp);
626 tmp = livebox_service_lb_script_group(node_name(node));
627 printf("%s)\n", tmp);
630 tmp = livebox_service_pd_script_path(node_name(node));
631 printf("PD Script: %s (", tmp);
634 tmp = livebox_service_pd_script_group(node_name(node));
635 printf("%s)\n", tmp);
638 i = livebox_service_mouse_event(node_name(node));
639 printf("Mouse event: %s\n", i ? "enabled" : "disabled");
641 i = livebox_service_touch_effect(node_name(node));
642 printf("Touch effect: %s\n", i ? "enabled" : "disabled");
645 printf("Not supported yet\n");
648 printf("Not supported yet\n");
650 case PROVIDER_INSTANCE:
651 printf("Not supported yet\n");
654 printf("Not supported yet\n");
657 printf("Invalid type\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) && *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)) {
881 static inline int do_capture(Display *disp, Pixmap id, const char *filename)
892 screen = DefaultScreenOfDisplay(disp);
893 visual = DefaultVisualOfScreen(screen);
895 if (get_pixmap_size(disp, id, NULL, NULL, &w, &h) < 0) {
896 printf("Failed to get size of a pixmap\n");
900 printf("Pixmap size: %dx%d\n", w, h);
901 bufsz = w * h * sizeof(int);
903 si.shmid = shmget(IPC_PRIVATE, bufsz, IPC_CREAT | 0666);
905 printf("shmget: %s\n", strerror(errno));
910 si.shmaddr = shmat(si.shmid, NULL, 0);
911 if (si.shmaddr == (void *)-1) {
913 if (shmctl(si.shmid, IPC_RMID, 0) < 0)
914 printf("shmctl: %s\n", strerror(errno));
921 * Use the 24 bits Pixmap for Video player
923 xim = XShmCreateImage(disp, visual, 24 /* (depth << 3) */, ZPixmap, NULL, &si, w, h);
925 if (shmdt(si.shmaddr) < 0)
926 printf("shmdt: %s\n", strerror(errno));
928 if (shmctl(si.shmid, IPC_RMID, 0) < 0)
929 printf("shmctl: %s\n", strerror(errno));
934 xim->data = si.shmaddr;
935 XShmAttach(disp, &si);
937 XShmGetImage(disp, id, xim, 0, 0, 0xFFFFFFFF);
940 fd = open(filename, O_CREAT | O_RDWR);
942 if (write(fd, xim->data, bufsz) != bufsz)
943 printf("Data is not fully written\n");
947 printf("Error: %sn\n", strerror(errno));
950 XShmDetach(disp, &si);
953 if (shmdt(si.shmaddr) < 0)
954 printf("shmdt: %s\n", strerror(errno));
956 if (shmctl(si.shmid, IPC_RMID, 0) < 0)
957 printf("shmctl: %s\n", strerror(errno));
962 static inline void do_x(const char *cmd)
968 while (*cmd && *cmd == ' ') cmd++;
972 disp = XOpenDisplay(NULL);
974 printf("Failed to connect to the X\n");
978 if (!strncasecmp(cmd, "damage ", 7)) {
981 XserverRegion region;
986 if (sscanf(cmd, "%u %d %d %d %d", &winId, &x, &y, &w, &h) != 5) {
987 printf("Invalid argument\nx damage WINID_DEC X Y W H\n");
994 region = XFixesCreateRegion(disp, &rect, 1);
995 XDamageAdd(disp, winId, region);
996 XFixesDestroyRegion(disp, region);
999 printf("Damage: %u %d %d %d %d\n", winId, x, y, w, h);
1000 } else if (!strncasecmp(cmd, "capture ", 8)) {
1006 if (sscanf(cmd, "%u %255[^ ]", &winId, filename) != 2) {
1007 printf("Invalid argument\nx capture WINID_DEC FILENAME (%s)\n", cmd);
1010 if (do_capture(disp, winId, filename) == 0)
1011 printf("Captured: %s\n", filename);
1012 } else if (!strncasecmp(cmd, "resize ", 7)) {
1019 if (sscanf(cmd, "%u %d %d", &winId, &w, &h) != 3) {
1020 printf("Invalid argument\nx resize WINID_DEC W H\n");
1024 XResizeWindow(disp, winId, w, h);
1025 printf("Resize: %u %d %d\n", winId, w, h);
1026 } else if (!strncasecmp(cmd, "move ", 5)) {
1032 if (sscanf(cmd, "%u %d %d", &winId, &x, &y) != 3) {
1033 printf("Invalid argument\nx move WINID_DEC X Y\n");
1037 XMoveWindow(disp, winId, x, y);
1038 printf("Move: %u %d %d\n", winId, x, y);
1039 } else if (!strncasecmp(cmd, "map ", 4)) {
1042 if (sscanf(cmd, "%u", &winId) != 1) {
1043 printf("Invalid argument\nx map WINID_DEC\n");
1046 XMapRaised(disp, winId);
1047 printf("Map: %u\n", winId);
1048 } else if (!strncasecmp(cmd, "unmap ", 6)) {
1051 if (sscanf(cmd, "%u", &winId) != 1) {
1052 printf("Invalid argument\nx unmap WINID_DEC\n");
1055 XUnmapWindow(disp, winId);
1056 printf("Unmap: %u\n", winId);
1058 printf("Unknown command\n");
1061 XCloseDisplay(disp);
1064 static inline void put_command(const char *cmd)
1066 if (s_info.history[s_info.history_top]) {
1067 free(s_info.history[s_info.history_top]);
1068 s_info.history[s_info.history_top] = NULL;
1071 s_info.history[s_info.history_top] = strdup(cmd);
1072 s_info.history_top = (s_info.history_top + !!s_info.history[s_info.history_top]) % (sizeof(s_info.history) / sizeof(s_info.history[0]));
1075 static inline const char *get_command(int idx)
1077 idx = s_info.history_top + idx;
1079 idx += (sizeof(s_info.history) / sizeof(s_info.history[0]));
1081 return s_info.history[idx];
1084 static inline void do_command(const char *cmd)
1086 /* Skip the first spaces */
1087 while (*cmd && *cmd == ' ') cmd++;
1089 if (strlen(cmd) && *cmd != '#') {
1090 if (!strncasecmp(cmd, "exit", 4) || !strncasecmp(cmd, "quit", 4)) {
1091 ecore_main_loop_quit();
1092 } else if (!strncasecmp(cmd, "set ", 4)) {
1093 if (do_set(cmd) == 0)
1095 } else if (!strncasecmp(cmd, "stat ", 5)) {
1097 } else if (!strncasecmp(cmd, "get ", 4)) {
1098 if (do_get(cmd) == 0)
1100 } else if (!strncasecmp(cmd, "ls", 2)) {
1101 if (do_ls(cmd) == 0)
1103 } else if (!strncasecmp(cmd, "cd", 2)) {
1104 if (do_cd(cmd) == 0)
1106 } else if (!strncasecmp(cmd, "rm", 2)) {
1107 if (do_rm(cmd) == 0)
1109 } else if (!strncasecmp(cmd, "sh ", 3)) {
1111 } else if (!strncasecmp(cmd, "x ", 2)) {
1122 static Eina_Bool input_cb(void *data, Ecore_Fd_Handler *fd_handler)
1125 static char cmd_buffer[256];
1129 const char escape_str[] = { 0x1b, 0x5b, 0x0 };
1130 const char *escape_ptr = escape_str;
1134 fd = ecore_main_fd_handler_fd_get(fd_handler);
1136 printf("FD is not valid: %d\n", fd);
1137 return ECORE_CALLBACK_CANCEL;
1140 fd = s_info.input_fd;
1145 * Using this routine, we can implement the command recommend algorithm.
1146 * When a few more characters are matched with history of command, we can show it to user
1147 * Then the user will choose one or write new command
1151 while ((ret = read(fd, &ch, sizeof(ch))) == sizeof(ch)) {
1152 if (*escape_ptr == '\0') {
1156 printf("%s2K%s1G", escape_str, escape_str);
1157 tmp = get_command(--s_info.history_idx);
1159 s_info.history_idx = 0;
1160 cmd_buffer[0] = '\0';
1163 strcpy(cmd_buffer, tmp);
1164 idx = strlen(cmd_buffer);
1168 case 0x42: /* DOWN */
1169 if (s_info.history_idx >= 0)
1172 printf("%s2K%s1G", escape_str, escape_str);
1173 tmp = get_command(++s_info.history_idx);
1174 if (s_info.history_idx == 0) {
1175 s_info.history_idx = 0;
1176 cmd_buffer[0] = '\0';
1179 strcpy(cmd_buffer, tmp);
1180 idx = strlen(cmd_buffer);
1184 case 0x43: /* RIGHT */
1186 case 0x44: /* LEFT */
1192 escape_ptr = escape_str;
1194 } else if (ch == *escape_ptr) {
1200 case 0x08: /* BKSP */
1201 cmd_buffer[idx] = '\0';
1204 cmd_buffer[idx] = ' ';
1209 cmd_buffer[idx] = '\0';
1213 case 0x09: /* TAB */
1214 if (!s_info.quick_search_node) {
1215 s_info.quick_search_node = node_child(s_info.curdir);
1216 s_info.quick_idx = idx;
1218 s_info.quick_search_node = node_next_sibling(s_info.quick_search_node);
1219 idx = s_info.quick_idx;
1222 if (!s_info.quick_search_node)
1225 printf("%s2K%s1G", escape_str, escape_str);
1226 strcpy(cmd_buffer + idx, node_name(s_info.quick_search_node));
1227 idx += strlen(node_name(s_info.quick_search_node));
1232 cmd_buffer[idx] = '\0';
1234 if (s_info.input_fd == STDIN_FILENO || s_info.verbose)
1235 putc((int)'\n', stdout);
1236 do_command(cmd_buffer);
1237 put_command(cmd_buffer);
1238 memset(cmd_buffer, 0, sizeof(cmd_buffer));
1239 s_info.history_idx = 0;
1240 s_info.quick_search_node = NULL;
1242 /* Make a main loop processing for command handling */
1243 return ECORE_CALLBACK_RENEW;
1245 cmd_buffer[idx++] = ch;
1247 if (s_info.input_fd == STDIN_FILENO || s_info.verbose)
1248 putc((int)ch, stdout);
1250 if (idx == sizeof(cmd_buffer) - 1) {
1251 cmd_buffer[idx] = '\0';
1252 printf("\nCommand buffer is overflow: %s\n", cmd_buffer);
1259 if (ret < 0 && !fd_handler)
1260 ecore_main_loop_quit();
1262 return ECORE_CALLBACK_RENEW;
1265 static void processing_line_buffer(const char *buffer)
1268 char slavename[256];
1286 struct package *pkginfo;
1287 struct instance *instinfo;
1288 struct slave *slaveinfo;
1291 switch (s_info.cmd) {
1293 if (sscanf(buffer, "%d %255[^ ] %255[^ ] %255[^ ] %d %d %d", &pid, slavename, pkgname, abi, &refcnt, &fault_count, &list_count) != 7) {
1294 printf("Invalid format : [%s]\n", buffer);
1298 node = node_find(s_info.targetdir, pkgname);
1300 pkginfo = calloc(1, sizeof(*pkginfo));
1302 printf("Error: %s\n", strerror(errno));
1306 pkginfo->pkgid = strdup("conf.file");
1307 if (!pkginfo->pkgid)
1308 printf("Error: %s\n", strerror(errno));
1310 pkginfo->primary = 1;
1312 node = node_create(s_info.targetdir, pkgname, NODE_DIR);
1314 free(pkginfo->pkgid);
1316 printf("Failed to create a new node (%s)\n", pkgname);
1320 node_set_mode(node, NODE_READ | NODE_EXEC);
1321 node_set_data(node, pkginfo);
1323 pkginfo = node_data(node);
1325 printf("Package info is inavlid\n");
1329 free(pkginfo->slavename);
1332 pkginfo->slavename = NULL;
1333 pkginfo->abi = NULL;
1336 node_set_age(node, s_info.age);
1338 pkginfo->slavename = strdup(slavename);
1339 if (!pkginfo->slavename)
1340 printf("Error: %s\n", strerror(errno));
1342 pkginfo->abi = strdup(abi);
1344 printf("Error: %s\n", strerror(errno));
1347 pkginfo->refcnt = refcnt;
1348 pkginfo->fault_count = fault_count;
1349 pkginfo->inst_count = list_count;
1352 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) {
1353 printf("Invalid format : [%s]\n", buffer);
1356 node = node_find(s_info.targetdir, slavename);
1358 slaveinfo = calloc(1, sizeof(*slaveinfo));
1360 printf("Error: %s\n", strerror(errno));
1364 node = node_create(s_info.targetdir, slavename, NODE_DIR);
1370 node_set_mode(node, NODE_READ | NODE_EXEC);
1371 node_set_data(node, slaveinfo);
1373 slaveinfo = node_data(node);
1376 node_set_age(node, s_info.age);
1378 free(slaveinfo->pkgname);
1379 free(slaveinfo->abi);
1380 free(slaveinfo->state);
1382 slaveinfo->pkgname = strdup(pkgname);
1383 if (!slaveinfo->pkgname)
1384 printf("Error: %s\n", strerror(errno));
1386 slaveinfo->abi = strdup(abi);
1387 if (!slaveinfo->abi)
1388 printf("Error: %s\n", strerror(errno));
1390 slaveinfo->state = strdup(state);
1391 if (!slaveinfo->state)
1392 printf("Error: %s\n", strerror(errno));
1394 slaveinfo->pid = pid;
1395 slaveinfo->secured = secured;
1396 slaveinfo->refcnt = refcnt;
1397 slaveinfo->fault_count = fault_count;
1398 slaveinfo->loaded_inst = loaded_inst;
1399 slaveinfo->loaded_pkg = loaded_pkg;
1400 slaveinfo->ttl = ttl;
1403 if (sscanf(buffer, "%[^ ] %[^ ] %[^ ] %lf %[^ ] %d %d", inst_id, cluster, category, &period, state, &width, &height) != 7) {
1404 printf("Invalid format : [%s]\n", buffer);
1408 for (i = strlen(inst_id); i > 0 && inst_id[i] != '/'; i--);
1409 i += (inst_id[i] == '/');
1411 node = node_find(s_info.targetdir, inst_id + i);
1413 instinfo = calloc(1, sizeof(*instinfo));
1415 printf("Error: %s\n", strerror(errno));
1419 node = node_create(s_info.targetdir, inst_id + i, NODE_FILE);
1425 node_set_mode(node, NODE_READ | NODE_WRITE);
1426 node_set_data(node, instinfo);
1428 instinfo = node_data(node);
1431 node_set_age(node, s_info.age);
1434 free(instinfo->cluster);
1435 free(instinfo->category);
1436 free(instinfo->state);
1438 instinfo->id = strdup(inst_id);
1440 printf("Error: %s\n", strerror(errno));
1442 instinfo->cluster = strdup(cluster);
1443 if (!instinfo->cluster)
1444 printf("Error: %s\n", strerror(errno));
1446 instinfo->category = strdup(category);
1447 if (!instinfo->category)
1448 printf("Error: %s\n", strerror(errno));
1450 instinfo->state = strdup(state);
1451 if (!instinfo->state)
1452 printf("Error: %s\n", strerror(errno));
1454 instinfo->period = period;
1455 instinfo->width = width;
1456 instinfo->height = height;
1459 sscanf(buffer, "%d", &i);
1460 printf("%s\n", strerror(i));
1461 printf("Result: %d\n", i);
1464 sscanf(buffer, "%d", &i);
1465 printf("Result: %d\n", i);
1468 sscanf(buffer, "%d", &i);
1469 printf("Result: %d\n", i);
1476 static inline void do_line_command(void)
1478 switch (s_info.cmd) {
1500 static Eina_Bool read_cb(void *data, Ecore_Fd_Handler *fd_handler)
1503 static char *line_buffer = NULL;
1504 static int line_index = 0;
1505 static int bufsz = 256;
1508 fd = ecore_main_fd_handler_fd_get(fd_handler);
1510 printf("FD is not valid: %d\n", fd);
1511 return ECORE_CALLBACK_CANCEL;
1514 if (read(fd, &ch, sizeof(ch)) != sizeof(ch)) {
1515 printf("Error: %s\n", strerror(errno));
1516 return ECORE_CALLBACK_CANCEL;
1521 line_buffer = malloc(bufsz);
1523 printf("Error: %s\n", strerror(errno));
1524 return ECORE_CALLBACK_CANCEL;
1528 if (ch == '\n') { /* End of a line */
1529 if (line_index == bufsz - 1) {
1531 new_buf = realloc(line_buffer, bufsz + 2);
1533 printf("Error: %s\n", strerror(errno));
1538 return ECORE_CALLBACK_CANCEL;
1541 line_buffer = new_buf;
1544 line_buffer[line_index] = '\0';
1546 if (!strcmp(line_buffer, "EOD")) {
1550 processing_line_buffer(line_buffer);
1560 line_buffer[line_index++] = ch;
1561 if (line_index == bufsz - 1) {
1563 new_buf = realloc(line_buffer, bufsz);
1565 printf("Error: %s\n", strerror(errno));
1570 return ECORE_CALLBACK_CANCEL;
1573 line_buffer = new_buf;
1577 return ECORE_CALLBACK_RENEW;
1580 static int ret_cb(pid_t pid, int handle, const struct packet *packet, void *data)
1582 const char *fifo_name;
1585 if (packet_get(packet, "si", &fifo_name, &ret) != 2) {
1586 printf("Invalid packet\n");
1591 printf("Returns %d\n", ret);
1595 printf("FIFO: %s\n", fifo_name);
1597 s_info.fifo_handle = open(fifo_name, O_RDONLY | O_NONBLOCK);
1598 if (s_info.fifo_handle < 0) {
1599 printf("Error: %s\n", strerror(errno));
1600 s_info.fifo_handle = -EINVAL;
1601 ecore_main_loop_quit();
1605 s_info.fd_handler = ecore_main_fd_handler_add(s_info.fifo_handle, ECORE_FD_READ, read_cb, NULL, NULL, NULL);
1606 if (!s_info.fd_handler) {
1607 printf("Failed to add a fd handler\n");
1608 close(s_info.fifo_handle);
1609 s_info.fifo_handle = -EINVAL;
1610 ecore_main_loop_quit();
1616 if (s_info.input_fd == STDIN_FILENO) {
1617 if (fcntl(s_info.input_fd, F_SETFL, O_NONBLOCK) < 0)
1618 printf("Error: %s\n", strerror(errno));
1620 s_info.in_handler = ecore_main_fd_handler_add(s_info.input_fd, ECORE_FD_READ, input_cb, NULL, NULL, NULL);
1621 if (!s_info.in_handler) {
1622 printf("Failed to add a input handler\n");
1623 ecore_main_loop_quit();
1631 static int disconnected_cb(int handle, void *data)
1633 printf("Disconnected\n");
1634 ecore_main_loop_quit();
1638 static int connected_cb(int handle, void *data)
1640 struct packet *packet;
1642 printf("Connected\n");
1644 packet = packet_create("liveinfo_hello", "d", 0.0f);
1646 printf("Failed to build a packet for hello\n");
1647 com_core_packet_client_fini(s_info.fd);
1648 s_info.fd = -EINVAL;
1654 if (com_core_packet_async_send(s_info.fd, packet, 0.0f, ret_cb, NULL) < 0) {
1655 printf("Failed to send a packet hello\n");
1656 packet_destroy(packet);
1657 com_core_packet_client_fini(s_info.fd);
1658 s_info.fd = -EINVAL;
1662 packet_destroy(packet);
1666 int main(int argc, char *argv[])
1668 struct termios ttystate;
1669 static struct method s_table[] = {
1675 static struct option long_options[] = {
1676 { "batchmode", required_argument, 0, 'b' },
1677 { "help", no_argument, 0, 'h' },
1678 { "verbose", required_argument, 0, 'v' },
1685 c = getopt_long(argc, argv, "b:hv:", long_options, &option_index);
1688 if (!optarg || !*optarg) {
1689 printf("Invalid argument\n");
1694 if (s_info.input_fd != STDIN_FILENO) {
1695 /* Close the previously, opened file */
1696 close(s_info.input_fd);
1699 s_info.input_fd = open(optarg, O_RDONLY);
1700 if (s_info.input_fd < 0) {
1701 printf("Unable to access %s (%s)\n", optarg, strerror(errno));
1709 if (!optarg || !*optarg) {
1710 printf("Invalid argument\n");
1715 s_info.verbose = !strcmp(optarg, "true");
1725 com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1726 com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1727 livebox_service_init();
1729 s_info.fd = com_core_packet_client_init(SOCKET_FILE, 0, s_table);
1730 if (s_info.fd < 0) {
1731 printf("Failed to make a connection\n");
1735 if (s_info.input_fd == STDIN_FILENO) {
1736 printf("Type your command on below empty line\n");
1738 if (tcgetattr(s_info.input_fd, &ttystate) < 0) {
1739 printf("Error: %s\n", strerror(errno));
1741 ttystate.c_lflag &= ~(ICANON | ECHO);
1742 ttystate.c_cc[VMIN] = 1;
1744 if (tcsetattr(s_info.input_fd, TCSANOW, &ttystate) < 0)
1745 printf("Error: %s\n", strerror(errno));
1748 printf("Batch mode enabled\n");
1751 if (setvbuf(stdout, (char *)NULL, _IONBF, 0) != 0)
1752 printf("Error: %s\n", strerror(errno));
1756 ecore_main_loop_begin();
1759 livebox_service_fini();
1761 if (s_info.fd > 0) {
1762 com_core_packet_client_fini(s_info.fd);
1763 s_info.fd = -EINVAL;
1766 if (s_info.fd_handler) {
1767 ecore_main_fd_handler_del(s_info.fd_handler);
1768 s_info.fd_handler = NULL;
1771 if (s_info.input_fd == STDIN_FILENO) {
1772 ttystate.c_lflag |= ICANON | ECHO;
1773 if (tcsetattr(s_info.input_fd, TCSANOW, &ttystate) < 0)
1774 printf("Error: %s\n", strerror(errno));
1776 close(s_info.input_fd);
1779 if (s_info.fifo_handle > 0) {
1780 close(s_info.fifo_handle);
1781 s_info.fifo_handle = -EINVAL;
1784 if (s_info.in_handler) {
1785 ecore_main_fd_handler_del(s_info.in_handler);
1786 s_info.in_handler = NULL;
1790 putc((int)'\n', stdout);