]> git.puffer.fish Git - mirror/frr.git/commitdiff
doc, vtysh: Fixup of history handling
authorDonald Sharp <sharpd@cumulusnetworks.com>
Wed, 13 Jan 2016 18:49:50 +0000 (10:49 -0800)
committerDonald Sharp <sharpd@cumulusnetworks.com>
Wed, 13 Jan 2016 18:49:50 +0000 (10:49 -0800)
This fix does two things:

1) If the ${HOME}/.history_quagga file does not exist, create it
for history storing.
2) Allow vtysh -c "..." commands to be stored in history file
as well

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
doc/vtysh.1
vtysh/vtysh_main.c
vtysh/vtysh_user.c
vtysh/vtysh_user.h

index ff4c2b4f84c1e8aad6c0cc9c773afee437673d62..d144cb642127ed8334b98bdbb94d380899765378 100644 (file)
@@ -76,6 +76,9 @@ config file.
 .BI /etc/quagga/Quagga.conf
 The default location of the integrated Quagga routing engine config file
 if integrated config file is in use (not default).
+.TP
+.BI ${HOME}/.history_quagga
+Location of history of commands entered via cli
 .SH WARNING
 This man page is intended to be a quick reference for command line
 options. The definitive document is the Info file \fBQuagga\fR.
index e160e3360b69854f2abbb5dd54af2f32a0d56241..033b273e58614c537b22ecdc36571eb7b454680a 100644 (file)
@@ -232,6 +232,7 @@ main (int argc, char **argv, char **env)
   int echo_command = 0;
   int no_error = 0;
   int markfile = 0;
+  char *homedir = NULL;
 
   /* Preserve name of myself. */
   progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
@@ -360,6 +361,27 @@ main (int argc, char **argv, char **env)
       exit(0);
     }
 
+  /*
+   * Setup history file for use by both -c and regular input
+   * If we can't find the home directory, then don't store
+   * the history information
+   */
+  homedir = vtysh_get_home ();
+  if (homedir)
+    {
+      snprintf(history_file, sizeof(history_file), "%s/.history_quagga", homedir);
+      if (read_history (history_file) != 0)
+       {
+         int fp;
+
+         fp = open (history_file, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
+         if (fp)
+           close (fp);
+
+         read_history (history_file);
+       }
+    }
+
   /* If eval mode. */
   if (cmd)
     {
@@ -375,6 +397,9 @@ main (int argc, char **argv, char **env)
            {
              *eol = '\0';
 
+             add_history (cmd->line);
+             append_history (1, history_file);
+
              if (echo_command)
                printf("%s%s\n", vtysh_prompt(), cmd->line);
              
@@ -391,6 +416,9 @@ main (int argc, char **argv, char **env)
              cmd->line = eol+1;
            }
 
+         add_history (cmd->line);
+         append_history (1, history_file);
+
          if (echo_command)
            printf("%s%s\n", vtysh_prompt(), cmd->line);
 
@@ -411,6 +439,8 @@ main (int argc, char **argv, char **env)
            XFREE(0, cr);
          }
         }
+
+      history_truncate_file(history_file,1000);
       exit (0);
     }
   
@@ -440,8 +470,6 @@ main (int argc, char **argv, char **env)
   sigsetjmp (jmpbuf, 1);
   jmpflag = 1;
 
-  snprintf(history_file, sizeof(history_file), "%s/.history_quagga", getenv("HOME"));
-  read_history(history_file);
   /* Main command loop. */
   while (vtysh_rl_gets ())
     vtysh_execute (line_read);
index 0e314bcbbda0d5e872f1f000782dd84d0f588fc9..214279fac40f523640aba15188306151cb763f36 100644 (file)
@@ -207,8 +207,18 @@ vtysh_auth ()
   return 0;
 }
 
+char *
+vtysh_get_home (void)
+{
+  struct passwd *passwd;
+
+  passwd = getpwuid (getuid ());
+
+  return passwd ? passwd->pw_dir : NULL;
+}
+
 void
-vtysh_user_init ()
+vtysh_user_init (void)
 {
   userlist = list_new ();
   install_element (CONFIG_NODE, &username_nopassword_cmd);
index 43d125505cb9d317d064811995a53c1f6b58b912..b0686a969902c82907c2ef9d6ffbdb3b092bb85a 100644 (file)
@@ -25,4 +25,6 @@
 int vtysh_auth ();
 void user_config_write(void);
 
+char *vtysh_get_home (void);
+
 #endif /* _VTYSH_USER_H */