Update liveinfo utility
authorSung-jae Park <nicesj.park@samsung.com>
Fri, 25 Jan 2013 03:30:37 +0000 (03:30 +0000)
committerSung-jae Park <nicesj.park@samsung.com>
Fri, 25 Jan 2013 04:37:22 +0000 (04:37 +0000)
Change-Id: Id8a766314844aea0a991694e5498f04bc34d6ee8

src/server.c
util_liveinfo/src/liveinfo.c

index 6997be4..ac13fe3 100644 (file)
@@ -4224,9 +4224,6 @@ static struct packet *liveinfo_slave_list(pid_t pid, int handle, const struct pa
        if (!fp)
                goto out;
 
-       fprintf(fp, "----------------------------------------------------------------------[Slave List]------------------------------------------------------------------------------\n");
-       fprintf(fp, "    pid          slave name                     package name                   abi     secured   refcnt   fault           state           inst   pkg     ttl    \n");
-       fprintf(fp, "----------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
        list = (Eina_List *)slave_list();
        EINA_LIST_FOREACH(list, l, slave) {
                fprintf(fp, "  %7d   %20s   %39s   %7s   %7s   %6d   %5d   %21s   %4d   %3d   %3.4lf  \n", 
@@ -4243,6 +4240,7 @@ static struct packet *liveinfo_slave_list(pid_t pid, int handle, const struct pa
                        slave_ttl(slave)
                );
        }
+       fprintf(fp, "EOD\n");
        liveinfo_close_fifo(info);
 
 out:
@@ -4287,7 +4285,7 @@ static struct packet *liveinfo_slave_load(pid_t pid, int handle, const struct pa
                if (package_slave(pkg) == slave)
                        fprintf(fp, "%s, ", package_name(pkg));
        }
-       fprintf(fp, "}\n");
+       fprintf(fp, "}\nEOD\n");
 
        liveinfo_close_fifo(info);
 
@@ -4351,10 +4349,6 @@ static struct packet *liveinfo_inst_list(pid_t pid, int handle, const struct pac
                goto out;
        }
 
-       fprintf(fp, "-----------------------------------------------[Instance List]---------------------------------------\n");
-       fprintf(fp, "         ID         |      Cluster ID    |   Sub cluster ID   | Period | Visibility | Width | Height \n");
-       fprintf(fp, "-----------------------------------------------------------------------------------------------------\n");
-
        inst_list = package_instance_list(pkg);
        EINA_LIST_FOREACH(inst_list, l, inst) {
                fprintf(fp, " %18s %18s %18s %3.3lf %10s %5d %6d\n",
@@ -4367,6 +4361,7 @@ static struct packet *liveinfo_inst_list(pid_t pid, int handle, const struct pac
                                                instance_lb_height(inst));
        }
 
+       fprintf(fp, "EOD\n");
        liveinfo_close_fifo(info);
 
 out:
@@ -4402,9 +4397,6 @@ static struct packet *liveinfo_pkg_list(pid_t pid, int handle, const struct pack
        if (!fp)
                goto out;
 
-       fprintf(fp, "+----------------------------------------------[Package List]------------------------------------------------+\n");
-       fprintf(fp, "    pid          slave name                     package name                   abi     refcnt   fault   inst  \n");
-       fprintf(fp, "+------------------------------------------------------------------------------------------------------------+\n");
        list = (Eina_List *)package_list();
        EINA_LIST_FOREACH(list, l, pkg) {
                slave = package_slave(pkg);
@@ -4429,6 +4421,7 @@ static struct packet *liveinfo_pkg_list(pid_t pid, int handle, const struct pack
                        eina_list_count(inst_list)
                );
        }
+       fprintf(fp, "EOD\n");
        liveinfo_close_fifo(info);
 
 out:
@@ -4445,6 +4438,36 @@ static struct packet *liveinfo_pkg_ctrl(pid_t pid, int handle, const struct pack
        return NULL;
 }
 
+static struct packet *liveinfo_toggle_debug(pid_t pid, int handle, const struct packet *packet)
+{
+       double timestamp;
+       struct liveinfo *info;
+       FILE *fp;
+
+       if (packet_get(packet, "d", &timestamp) != 1) {
+               ErrPrint("Invalid argument\n");
+               goto out;
+       }
+
+       info = liveinfo_find_by_pid(pid);
+       if (!info) {
+               ErrPrint("Invalid request\n");
+               goto out;
+       }
+
+       liveinfo_open_fifo(info);
+       fp = liveinfo_fifo(info);
+       if (!fp)
+               goto out;
+
+       g_conf.debug_mode = !g_conf.debug_mode;
+       fprintf(fp, "Debug mode is %s\nEOD\n", g_conf.debug_mode ? "enabled" : "disabled");
+       liveinfo_close_fifo(info);
+
+out:
+       return NULL;
+}
+
 static struct method s_info_table[] = {
        {
                .cmd = "liveinfo_hello",
@@ -4475,6 +4498,10 @@ static struct method s_info_table[] = {
                .handler = liveinfo_pkg_ctrl,
        },
        {
+               .cmd = "toggle_debug",
+               .handler = liveinfo_toggle_debug,
+       },
+       {
                .cmd = NULL,
                .handler = NULL,
        },
index ab3dcf8..41f82ad 100644 (file)
@@ -21,6 +21,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <termios.h>
 
 #include <glib.h>
 #include <glib-object.h>
 
 #include <Ecore.h>
 
+#define PROMPT "liveinfo) "
+
 static struct info {
        int fifo_handle;
        int fd;
        Ecore_Fd_Handler *fd_handler;
        Ecore_Fd_Handler *in_handler;
+       int input_fd;
 } s_info = {
        .fifo_handle = -EINVAL,
        .fd = -EINVAL,
        .fd_handler = NULL,
        .in_handler = NULL,
+       .input_fd = -1,
 };
 
 static void send_slave_list(void)
 {
        struct packet *packet;
 
-       printf("Send request SLAVE LIST\n");
        packet = packet_create_noack("slave_list", "d", 0.0f);
        if (!packet) {
                fprintf(stderr, "Failed to create a packet\n");
@@ -56,13 +60,16 @@ static void send_slave_list(void)
 
        com_core_packet_send_only(s_info.fd, packet);
        packet_destroy(packet);
+
+       printf("----------------------------------------------------------------------[Slave List]------------------------------------------------------------------------------\n");
+       printf("    pid          slave name                     package name                   abi     secured   refcnt   fault           state           inst   pkg     ttl    \n");
+       printf("----------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
 }
 
 static void send_pkg_list(void)
 {
        struct packet *packet;
 
-       printf("Send request PACKAGE LIST\n");
        packet = packet_create_noack("pkg_list", "d", 0.0f);
        if (!packet) {
                fprintf(stderr, "Failed to create a packet\n");
@@ -71,13 +78,30 @@ static void send_pkg_list(void)
 
        com_core_packet_send_only(s_info.fd, packet);
        packet_destroy(packet);
+
+       printf("+----------------------------------------------[Package List]------------------------------------------------+\n");
+       printf("    pid          slave name                     package name                   abi     refcnt   fault   inst  \n");
+       printf("+------------------------------------------------------------------------------------------------------------+\n");
+}
+
+static void send_toggle_debug(void)
+{
+       struct packet *packet;
+
+       packet = packet_create_noack("toggle_debug", "d", 0.0f);
+       if (!packet) {
+               fprintf(stderr, "Failed to create a packet\n");
+               return;
+       }
+
+       com_core_packet_send_only(s_info.fd, packet);
+       packet_destroy(packet);
 }
 
 static void send_slave_load(pid_t pid)
 {
        struct packet *packet;
 
-       printf("Send request Loaded package list\n");
        packet = packet_create_noack("slave_load", "i", pid);
        if (!packet) {
                fprintf(stderr, "Failed to create a packet\n");
@@ -92,7 +116,6 @@ static void send_inst_list(const char *pkgname)
 {
        struct packet *packet;
 
-       printf("Send request instance list\n");
        packet = packet_create_noack("inst_list", "s", pkgname);
        if (!packet) {
                fprintf(stderr, "Failed to create a packet\n");
@@ -101,6 +124,10 @@ static void send_inst_list(const char *pkgname)
 
        com_core_packet_send_only(s_info.fd, packet);
        packet_destroy(packet);
+
+       printf("-----------------------------------------------[Instance List]---------------------------------------\n");
+       printf("         ID         |      Cluster ID    |   Sub cluster ID   | Period | Visibility | Width | Height \n");
+       printf("-----------------------------------------------------------------------------------------------------\n");
 }
 
 static inline void help(void)
@@ -111,6 +138,7 @@ static inline void help(void)
        printf("\e[33mslave_list\e[0m - Display the slave list\n");
        printf("\e[33minst_list\e[0m \e[37mLIVEBOX_PKGNAME\e[0m - Display the instance list of this LIVEBOX_PKGNAME\n");
        printf("\e[33mslave_load\e[0m \e[37mSLAVE_PID\e[0m - Display the loaded livebox instance list on the given slave\n");
+       printf("\e[33mtoggle_debug\e[0m - Enable/Disable debug mode\n");
        printf("\e[32mexit - \e[0m\n");
        printf("\e[32mquit - \e[0m\n");
        printf("----------------------------------------------------------------------------\n");
@@ -119,30 +147,40 @@ static inline void help(void)
 static inline void do_command(const char *cmd)
 {
        char command[256];
-       char argument[256];
-
-       if (sscanf(cmd, "%255[^ ] %255s", command, argument) == 2) {
-               if (!strcasecmp(command, "inst_list")) {
-                       send_inst_list(argument);
-               } else if (!strcasecmp(command, "slave_load")) {
-                       pid_t pid;
-                       if (sscanf(argument, "%d", &pid) == 1)
-                               send_slave_load(pid);
-               } else {
-                       help();
-               }
+       char argument[256] = { '\0', };
+
+       if (sscanf(cmd, "%255[^ ] %255s", command, argument) == 2)
+               cmd = command;
+
+       if (!strcasecmp(cmd, "inst_list") && *argument) {
+               send_inst_list(argument);
+       } else if (!strcasecmp(cmd, "slave_load") && *argument) {
+               pid_t pid;
+
+               if (sscanf(argument, "%d", &pid) == 1)
+                       send_slave_load(pid);
+               else
+                       goto errout;
+       } else if (!strcasecmp(cmd, "pkg_list")) {
+               send_pkg_list();
+       } else if (!strcasecmp(cmd, "slave_list")) {
+               send_slave_list();
+       } else if (!strcasecmp(cmd, "exit") || !strcasecmp(cmd, "quit")) {
+               ecore_main_loop_quit();
+       } else if (!strcasecmp(cmd, "toggle_debug")) {
+               send_toggle_debug();
+       } else if (!strcasecmp(cmd, "help")) {
+               goto errout;
        } else {
-               if (!strcasecmp(cmd, "pkg_list"))
-                       send_pkg_list();
-               else if (!strcasecmp(cmd, "slave_list"))
-                       send_slave_list();
-               else if (!strcasecmp(cmd, "exit"))
-                       ecore_main_loop_quit();
-               else if (!strcasecmp(cmd, "quit"))
-                       ecore_main_loop_quit();
-               else if (!strcasecmp(cmd, "help"))
-                       help();
+               printf("Unknown command - \"help\"\n");
+               fputs(PROMPT, stdout);
        }
+
+       return;
+
+errout:
+       help();
+       fputs(PROMPT, stdout);
 }
 
 static Eina_Bool input_cb(void *data, Ecore_Fd_Handler *fd_handler)
@@ -171,19 +209,25 @@ static Eina_Bool input_cb(void *data, Ecore_Fd_Handler *fd_handler)
 
        switch (ch) {
        case 0x08: /* BKSP */
-               printf("\033[3D");
+               cmd_buffer[idx] = '\0';
                if (idx > 0)
                        idx--;
+               cmd_buffer[idx] = ' ';
+               printf("\r"PROMPT"%s", cmd_buffer); /* Cleare the last bytes */
+               cmd_buffer[idx] = '\0';
+               printf("\r"PROMPT"%s", cmd_buffer); /* Cleare the last bytes */
                break;
        case '\n':
        case '\r':
                cmd_buffer[idx] = '\0';
                idx = 0;
-               printf("\n");
+               putc((int)'\n', stdout);
                do_command(cmd_buffer);
+               memset(cmd_buffer, 0, sizeof(cmd_buffer));
                break;
        default:
                cmd_buffer[idx++] = ch;
+               putc((int)ch, stdout);
                if (idx == sizeof(cmd_buffer) - 1) {
                        cmd_buffer[idx] = '\0';
                        printf("\nCommand buffer is overflow: %s\n", cmd_buffer);
@@ -198,8 +242,8 @@ static Eina_Bool input_cb(void *data, Ecore_Fd_Handler *fd_handler)
 static Eina_Bool read_cb(void *data, Ecore_Fd_Handler *fd_handler)
 {
        int fd;
-       char buffer[1024];
-       int len;
+       static const char *eod = "EOD\n";
+       char ch;
 
        fd = ecore_main_fd_handler_fd_get(fd_handler);
        if (fd < 0) {
@@ -207,12 +251,18 @@ static Eina_Bool read_cb(void *data, Ecore_Fd_Handler *fd_handler)
                return ECORE_CALLBACK_RENEW;
        }
 
-       while ((len = read(fd, buffer, sizeof(buffer) - 1)) > 0) {
-               buffer[len] = '\0';
-               fputs(buffer, stdout);
-       }
+       read(fd, &ch, sizeof(ch));
+       if (ch == *eod)
+               eod++;
+       else
+               eod = "EOD\n";
+
+       putc(ch, stdout);
 
-       fflush(stdout);
+       if (*eod == '\0') {
+               fputs(PROMPT, stdout);
+               eod = "EOD\n";
+       }
 
        return ECORE_CALLBACK_RENEW;
 }
@@ -251,6 +301,10 @@ static int ret_cb(pid_t pid, int handle, const struct packet *packet, void *data
                return -EFAULT;
        }
 
+       fputs(PROMPT, stdout);
+        if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) < 0)
+                fprintf(stderr, "Error: %s\n", strerror(errno));
+
        s_info.in_handler = ecore_main_fd_handler_add(STDIN_FILENO, ECORE_FD_READ, input_cb, NULL, NULL, NULL);
        if (!s_info.in_handler) {
                fprintf(stderr, "Failed to add a input handler\n");
@@ -298,6 +352,7 @@ static int connected_cb(int handle, void *data)
 
 int main(int argc, char *argv[])
 {
+       struct termios ttystate;
        static struct method s_table[] = {
                {
                        .cmd = NULL,
@@ -319,8 +374,25 @@ int main(int argc, char *argv[])
 
        printf("Type your command on below empty line\n");
 
+       if (tcgetattr(STDIN_FILENO, &ttystate) < 0) {
+               fprintf(stderr, "Error: %s\n", strerror(errno));
+       } else {
+               ttystate.c_lflag &= ~(ICANON | ECHO);
+               ttystate.c_cc[VMIN] = 1;
+
+               if (tcsetattr(STDIN_FILENO, TCSANOW, &ttystate) < 0)
+                       fprintf(stderr, "Error: %s\n", strerror(errno));
+       }
+
+       if (setvbuf(stdout, (char *)NULL, _IONBF, 0) != 0)
+               fprintf(stderr, "Error: %s\n", strerror(errno));
+
        ecore_main_loop_begin();
 
+       ttystate.c_lflag |= ICANON | ECHO;
+       if (tcsetattr(STDIN_FILENO, TCSANOW, &ttystate) < 0)
+               fprintf(stderr, "Error: %s\n", strerror(errno));
+
        if (s_info.fd > 0) {
                com_core_packet_client_fini(s_info.fd);
                s_info.fd = -EINVAL;