From 04b3ea0e67e1f1fe8e9af71f6d532af5471da0e3 Mon Sep 17 00:00:00 2001 From: Leo C Date: Wed, 25 May 2016 13:50:53 +0200 Subject: [PATCH] new command: source - run commands from a file --- avr/Tupfile | 3 +- avr/cli.c | 26 +----------- avr/cli_readline.c | 2 +- avr/cmd_boot.c | 6 +-- avr/cmd_run.c | 92 ++++++++++++++++++++++++++++++++++++++++++ avr/command_tbl.c | 13 ++++-- include/avr/ffconf.h | 2 +- include/cli.h | 2 +- include/cli_readline.h | 2 +- include/command.h | 6 +-- 10 files changed, 114 insertions(+), 40 deletions(-) create mode 100644 avr/cmd_run.c diff --git a/avr/Tupfile b/avr/Tupfile index 331c11f..9bb0d11 100644 --- a/avr/Tupfile +++ b/avr/Tupfile @@ -6,7 +6,8 @@ FATFS = $(TOP)/fatfs/src/ff.c SRC = main.c SRC += cli.c cli_readline.c command.c command_tbl.c -SRC += cmd_help.c cmd_date.c cmd_mem.c cmd_boot.c cmd_gpio.c cmd_misc.c +SRC += cmd_help.c cmd_run.c cmd_boot.c cmd_misc.c +SRC += cmd_date.c cmd_mem.c cmd_gpio.c SRC += cmd_loadihex.c cmd_loadcpm3.c cmd_sd.c cmd_fat.c SRC += env.c xmalloc.c con-utils.c print-utils.c getopt-min.c SRC += timer.c serial.c i2c.c bcd.c pcf8583.c mmc.c diff --git a/avr/cli.c b/avr/cli.c index aa2c388..6e27fce 100644 --- a/avr/cli.c +++ b/avr/cli.c @@ -1,5 +1,5 @@ /* - * (C) Copyright 2014 Leo C. + * (C) Copyright 2014-2016 Leo C. * * (C) Copyright 2000 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. @@ -385,27 +385,3 @@ void cli_loop(void) } } } - - -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; -} diff --git a/avr/cli_readline.c b/avr/cli_readline.c index e4c3f5a..32c15c8 100644 --- a/avr/cli_readline.c +++ b/avr/cli_readline.c @@ -1,5 +1,5 @@ /* - * (C) Copyright 2014 Leo C. + * (C) Copyright 2014-2016 Leo C. * * (C) Copyright 2000 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. diff --git a/avr/cmd_boot.c b/avr/cmd_boot.c index f3bce9d..0d98296 100644 --- a/avr/cmd_boot.c +++ b/avr/cmd_boot.c @@ -1,5 +1,5 @@ /* - * (C) Copyright 2014 Leo C. + * (C) Copyright 2014-2016 Leo C. * * (C) Copyright 2000-2003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. @@ -16,8 +16,8 @@ #include #include "command.h" -#include "cli_readline.h" -#include "cli.h" +#include "cli_readline.h" /* console_buffer[] */ +#include "cli.h" /* run_command() */ #include "env.h" #include "con-utils.h" #include "z80-if.h" 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. + * + * SPDX-License-Identifier: GPL-2.0 + */ + +#include "common.h" +#include +#include + +#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; +} diff --git a/avr/command_tbl.c b/avr/command_tbl.c index 2c91355..5dcbf1f 100644 --- a/avr/command_tbl.c +++ b/avr/command_tbl.c @@ -1,5 +1,5 @@ /* - * (C) Copyright 2014 Leo C. + * (C) Copyright 2014-2016 Leo C. * * SPDX-License-Identifier: GPL-2.0 */ @@ -34,9 +34,9 @@ extern command_ret_t do_gpio(cmd_tbl_t *, int, int, char * const []); extern command_ret_t do_sd(cmd_tbl_t *, int, int, char * const []); extern command_ret_t do_fat_stat(cmd_tbl_t *, int, int, char * const []); extern command_ret_t do_fat_ls(cmd_tbl_t *, int, int, char * const []); -//extern command_ret_t do_fat_read(cmd_tbl_t *, int, int, char * const []); -//extern command_ret_t do_fat_write(cmd_tbl_t *, int, int, char * const []); extern command_ret_t do_fat_rw(cmd_tbl_t *, int, int, char * const []); +extern command_ret_t do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); +extern command_ret_t do_source(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); #ifdef CONFIG_SYS_LONGHELP const FLASH char sd_help_text[] = @@ -114,6 +114,13 @@ CMD_TBL_ITEM_COMPLETE( " - run the commands in the environment variable(s) 'var'", var_complete ), +CMD_TBL_ITEM_COMPLETE( + source, CONFIG_SYS_MAXARGS, 1, do_source, + "run commands from a file", + "filename\n" + " - run the commands in the script file 'filename'", + var_complete +), CMD_TBL_ITEM_COMPLETE( printenv, CONFIG_SYS_MAXARGS, 1, do_env_print, "print environment variables", diff --git a/include/avr/ffconf.h b/include/avr/ffconf.h index fadbe3d..da0052b 100644 --- a/include/avr/ffconf.h +++ b/include/avr/ffconf.h @@ -33,7 +33,7 @@ / 3: f_lseek() function is removed in addition to 2. */ -#define _USE_STRFUNC 0 +#define _USE_STRFUNC 2 /* This option switches string functions, f_gets(), f_putc(), f_puts() and / f_printf(). / diff --git a/include/cli.h b/include/cli.h index 20e852f..28e92be 100644 --- a/include/cli.h +++ b/include/cli.h @@ -1,5 +1,5 @@ /* - * (C) Copyright 2014 Leo C. + * (C) Copyright 2014-2016 Leo C. * * (C) Copyright 2014 Google, Inc * Simon Glass diff --git a/include/cli_readline.h b/include/cli_readline.h index 0e165cc..0ce28a5 100644 --- a/include/cli_readline.h +++ b/include/cli_readline.h @@ -1,5 +1,5 @@ /* - * (C) Copyright 2014 Leo C. + * (C) Copyright 2014-2016 Leo C. * * (C) Copyright 2014 Google, Inc * Simon Glass diff --git a/include/command.h b/include/command.h index 5582fa5..db469e3 100644 --- a/include/command.h +++ b/include/command.h @@ -1,10 +1,10 @@ /* - * (C) Copyright 2014 Leo C. + * (C) Copyright 2014-2016 Leo C. * * (C) Copyright 2000-2009 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. * - * SPDX-License-Identifier: GPL-2.0+ + * SPDX-License-Identifier: GPL-2.0 */ /* @@ -61,8 +61,6 @@ struct cmd_tbl_s { typedef const FLASH struct cmd_tbl_s cmd_tbl_t; -extern command_ret_t do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); - /** * Process a command with arguments. We look up the command and execute it * if valid. Otherwise we print a usage message. -- 2.39.2