0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2025-01-18 15:01:53 +00:00

BIRD Client: file base saved/restored history of CLI

In interactive mode in birdc is stored history of all commands in
~/.birdc_history file
This commit is contained in:
Pavel Tvrdik 2016-04-26 10:59:26 +02:00
parent f90dde08f3
commit 01e7e79d26

View File

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