murphy-console: use breedline for prompt handling.
authorKrisztian Litkey <krisztian.litkey@intel.com>
Mon, 26 Nov 2012 15:43:55 +0000 (17:43 +0200)
committerKrisztian Litkey <krisztian.litkey@intel.com>
Mon, 26 Nov 2012 16:11:42 +0000 (18:11 +0200)
configure.ac
src/Makefile.am
src/console-client/client.c

index 47c949f..701dd0a 100644 (file)
@@ -233,8 +233,7 @@ AC_ARG_ENABLE(console,
 if test "$enable_console" = "no"; then
     AC_MSG_NOTICE([Murphy console binary is disabled.])
 else
-    AC_MSG_NOTICE([Murphy console binary is currently forcibly disabled.])
-    enable_console=no
+    AC_MSG_NOTICE([Murphy console binary is enabled.])
 fi
 
 if test "$enable_console" = "yes"; then
index 31146c8..4c47ade 100644 (file)
@@ -1134,6 +1134,8 @@ murphy_console_CFLAGS  =          \
                $(AM_CFLAGS)
 
 murphy_console_LDADD  =                        \
+               libbreedline-murphy.la  \
+               libbreedline.la         \
                libmurphy-common.la
 
 if DBUS_ENABLED
index 081a08b..3154a1e 100644 (file)
 #include <sys/types.h>
 #include <sys/socket.h>
 
-#include <readline/readline.h>
-#include <readline/history.h>
-
 #include <murphy/common.h>
 #include <murphy/plugins/console-protocol.h>
 
+#include <breedline/breedline-murphy.h>
+
 #define client_info  mrp_log_info
 #define client_warn  mrp_log_warning
 #define client_error mrp_log_error
 
-#define DEFAULT_PROMPT  "murphy"
+#define DEFAULT_PROMPT  "murphy"
 #define DEFAULT_ADDRESS "tcp4:127.0.0.1:3000"
 
-static void input_process_cb(char *input);
 
 /*
  * message types
@@ -84,57 +82,16 @@ typedef struct {
 
 typedef struct {
     mrp_mainloop_t  *ml;                 /* murphy mainloop */
-    int              in;                 /* terminal input */
-    mrp_io_watch_t  *inw;                /* input I/O watch */
     mrp_transport_t *t;                  /* transport to server */
-    int             seqno;               /* sequence number */
-    recvbuf_t       buf;                 /* receive buffer */
-    char            prompt_str[256];
-    int             prompt_len;
+    int              seqno;              /* sequence number */
+    recvbuf_t        buf;                /* receive buffer */
+    brl_t           *brl;                /* breedline for terminal input */
 } client_t;
 
 
-
-/*
- * prompt handling and readline glue
- */
-
-/*
- * Although readline has an interface for mainloop integration, it does
- * not support opaque 'user_data' in the callbacks so one cannot pass in
- * any application-specific data. We need to store the client context in
- * this global just for that...
- */
-static client_t *rl_client;
-
-
-void prompt_set(client_t *c, const char *prompt)
-{
-    strncpy(c->prompt_str, prompt, sizeof(c->prompt_str) - 1);
-    c->prompt_len = strlen(c->prompt_str);
-}
-
-
-void prompt_erase(client_t *c)
-{
-    int n = c->prompt_len;
-
-    printf("\r");
-    while (n-- > 0)
-        printf(" ");
-    printf("\r");
-}
-
-
-void prompt_display(client_t *c)
-{
-    rl_callback_handler_remove();
-    rl_callback_handler_install(c->prompt_str, input_process_cb);
-}
-
-
-void prompt_process_input(client_t *c, char *input)
+void input_cb(brl_t *brl, const char *input, void *user_data)
 {
+    client_t  *c = (client_t *)user_data;
     mrp_msg_t *msg;
     uint16_t   tag, type;
     uint32_t   len;
@@ -142,8 +99,8 @@ void prompt_process_input(client_t *c, char *input)
     len = input ? strlen(input) + 1: 0;
 
     if (len > 1) {
-        add_history(input);
-        prompt_erase(c);
+        brl_add_history(brl, input);
+        brl_hide_prompt(brl);
 
         tag  = MRP_CONSOLE_INPUT;
         type = MRP_MSG_FIELD_BLOB;
@@ -154,7 +111,7 @@ void prompt_process_input(client_t *c, char *input)
             mrp_msg_unref(msg);
         }
 
-        prompt_display(c);
+        brl_show_prompt(brl);
         return;
     }
 
