summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDonald Sharp <sharpd@cumulusnetworks.com>2016-01-13 10:49:50 -0800
committerPaul Jakma <paul.jakma@hpe.com>2016-02-26 14:11:46 +0000
commit2e320423337c628bfeb814ec15fd5f575ebc5eed (patch)
tree21174999e4fc60d7d0ab13313bc92b98728a1118
parent9099f9b2a66e86f8a90d7fe18f61bd2bb1bc6744 (diff)
downloadquagga-2e320423337c628bfeb814ec15fd5f575ebc5eed.tar.bz2
quagga-2e320423337c628bfeb814ec15fd5f575ebc5eed.tar.xz
doc, vtysh: Fixup of history handling
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>
-rw-r--r--doc/vtysh.13
-rw-r--r--vtysh/vtysh_main.c32
-rw-r--r--vtysh/vtysh_user.c10
-rw-r--r--vtysh/vtysh_user.h2
4 files changed, 45 insertions, 2 deletions
diff --git a/doc/vtysh.1 b/doc/vtysh.1
index de4913e5..a2afa9ff 100644
--- a/doc/vtysh.1
+++ b/doc/vtysh.1
@@ -76,6 +76,9 @@ config file.
.BI /usr/local/etc/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.
diff --git a/vtysh/vtysh_main.c b/vtysh/vtysh_main.c
index aa7d0218..02a19b7c 100644
--- a/vtysh/vtysh_main.c
+++ b/vtysh/vtysh_main.c
@@ -223,6 +223,7 @@ main (int argc, char **argv, char **env)
struct cmd_rec *tail = NULL;
int echo_command = 0;
int no_error = 0;
+ char *homedir = NULL;
/* Preserve name of myself. */
progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
@@ -317,6 +318,27 @@ main (int argc, char **argv, char **env)
exit(1);
}
+ /*
+ * 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)
{
@@ -332,6 +354,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);
@@ -348,6 +373,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);
@@ -368,6 +396,8 @@ main (int argc, char **argv, char **env)
XFREE(0, cr);
}
}
+
+ history_truncate_file(history_file,1000);
exit (0);
}
@@ -397,8 +427,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);
diff --git a/vtysh/vtysh_user.c b/vtysh/vtysh_user.c
index 7e10d68e..239a633f 100644
--- a/vtysh/vtysh_user.c
+++ b/vtysh/vtysh_user.c
@@ -191,6 +191,16 @@ vtysh_auth (void)
return 0;
}
+char *
+vtysh_get_home (void)
+{
+ struct passwd *passwd;
+
+ passwd = getpwuid (getuid ());
+
+ return passwd ? passwd->pw_dir : NULL;
+}
+
void
vtysh_user_init (void)
{
diff --git a/vtysh/vtysh_user.h b/vtysh/vtysh_user.h
index c485c23f..a6c8b99b 100644
--- a/vtysh/vtysh_user.h
+++ b/vtysh/vtysh_user.h
@@ -25,4 +25,6 @@
int vtysh_auth (void);
void vtysh_user_init (void);
+char *vtysh_get_home (void);
+
#endif /* _VTYSH_USER_H */