diff options
author | John Ankarström <john@ankarstrom.se> | 2021-06-08 21:29:32 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-06-08 21:29:32 +0200 |
commit | 213d5d27adae538c594dc09c9974ff62797994d2 (patch) | |
tree | aa5086b1969ab06df6966482776b734f7a7f969c | |
parent | 96faef1269486961872edf43e7f5836bbbd339cb (diff) | |
download | repl-213d5d27adae538c594dc09c9974ff62797994d2.tar.gz |
Implement '!' shell commands
-rw-r--r-- | repl.1 | 4 | ||||
-rw-r--r-- | repl.c | 10 |
2 files changed, 9 insertions, 5 deletions
@@ -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 @@ -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); |