lib/vty: put stdin in raw mode for vty

The interactive CLI actually works just fine, if we just put the
terminal in raw mode to get keystrokes as they come.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
diff --git a/lib/vty.c b/lib/vty.c
index 24df32c..a3f1a3c 100644
--- a/lib/vty.c
+++ b/lib/vty.c
@@ -38,6 +38,7 @@
 #include "network.h"
 
 #include <arpa/telnet.h>
+#include <termios.h>
 
 /* Vty events */
 enum event 
@@ -1700,12 +1701,30 @@
 }
 
 /* create vty for stdio */
+static struct termios stdio_orig_termios;
+static struct vty *stdio_vty = NULL;
+
+static void
+vty_stdio_reset (void)
+{
+  if (stdio_vty)
+    {
+      tcsetattr (0, TCSANOW, &stdio_orig_termios);
+      stdio_vty = NULL;
+    }
+}
+
 struct vty *
 vty_stdio (void)
 {
   struct vty *vty;
+  struct termios termios;
 
-  vty = vty_new_init (0);
+  /* refuse creating two vtys on stdio */
+  if (stdio_vty)
+    return NULL;
+
+  vty = stdio_vty = vty_new_init (0);
   vty->wfd = 1;
 
   /* always have stdio vty in a known _unchangeable_ state, don't want config
@@ -1714,6 +1733,18 @@
   vty->v_timeout = 0;
   strcpy (vty->address, "console");
 
+  if (!tcgetattr (0, &stdio_orig_termios))
+    {
+      termios = stdio_orig_termios;
+      termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
+                           | INLCR | IGNCR | ICRNL | IXON);
+      termios.c_oflag &= ~OPOST;
+      termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
+      termios.c_cflag &= ~(CSIZE | PARENB);
+      termios.c_cflag |= CS8;
+      tcsetattr (0, TCSANOW, &termios);
+    }
+
   vty_prompt (vty);
 
   /* Add read/write thread. */
@@ -2225,6 +2256,8 @@
   /* Close socket. */
   if (vty->fd > 0)
     close (vty->fd);
+  else
+    vty_stdio_reset ();
 
   if (vty->buf)
     XFREE (MTYPE_VTY, vty->buf);
@@ -3004,6 +3037,8 @@
 
   master = master_thread;
 
+  atexit (vty_stdio_reset);
+
   /* Initilize server thread vector. */
   Vvty_serv_thread = vector_init (VECTOR_MIN_SIZE);