Added non-interactive command processing.

This commit is contained in:
Daniel Beer 2009-11-17 15:34:10 +13:00
parent 2057f3ea6a
commit 7c9b6432a5
1 changed files with 31 additions and 16 deletions

47
main.c
View File

@ -544,6 +544,22 @@ static void sigint_handler(int signum)
{ {
} }
static void process_command(char *arg)
{
const char *cmd_text;
cmd_text = get_arg(&arg);
if (cmd_text) {
const struct command *cmd = find_command(cmd_text);
if (cmd)
cmd->func(&arg);
else
fprintf(stderr, "unknown command: %s (try \"help\")\n",
cmd_text);
}
}
static void reader_loop(void) static void reader_loop(void)
{ {
const static struct sigaction siga = { const static struct sigaction siga = {
@ -557,8 +573,6 @@ static void reader_loop(void)
for (;;) { for (;;) {
char buf[128]; char buf[128];
int len; int len;
char *arg = buf;
char *cmd_text;
printf("(mspdebug) "); printf("(mspdebug) ");
fflush(stdout); fflush(stdout);
@ -574,17 +588,7 @@ static void reader_loop(void)
len--; len--;
buf[len] = 0; buf[len] = 0;
cmd_text = get_arg(&arg); process_command(buf);
if (cmd_text) {
const struct command *cmd = find_command(cmd_text);
if (cmd)
cmd->func(&arg);
else
fprintf(stderr, "unknown command: %s "
"(try \"help\")\n",
cmd_text);
}
} }
printf("\n"); printf("\n");
@ -592,10 +596,15 @@ static void reader_loop(void)
static void usage(const char *progname) static void usage(const char *progname)
{ {
fprintf(stderr, "Usage: %s [-u device]\n" fprintf(stderr, "Usage: %s [-u device] [command ...]\n"
"\n"
"By default, the first RF2500 device on the USB bus is opened. If -u is\n" "By default, the first RF2500 device on the USB bus is opened. If -u is\n"
"given, then a UIF device attached to the specified serial port is\n" "given, then a UIF device attached to the specified serial port is\n"
"opened.\n", progname); "opened.\n"
"\n"
"If commands are given, they will be executed. Otherwise, an interactive\n"
"command reader is started.\n",
progname);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
@ -630,7 +639,13 @@ int main(int argc, char **argv)
if (result < 0) if (result < 0)
return -1; return -1;
reader_loop(); if (optind < argc) {
while (optind < argc)
process_command(argv[optind++]);
} else {
reader_loop();
}
fet_run(); fet_run();
fet_close(); fet_close();