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

Parse commands from argv instead of hardcoding them

parent 2832d8a1
......@@ -158,6 +158,14 @@ static void resp_eic(bool success, char *resp) {
printf("Instruction Cache now enabled!");
}
static void print_ok() {
printf(SHELL_ESC"[;32;02m[ok] "SHELL_ESC"[m");
}
static void print_er() {
printf(SHELL_ESC"[;31;02m[ER] "SHELL_ESC"[m");
}
static void response_loop(FILE *fh, void (*cb)(bool, char*)) {
bool done = false;
char buf[128];
......@@ -169,12 +177,12 @@ static void response_loop(FILE *fh, void (*cb)(bool, char*)) {
switch(buf[0]) {
case 'o':
success = true;
printf(SHELL_ESC"[;32;02m[ok] "SHELL_ESC"[m");
print_ok();
break;
case 'e':
success = false;
printf(SHELL_ESC"[;31;02m[ER] "SHELL_ESC"[m");
print_er();
break;
default:
......@@ -187,9 +195,24 @@ static void response_loop(FILE *fh, void (*cb)(bool, char*)) {
}
}
struct command {
char *cmd;
uint32_t nparam;
void (*callback)(bool, char*);
};
static struct command commands[] = {
{"bch", 1, resp_bch},
{"dic", 0, resp_dic},
{"eic", 0, resp_eic},
{"inf", 0, resp_inf},
{"rst", 0, NULL}
};
static const size_t commands_count = sizeof(commands)/sizeof(struct command);
int main(int argc, char *argv[]) {
if(4 != argc) {
fprintf(stderr, "Usage: %s <tty> <dic/eic> <input>\n", argv[0]);
if(3 > argc) {
fprintf(stderr, "Usage: %s <tty> <command>...\n", argv[0]);
return EXIT_FAILURE;
}
......@@ -200,15 +223,30 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}
fprintf(fh, "inf \n"); fflush(fh);
fprintf(fh, "inf \n");
fflush(fh);
response_loop(fh, resp_inf);
for(int i = 2; i < argc; ++i) {
struct command *cmd = NULL;
fprintf(fh, "%s \n", argv[2]); fflush(fh);
response_loop(fh, streq(argv[2], "dic") ? resp_dic : resp_eic);
for(int j = 0; j < commands_count && NULL == cmd; ++j) {
if(!strncmp(argv[i], commands[j].cmd, strlen(commands[j].cmd))) {
cmd = &commands[j];
}
}
fprintf(fh, "bch %s\n", argv[3]); fflush(fh);
response_loop(fh, resp_bch);
return 0;
if(NULL == cmd) {
print_er();
printf("Unknown command `%s`; ABORTING\n", argv[i]);
return EXIT_FAILURE;
}
fprintf(fh, "%s \n", argv[i]);
fflush(fh);
response_loop(fh, cmd->callback);
}
return EXIT_SUCCESS;
}
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