@@ -162,43 +119,21 @@ void prompt_process_input(client_t *c, char *input)
 }
 
 
-static void input_cb(mrp_mainloop_t *ml, mrp_io_watch_t *w, int fd,
-                     mrp_io_event_t events, void *user_data)
-{
-    MRP_UNUSED(w);
-    MRP_UNUSED(fd);
-    MRP_UNUSED(user_data);
-
-    if (events & MRP_IO_EVENT_IN)
-        rl_callback_read_char();
-
-    if (events & MRP_IO_EVENT_HUP)
-        mrp_mainloop_quit(ml, 0);
-}
-
-
-static void input_process_cb(char *input)
-{
-    prompt_process_input(rl_client, input);
-    free(input);
-}
-
-
 static int input_setup(client_t *c)
 {
-    mrp_io_event_t events;
+    int         fd;
+    const char *prompt;
 
-    c->in  = fileno(stdin);
-    events = MRP_IO_EVENT_IN | MRP_IO_EVENT_HUP;
-    c->inw = mrp_add_io_watch(c->ml, c->in,events, input_cb, c);
+    fd     = fileno(stdin);
+    prompt = DEFAULT_PROMPT;
+    c->brl = brl_create_with_murphy(fd, prompt, c->ml, input_cb, c);
 
-    if (c->inw != NULL) {
-        prompt_set(c, DEFAULT_PROMPT);
-        prompt_display(c);
+    if (c->brl != NULL) {
+        brl_show_prompt(c->brl);
         return TRUE;
     }
     else {
-        mrp_log_error("Failed to create I/O watch for console input.");
+        mrp_log_error("Failed to breedline for console input.");
         return FALSE;
     }
 }
@@ -206,9 +141,10 @@ static int input_setup(client_t *c)
 
 static void input_cleanup(client_t *c)
 {
-    mrp_del_io_watch(c->inw);
-    c->inw = NULL;
-    rl_callback_handler_remove();
+    if (c->brl != NULL) {
+        brl_destroy(c->brl);
+        c->brl = NULL;
+    }
 }
 
 
@@ -217,14 +153,14 @@ void recvfrom_evt(mrp_transport_t *t, mrp_msg_t *msg,
 {
     client_t        *c = (client_t *)user_data;
     mrp_msg_field_t *f;
-    char           *prompt, *output, buf[128];
+    char           *prompt, *output;
     size_t          size;
 
     MRP_UNUSED(t);
     MRP_UNUSED(addr);
     MRP_UNUSED(addrlen);
 
-    prompt_erase(c);
+    brl_hide_prompt(c->brl);
 
     if ((f = mrp_msg_find(msg, MRP_CONSOLE_OUTPUT)) != NULL) {
         output = f->str;
@@ -233,15 +169,14 @@ void recvfrom_evt(mrp_transport_t *t, mrp_msg_t *msg,
     }
     else if ((f = mrp_msg_find(msg, MRP_CONSOLE_PROMPT)) != NULL) {
         prompt = f->str;
-        snprintf(buf, sizeof(buf), "%s> ", prompt);
-        prompt_set(c, buf);
+        brl_set_prompt(c->brl, prompt);
     }
     else if ((f = mrp_msg_find(msg, MRP_CONSOLE_BYE)) != NULL) {
         mrp_mainloop_quit(c->ml, 0);
         return;
     }
 
-    prompt_display(c);
+    brl_show_prompt(c->brl);
 }
 
 
@@ -361,8 +296,6 @@ int main(int argc, char *argv[])
     if (!input_setup(&c) || !client_setup(&c, addr))
         goto fail;
 
-    rl_client = &c;                      /* readline has not user_data */
-
     mrp_mainloop_run(c.ml);
 
     client_cleanup(&c);