]> git.droids-corp.org - libcmdline.git/commitdiff
cmdline (merge-intel): move basic_char_loop() in cmdline lib, as cmdline_interact()
authorOlivier Matz <zer0@droids-corp.org>
Fri, 24 Dec 2010 12:54:25 +0000 (13:54 +0100)
committerOlivier Matz <zer0@droids-corp.org>
Sun, 2 Jan 2011 20:53:14 +0000 (21:53 +0100)
Signed-off-by: Olivier Matz <olivier.matz@6wind.com>
src/lib/cmdline.c
src/lib/cmdline.h
src/lib/cmdline_parse.c
src/lib/cmdline_socket.c
src/lib/cmdline_socket.h
src/lib/cmdline_vt100.c

index 4d2eaeac4efe03b6a35456db67a35656d6e4ff50..31af8194d7699ee2ee80350140df1befb9c007c1 100644 (file)
@@ -66,6 +66,7 @@
 #include <stdarg.h>
 #include <inttypes.h>
 #include <fcntl.h>
+#include <termios.h>
 
 #include <netinet/in.h>
 
@@ -194,3 +195,18 @@ cmdline_in(struct cmdline *cl, const char *buf, int size)
        }
        return i;
 }
+
+void
+cmdline_interact(struct cmdline *cl)
+{
+       char c;
+
+       c = -1;
+       while (1) {
+               read(cl->s_in, &c, 1);
+               if (cmdline_in(cl, &c, 1) < 0) {
+                       break;
+               }
+       }
+       cmdline_free(cl);
+}
index 18320996bc25d04c2e6f0d6de7f641f0a837e1c0..c28cf8b476809a8fe314c7c6b26a4be80a666357 100644 (file)
@@ -70,6 +70,9 @@ struct cmdline {
        cmdline_parse_ctx_t *ctx;
        struct rdline rdl;
        char prompt[RDLINE_PROMPT_SIZE];
+#ifdef CMDLINE_TERMIOS
+       struct termios oldterm;
+#endif
 };
 
 struct cmdline *cmdline_new(cmdline_parse_ctx_t *ctx, const char *prompt, int s_in, int s_out);
@@ -78,5 +81,6 @@ void cmdline_free(struct cmdline *cl);
 void cmdline_printf(const struct cmdline *cl, const char *fmt, ...);
 int cmdline_in(struct cmdline *cl, const char *buf, int size);
 void cmdline_write_char(struct rdline *rdl, char c);
+void cmdline_interact(struct cmdline *cl);
 
 #endif /* _CMDLINE_SOCKET_H_ */
index 334b3e37884b5f8114aedbe20c120a64afb65e5d..053e14cd20c85cdfc100ca40729727d923031b2f 100644 (file)
@@ -63,6 +63,7 @@
 #include <string.h>
 #include <inttypes.h>
 #include <ctype.h>
+#include <termios.h>
 
 #include <netinet/in.h>
 
index abff38b24b9a0385f82ee76df4708fafb3e3bfeb..f9cfebd245e9f74e18080cdff7357bb326c3d3bb 100644 (file)
@@ -66,6 +66,7 @@
 #include <stdarg.h>
 #include <inttypes.h>
 #include <fcntl.h>
+#include <termios.h>
 
 #ifndef CMDLINE_NO_SOCKET
 #include <sys/socket.h>
@@ -225,5 +226,32 @@ cmdline_file_new(cmdline_parse_ctx_t *ctx, const char *prompt, const char *path)
 struct cmdline *
 cmdline_stdin_new(cmdline_parse_ctx_t *ctx, const char *prompt)
 {
-       return (cmdline_new(ctx, prompt, 0, 1));
+       struct cmdline *cl;
+#ifdef CMDLINE_TERMIOS
+       struct termios oldterm, term;
+
+       tcgetattr(0, &oldterm);
+       memcpy(&term, &oldterm, sizeof(term));
+       term.c_lflag &= ~(ICANON | ECHO | ISIG);
+       tcsetattr(0, TCSANOW, &term);
+       setbuf(stdin, NULL);
+#endif
+
+       cl = cmdline_new(ctx, prompt, 0, 1);
+
+#ifdef CMDLINE_TERMIOS
+       memcpy(&cl->oldterm, &oldterm, sizeof(term));
+#endif
+       return cl;
+}
+
+void
+cmdline_stdin_exit(struct cmdline *cl)
+{
+#ifdef CMDLINE_TERMIOS
+       tcsetattr(fileno(stdin), TCSANOW, &cl->oldterm);
+#else
+       /* silent the compiler */
+       cl = cl;
+#endif
 }
index 5ef97edbd20dc70704d6216395e2096fd37fc1be..37a378d3b3315977af8b6acece2dd26e70ee6ea3 100644 (file)
@@ -71,5 +71,6 @@ struct cmdline *cmdline_accept(cmdline_parse_ctx_t *ctx, const char *prompt, int
 
 struct cmdline *cmdline_file_new(cmdline_parse_ctx_t *ctx, const char *prompt, const char *path);
 struct cmdline *cmdline_stdin_new(cmdline_parse_ctx_t *ctx, const char *prompt);
+void cmdline_stdin_exit(struct cmdline *cl);
 
 #endif /* _CMDLINE_SOCKET_H_ */
index 3ca63d612761412a15b31b7a57cd4fad5d51dbdb..2480974a182bc09759c03721e9722d5901b8b0e9 100644 (file)
@@ -65,6 +65,7 @@
 #include <string.h>
 #include <stdarg.h>
 #include <ctype.h>
+#include <termios.h>
 
 #include "cmdline_vt100.h"