rdline: don't display prompt if stopped by user
[libcmdline.git] / src / lib / cmdline.c
index 5e7f009..70267de 100644 (file)
 #include <stdarg.h>
 #include <inttypes.h>
 #include <fcntl.h>
-
-#include <sys/socket.h>
+#include <termios.h>
 #include <netinet/in.h>
-#include <sys/socket.h>
-#include <sys/un.h>
 
 #include "cmdline_parse.h"
 #include "cmdline_rdline.h"
 #include "cmdline.h"
 
-
-#define PROMPT_SUFFIX "> "
-
-
-/**********************/
-
-void
-cmdline_valid_buffer(struct rdline *rdl, const char *buf,
-                    __attribute__((unused)) unsigned int size)
+static void
+cmdline_default_valid_buffer(struct rdline *rdl, const char *buf,
+                            __attribute__((unused)) unsigned int size)
 {
        struct cmdline *cl = rdl->opaque;
        int ret;
-       ret = cmdline_parse(cl, buf);
+       ret = cmdline_parse(cl->ctx, buf, cl);
        if (ret == CMDLINE_PARSE_AMBIGUOUS)
                cmdline_printf(cl, "Ambiguous command\n");
        else if (ret == CMDLINE_PARSE_NOMATCH)
-               cmdline_printf(cl, "Command not found\n");
-       else if (ret == CMDLINE_PARSE_BAD_ARGS)
                cmdline_printf(cl, "Bad arguments\n");
+       else if (ret == CMDLINE_PARSE_UNTERMINATED_QUOTE)
+               cmdline_printf(cl, "Unterminated quote\n");
 }
 
-int
-cmdline_complete_buffer(struct rdline *rdl, const char *buf,
-                       char *dstbuf, unsigned int dstsize,
-                       int *state)
+static int
+cmdline_default_complete_buffer(struct rdline *rdl, const char *buf,
+                               char *dstbuf, unsigned int dstsize)
 {
        struct cmdline *cl = rdl->opaque;
-       return cmdline_complete(cl, buf, state, dstbuf, dstsize);
+       // RDLINE_RES_COMPLETE ?
+       return cmdline_complete(cl->ctx, buf, dstbuf, dstsize);
 }
 
-void
-cmdline_write_char(struct rdline *rdl, char c)
+
+static int
+cmdline_default_help(struct rdline *rdl, const char *buf,
+                    rdline_write_t *write_cb, void *write_arg)
 {
        struct cmdline *cl = rdl->opaque;
-       if (cl->s_out >= 0) {
-               write(cl->s_out, &c, 1);
-       }
+       return cmdline_help(cl->ctx, buf, cmdline_write, write_arg);
 }
 
+/* ---- Some rdline wrappers ---- */
+
+/* write a buffer, just use rdline */
+ssize_t
+cmdline_write(void *arg, void *buf, size_t count)
+{
+       struct rdline *rdl = arg;
+#ifdef NO_PAGER
+       return rdline_write(rdl, buf, count);
+#else
+       return rdline_asyncpager_write(rdl, buf, count);
+#endif
+}
 
 void
 cmdline_set_prompt(struct cmdline *cl, const char *prompt)
 {
-       snprintf(cl->prompt, sizeof(cl->prompt), prompt);
+       snprintf(cl->prompt, sizeof(cl->prompt), "%s", prompt);
 }
 
 struct cmdline *
 cmdline_new(cmdline_parse_ctx_t *ctx, const char *prompt, int s_in, int s_out)
 {
        struct cmdline *cl;
+
        cl = malloc(sizeof(struct cmdline));
        if (cl == NULL)
                return NULL;
+
+       /* init cmdline structure */
        memset(cl, 0, sizeof(struct cmdline));
        cl->s_in = s_in;
        cl->s_out = s_out;
        cl->ctx = ctx;
 
-       rdline_init(&cl->rdl, cmdline_write_char,
-                   cmdline_valid_buffer, cmdline_complete_buffer);
+       /* init embedded rdline */
+       rdline_init(&cl->rdl, s_in, s_out,
+                   cmdline_default_valid_buffer,
+                   cmdline_default_complete_buffer,
+                   cmdline_default_help);
+
        cl->rdl.opaque = cl;
        cmdline_set_prompt(cl, prompt);
        rdline_newline(&cl->rdl, cl->prompt);
@@ -154,46 +165,69 @@ cmdline_free(struct cmdline *cl)
        free(cl);
 }
 
-void
+int
 cmdline_printf(const struct cmdline *cl, const char *fmt, ...)
 {
        va_list ap;
-       char buffer[BUFSIZ];
+       int ret;
 
        va_start(ap, fmt);
-       vsnprintf(buffer, 512, fmt, ap);
+       ret = rdline_vprintf(&cl->rdl, fmt, ap);
        va_end(ap);
-       if (cl->s_out >= 0) {
-               write(cl->s_out, buffer, strlen(buffer));
-       }
+
+       return ret;
 }
 
+/* Push an input buffer in the command line. Typically, this function
+ * is called by cmdline_interact() to send the input characters to the
+ * cmdline process. It can also be called by a user callback function,
+ * when a buffer is received from the input socket.
+ *
+ * The function returns the number of processed characters, or a
+ * negative value on error (EOF reached or command line exited. */
 int
 cmdline_in(struct cmdline *cl, const char *buf, int size)
 {
-       const char *history, *buffer;
        int ret = 0;
-       int i, same;
+       int i;
 
-       /* XXX use defines instead of hardcoded values */
-       for (i=0; i<size; i++) {
+       for (i = 0; i < size; i++) {
                ret = rdline_char_in(&cl->rdl, buf[i]);
 
-               if (ret == 1) {
-                       buffer = rdline_get_buffer(&cl->rdl);
-                       history = rdline_get_history_item(&cl->rdl, 0);
-                       if (history) {
-                               same = !memcmp(buffer, history, strlen(history)) &&
-                                       buffer[strlen(history)] == '\n';
-                       }
-                       else
-                               same = 0;
-                       if (strlen(buffer) > 1 && !same)
-                               rdline_add_history(&cl->rdl, buffer);
+               if (ret == RDLINE_RES_VALIDATED &&
+                   cl->rdl.status == RDLINE_STOPPED)
+                       break;
+
+               if (ret == RDLINE_RES_VALIDATED)
                        rdline_newline(&cl->rdl, cl->prompt);
-               }
-               else if (ret == -2)
+               else if (ret == RDLINE_RES_EOF)
+                       return -1;
+               else if (ret == RDLINE_RES_EXITED)
                        return -1;
        }
        return i;
 }
+
+/* Interrupt a running command line process */
+void
+cmdline_quit(struct cmdline *cl)
+{
+       rdline_quit(&cl->rdl);
+}
+
+/* Start command line on configured file descriptor. This function
+ * loops until the user explicitelly call cmdline_quit(), or if the
+ * input fd reaches EOF. */
+void
+cmdline_interact(struct cmdline *cl)
+{
+       char c;
+
+       c = -1;
+       while (1) {
+               if (read(cl->s_in, &c, 1) < 0)
+                       break;
+               if (cmdline_in(cl, &c, 1) < 0)
+                       break;
+       }
+}