summaryrefslogtreecommitdiff
path: root/avr
diff options
context:
space:
mode:
authorLeo C2016-05-25 13:50:53 +0200
committerLeo C2016-05-25 13:50:53 +0200
commit04b3ea0e67e1f1fe8e9af71f6d532af5471da0e3 (patch)
tree7c061f9f7f6ee41ed02f76f6f67b63a322375cd8 /avr
parent32154e5a9e9bf0a52269b3715711b76dc8fcac0f (diff)
downloadz180-stamp-04b3ea0e67e1f1fe8e9af71f6d532af5471da0e3.zip
new command: source - run commands from a file
Diffstat (limited to 'avr')
-rw-r--r--avr/Tupfile3
-rw-r--r--avr/cli.c26
-rw-r--r--avr/cli_readline.c2
-rw-r--r--avr/cmd_boot.c6
-rw-r--r--avr/cmd_run.c92
-rw-r--r--avr/command_tbl.c13
6 files changed, 109 insertions, 33 deletions
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. <erbl259-lmu@yahoo.de>
+ * (C) Copyright 2014-2016 Leo C. <erbl259-lmu@yahoo.de>
*
* (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. <erbl259-lmu@yahoo.de>
+ * (C) Copyright 2014-2016 Leo C. <erbl259-lmu@yahoo.de>
*
* (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. <erbl259-lmu@yahoo.de>
+ * (C) Copyright 2014-2016 Leo C. <erbl259-lmu@yahoo.de>
*
* (C) Copyright 2000-2003
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
@@ -16,8 +16,8 @@
#include <util/atomic.h>
#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. <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;
+}
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. <erbl259-lmu@yahoo.de>
+ * (C) Copyright 2014-2016 Leo C. <erbl259-lmu@yahoo.de>
*
* 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[] =
@@ -115,6 +115,13 @@ CMD_TBL_ITEM_COMPLETE(
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",
"\n"