--execute

This commit is contained in:
anon 2024-08-06 19:33:46 +02:00
parent 733d8580dd
commit 191b3e0d8b
6 changed files with 34 additions and 9 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
histui
.gdb_history
object/
*.out

View File

@ -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

View File

@ -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; }
;

View File

@ -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

View File

@ -1,10 +1,12 @@
#include <locale.h>
#include <sys/ioctl.h>
#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) {

View File

@ -4,8 +4,6 @@
#include <ncurses.h>
#include <readline/readline.h>
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);