From 6a421fd01e7e9240d5b9ba84743ea176a81eba2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Thu, 10 Jan 2019 00:33:54 +0100 Subject: working (no history) --- Makefile | 4 ++++ repl.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 Makefile create mode 100644 repl.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6c6bc49 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +CFLAGS = -Werror -Wall +LDFLAGS = -lreadline -ltermcap + +repl: repl.c diff --git a/repl.c b/repl.c new file mode 100644 index 0000000..090b65d --- /dev/null +++ b/repl.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include +#include + +void handle_int(int sig) { + printf("\n"); + rl_on_new_line(); + rl_replace_line("", 0); + rl_redisplay(); /* thanks James Taylor on Stack Overflow */ +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + fprintf(stderr, "usage: %s command\n", argv[0]); + return 1; + } + + int size = strlen(argv[1]) + 3 + 1; + char *prompt = malloc(size); + snprintf(prompt, size, "%s > ", argv[1]); + + struct sigaction act; + act.sa_handler = handle_int; + sigaction(SIGINT, &act, NULL); + + rl_clear_signals(); + + while (true) { + char *input = readline(prompt); + + if (input == NULL) { /* ctrl-d */ + printf("\n"); + break; + } + + int size = strlen(argv[1]) + 1 + strlen(input) + 1; + char *command = malloc(size); + snprintf(command, size, "%s %s", argv[1], input); + + system(command); + + free(input); + } + return 0; +} -- cgit v1.2.3