From: Olivier Matz Date: Fri, 24 Dec 2010 12:54:25 +0000 (+0100) Subject: cmdline (merge-intel): move basic_char_loop() in cmdline lib, as cmdline_interact() X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;h=f7c21c76abb33582a06ceba7c39b979764261759;p=libcmdline.git cmdline (merge-intel): move basic_char_loop() in cmdline lib, as cmdline_interact() Signed-off-by: Olivier Matz --- diff --git a/src/lib/cmdline.c b/src/lib/cmdline.c index 4d2eaea..31af819 100644 --- a/src/lib/cmdline.c +++ b/src/lib/cmdline.c @@ -66,6 +66,7 @@ #include #include #include +#include #include @@ -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); +} diff --git a/src/lib/cmdline.h b/src/lib/cmdline.h index 1832099..c28cf8b 100644 --- a/src/lib/cmdline.h +++ b/src/lib/cmdline.h @@ -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_ */ diff --git a/src/lib/cmdline_parse.c b/src/lib/cmdline_parse.c index 334b3e3..053e14c 100644 --- a/src/lib/cmdline_parse.c +++ b/src/lib/cmdline_parse.c @@ -63,6 +63,7 @@ #include #include #include +#include #include diff --git a/src/lib/cmdline_socket.c b/src/lib/cmdline_socket.c index abff38b..f9cfebd 100644 --- a/src/lib/cmdline_socket.c +++ b/src/lib/cmdline_socket.c @@ -66,6 +66,7 @@ #include #include #include +#include #ifndef CMDLINE_NO_SOCKET #include @@ -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 } diff --git a/src/lib/cmdline_socket.h b/src/lib/cmdline_socket.h index 5ef97ed..37a378d 100644 --- a/src/lib/cmdline_socket.h +++ b/src/lib/cmdline_socket.h @@ -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_ */ diff --git a/src/lib/cmdline_vt100.c b/src/lib/cmdline_vt100.c index 3ca63d6..2480974 100644 --- a/src/lib/cmdline_vt100.c +++ b/src/lib/cmdline_vt100.c @@ -65,6 +65,7 @@ #include #include #include +#include #include "cmdline_vt100.h"