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);
737 i = livebox_service_mouse_event(node_name(node));
738 printf("Mouse event: %s\n", i ? "enabled" : "disabled");
742 printf("Not supported yet\n");
745 printf("Not supported yet\n");
747 case PROVIDER_INSTANCE:
748 printf("Not supported yet\n");
751 printf("Not supported yet\n");
758 static int do_set(const char *cmd)
761 char variable[4096] = { '0', };
764 i = get_token(cmd, variable);
767 while (*cmd && *cmd == ' ') {
772 printf("Invalid argument(%s): set [VAR] [VAL]\n", cmd);
776 send_command("set", variable, cmd);
780 static inline int do_get(const char *cmd)
784 while (*cmd && *cmd == ' ') cmd++;
786 printf("Invalid argument(%s): get [VAR]\n", cmd);
790 send_command("get", cmd, "");
794 static inline int do_ls(const char *cmd)
801 while (*cmd && *cmd == ' ') {
805 s_info.targetdir = *cmd ? update_target_dir(cmd) : s_info.curdir;
806 if (!s_info.targetdir) {
807 printf("%s is not exists\n", cmd);
811 name = node_name(s_info.targetdir);
813 if (!strcmp(name, "package")) {
814 if (s_info.cmd == NOP) {
819 printf("Waiting the server response\n");
821 } else if (!strcmp(name, "provider")) {
822 if (s_info.cmd == NOP) {
827 printf("Waiting the server response\n");
832 parent = node_parent(s_info.targetdir);
833 if (parent && node_name(parent)) {
834 if (!strcmp(node_name(parent), "package")) {
835 if (s_info.cmd != NOP) {
836 printf("Waiting the server response\n");
840 send_inst_list(name);
849 static inline int do_cd(const char *cmd)
853 while (*cmd && *cmd == ' ') {
861 if (s_info.cmd != NOP) {
862 printf("Waiting the server response\n");
866 s_info.targetdir = update_target_dir(cmd);
867 if (!s_info.targetdir) {
868 printf("%s is not exists\n", cmd);
872 if (node_type(s_info.targetdir) != NODE_DIR) {
873 printf("Unable change directory to %s\n", cmd);
877 if (!(node_mode(s_info.targetdir) & NODE_EXEC)) {
878 printf("Access denied %s\n", cmd);
882 s_info.curdir = s_info.targetdir;
886 static inline int do_rm(const char *cmd)
889 while (*cmd && *cmd == ' ') cmd++;
894 if (s_info.cmd != NOP) {
895 printf("Waiting the server response\n");
899 s_info.targetdir = update_target_dir(cmd);
900 if (!s_info.targetdir) {
901 printf("%s is not exists\n", cmd);
905 if (!(node_mode(s_info.targetdir) & NODE_WRITE)) {
906 printf("Access denied %s\n", cmd);
914 static inline int do_fault(const char *cmd)
917 while (*cmd && *cmd == ' ') cmd++;
922 if (s_info.cmd != NOP) {
923 printf("Waiting the server response\n");
927 s_info.targetdir = update_target_dir(cmd);
928 if (!s_info.targetdir) {
929 printf("%s is not exists\n", cmd);
933 if (!(node_mode(s_info.targetdir) & NODE_WRITE)) {
934 printf("Access denied %s\n", cmd);
942 #if !defined(WCOREDUMP)
943 #define WCOREDUMP(a) 0
946 static void do_sh(const char *cmd)
952 while (*cmd && *cmd == ' ') {
966 while (idx < (sizeof(command) - 1) && *cmd && *cmd != ' ') {
967 command[idx++] = *cmd++;
971 if (execl(command, cmd, NULL) < 0) {
972 printf("Failed to execute: %s\n", strerror(errno));
976 } else if (pid < 0) {
977 printf("Failed to create a new process: %s\n", strerror(errno));
980 if (waitpid(pid, &status, 0) < 0) {
981 printf("error: %s\n", strerror(errno));
983 if (WIFEXITED(status)) {
984 printf("Exit: %d\n", WEXITSTATUS(status));
985 } else if (WIFSIGNALED(status)) {
986 printf("Terminated by %d %s\n", WTERMSIG(status), WCOREDUMP(status) ? " - core generated" : "");
987 } else if (WIFSTOPPED(status)) {
988 printf("Stopped by %d\n", WSTOPSIG(status));
989 } else if (WIFCONTINUED(status)) {
990 printf("Child is resumed\n");
996 static inline int get_pixmap_size(Display *disp, Pixmap id, int *x, int *y, unsigned int *w, unsigned int *h)
999 unsigned int dummy_border, dummy_depth;
1011 if (!XGetGeometry(disp, id, &dummy_win, x, y, w, h, &dummy_border, &dummy_depth)) {
1018 static inline int do_capture(Display *disp, Pixmap id, const char *filename)
1029 screen = DefaultScreenOfDisplay(disp);
1030 visual = DefaultVisualOfScreen(screen);
1032 if (get_pixmap_size(disp, id, NULL, NULL, &w, &h) < 0) {
1033 printf("Failed to get size of a pixmap\n");
1037 printf("Pixmap size: %dx%d\n", w, h);
1038 bufsz = w * h * sizeof(int);
1040 si.shmid = shmget(IPC_PRIVATE, bufsz, IPC_CREAT | 0666);
1042 printf("shmget: %s\n", strerror(errno));
1046 si.readOnly = False;
1047 si.shmaddr = shmat(si.shmid, NULL, 0);
1048 if (si.shmaddr == (void *)-1) {
1050 if (shmctl(si.shmid, IPC_RMID, 0) < 0) {
1051 printf("shmctl: %s\n", strerror(errno));
1059 * Use the 24 bits Pixmap for Video player
1061 xim = XShmCreateImage(disp, visual, 24 /* (depth << 3) */, ZPixmap, NULL, &si, w, h);
1063 if (shmdt(si.shmaddr) < 0) {
1064 printf("shmdt: %s\n", strerror(errno));
1067 if (shmctl(si.shmid, IPC_RMID, 0) < 0) {
1068 printf("shmctl: %s\n", strerror(errno));
1074 xim->data = si.shmaddr;
1075 XShmAttach(disp, &si);
1077 XShmGetImage(disp, id, xim, 0, 0, 0xFFFFFFFF);
1080 fd = open(filename, O_CREAT | O_RDWR, 0644);
1082 if (write(fd, xim->data, bufsz) != bufsz) {
1083 printf("Data is not fully written\n");
1086 if (close(fd) < 0) {
1087 printf("close: %s\n", strerror(errno));
1090 printf("Error: %sn\n", strerror(errno));
1093 XShmDetach(disp, &si);
1096 if (shmdt(si.shmaddr) < 0) {
1097 printf("shmdt: %s\n", strerror(errno));
1100 if (shmctl(si.shmid, IPC_RMID, 0) < 0) {
1101 printf("shmctl: %s\n", strerror(errno));
1107 static void do_x(const char *cmd)
1113 while (*cmd && *cmd == ' ') {
1121 disp = XOpenDisplay(NULL);
1123 printf("Failed to connect to the X\n");
1127 if (!strncasecmp(cmd, "damage ", 7)) {
1130 XserverRegion region;
1135 if (sscanf(cmd, "%u %d %d %d %d", &winId, &x, &y, &w, &h) != 5) {
1136 printf("Invalid argument\nx damage WINID_DEC X Y W H\n");
1143 region = XFixesCreateRegion(disp, &rect, 1);
1144 XDamageAdd(disp, winId, region);
1145 XFixesDestroyRegion(disp, region);
1148 printf("Damage: %u %d %d %d %d\n", winId, x, y, w, h);
1149 } else if (!strncasecmp(cmd, "capture ", 8)) {
1155 if (sscanf(cmd, "%u %255[^ ]", &winId, filename) != 2) {
1156 printf("Invalid argument\nx capture WINID_DEC FILENAME (%s)\n", cmd);
1159 if (do_capture(disp, winId, filename) == 0) {
1160 printf("Captured: %s\n", filename);
1162 } else if (!strncasecmp(cmd, "resize ", 7)) {
1169 if (sscanf(cmd, "%u %d %d", &winId, &w, &h) != 3) {
1170 printf("Invalid argument\nx resize WINID_DEC W H\n");
1174 XResizeWindow(disp, winId, w, h);
1175 printf("Resize: %u %d %d\n", winId, w, h);
1176 } else if (!strncasecmp(cmd, "move ", 5)) {
1182 if (sscanf(cmd, "%u %d %d", &winId, &x, &y) != 3) {
1183 printf("Invalid argument\nx move WINID_DEC X Y\n");
1187 XMoveWindow(disp, winId, x, y);
1188 printf("Move: %u %d %d\n", winId, x, y);
1189 } else if (!strncasecmp(cmd, "map ", 4)) {
1192 if (sscanf(cmd, "%u", &winId) != 1) {
1193 printf("Invalid argument\nx map WINID_DEC\n");
1196 XMapRaised(disp, winId);
1197 printf("Map: %u\n", winId);
1198 } else if (!strncasecmp(cmd, "unmap ", 6)) {
1201 if (sscanf(cmd, "%u", &winId) != 1) {
1202 printf("Invalid argument\nx unmap WINID_DEC\n");
1205 XUnmapWindow(disp, winId);
1206 printf("Unmap: %u\n", winId);
1208 printf("Unknown command\n");
1211 XCloseDisplay(disp);
1214 static inline void put_command(const char *cmd)
1216 if (s_info.history[s_info.history_top]) {
1217 free(s_info.history[s_info.history_top]);
1218 s_info.history[s_info.history_top] = NULL;
1221 s_info.history[s_info.history_top] = strdup(cmd);
1222 s_info.history_top = (s_info.history_top + !!s_info.history[s_info.history_top]) % (sizeof(s_info.history) / sizeof(s_info.history[0]));
1225 static inline const char *get_command(int idx)
1227 idx = s_info.history_top + idx;
1229 idx += (sizeof(s_info.history) / sizeof(s_info.history[0]));
1232 return s_info.history[idx];
1235 static inline void do_command(const char *cmd)
1237 /* Skip the first spaces */
1238 while (*cmd && *cmd == ' ') {
1242 if (strlen(cmd) && *cmd != '#') {
1243 if (!strncasecmp(cmd, "exit", 4) || !strncasecmp(cmd, "quit", 4)) {
1244 ecore_main_loop_quit();
1245 } else if (!strncasecmp(cmd, "set ", 4)) {
1246 if (do_set(cmd) == 0) {
1249 } else if (!strncasecmp(cmd, "stat ", 5)) {
1251 } else if (!strncasecmp(cmd, "get ", 4)) {
1252 if (do_get(cmd) == 0) {
1255 } else if (!strncasecmp(cmd, "ls", 2)) {
1256 if (do_ls(cmd) == 0) {
1259 } else if (!strncasecmp(cmd, "cd", 2)) {
1260 if (do_cd(cmd) == 0) {
1263 } else if (!strncasecmp(cmd, "rm", 2)) {
1264 if (do_rm(cmd) == 0) {
1267 } else if (!strncasecmp(cmd, "fault", 5)) {
1268 if (do_fault(cmd) == 0) {
1271 } else if (!strncasecmp(cmd, "sh ", 3)) {
1273 } else if (!strncasecmp(cmd, "x ", 2)) {
1284 static Eina_Bool input_cb(void *data, Ecore_Fd_Handler *fd_handler)
1287 static char cmd_buffer[256];
1291 const char escape_str[] = { 0x1b, 0x5b, 0x0 };
1292 const char *escape_ptr = escape_str;
1296 fd = ecore_main_fd_handler_fd_get(fd_handler);
1298 printf("FD is not valid: %d\n", fd);
1299 return ECORE_CALLBACK_CANCEL;
1302 fd = s_info.input_fd;
1307 * Using this routine, we can implement the command recommend algorithm.
1308 * When a few more characters are matched with history of command, we can show it to user
1309 * Then the user will choose one or write new command
1313 while ((ret = read(fd, &ch, sizeof(ch))) == sizeof(ch)) {
1314 if (*escape_ptr == '\0') {
1318 printf("%s2K%s1G", escape_str, escape_str);
1319 tmp = get_command(--s_info.history_idx);
1321 s_info.history_idx = 0;
1322 cmd_buffer[0] = '\0';
1325 strcpy(cmd_buffer, tmp);
1326 idx = strlen(cmd_buffer);
1330 case 0x42: /* DOWN */
1331 if (s_info.history_idx >= 0) {
1335 printf("%s2K%s1G", escape_str, escape_str);
1336 tmp = get_command(++s_info.history_idx);
1337 if (s_info.history_idx == 0) {
1338 s_info.history_idx = 0;
1339 cmd_buffer[0] = '\0';
1342 strcpy(cmd_buffer, tmp);
1343 idx = strlen(cmd_buffer);
1347 case 0x43: /* RIGHT */
1349 case 0x44: /* LEFT */
1355 escape_ptr = escape_str;
1357 } else if (ch == *escape_ptr) {
1363 case 0x08: /* BKSP */
1364 cmd_buffer[idx] = '\0';
1367 cmd_buffer[idx] = ' ';
1372 cmd_buffer[idx] = '\0';
1376 case 0x09: /* TAB */
1377 if (!s_info.quick_search_node) {
1378 s_info.quick_search_node = node_child(s_info.curdir);
1379 s_info.quick_idx = idx;
1381 s_info.quick_search_node = node_next_sibling(s_info.quick_search_node);
1382 idx = s_info.quick_idx;
1385 if (!s_info.quick_search_node) {
1389 printf("%s2K%s1G", escape_str, escape_str);
1390 strcpy(cmd_buffer + idx, node_name(s_info.quick_search_node));
1391 idx += strlen(node_name(s_info.quick_search_node));
1396 cmd_buffer[idx] = '\0';
1398 if (s_info.input_fd == STDIN_FILENO || s_info.verbose) {
1399 putc((int)'\n', stdout);
1401 do_command(cmd_buffer);
1402 put_command(cmd_buffer);
1403 memset(cmd_buffer, 0, sizeof(cmd_buffer));
1404 s_info.history_idx = 0;
1405 s_info.quick_search_node = NULL;
1407 /* Make a main loop processing for command handling */
1408 return ECORE_CALLBACK_RENEW;
1410 cmd_buffer[idx++] = ch;
1412 if (s_info.input_fd == STDIN_FILENO || s_info.verbose) {
1413 putc((int)ch, stdout);
1416 if (idx == sizeof(cmd_buffer) - 1) {
1417 cmd_buffer[idx] = '\0';
1418 printf("\nCommand buffer is overflow: %s\n", cmd_buffer);
1425 if (ret < 0 && !fd_handler) {
1426 ecore_main_loop_quit();
1429 return ECORE_CALLBACK_RENEW;
1432 static void processing_line_buffer(const char *buffer)
1435 char slavename[256];
1453 struct package *pkginfo;
1454 struct instance *instinfo;
1455 struct slave *slaveinfo;
1458 switch (s_info.cmd) {
1460 if (sscanf(buffer, "%d %255[^ ] %255[^ ] %255[^ ] %d %d %d", &pid, slavename, pkgname, abi, &refcnt, &fault_count, &list_count) != 7) {
1461 printf("Invalid format : [%s]\n", buffer);
1465 node = node_find(s_info.targetdir, pkgname);
1467 pkginfo = calloc(1, sizeof(*pkginfo));
1469 printf("Error: %s\n", strerror(errno));
1473 pkginfo->pkgid = strdup("conf.file");
1474 if (!pkginfo->pkgid) {
1475 printf("Error: %s\n", strerror(errno));
1478 pkginfo->primary = 1;
1480 node = node_create(s_info.targetdir, pkgname, NODE_DIR);
1482 free(pkginfo->pkgid);
1484 printf("Failed to create a new node (%s)\n", pkgname);
1488 node_set_mode(node, NODE_READ | NODE_EXEC);
1489 node_set_data(node, pkginfo);
1491 pkginfo = node_data(node);
1493 printf("Package info is inavlid\n");
1497 free(pkginfo->slavename);
1500 pkginfo->slavename = NULL;
1501 pkginfo->abi = NULL;
1504 node_set_age(node, s_info.age);
1506 pkginfo->slavename = strdup(slavename);
1507 if (!pkginfo->slavename) {
1508 printf("Error: %s\n", strerror(errno));
1511 pkginfo->abi = strdup(abi);
1512 if (!pkginfo->abi) {
1513 printf("Error: %s\n", strerror(errno));
1517 pkginfo->refcnt = refcnt;
1518 pkginfo->fault_count = fault_count;
1519 pkginfo->inst_count = list_count;
1522 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) {
1523 printf("Invalid format : [%s]\n", buffer);
1526 node = node_find(s_info.targetdir, slavename);
1528 slaveinfo = calloc(1, sizeof(*slaveinfo));
1530 printf("Error: %s\n", strerror(errno));
1534 node = node_create(s_info.targetdir, slavename, NODE_DIR);
1540 node_set_mode(node, NODE_READ | NODE_EXEC);
1541 node_set_data(node, slaveinfo);
1543 slaveinfo = node_data(node);
1546 node_set_age(node, s_info.age);
1548 free(slaveinfo->pkgname);
1549 free(slaveinfo->abi);
1550 free(slaveinfo->state);
1552 slaveinfo->pkgname = strdup(pkgname);
1553 if (!slaveinfo->pkgname) {
1554 printf("Error: %s\n", strerror(errno));
1557 slaveinfo->abi = strdup(abi);
1558 if (!slaveinfo->abi) {
1559 printf("Error: %s\n", strerror(errno));
1562 slaveinfo->state = strdup(state);
1563 if (!slaveinfo->state) {
1564 printf("Error: %s\n", strerror(errno));
1567 slaveinfo->pid = pid;
1568 slaveinfo->secured = secured;
1569 slaveinfo->refcnt = refcnt;
1570 slaveinfo->fault_count = fault_count;
1571 slaveinfo->loaded_inst = loaded_inst;
1572 slaveinfo->loaded_pkg = loaded_pkg;
1573 slaveinfo->ttl = ttl;
1576 if (sscanf(buffer, "%[^ ] %[^ ] %[^ ] %lf %[^ ] %d %d", inst_id, cluster, category, &period, state, &width, &height) != 7) {
1577 printf("Invalid format : [%s]\n", buffer);
1581 for (i = strlen(inst_id); i > 0 && inst_id[i] != '/'; i--);
1582 i += (inst_id[i] == '/');
1584 node = node_find(s_info.targetdir, inst_id + i);
1586 instinfo = calloc(1, sizeof(*instinfo));
1588 printf("Error: %s\n", strerror(errno));
1592 node = node_create(s_info.targetdir, inst_id + i, NODE_FILE);
1598 node_set_mode(node, NODE_READ | NODE_WRITE);
1599 node_set_data(node, instinfo);
1601 instinfo = node_data(node);
1604 node_set_age(node, s_info.age);
1607 free(instinfo->cluster);
1608 free(instinfo->category);
1609 free(instinfo->state);
1611 instinfo->id = strdup(inst_id);
1612 if (!instinfo->id) {
1613 printf("Error: %s\n", strerror(errno));
1616 instinfo->cluster = strdup(cluster);
1617 if (!instinfo->cluster) {
1618 printf("Error: %s\n", strerror(errno));
1621 instinfo->category = strdup(category);
1622 if (!instinfo->category) {
1623 printf("Error: %s\n", strerror(errno));
1626 instinfo->state = strdup(state);
1627 if (!instinfo->state) {
1628 printf("Error: %s\n", strerror(errno));
1631 instinfo->period = period;
1632 instinfo->width = width;
1633 instinfo->height = height;
1636 sscanf(buffer, "%d", &i);
1637 printf("%s\n", strerror(i));
1638 printf("Result: %d\n", i);
1641 sscanf(buffer, "%d", &i);
1642 printf("Result: %d\n", i);
1645 sscanf(buffer, "%d", &i);
1646 printf("Result: %d\n", i);
1653 static inline void do_line_command(void)
1655 switch (s_info.cmd) {
1677 static Eina_Bool read_cb(void *data, Ecore_Fd_Handler *fd_handler)
1680 static char *line_buffer = NULL;
1681 static int line_index = 0;
1682 static int bufsz = 256;
1685 fd = ecore_main_fd_handler_fd_get(fd_handler);
1687 printf("FD is not valid: %d\n", fd);
1688 return ECORE_CALLBACK_CANCEL;
1691 if (read(fd, &ch, sizeof(ch)) != sizeof(ch)) {
1692 printf("Error: %s\n", strerror(errno));
1693 return ECORE_CALLBACK_CANCEL;
1698 line_buffer = malloc(bufsz);
1700 printf("Error: %s\n", strerror(errno));
1701 return ECORE_CALLBACK_CANCEL;
1705 if (ch == '\n') { /* End of a line */
1706 if (line_index == bufsz - 1) {
1708 new_buf = realloc(line_buffer, bufsz + 2);
1710 printf("Error: %s\n", strerror(errno));
1715 return ECORE_CALLBACK_CANCEL;
1718 line_buffer = new_buf;
1721 line_buffer[line_index] = '\0';
1723 if (!strcmp(line_buffer, "EOD")) {
1727 processing_line_buffer(line_buffer);
1737 line_buffer[line_index++] = ch;
1738 if (line_index == bufsz - 1) {
1740 new_buf = realloc(line_buffer, bufsz);
1742 printf("Error: %s\n", strerror(errno));
1747 return ECORE_CALLBACK_CANCEL;
1750 line_buffer = new_buf;
1754 return ECORE_CALLBACK_RENEW;
1757 static int ret_cb(pid_t pid, int handle, const struct packet *packet, void *data)
1759 const char *fifo_name;
1762 if (packet_get(packet, "si", &fifo_name, &ret) != 2) {
1763 printf("Invalid packet\n");
1768 printf("Returns %d\n", ret);
1772 printf("FIFO: %s\n", fifo_name);
1774 s_info.fifo_handle = open(fifo_name, O_RDONLY | O_NONBLOCK);
1775 if (s_info.fifo_handle < 0) {
1776 printf("Error: %s\n", strerror(errno));
1777 s_info.fifo_handle = -EINVAL;
1778 ecore_main_loop_quit();
1782 s_info.fd_handler = ecore_main_fd_handler_add(s_info.fifo_handle, ECORE_FD_READ, read_cb, NULL, NULL, NULL);
1783 if (!s_info.fd_handler) {
1784 printf("Failed to add a fd handler\n");
1785 if (close(s_info.fifo_handle) < 0) {
1786 printf("close: %s\n", strerror(errno));
1788 s_info.fifo_handle = -EINVAL;
1789 ecore_main_loop_quit();
1795 if (s_info.input_fd == STDIN_FILENO) {
1796 if (fcntl(s_info.input_fd, F_SETFL, O_NONBLOCK) < 0) {
1797 printf("Error: %s\n", strerror(errno));
1800 s_info.in_handler = ecore_main_fd_handler_add(s_info.input_fd, ECORE_FD_READ, input_cb, NULL, NULL, NULL);
1801 if (!s_info.in_handler) {
1802 printf("Failed to add a input handler\n");
1803 ecore_main_loop_quit();
1811 static int disconnected_cb(int handle, void *data)
1813 printf("Disconnected\n");
1814 ecore_main_loop_quit();
1818 static int connected_cb(int handle, void *data)
1820 struct packet *packet;
1822 printf("Connected\n");
1824 packet = packet_create("liveinfo_hello", "d", 0.0f);
1826 printf("Failed to build a packet for hello\n");
1827 com_core_packet_client_fini(s_info.fd);
1828 s_info.fd = -EINVAL;
1834 if (com_core_packet_async_send(s_info.fd, packet, 0.0f, ret_cb, NULL) < 0) {
1835 printf("Failed to send a packet hello\n");
1836 packet_destroy(packet);
1837 com_core_packet_client_fini(s_info.fd);
1838 s_info.fd = -EINVAL;
1842 packet_destroy(packet);
1846 int main(int argc, char *argv[])
1848 struct termios ttystate;
1849 static struct method s_table[] = {
1855 static struct option long_options[] = {
1856 { "batchmode", required_argument, 0, 'b' },
1857 { "help", no_argument, 0, 'h' },
1858 { "verbose", required_argument, 0, 'v' },
1859 { "execute", required_argument, 0, 'x' },
1866 c = getopt_long(argc, argv, "b:hv:", long_options, &option_index);
1869 if (!optarg || !*optarg) {
1870 printf("Invalid argument\n");
1875 if (s_info.input_fd != STDIN_FILENO) {
1876 /* Close the previously, opened file */
1877 if (close(s_info.input_fd) < 0) {
1878 printf("close: %s\n", strerror(errno));
1882 s_info.input_fd = open(optarg, O_RDONLY);
1883 if (s_info.input_fd < 0) {
1884 printf("Unable to access %s (%s)\n", optarg, strerror(errno));
1892 if (!optarg || !*optarg) {
1893 printf("Invalid argument\n");
1898 s_info.verbose = !strcmp(optarg, "true");
1901 if (!optarg || !*optarg) {
1902 printf("Invalid argument\n");
1914 #if (GLIB_MAJOR_VERSION <= 2 && GLIB_MINOR_VERSION < 36)
1918 com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1919 com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1920 livebox_service_init();
1922 s_info.fd = com_core_packet_client_init(SOCKET_FILE, 0, s_table);
1923 if (s_info.fd < 0) {
1924 printf("Failed to make a connection\n");
1928 if (s_info.input_fd == STDIN_FILENO) {
1929 printf("Type your command on below empty line\n");
1931 if (tcgetattr(s_info.input_fd, &ttystate) < 0) {
1932 printf("Error: %s\n", strerror(errno));
1934 ttystate.c_lflag &= ~(ICANON | ECHO);
1935 ttystate.c_cc[VMIN] = 1;
1937 if (tcsetattr(s_info.input_fd, TCSANOW, &ttystate) < 0) {
1938 printf("Error: %s\n", strerror(errno));
1942 printf("Batch mode enabled\n");
1945 if (setvbuf(stdout, (char *)NULL, _IONBF, 0) != 0) {
1946 printf("Error: %s\n", strerror(errno));
1951 ecore_main_loop_begin();
1954 livebox_service_fini();
1956 if (s_info.fd > 0) {
1957 com_core_packet_client_fini(s_info.fd);
1958 s_info.fd = -EINVAL;
1961 if (s_info.fd_handler) {
1962 ecore_main_fd_handler_del(s_info.fd_handler);
1963 s_info.fd_handler = NULL;
1966 if (s_info.input_fd == STDIN_FILENO) {
1967 ttystate.c_lflag |= ICANON | ECHO;
1968 if (tcsetattr(s_info.input_fd, TCSANOW, &ttystate) < 0) {
1969 printf("Error: %s\n", strerror(errno));
1972 if (close(s_info.input_fd) < 0) {
1973 printf("close: %s\n", strerror(errno));
1977 if (s_info.fifo_handle > 0) {
1978 if (close(s_info.fifo_handle) < 0) {
1979 printf("close: %s\n", strerror(errno));
1981 s_info.fifo_handle = -EINVAL;
1984 if (s_info.in_handler) {
1985 ecore_main_fd_handler_del(s_info.in_handler);
1986 s_info.in_handler = NULL;
1990 putc((int)'\n', stdout);