Files
bs_explorer/bs/main.c
François 65ebe6b00c bs: persist REPL command history across sessions
Load and save readline history in $HOME/.bs_explorer_history (capped at
1000 entries), so up-arrow recalls commands from previous sessions.
Saved on exit/quit, EOF and Ctrl-C.
2026-05-24 00:55:13 +02:00

148 lines
3.3 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "config/bs_defines.h"
#include "config/version.h"
#include "jtag_core/jtag_core.h"
#include "script/script.h"
#include "init.h"
#define PROMPT "bs_explorer> "
static jtag_core *jc = NULL;
static script_ctx *sctx = NULL;
#define HISTORY_MAX_ENTRIES 1000
static char history_file[1024] = "";
/* Persist the REPL command history across sessions in
* $HOME/.bs_explorer_history (skipped if HOME is unset). */
static void init_history(void)
{
const char *home = getenv("HOME");
if (home && *home) {
snprintf(history_file, sizeof(history_file), "%s/.bs_explorer_history", home);
using_history();
read_history(history_file);
}
}
static void save_history(void)
{
if (history_file[0]) {
write_history(history_file);
history_truncate_file(history_file, HISTORY_MAX_ENTRIES);
}
}
static void handle_sigint(int sig)
{
(void)sig;
fputs("\n", stdout);
save_history();
bsexp_deinit(jc, sctx);
exit(0);
}
static char *command_generator(const char *text, int state)
{
static int idx;
static size_t len;
const char *name;
if (!state) {
idx = 0;
len = strlen(text);
}
while ((name = script_commands_list[idx].command) != NULL) {
idx++;
if (strncmp(name, text, len) == 0) {
return strdup(name);
}
}
return NULL;
}
static char **bs_completion(const char *text, int start, int end)
{
(void)end;
rl_attempted_completion_over = 0;
if (start == 0) {
return rl_completion_matches(text, command_generator);
}
return NULL;
}
static int is_blank(const char *s)
{
while (*s) {
if (*s != ' ' && *s != '\t') return 0;
s++;
}
return 1;
}
static void print_banner(void)
{
int n_drv = jtagcore_get_number_of_probes_drv(jc);
printf("\n");
printf(" Boundary Scan Explorer " APP_VER_STR(APP_VER) "\n");
printf(" Based on Viveris jtag-boundary-scanner\n");
printf("\n");
printf(" %d probe driver(s) available.\n", n_drv);
printf(" Type 'help' or '?' to list commands, 'exit' or Ctrl-D to quit.\n");
printf(" <Tab> completes commands.\n");
printf("\n");
}
int main(void)
{
char *line = NULL;
int error = 0;
bsexp_init(&jc, &sctx);
if (NULL == jc || NULL == sctx) {
fputs("JTAG Core initialization failed!\n", stderr);
return JTAG_CORE_MEM_ERROR;
}
signal(SIGINT, handle_sigint);
rl_attempted_completion_function = bs_completion;
rl_readline_name = "bs_explorer";
init_history();
print_banner();
while ((line = readline(PROMPT)) != NULL) {
if (is_blank(line)) {
free(line);
continue;
}
if (strcmp(line, "exit") == 0 || strcmp(line, "quit") == 0) {
free(line);
break;
}
add_history(line);
error = execute_line_script(sctx, line);
if (error != JTAG_CORE_NO_ERROR && error != JTAG_CORE_NOT_FOUND) {
printf("Command failed with code: %d\n", error);
}
free(line);
}
if (line == NULL) {
fputs("\n", stdout);
}
save_history();
bsexp_deinit(jc, sctx);
return 0;
}