cmdline: check size of result buffer to avoid overflow
[libcmdline.git] / src / lib / cmdline_socket.c
index bba3847..4474c6f 100644 (file)
@@ -66,8 +66,9 @@
 #include <stdarg.h>
 #include <inttypes.h>
 #include <fcntl.h>
+#include <termios.h>
 
-#ifndef CMDLINE_NO_SOCKET
+#ifdef CMDLINE_HAVE_SOCKET
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <sys/socket.h>
 
 #include "cmdline_parse.h"
 #include "cmdline_rdline.h"
+#include "cmdline_socket.h"
 #include "cmdline.h"
 
 /**********************/
 
-#ifndef CMDLINE_NO_SOCKET
+#ifdef CMDLINE_HAVE_SOCKET
 int
 cmdline_tcpv4_listen(in_addr_t addr, uint16_t port)
 {
@@ -224,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
 }