]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib/vty: add vty_stdio()
authorDavid Lamparter <equinox@diac24.net>
Thu, 30 May 2013 14:33:45 +0000 (16:33 +0200)
committerDonald Sharp <sharpd@cumulusnetworks.com>
Wed, 8 Jun 2016 19:08:24 +0000 (15:08 -0400)
this introduces a new public/API function to the vty code for opening a
VTY on stdin/stdout.  Intended for unrestricted use by the individual
daemons, i.e. "offical API".

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
lib/vty.c
lib/vty.h

index 21f6f0da6f00cb9966bcc28f4875d8371d19cd1e..98518d664897592421bb4f3d1ddf96eef3794056 100644 (file)
--- a/lib/vty.c
+++ b/lib/vty.c
@@ -1611,6 +1611,34 @@ vty_flush (struct thread *thread)
   return 0;
 }
 
+/* allocate and initialise vty */
+static struct vty *
+vty_new_init (int vty_sock)
+{
+  struct vty *vty;
+
+  vty = vty_new ();
+  vty->fd = vty_sock;
+  vty->wfd = vty_sock;
+  vty->type = VTY_TERM;
+  vty->node = AUTH_NODE;
+  vty->fail = 0;
+  vty->cp = 0;
+  vty_clear_buf (vty);
+  vty->length = 0;
+  memset (vty->hist, 0, sizeof (vty->hist));
+  vty->hp = 0;
+  vty->hindex = 0;
+  vector_set_index (vtyvec, vty_sock, vty);
+  vty->status = VTY_NORMAL;
+  vty->lines = -1;
+  vty->iac = 0;
+  vty->iac_sb_in_progress = 0;
+  vty->sb_len = 0;
+
+  return vty;
+}
+
 /* Create new vty structure. */
 static struct vty *
 vty_create (int vty_sock, union sockunion *su)
@@ -1621,10 +1649,10 @@ vty_create (int vty_sock, union sockunion *su)
   sockunion2str(su, buf, SU_ADDRSTRLEN);
 
   /* Allocate new vty structure and set up default values. */
-  vty = vty_new ();
-  vty->fd = vty_sock;
-  vty->wfd = vty_sock;
-  vty->type = VTY_TERM;
+  vty = vty_new_init (vty_sock);
+
+  /* configurable parameters not part of basic init */
+  vty->v_timeout = vty_timeout_val;
   strcpy (vty->address, buf);
   if (no_password_check)
     {
@@ -1635,25 +1663,8 @@ vty_create (int vty_sock, union sockunion *su)
       else
        vty->node = VIEW_NODE;
     }
-  else
-    vty->node = AUTH_NODE;
-  vty->fail = 0;
-  vty->cp = 0;
-  vty_clear_buf (vty);
-  vty->length = 0;
-  memset (vty->hist, 0, sizeof (vty->hist));
-  vty->hp = 0;
-  vty->hindex = 0;
-  vector_set_index (vtyvec, vty_sock, vty);
-  vty->status = VTY_NORMAL;
-  vty->v_timeout = vty_timeout_val;
   if (host.lines >= 0)
     vty->lines = host.lines;
-  else
-    vty->lines = -1;
-  vty->iac = 0;
-  vty->iac_sb_in_progress = 0;
-  vty->sb_len = 0;
 
   if (! no_password_check)
     {
@@ -1689,6 +1700,30 @@ vty_create (int vty_sock, union sockunion *su)
   return vty;
 }
 
+/* create vty for stdio */
+struct vty *
+vty_stdio (void)
+{
+  struct vty *vty;
+
+  vty = vty_new_init (0);
+  vty->wfd = 1;
+
+  /* always have stdio vty in a known _unchangeable_ state, don't want config
+   * to have any effect here to make sure scripting this works as intended */
+  vty->node = ENABLE_NODE;
+  vty->v_timeout = 0;
+  strcpy (vty->address, "console");
+
+  vty_prompt (vty);
+
+  /* Add read/write thread. */
+  vty_event (VTY_WRITE, 1, vty);
+  vty_event (VTY_READ, 0, vty);
+
+  return vty;
+}
+
 /* Accept connection from the network. */
 static int
 vty_accept (struct thread *thread)
index 7968e92fd5bbb034c1fb930b4dcabf05618b83b8..a248680dbcb1d2748f75ded17752b151f282c844 100644 (file)
--- a/lib/vty.h
+++ b/lib/vty.h
@@ -273,6 +273,7 @@ extern void vty_init_vtysh (void);
 extern void vty_terminate (void);
 extern void vty_reset (void);
 extern struct vty *vty_new (void);
+extern struct vty *vty_stdio (void);
 extern int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
 extern void vty_read_config (char *, char *);
 extern void vty_time_print (struct vty *, int);