#include #include #include #include #include #include #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; static void handle_sigint(int sig) { (void)sig; fputs("\n", stdout); 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(" 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"; 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); } bsexp_deinit(jc, sctx); return 0; }