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);
183 static void package_del_cb(struct node *node)
185 struct package *info;
187 info = node_data(node);
193 free(info->slavename);
198 static void inst_del_cb(struct node *node)
200 struct instance *info;
202 info = node_data(node);
209 free(info->category);
221 struct node *next_node;
223 if (!(node_mode(s_info.targetdir) & NODE_READ)) {
224 printf("Access denied\n");
228 is_package = node_name(s_info.targetdir) && !strcmp(node_name(s_info.targetdir), "package");
229 is_provider = !is_package && node_name(s_info.targetdir) && !strcmp(node_name(s_info.targetdir), "provider");
230 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");
232 node = node_child(s_info.targetdir);
235 struct package *info;
237 next_node = node_next_sibling(node);
238 if (node_age(node) != s_info.age) {
239 node_delete(node, package_del_cb);
244 info = node_data(node);
245 printf(" %3d %20s %5s ", info->inst_count, info->slavename ? info->slavename : "(none)", info->abi ? info->abi : "?");
246 } else if (is_provider) {
249 next_node = node_next_sibling(node);
250 if (node_age(node) != s_info.age) {
251 node_delete(node, provider_del_cb);
256 info = node_data(node);
257 printf("%6d %3d %5s %5.2f ", info->pid, info->loaded_inst, info->abi ? info->abi : "?", info->ttl);
258 } else if (is_instance) {
259 struct instance *info;
263 next_node = node_next_sibling(node);
265 if (node_age(node) != s_info.age) {
266 node_delete(node, inst_del_cb);
271 info = node_data(node);
273 printf(" %5.2f %6s %10s %10s %4dx%-4d ", info->period, info->state, info->cluster, info->category, info->width, info->height);
274 snprintf(buf, sizeof(buf), "/opt/usr/share/live_magazine/reader/%s", node_name(node));
275 if (lstat(buf, &stat) < 0) {
276 printf("%3d ERR ", errno);
278 printf("%2.2lf KB ", (double)stat.st_size / 1024.0f);
282 if (node_type(node) == NODE_DIR) {
283 printf("%s/", node_name(node));
284 } else if (node_type(node) == NODE_FILE) {
285 printf("%s", node_name(node));
289 node = node_next_sibling(node);
293 printf("Total: %d\n", cnt);
296 static void send_slave_list(void)
298 struct packet *packet;
301 if (s_info.cmd != NOP) {
302 printf("Previous command is not finished\n");
306 packet = packet_create_noack("slave_list", "d", 0.0f);
308 printf("Failed to create a packet\n");
312 ret = com_core_packet_send_only(s_info.fd, packet);
313 packet_destroy(packet);
315 printf("Failed to send a packet: %d\n", ret);
319 s_info.cmd = SLAVE_LIST;
324 * var = debug, slave_max_load
327 static void send_command(const char *cmd, const char *var, const char *val)
329 struct packet *packet;
332 if (s_info.cmd != NOP) {
333 printf("Previous command is not finished\n");
337 packet = packet_create_noack("master_ctrl", "sss", cmd, var, val);
339 printf("Failed to create a ctrl packet\n");
343 ret = com_core_packet_send_only(s_info.fd, packet);
344 packet_destroy(packet);
346 printf("Failed to send packet ctrl\n");
350 s_info.cmd = MASTER_CTRL;
354 static int pkglist_cb(const char *appid, const char *lbid, int is_prime, void *data)
356 struct node *parent = data;
358 struct package *info;
360 node = node_find(parent, lbid);
362 info = node_data(node);
364 printf("Invalid node\n");
369 info->pkgid = strdup(appid);
371 printf("Error: %s\n", strerror(errno));
375 node_set_age(node, s_info.age);
379 info = calloc(1, sizeof(*info));
381 printf("Error: %s\n", strerror(errno));
385 info->pkgid = strdup(appid);
387 printf("Error: %s\n", strerror(errno));
392 info->primary = is_prime;
394 node = node_create(parent, lbid, NODE_DIR);
401 node_set_mode(node, NODE_READ | NODE_EXEC);
402 node_set_data(node, info);
403 node_set_age(node, s_info.age);
407 static void send_pkg_list(void)
409 struct packet *packet;
412 if (s_info.cmd != NOP) {
413 printf("Previous command is not finished\n");
417 packet = packet_create_noack("pkg_list", "d", 0.0f);
419 printf("Failed to create a packet\n");
423 ret = com_core_packet_send_only(s_info.fd, packet);
424 packet_destroy(packet);
426 printf("Failed to create a packet\n");
430 s_info.cmd = PKG_LIST;
433 livebox_service_get_pkglist(pkglist_cb, s_info.targetdir);
436 static void send_inst_delete(void)
438 struct packet *packet;
441 struct instance *inst;
444 if (s_info.cmd != NOP) {
445 printf("Previous command is not finished\n");
449 parent = node_parent(s_info.targetdir);
451 printf("Invalid argument\n");
455 if (!node_parent(parent)) {
456 printf("Invalid argument\n");
460 name = node_name(node_parent(parent));
461 if (!name || strcmp(name, "package")) {
462 printf("Invalid argument\n");
466 inst = node_data(s_info.targetdir);
467 name = node_name(parent);
469 packet = packet_create_noack("pkg_ctrl", "sss", "rminst", name, inst->id);
471 printf("Failed to create a packet\n");
475 ret = com_core_packet_send_only(s_info.fd, packet);
476 packet_destroy(packet);
478 printf("Failed to send a packet: %d\n", ret);
482 s_info.cmd = INST_CTRL;
486 static void send_inst_fault(void)
488 struct packet *packet;
491 struct instance *inst;
494 if (s_info.cmd != NOP) {
495 printf("Previous command is not finished\n");
499 parent = node_parent(s_info.targetdir);
501 printf("Invalid argument\n");
505 if (!node_parent(parent)) {
506 printf("Invalid argument\n");
510 name = node_name(node_parent(parent));
511 if (!name || strcmp(name, "package")) {
512 printf("Invalid argument\n");
516 inst = node_data(s_info.targetdir);
517 name = node_name(parent);
519 packet = packet_create_noack("pkg_ctrl", "sss", "faultinst", name, inst->id);
521 printf("Failed to create a packet\n");
525 ret = com_core_packet_send_only(s_info.fd, packet);
526 packet_destroy(packet);
528 printf("Failed to send a packet: %d\n", ret);
532 s_info.cmd = INST_CTRL;
536 static void send_inst_list(const char *pkgname)
538 struct packet *packet;
541 if (s_info.cmd != NOP) {
542 printf("Previous command is not finished\n");
546 packet = packet_create_noack("inst_list", "s", pkgname);
548 printf("Failed to create a packet\n");
552 ret = com_core_packet_send_only(s_info.fd, packet);
553 packet_destroy(packet);
555 printf("Failed to send a packet: %d\n", ret);
559 s_info.cmd = INST_LIST;
563 static void help(void)
565 printf("liveinfo - Livebox utility\n");
566 printf("------------------------------ [Option] ------------------------------\n");
567 printf("-b Batch mode\n");
568 printf("-x execute command\n");
569 printf("------------------------------ [Command list] ------------------------------\n");
570 printf("
\e[32mcd [PATH] - Change directory
\e[0m\n");
571 printf("
\e[32mls [ | PATH] - List up content as a file
\e[0m\n");
572 printf("
\e[32mrm [PKG_ID|INST_ID] - Delete package or instance
\e[0m\n");
573 printf("
\e[32mstat [path] - Display the information of given path
\e[0m\n");
574 printf("
\e[32mset [debug] [on|off] Set the control variable of master provider
\e[0m\n");
575 printf("
\e[32mx damage Pix x y w h - Create damage event for given pixmap
\e[0m\n");
576 printf("
\e[32mx move Pix x y - Move the window
\e[0m\n");
577 printf("
\e[32mx resize Pix w h - Resize the window
\e[0m\n");
578 printf("
\e[32mx map Pix - Show the window
\e[0m\n");
579 printf("
\e[32mx unmap Pix - Hide the window
\e[0m\n");
580 printf("
\e[32mx capture Pix outfile - Capture pixmap and save it to outfile
\e[0m\n");
581 printf("
\e[32msh [command] Execute shell command, [command] should be abspath
\e[0m\n");
582 printf("
\e[32mexit -
\e[0m\n");
583 printf("
\e[32mquit -
\e[0m\n");
584 printf("----------------------------------------------------------------------------\n");
587 static inline void init_directory(void)
590 s_info.rootdir = node_create(NULL, NULL, NODE_DIR);
591 if (!s_info.rootdir) {
594 node_set_mode(s_info.rootdir, NODE_READ | NODE_EXEC);
596 node = node_create(s_info.rootdir, "provider", NODE_DIR);
598 node_destroy(s_info.rootdir);
599 s_info.rootdir = NULL;
602 node_set_mode(node, NODE_READ | NODE_EXEC);
604 node = node_create(s_info.rootdir, "package", NODE_DIR);
606 node_destroy(node_child(s_info.rootdir));
607 node_destroy(s_info.rootdir);
608 s_info.rootdir = NULL;
611 node_set_mode(node, NODE_READ | NODE_EXEC);
613 s_info.curdir = s_info.rootdir;
617 static inline void fini_directory(void)
621 static struct node *update_target_dir(const char *cmd)
625 node = (*cmd == '/') ? s_info.rootdir : s_info.curdir;
626 node = node_find(node, cmd);
631 static int get_token(const char *src, char *out)
634 while (*src && *src == ' ') src++;
640 while (*src && *src != ' ') {
649 static inline int do_stat(const char *cmd)
664 while (*cmd && *cmd == ' ') cmd++;
667 printf("Invalid argument\n");
671 node = node_find(*cmd == '/' ? s_info.rootdir : s_info.curdir, cmd);
673 printf("Invalid path\n");
679 parent = node_parent(node);
681 if (!node_name(parent)) {
682 printf("%s has no info\n", node_name(node));
684 } else if (!strcmp(node_name(parent), "package")) {
685 type = (i == 0) ? PKG : PKG_INSTANCE;
687 } else if (!strcmp(node_name(parent), "provider")){
688 type = (i == 0) ? PROVIDER : PROVIDER_INSTANCE;
692 parent = node_parent(parent);
695 printf("%s is invalid path\n", node_name(node));
702 tmp = livebox_service_i18n_name(node_name(node), NULL);
703 printf("Name: %s (", tmp);
706 i = livebox_service_is_enabled(node_name(node));
707 printf("%s)\n", i ? "enabled" : "disabled");
709 tmp = livebox_service_i18n_icon(node_name(node), NULL);
710 printf("Icon: %s\n", tmp);
713 tmp = livebox_service_provider_name(node_name(node));
714 printf("Provider: %s (content:", tmp);
717 tmp = livebox_service_content(node_name(node));
718 printf("%s)\n", tmp);
721 tmp = livebox_service_lb_script_path(node_name(node));
722 printf("LB Script: %s (", tmp);
725 tmp = livebox_service_lb_script_group(node_name(node));
726 printf("%s)\n", tmp);
729 tmp = livebox_service_pd_script_path(node_name(node));
730 printf("PD Script: %s (", tmp);
733 tmp = livebox_service_pd_script_group(node_name(node));
734 printf("%s)\n", tmp);
739 printf("Not supported yet\n");
742 printf("Not supported yet\n");
744 case PROVIDER_INSTANCE:
745 printf("Not supported yet\n");
748 printf("Not supported yet\n");
755 static int do_set(const char *cmd)
758 char variable[4096] = { '0', };
761 i = get_token(cmd, variable);
764 while (*cmd && *cmd == ' ') {
769 printf("Invalid argument(%s): set [VAR] [VAL]\n", cmd);
773 send_command("set", variable, cmd);
777 static inline int do_get(const char *cmd)
781 while (*cmd && *cmd == ' ') cmd++;
783 printf("Invalid argument(%s): get [VAR]\n", cmd);
787 send_command("get", cmd, "");
791 static inline int do_ls(const char *cmd)
798 while (*cmd && *cmd == ' ') {
802 s_info.targetdir = *cmd ? update_target_dir(cmd) : s_info.curdir;
803 if (!s_info.targetdir) {
804 printf("%s is not exists\n", cmd);
808 name = node_name(s_info.targetdir);
810 if (!strcmp(name, "package")) {
811 if (s_info.cmd == NOP) {
816 printf("Waiting the server response\n");
818 } else if (!strcmp(name, "provider")) {
819 if (s_info.cmd == NOP) {
824 printf("Waiting the server response\n");
829 parent = node_parent(s_info.targetdir);
830 if (parent && node_name(parent)) {
831 if (!strcmp(node_name(parent), "package")) {
832 if (s_info.cmd != NOP) {
833 printf("Waiting the server response\n");
837 send_inst_list(name);
846 static inline int do_cd(const char *cmd)
850 while (*cmd && *cmd == ' ') {
858 if (s_info.cmd != NOP) {
859 printf("Waiting the server response\n");
863 s_info.targetdir = update_target_dir(cmd);
864 if (!s_info.targetdir) {
865 printf("%s is not exists\n", cmd);
869 if (node_type(s_info.targetdir) != NODE_DIR) {
870 printf("Unable change directory to %s\n", cmd);
874 if (!(node_mode(s_info.targetdir) & NODE_EXEC)) {
875 printf("Access denied %s\n", cmd);
879 s_info.curdir = s_info.targetdir;
883 static inline int do_rm(const char *cmd)
886 while (*cmd && *cmd == ' ') cmd++;
891 if (s_info.cmd != NOP) {
892 printf("Waiting the server response\n");
896 s_info.targetdir = update_target_dir(cmd);
897 if (!s_info.targetdir) {
898 printf("%s is not exists\n", cmd);
902 if (!(node_mode(s_info.targetdir) & NODE_WRITE)) {
903 printf("Access denied %s\n", cmd);
911 static inline int do_fault(const char *cmd)
914 while (*cmd && *cmd == ' ') cmd++;
919 if (s_info.cmd != NOP) {
920 printf("Waiting the server response\n");
924 s_info.targetdir = update_target_dir(cmd);
925 if (!s_info.targetdir) {
926 printf("%s is not exists\n", cmd);
930 if (!(node_mode(s_info.targetdir) & NODE_WRITE)) {
931 printf("Access denied %s\n", cmd);
939 #if !defined(WCOREDUMP)
940 #define WCOREDUMP(a) 0
943 static void do_sh(const char *cmd)
949 while (*cmd && *cmd == ' ') {
963 while (idx < (sizeof(command) - 1) && *cmd && *cmd != ' ') {
964 command[idx++] = *cmd++;
968 if (execl(command, cmd, NULL) < 0) {
969 printf("Failed to execute: %s\n", strerror(errno));
973 } else if (pid < 0) {
974 printf("Failed to create a new process: %s\n", strerror(errno));
977 if (waitpid(pid, &status, 0) < 0) {
978 printf("error: %s\n", strerror(errno));
980 if (WIFEXITED(status)) {
981 printf("Exit: %d\n", WEXITSTATUS(status));
982 } else if (WIFSIGNALED(status)) {
983 printf("Terminated by %d %s\n", WTERMSIG(status), WCOREDUMP(status) ? " - core generated" : "");
984 } else if (WIFSTOPPED(status)) {
985 printf("Stopped by %d\n", WSTOPSIG(status));
986 } else if (WIFCONTINUED(status)) {
987 printf("Child is resumed\n");
993 static inline int get_pixmap_size(Display *disp, Pixmap id, int *x, int *y, unsigned int *w, unsigned int *h)
996 unsigned int dummy_border, dummy_depth;
1008 if (!XGetGeometry(disp, id, &dummy_win, x, y, w, h, &dummy_border, &dummy_depth)) {
1015 static inline int do_capture(Display *disp, Pixmap id, const char *filename)
1026 screen = DefaultScreenOfDisplay(disp);
1027 visual = DefaultVisualOfScreen(screen);
1029 if (get_pixmap_size(disp, id, NULL, NULL, &w, &h) < 0) {
1030 printf("Failed to get size of a pixmap\n");
1034 printf("Pixmap size: %dx%d\n", w, h);
1035 bufsz = w * h * sizeof(int);
1037 si.shmid = shmget(IPC_PRIVATE, bufsz, IPC_CREAT | 0666);
1039 printf("shmget: %s\n", strerror(errno));
1043 si.readOnly = False;
1044 si.shmaddr = shmat(si.shmid, NULL, 0);
1045 if (si.shmaddr == (void *)-1) {
1047 if (shmctl(si.shmid, IPC_RMID, 0) < 0) {
1048 printf("shmctl: %s\n", strerror(errno));
1056 * Use the 24 bits Pixmap for Video player
1058 xim = XShmCreateImage(disp, visual, 24 /* (depth << 3) */, ZPixmap, NULL, &si, w, h);
1060 if (shmdt(si.shmaddr) < 0) {
1061 printf("shmdt: %s\n", strerror(errno));
1064 if (shmctl(si.shmid, IPC_RMID, 0) < 0) {
1065 printf("shmctl: %s\n", strerror(errno));
1071 xim->data = si.shmaddr;
1072 XShmAttach(disp, &si);
1074 XShmGetImage(disp, id, xim, 0, 0, 0xFFFFFFFF);
1077 fd = open(filename, O_CREAT | O_RDWR, 0644);
1079 if (write(fd, xim->data, bufsz) != bufsz) {
1080 printf("Data is not fully written\n");
1083 if (close(fd) < 0) {
1084 printf("close: %s\n", strerror(errno));
1087 printf("Error: %sn\n", strerror(errno));
1090 XShmDetach(disp, &si);
1093 if (shmdt(si.shmaddr) < 0) {
1094 printf("shmdt: %s\n", strerror(errno));
1097 if (shmctl(si.shmid, IPC_RMID, 0) < 0) {
1098 printf("shmctl: %s\n", strerror(errno));
1104 static void do_x(const char *cmd)
1110 while (*cmd && *cmd == ' ') {
1118 disp = XOpenDisplay(NULL);
1120 printf("Failed to connect to the X\n");
1124 if (!strncasecmp(cmd, "damage ", 7)) {
1127 XserverRegion region;
1132 if (sscanf(cmd, "%u %d %d %d %d", &winId, &x, &y, &w, &h) != 5) {
1133 printf("Invalid argument\nx damage WINID_DEC X Y W H\n");
1140 region = XFixesCreateRegion(disp, &rect, 1);
1141 XDamageAdd(disp, winId, region);
1142 XFixesDestroyRegion(disp, region);
1145 printf("Damage: %u %d %d %d %d\n", winId, x, y, w, h);
1146 } else if (!strncasecmp(cmd, "capture ", 8)) {
1152 if (sscanf(cmd, "%u %255[^ ]", &winId, filename) != 2) {
1153 printf("Invalid argument\nx capture WINID_DEC FILENAME (%s)\n", cmd);
1156 if (do_capture(disp, winId, filename) == 0) {
1157 printf("Captured: %s\n", filename);
1159 } else if (!strncasecmp(cmd, "resize ", 7)) {
1166 if (sscanf(cmd, "%u %d %d", &winId, &w, &h) != 3) {
1167 printf("Invalid argument\nx resize WINID_DEC W H\n");
1171 XResizeWindow(disp, winId, w, h);
1172 printf("Resize: %u %d %d\n", winId, w, h);
1173 } else if (!strncasecmp(cmd, "move ", 5)) {
1179 if (sscanf(cmd, "%u %d %d", &winId, &x, &y) != 3) {
1180 printf("Invalid argument\nx move WINID_DEC X Y\n");
1184 XMoveWindow(disp, winId, x, y);
1185 printf("Move: %u %d %d\n", winId, x, y);
1186 } else if (!strncasecmp(cmd, "map ", 4)) {
1189 if (sscanf(cmd, "%u", &winId) != 1) {
1190 printf("Invalid argument\nx map WINID_DEC\n");
1193 XMapRaised(disp, winId);
1194 printf("Map: %u\n", winId);
1195 } else if (!strncasecmp(cmd, "unmap ", 6)) {
1198 if (sscanf(cmd, "%u", &winId) != 1) {
1199 printf("Invalid argument\nx unmap WINID_DEC\n");
1202 XUnmapWindow(disp, winId);
1203 printf("Unmap: %u\n", winId);
1205 printf("Unknown command\n");
1208 XCloseDisplay(disp);
1211 static inline void put_command(const char *cmd)
1213 if (s_info.history[s_info.history_top]) {
1214 free(s_info.history[s_info.history_top]);
1215 s_info.history[s_info.history_top] = NULL;
1218 s_info.history[s_info.history_top] = strdup(cmd);
1219 s_info.history_top = (s_info.history_top + !!s_info.history[s_info.history_top]) % (sizeof(s_info.history) / sizeof(s_info.history[0]));
1222 static inline const char *get_command(int idx)
1224 idx = s_info.history_top + idx;
1226 idx += (sizeof(s_info.history) / sizeof(s_info.history[0]));
1229 return s_info.history[idx];
1232 static inline void do_command(const char *cmd)
1234 /* Skip the first spaces */
1235 while (*cmd && *cmd == ' ') {
1239 if (strlen(cmd) && *cmd != '#') {
1240 if (!strncasecmp(cmd, "exit", 4) || !strncasecmp(cmd, "quit", 4)) {
1241 ecore_main_loop_quit();
1242 } else if (!strncasecmp(cmd, "set ", 4)) {
1243 if (do_set(cmd) == 0) {
1246 } else if (!strncasecmp(cmd, "stat ", 5)) {
1248 } else if (!strncasecmp(cmd, "get ", 4)) {
1249 if (do_get(cmd) == 0) {
1252 } else if (!strncasecmp(cmd, "ls", 2)) {
1253 if (do_ls(cmd) == 0) {
1256 } else if (!strncasecmp(cmd, "cd", 2)) {
1257 if (do_cd(cmd) == 0) {
1260 } else if (!strncasecmp(cmd, "rm", 2)) {
1261 if (do_rm(cmd) == 0) {
1264 } else if (!strncasecmp(cmd, "fault", 5)) {
1265 if (do_fault(cmd) == 0) {
1268 } else if (!strncasecmp(cmd, "sh ", 3)) {
1270 } else if (!strncasecmp(cmd, "x ", 2)) {
1281 static Eina_Bool input_cb(void *data, Ecore_Fd_Handler *fd_handler)
1284 static char cmd_buffer[256];
1288 const char escape_str[] = { 0x1b, 0x5b, 0x0 };
1289 const char *escape_ptr = escape_str;
1293 fd = ecore_main_fd_handler_fd_get(fd_handler);
1295 printf("FD is not valid: %d\n", fd);
1296 return ECORE_CALLBACK_CANCEL;
1299 fd = s_info.input_fd;
1304 * Using this routine, we can implement the command recommend algorithm.
1305 * When a few more characters are matched with history of command, we can show it to user
1306 * Then the user will choose one or write new command
1310 while ((ret = read(fd, &ch, sizeof(ch))) == sizeof(ch)) {
1311 if (*escape_ptr == '\0') {
1315 printf("%s2K%s1G", escape_str, escape_str);
1316 tmp = get_command(--s_info.history_idx);
1318 s_info.history_idx = 0;
1319 cmd_buffer[0] = '\0';
1322 strcpy(cmd_buffer, tmp);
1323 idx = strlen(cmd_buffer);
1327 case 0x42: /* DOWN */
1328 if (s_info.history_idx >= 0) {
1332 printf("%s2K%s1G", escape_str, escape_str);
1333 tmp = get_command(++s_info.history_idx);
1334 if (s_info.history_idx == 0) {
1335 s_info.history_idx = 0;
1336 cmd_buffer[0] = '\0';
1339 strcpy(cmd_buffer, tmp);
1340 idx = strlen(cmd_buffer);
1344 case 0x43: /* RIGHT */
1346 case 0x44: /* LEFT */
1352 escape_ptr = escape_str;
1354 } else if (ch == *escape_ptr) {
1360 case 0x08: /* BKSP */
1361 cmd_buffer[idx] = '\0';
1364 cmd_buffer[idx] = ' ';
1369 cmd_buffer[idx] = '\0';
1373 case 0x09: /* TAB */
1374 if (!s_info.quick_search_node) {
1375 s_info.quick_search_node = node_child(s_info.curdir);
1376 s_info.quick_idx = idx;
1378 s_info.quick_search_node = node_next_sibling(s_info.quick_search_node);
1379 idx = s_info.quick_idx;
1382 if (!s_info.quick_search_node) {
1386 printf("%s2K%s1G", escape_str, escape_str);
1387 strcpy(cmd_buffer + idx, node_name(s_info.quick_search_node));
1388 idx += strlen(node_name(s_info.quick_search_node));
1393 cmd_buffer[idx] = '\0';
1395 if (s_info.input_fd == STDIN_FILENO || s_info.verbose) {
1396 putc((int)'\n', stdout);
1398 do_command(cmd_buffer);
1399 put_command(cmd_buffer);
1400 memset(cmd_buffer, 0, sizeof(cmd_buffer));
1401 s_info.history_idx = 0;
1402 s_info.quick_search_node = NULL;
1404 /* Make a main loop processing for command handling */
1405 return ECORE_CALLBACK_RENEW;
1407 cmd_buffer[idx++] = ch;
1409 if (s_info.input_fd == STDIN_FILENO || s_info.verbose) {
1410 putc((int)ch, stdout);
1413 if (idx == sizeof(cmd_buffer) - 1) {
1414 cmd_buffer[idx] = '\0';
1415 printf("\nCommand buffer is overflow: %s\n", cmd_buffer);
1422 if (ret < 0 && !fd_handler) {
1423 ecore_main_loop_quit();
1426 return ECORE_CALLBACK_RENEW;
1429 static void processing_line_buffer(const char *buffer)
1432 char slavename[256];
1450 struct package *pkginfo;
1451 struct instance *instinfo;
1452 struct slave *slaveinfo;
1455 switch (s_info.cmd) {
1457 if (sscanf(buffer, "%d %255[^ ] %255[^ ] %255[^ ] %d %d %d", &pid, slavename, pkgname, abi, &refcnt, &fault_count, &list_count) != 7) {
1458 printf("Invalid format : [%s]\n", buffer);
1462 node = node_find(s_info.targetdir, pkgname);
1464 pkginfo = calloc(1, sizeof(*pkginfo));
1466 printf("Error: %s\n", strerror(errno));
1470 pkginfo->pkgid = strdup("conf.file");
1471 if (!pkginfo->pkgid) {
1472 printf("Error: %s\n", strerror(errno));
1475 pkginfo->primary = 1;
1477 node = node_create(s_info.targetdir, pkgname, NODE_DIR);
1479 free(pkginfo->pkgid);
1481 printf("Failed to create a new node (%s)\n", pkgname);
1485 node_set_mode(node, NODE_READ | NODE_EXEC);
1486 node_set_data(node, pkginfo);
1488 pkginfo = node_data(node);
1490 printf("Package info is inavlid\n");
1494 free(pkginfo->slavename);
1497 pkginfo->slavename = NULL;
1498 pkginfo->abi = NULL;
1501 node_set_age(node, s_info.age);
1503 pkginfo->slavename = strdup(slavename);
1504 if (!pkginfo->slavename) {
1505 printf("Error: %s\n", strerror(errno));
1508 pkginfo->abi = strdup(abi);
1509 if (!pkginfo->abi) {
1510 printf("Error: %s\n", strerror(errno));
1514 pkginfo->refcnt = refcnt;
1515 pkginfo->fault_count = fault_count;
1516 pkginfo->inst_count = list_count;
1519 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) {
1520 printf("Invalid format : [%s]\n", buffer);
1523 node = node_find(s_info.targetdir, slavename);
1525 slaveinfo = calloc(1, sizeof(*slaveinfo));
1527 printf("Error: %s\n", strerror(errno));
1531 node = node_create(s_info.targetdir, slavename, NODE_DIR);
1537 node_set_mode(node, NODE_READ | NODE_EXEC);
1538 node_set_data(node, slaveinfo);
1540 slaveinfo = node_data(node);
1543 node_set_age(node, s_info.age);
1545 free(slaveinfo->pkgname);
1546 free(slaveinfo->abi);
1547 free(slaveinfo->state);
1549 slaveinfo->pkgname = strdup(pkgname);
1550 if (!slaveinfo->pkgname) {
1551 printf("Error: %s\n", strerror(errno));
1554 slaveinfo->abi = strdup(abi);
1555 if (!slaveinfo->abi) {
1556 printf("Error: %s\n", strerror(errno));
1559 slaveinfo->state = strdup(state);
1560 if (!slaveinfo->state) {
1561 printf("Error: %s\n", strerror(errno));
1564 slaveinfo->pid = pid;
1565 slaveinfo->secured = secured;
1566 slaveinfo->refcnt = refcnt;
1567 slaveinfo->fault_count = fault_count;
1568 slaveinfo->loaded_inst = loaded_inst;
1569 slaveinfo->loaded_pkg = loaded_pkg;
1570 slaveinfo->ttl = ttl;
1573 if (sscanf(buffer, "%[^ ] %[^ ] %[^ ] %lf %[^ ] %d %d", inst_id, cluster, category, &period, state, &width, &height) != 7) {
1574 printf("Invalid format : [%s]\n", buffer);
1578 for (i = strlen(inst_id); i > 0 && inst_id[i] != '/'; i--);
1579 i += (inst_id[i] == '/');
1581 node = node_find(s_info.targetdir, inst_id + i);
1583 instinfo = calloc(1, sizeof(*instinfo));
1585 printf("Error: %s\n", strerror(errno));
1589 node = node_create(s_info.targetdir, inst_id + i, NODE_FILE);
1595 node_set_mode(node, NODE_READ | NODE_WRITE);
1596 node_set_data(node, instinfo);
1598 instinfo = node_data(node);
1601 node_set_age(node, s_info.age);
1604 free(instinfo->cluster);
1605 free(instinfo->category);
1606 free(instinfo->state);
1608 instinfo->id = strdup(inst_id);
1609 if (!instinfo->id) {
1610 printf("Error: %s\n", strerror(errno));
1613 instinfo->cluster = strdup(cluster);
1614 if (!instinfo->cluster) {
1615 printf("Error: %s\n", strerror(errno));
1618 instinfo->category = strdup(category);
1619 if (!instinfo->category) {
1620 printf("Error: %s\n", strerror(errno));
1623 instinfo->state = strdup(state);
1624 if (!instinfo->state) {
1625 printf("Error: %s\n", strerror(errno));
1628 instinfo->period = period;
1629 instinfo->width = width;
1630 instinfo->height = height;
1633 sscanf(buffer, "%d", &i);
1634 printf("%s\n", strerror(i));
1635 printf("Result: %d\n", i);
1638 sscanf(buffer, "%d", &i);
1639 printf("Result: %d\n", i);
1642 sscanf(buffer, "%d", &i);
1643 printf("Result: %d\n", i);
1650 static inline void do_line_command(void)
1652 switch (s_info.cmd) {
1674 static Eina_Bool read_cb(void *data, Ecore_Fd_Handler *fd_handler)
1677 static char *line_buffer = NULL;
1678 static int line_index = 0;
1679 static int bufsz = 256;
1682 fd = ecore_main_fd_handler_fd_get(fd_handler);
1684 printf("FD is not valid: %d\n", fd);
1685 return ECORE_CALLBACK_CANCEL;
1688 if (read(fd, &ch, sizeof(ch)) != sizeof(ch)) {
1689 printf("Error: %s\n", strerror(errno));
1690 return ECORE_CALLBACK_CANCEL;
1695 line_buffer = malloc(bufsz);
1697 printf("Error: %s\n", strerror(errno));
1698 return ECORE_CALLBACK_CANCEL;
1702 if (ch == '\n') { /* End of a line */
1703 if (line_index == bufsz - 1) {
1705 new_buf = realloc(line_buffer, bufsz + 2);
1707 printf("Error: %s\n", strerror(errno));
1712 return ECORE_CALLBACK_CANCEL;
1715 line_buffer = new_buf;
1718 line_buffer[line_index] = '\0';
1720 if (!strcmp(line_buffer, "EOD")) {
1724 processing_line_buffer(line_buffer);
1734 line_buffer[line_index++] = ch;
1735 if (line_index == bufsz - 1) {
1737 new_buf = realloc(line_buffer, bufsz);
1739 printf("Error: %s\n", strerror(errno));
1744 return ECORE_CALLBACK_CANCEL;
1747 line_buffer = new_buf;
1751 return ECORE_CALLBACK_RENEW;
1754 static int ret_cb(pid_t pid, int handle, const struct packet *packet, void *data)
1756 const char *fifo_name;
1759 if (packet_get(packet, "si", &fifo_name, &ret) != 2) {
1760 printf("Invalid packet\n");
1765 printf("Returns %d\n", ret);
1769 printf("FIFO: %s\n", fifo_name);
1771 s_info.fifo_handle = open(fifo_name, O_RDONLY | O_NONBLOCK);
1772 if (s_info.fifo_handle < 0) {
1773 printf("Error: %s\n", strerror(errno));
1774 s_info.fifo_handle = -EINVAL;
1775 ecore_main_loop_quit();
1779 s_info.fd_handler = ecore_main_fd_handler_add(s_info.fifo_handle, ECORE_FD_READ, read_cb, NULL, NULL, NULL);
1780 if (!s_info.fd_handler) {
1781 printf("Failed to add a fd handler\n");
1782 if (close(s_info.fifo_handle) < 0) {
1783 printf("close: %s\n", strerror(errno));
1785 s_info.fifo_handle = -EINVAL;
1786 ecore_main_loop_quit();
1792 if (s_info.input_fd == STDIN_FILENO) {
1793 if (fcntl(s_info.input_fd, F_SETFL, O_NONBLOCK) < 0) {
1794 printf("Error: %s\n", strerror(errno));
1797 s_info.in_handler = ecore_main_fd_handler_add(s_info.input_fd, ECORE_FD_READ, input_cb, NULL, NULL, NULL);
1798 if (!s_info.in_handler) {
1799 printf("Failed to add a input handler\n");
1800 ecore_main_loop_quit();
1808 static int disconnected_cb(int handle, void *data)
1810 printf("Disconnected\n");
1811 ecore_main_loop_quit();
1815 static int connected_cb(int handle, void *data)
1817 struct packet *packet;
1819 printf("Connected\n");
1821 packet = packet_create("liveinfo_hello", "d", 0.0f);
1823 printf("Failed to build a packet for hello\n");
1824 com_core_packet_client_fini(s_info.fd);
1825 s_info.fd = -EINVAL;
1831 if (com_core_packet_async_send(s_info.fd, packet, 0.0f, ret_cb, NULL) < 0) {
1832 printf("Failed to send a packet hello\n");
1833 packet_destroy(packet);
1834 com_core_packet_client_fini(s_info.fd);
1835 s_info.fd = -EINVAL;
1839 packet_destroy(packet);
1843 int main(int argc, char *argv[])
1845 struct termios ttystate;
1846 static struct method s_table[] = {
1852 static struct option long_options[] = {
1853 { "batchmode", required_argument, 0, 'b' },
1854 { "help", no_argument, 0, 'h' },
1855 { "verbose", required_argument, 0, 'v' },
1856 { "execute", required_argument, 0, 'x' },
1863 c = getopt_long(argc, argv, "b:hv:x:", long_options, &option_index);
1866 if (!optarg || !*optarg) {
1867 printf("Invalid argument\n");
1872 if (s_info.input_fd != STDIN_FILENO) {
1873 /* Close the previously, opened file */
1874 if (close(s_info.input_fd) < 0) {
1875 printf("close: %s\n", strerror(errno));
1879 s_info.input_fd = open(optarg, O_RDONLY);
1880 if (s_info.input_fd < 0) {
1881 printf("Unable to access %s (%s)\n", optarg, strerror(errno));
1889 if (!optarg || !*optarg) {
1890 printf("Invalid argument\n");
1895 s_info.verbose = !strcmp(optarg, "true");
1898 if (!optarg || !*optarg) {
1899 printf("Invalid argument\n");
1911 #if (GLIB_MAJOR_VERSION <= 2 && GLIB_MINOR_VERSION < 36)
1915 com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1916 com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1917 livebox_service_init();
1919 s_info.fd = com_core_packet_client_init(SOCKET_FILE, 0, s_table);
1920 if (s_info.fd < 0) {
1921 printf("Failed to make a connection\n");
1925 if (s_info.input_fd == STDIN_FILENO) {
1926 printf("Type your command on below empty line\n");
1928 if (tcgetattr(s_info.input_fd, &ttystate) < 0) {
1929 printf("Error: %s\n", strerror(errno));
1931 ttystate.c_lflag &= ~(ICANON | ECHO);
1932 ttystate.c_cc[VMIN] = 1;
1934 if (tcsetattr(s_info.input_fd, TCSANOW, &ttystate) < 0) {
1935 printf("Error: %s\n", strerror(errno));
1939 printf("Batch mode enabled\n");
1942 if (setvbuf(stdout, (char *)NULL, _IONBF, 0) != 0) {
1943 printf("Error: %s\n", strerror(errno));
1948 ecore_main_loop_begin();
1951 livebox_service_fini();
1953 if (s_info.fd > 0) {
1954 com_core_packet_client_fini(s_info.fd);
1955 s_info.fd = -EINVAL;
1958 if (s_info.fd_handler) {
1959 ecore_main_fd_handler_del(s_info.fd_handler);
1960 s_info.fd_handler = NULL;
1963 if (s_info.input_fd == STDIN_FILENO) {
1964 ttystate.c_lflag |= ICANON | ECHO;
1965 if (tcsetattr(s_info.input_fd, TCSANOW, &ttystate) < 0) {
1966 printf("Error: %s\n", strerror(errno));
1969 if (close(s_info.input_fd) < 0) {
1970 printf("close: %s\n", strerror(errno));
1974 if (s_info.fifo_handle > 0) {
1975 if (close(s_info.fifo_handle) < 0) {
1976 printf("close: %s\n", strerror(errno));
1978 s_info.fifo_handle = -EINVAL;
1981 if (s_info.in_handler) {
1982 ecore_main_fd_handler_del(s_info.in_handler);
1983 s_info.in_handler = NULL;
1987 putc((int)'\n', stdout);