]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
BIRD Client: file base saved/restored history of CLI
authorPavel Tvrdik <pawel.tvrdik@gmail.com>
Tue, 26 Apr 2016 08:59:26 +0000 (10:59 +0200)
committerPavel Tvrdik <pawel.tvrdik@gmail.com>
Tue, 26 Apr 2016 12:42:07 +0000 (14:42 +0200)
In interactive mode in birdc is stored history of all commands in
~/.birdc_history file

client/birdc.c

index 3a52c3293a4c8eb7c8e444f118d79de4743c5828..e84400fcfc968e083eaeb053c99734434db0069d 100644 (file)
@@ -7,6 +7,7 @@
  */
 
 #include <stdio.h>
+#include <limits.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <termios.h>
@@ -20,6 +21,9 @@
 #include "lib/string.h"
 #include "client/client.h"
 
+#define BIRDC_HISTORY_FILENAME         ".birdc_history"
+#define MAX_FILEPATH_LENGTH    256
+
 static int input_hidden_end;
 static int prompt_active;
 
@@ -137,21 +141,35 @@ input_help(int arg, int key UNUSED)
   return 0;
 }
 
+static char *
+get_filepath_cli_history(char *buf, size_t size)
+{
+  char *home = getenv("HOME");
+  if (!home)
+    return NULL;
+  if (snprintf(buf, size, "%s/%s", home, BIRDC_HISTORY_FILENAME) >= sizeof(buf))
+    return NULL;
+  return buf;
+}
+
 void
 input_init(void)
 {
+  char path[MAX_FILEPATH_LENGTH];
   prompt_active = 0;
 
   if (interactive)
   {
     prompt_active = 1;
 
+
     retrieve_symbols();
     printf("BIRD Client " BIRD_VERSION " ready.\n");
 
     rl_readline_name = "birdc";
     rl_add_defun("bird-complete", input_complete, '\t');
     rl_add_defun("bird-help", input_help, '?');
+    read_history(get_filepath_cli_history(path, sizeof(path)));
     rl_callback_handler_install("bird> ", input_got_line);
   }
 
@@ -224,9 +242,14 @@ more_end(void)
 void
 cleanup(void)
 {
+  char path[MAX_FILEPATH_LENGTH];
+
   if (init)
     return;
 
+  if (interactive)
+    write_history(get_filepath_cli_history(path, sizeof(path)));
+
   input_hide();
   rl_callback_handler_remove();
 }