From 213d5d27adae538c594dc09c9974ff62797994d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Tue, 8 Jun 2021 21:29:32 +0200 Subject: Implement '!' shell commands --- repl.1 | 4 ++++ repl.c | 10 +++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/repl.1 b/repl.1 index 15c848d..1c704cc 100644 --- a/repl.1 +++ b/repl.1 @@ -28,6 +28,10 @@ Built on GNU readline, .Nm supports history and Emacs key bindings. +Like in many other line-based UNIX interfaces, +you can execute a shell command without a prefix +by adding an exclamation mark before it. + .Nm repl is useful for command-line interfaces built on a .Dq prefix command diff --git a/repl.c b/repl.c index 3794a17..1f439b1 100644 --- a/repl.c +++ b/repl.c @@ -33,7 +33,7 @@ int main(int argc, char *argv[]) { int size = strlen(argv[argc-1]) + 3 + 1; char *prompt = malloc(size); if (prompt == NULL) err(1, NULL); - snprintf(prompt, size, "%s > ", argv[argc-1]); + snprintf(prompt, size, "%s& ", argv[argc-1]); struct sigaction act = {0}; act.sa_handler = handle_int; @@ -44,17 +44,17 @@ int main(int argc, char *argv[]) { while (true) { char *input = readline(prompt); - if (input == NULL) { /* ctrl-d */ - printf("\n"); + if (input == NULL) /* ctrl-d */ break; - } if (input[0] != '\0') add_history(input); int size = strlen(argv[argc-1]) + 1 + strlen(input) + 1; char *command = malloc(size); if (command == NULL) err(1, NULL); - if (append) + if (input[0] == '!') + snprintf(command, size, "%s", input + 1); + else if (append) snprintf(command, size, "%s %s", input, argv[argc-1]); else snprintf(command, size, "%s %s", argv[argc-1], input); -- cgit v1.2.3