diff options
author | Leo C | 2016-05-25 13:50:53 +0200 |
---|---|---|
committer | Leo C | 2016-05-25 13:50:53 +0200 |
commit | 04b3ea0e67e1f1fe8e9af71f6d532af5471da0e3 (patch) | |
tree | 7c061f9f7f6ee41ed02f76f6f67b63a322375cd8 /avr/cmd_run.c | |
parent | 32154e5a9e9bf0a52269b3715711b76dc8fcac0f (diff) | |
download | z180-stamp-04b3ea0e67e1f1fe8e9af71f6d532af5471da0e3.zip |
new command: source - run commands from a file
Diffstat (limited to 'avr/cmd_run.c')
-rw-r--r-- | avr/cmd_run.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/avr/cmd_run.c b/avr/cmd_run.c new file mode 100644 index 0000000..97d39d1 --- /dev/null +++ b/avr/cmd_run.c @@ -0,0 +1,92 @@ +/* + * (C) Copyright 2016 Leo C. <erbl259-lmu@yahoo.de> + * + * SPDX-License-Identifier: GPL-2.0 + */ + +#include "common.h" +#include <string.h> +#include <stdio.h> + +#include "ff.h" +#include "config.h" +#include "command.h" +#include "cli_readline.h" /* console_buffer[] */ +#include "cli.h" /* run_command() */ +#include "env.h" + + +command_ret_t do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ + int i; + (void) cmdtp; + + if (argc < 2) + return CMD_RET_USAGE; + + for (i = 1; i < argc; ++i) { + char *arg; + + arg = getenv_char(argv[i]); + if (arg == NULL) { + printf_P(PSTR("## Error: \"%s\" is not set\n"), argv[i]); + return CMD_RET_FAILURE; + } + + if (run_command(arg, flag) != 0) + return CMD_RET_FAILURE; + } + return CMD_RET_SUCCESS; +} + +static int source(FIL *fp, int flag, int argc, char * const argv[]) +{ + int lineno = 0; + int res = 0; + + (void)argc; (void)argv; + + while (!f_eof(fp) && !f_error(fp) && !res) { + lineno++; + if (f_gets(console_buffer, CONFIG_SYS_CBSIZE, fp)) { + int i = strlen(console_buffer) - 1; + if (i != 0) { + if (i > 0) { + if (console_buffer[i] != '\n') { + printf_P(PSTR("Error: line %d to long\n"), lineno); + res = -1; + break; + } + } + console_buffer[i] = 0; + res = run_command(console_buffer, flag); + } + } + } + return !f_eof(fp) || res; +} + +command_ret_t do_source(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ + FIL File; + int res; + + (void) cmdtp; + + if (argc < 2) + return CMD_RET_USAGE; + + res = f_open(&File, argv[1], FA_READ ); + if (res) { + printf_P(PSTR("Error: failed to open script '%s'\n"), argv[1]); + return CMD_RET_FAILURE; + } + + printf_P(PSTR("Executing script: '%s'...\n"), argv[1]); + res = source(&File, flag, --argc, ++argv); + f_close(&File); + if (res != 0) { + return CMD_RET_FAILURE; + } + return CMD_RET_SUCCESS; +} |