]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib/vty: put stdin in raw mode for vty
authorDavid Lamparter <equinox@opensourcerouting.org>
Tue, 5 May 2015 09:04:46 +0000 (11:04 +0200)
committerDonald Sharp <sharpd@cumulusnetworks.com>
Wed, 8 Jun 2016 19:09:24 +0000 (15:09 -0400)
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>
(cherry picked from commit ba53a8fdecef07577dcc4109e5c82bb124d49c58)

lib/vty.c

index 98518d664897592421bb4f3d1ddf96eef3794056..d202f72fa3280eef08e5e98db34ddd29a01ae0c3 100644 (file)
--- 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 
@@ -1701,12 +1702,30 @@ vty_create (int vty_sock, union sockunion *su)
 }
 
 /* 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
@@ -1715,6 +1734,18 @@ vty_stdio (void)
   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. */
@@ -2226,6 +2257,8 @@ vty_close (struct vty *vty)
   /* Close socket. */
   if (vty->fd > 0)
     close (vty->fd);
+  else
+    vty_stdio_reset ();
 
   if (vty->buf)
     XFREE (MTYPE_VTY, vty->buf);
@@ -3039,6 +3072,8 @@ vty_init (struct thread_master *master_thread)
 
   vty_master = master_thread;
 
+  atexit (vty_stdio_reset);
+
   /* Initilize server thread vector. */
   Vvty_serv_thread = vector_init (VECTOR_MIN_SIZE);