Commit fe44d127 authored by Christian Eichler's avatar Christian Eichler
Browse files

Read commands from stdin instead from argv

parent ec0722e6
......@@ -29,7 +29,7 @@ static void print_er() {
}
static void print_vb(char *msg) {
printf(SHELL_ESC"[;36;02m[vb] "SHELL_ESC"[m%s", msg);
fprintf(stderr, SHELL_ESC"[;36;02m[vb] "SHELL_ESC"[m%s", msg);
}
static bool recv_response(FILE *fh, char *buf, size_t buf_size) {
......@@ -66,7 +66,7 @@ static int connect_tty(char *tty) {
unsigned retry = 0;
do {
print_vb("");
printf("open('%s') try %u\n", tty, retry);
fprintf(stderr, "open('%s') try %u\n", tty, retry);
++retry;
struct stat sbuf;
......@@ -86,7 +86,7 @@ static int connect_tty(char *tty) {
}
exit(EXIT_FAILURE);
} else {
print_vb(""); printf("%s successfully opened\n", tty);
print_vb(""); fprintf(stderr, "%s successfully opened\n", tty);
}
// setup control structure
......@@ -222,6 +222,7 @@ static void response_loop(FILE *fh, void (*cb)(bool, char*)) {
}
printf(SHELL_ESC"[m\n");
fflush(stdout);
}
}
......@@ -241,8 +242,8 @@ static struct command commands[] = {
static const size_t commands_count = sizeof(commands)/sizeof(struct command);
int main(int argc, char *argv[]) {
if(3 > argc) {
fprintf(stderr, "Usage: %s <tty> <command>...\n", argv[0]);
if(2 != argc) {
fprintf(stderr, "Usage: %s <tty> \n", argv[0]);
return EXIT_FAILURE;
}
......@@ -259,24 +260,29 @@ int main(int argc, char *argv[]) {
response_loop(fh, resp_inf);
print_vb("Sending user commands...\n");
for(int i = 2; i < argc; ++i) {
char input[512];
while(NULL != fgets(input, sizeof(input), stdin)) {
struct command *cmd = NULL;
input[strlen(input) - 1] = '\0';
for(int j = 0; j < commands_count && NULL == cmd; ++j) {
if(!strncmp(argv[i], commands[j].cmd, strlen(commands[j].cmd))) {
if(!strncmp(input, commands[j].cmd, strlen(commands[j].cmd))) {
cmd = &commands[j];
}
}
if(NULL == cmd) {
print_er();
printf("Unknown command `%s`; ABORTING\n", argv[i]);
printf("Unknown command `%s`; ABORTING\n", input);
return EXIT_FAILURE;
}
print_vb(""); printf("Sending command '%s'...\n", argv[i]);
print_vb(""); fprintf(stderr, "Sending command '%s'...\n", input);
fprintf(fh, "%s \n", argv[i]);
fprintf(fh, "%s \n", input);
fflush(fh);
response_loop(fh, cmd->callback);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment