aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarstr\xf6m <john@ankarstrom.se>2021-06-01 12:10:35 +0200
committerJohn Ankarstr\xf6m <john@ankarstrom.se>2021-06-01 12:12:24 +0200
commite3b53d6519cc81501d146cb2a158ee049ac4ccd4 (patch)
treec639476f24bf410be28f142d70bdb8e6ee52df9b
parent2fab304c457ad7e08332c1b950f7c97a5dffc058 (diff)
downloadrepl-e3b53d6519cc81501d146cb2a158ee049ac4ccd4.tar.gz
Implement -a (append) option
-rw-r--r--repl.c28
1 files changed, 21 insertions, 7 deletions
diff --git a/repl.c b/repl.c
index 9991338..3794a17 100644
--- a/repl.c
+++ b/repl.c
@@ -2,6 +2,7 @@
#include <stdlib.h>
#include <stdbool.h>
#include <signal.h>
+#include <string.h>
#include <err.h>
#include <readline/readline.h>
#include <readline/history.h>
@@ -13,16 +14,26 @@ void handle_int() { /* handle ctrl-c */
rl_redisplay(); /* thanks James Taylor on Stack Overflow */
}
+void usage(char **argv) {
+ fprintf(stderr, "usage: %s [-a] command\n", argv[0]);
+ exit(1);
+}
+
int main(int argc, char *argv[]) {
- if (argc != 2) {
- fprintf(stderr, "usage: %s command\n", argv[0]);
- return 1;
+ bool append = false;
+
+ if (argc < 2 || argc > 3)
+ usage(argv);
+ if (argc > 1 && argv[1][0] == '-') {
+ if (argv[1][1] != 'a' || argc < 3)
+ usage(argv);
+ append = true;
}
- int size = strlen(argv[1]) + 3 + 1;
+ int size = strlen(argv[argc-1]) + 3 + 1;
char *prompt = malloc(size);
if (prompt == NULL) err(1, NULL);
- snprintf(prompt, size, "%s > ", argv[1]);
+ snprintf(prompt, size, "%s > ", argv[argc-1]);
struct sigaction act = {0};
act.sa_handler = handle_int;
@@ -40,10 +51,13 @@ int main(int argc, char *argv[]) {
if (input[0] != '\0') add_history(input);
- int size = strlen(argv[1]) + 1 + strlen(input) + 1;
+ int size = strlen(argv[argc-1]) + 1 + strlen(input) + 1;
char *command = malloc(size);
if (command == NULL) err(1, NULL);
- snprintf(command, size, "%s %s", argv[1], input);
+ if (append)
+ snprintf(command, size, "%s %s", input, argv[argc-1]);
+ else
+ snprintf(command, size, "%s %s", argv[argc-1], input);
system(command);