diff options
author | John Ankarström <john@ankarstrom.se> | 2019-01-10 00:33:54 +0100 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2019-01-10 00:33:54 +0100 |
commit | 6a421fd01e7e9240d5b9ba84743ea176a81eba2a (patch) | |
tree | 310b1fb105df876ac463b2b74f427919a03df49a | |
download | repl-6a421fd01e7e9240d5b9ba84743ea176a81eba2a.tar.gz |
working (no history)
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | repl.c | 49 |
2 files changed, 53 insertions, 0 deletions
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 @@ -0,0 +1,49 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <signal.h> +#include <errno.h> +#include <readline/readline.h> +#include <readline/history.h> + +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; +} |