From 191b3e0d8b8ee26f072de9718000ca1d37aaabad Mon Sep 17 00:00:00 2001 From: anon Date: Tue, 6 Aug 2024 19:33:46 +0200 Subject: [PATCH] --execute --- .gitignore | 2 ++ documentation/TODO.md | 1 - source/argument_yy.y | 4 +++- source/cli.cpp | 3 +++ source/main.cpp | 30 +++++++++++++++++++++++++----- source/tui.cpp | 3 +-- 6 files changed, 34 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index ff42fac..2a2134f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ histui +.gdb_history object/ +*.out diff --git a/documentation/TODO.md b/documentation/TODO.md index 4b65059..831f129 100644 --- a/documentation/TODO.md +++ b/documentation/TODO.md @@ -1,5 +1,4 @@ + make (potentially slow) queries cancel using multi threading -+ instant run mode + scroll lags + technical dept in tui.cpp + migrate to C diff --git a/source/argument_yy.y b/source/argument_yy.y index f277c19..e5559d9 100644 --- a/source/argument_yy.y +++ b/source/argument_yy.y @@ -1,9 +1,10 @@ -%token HELP VERSION +%token HELP VERSION EXECUTE %token TUI ENABLE %token LEVENSTEIN CASELESS %{ #include "cli.hpp" #include "storage.hpp" + extern bool do_execute; %} %% histui_args: global_args verb_and_args @@ -19,6 +20,7 @@ verb_and_args: ENABLE { enable(); exit(0); } ; tui_args: %empty + | EXECUTE tui_args { do_execute = true; } | LEVENSTEIN tui_args { is_levenstein = true; } | CASELESS tui_args { is_caseless = true; } ; diff --git a/source/cli.cpp b/source/cli.cpp index ad0d735..17746c9 100644 --- a/source/cli.cpp +++ b/source/cli.cpp @@ -92,6 +92,9 @@ void parse_arguments(const int argc, const char * const * const argv) { if (!strcmp(argv[i], "enable")) { tokens[token_empty_head++] = ENABLE; } else + if (!strcmp(argv[i], "--execute")) { + tokens[token_empty_head++] = EXECUTE; + } else if (!strcmp(argv[i], "--levenstein")) { tokens[token_empty_head++] = LEVENSTEIN; } else diff --git a/source/main.cpp b/source/main.cpp index 28518bb..3649ba3 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,10 +1,12 @@ #include +#include #include "cli.hpp" #include "bash_history.yy.hpp" #include "storage.hpp" #include "tui.hpp" bool do_run = true; +bool do_execute = false; void init(void); void deinit(void); @@ -38,11 +40,29 @@ void deinit(void) { } void export_result(const char * const result) { - int fd[2]; - pipe(fd); - dprintf(3, result); - close(fd[0]); - close(fd[1]); + if (do_execute) { + /* Inject the command and a newline to STDIN directly. + * Some systems could theoretically be configured to disallow it. + * Not my problem. + */ + for (size_t i = 0; i < strlen(result); i++) { + if (ioctl(STDIN_FILENO, TIOCSTI, &result[i]) == -1) { + perror("ioctl TIOCSTI"); + } + } + const char newline = '\n'; + ioctl(STDIN_FILENO, TIOCSTI, &newline); + } else { + /* Copy to a 3th pipe file descriptor which we can + * scoop up from a file and copy with READLINE_LINE. + * XXX: if anyone knows a better method, please tell me + */ + int fd[2]; + pipe(fd); + dprintf(3, result); + close(fd[0]); + close(fd[1]); + } } signed main(const int argc, const char * const * const argv) { diff --git a/source/tui.cpp b/source/tui.cpp index aadaf6d..87a9caf 100644 --- a/source/tui.cpp +++ b/source/tui.cpp @@ -4,8 +4,6 @@ #include #include -extern bool do_run; - size_t entry_lines; bool is_input_changed = true; @@ -127,6 +125,7 @@ void tui_refresh(void) { void tui_take_input(void) { + extern bool do_run; const size_t paging_size = entry_lines / 2; input = wgetch(stdscr);