aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-06-08 21:29:32 +0200
committerJohn Ankarström <john@ankarstrom.se>2021-06-08 21:29:32 +0200
commit213d5d27adae538c594dc09c9974ff62797994d2 (patch)
treeaa5086b1969ab06df6966482776b734f7a7f969c
parent96faef1269486961872edf43e7f5836bbbd339cb (diff)
downloadrepl-213d5d27adae538c594dc09c9974ff62797994d2.tar.gz
Implement '!' shell commands
-rw-r--r--repl.14
-rw-r--r--repl.c10
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);