Commit 01e7e79d authored by Pavel Tvrdik's avatar Pavel Tvrdik
Browse files

BIRD Client: file base saved/restored history of CLI

In interactive mode in birdc is stored history of all commands in
~/.birdc_history file
parent f90dde08
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -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();
}