]> cloudbase.mooo.com Git - z180-stamp.git/commitdiff
new command: source - run commands from a file
authorLeo C <erbl259-lmu@yahoo.de>
Wed, 25 May 2016 11:50:53 +0000 (13:50 +0200)
committerLeo C <erbl259-lmu@yahoo.de>
Wed, 25 May 2016 11:50:53 +0000 (13:50 +0200)
avr/Tupfile
avr/cli.c
avr/cli_readline.c
avr/cmd_boot.c
avr/cmd_run.c [new file with mode: 0644]
avr/command_tbl.c
include/avr/ffconf.h
include/cli.h
include/cli_readline.h
include/command.h

index 331c11f0196f0e95404dd92660203ac03ad87696..9bb0d110fbf49ee2eed03f56f09af228d0577b02 100644 (file)
@@ -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
index aa2c38883b1f58fe230166aa11bc9e814b73c080..6e27fcec335ca9542d0fc2ffbcee7897e7e1dc38 100644 (file)
--- 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;
-}
index e4c3f5ad5893fbee9e75d62a9c7fe5481b056c3e..32c15c8a59b4fd5b49eab2278ad78db930d34c06 100644 (file)
@@ -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.
index f3bce9d9544e9d5eda4c13591727e4d6acf5dc37..0d98296d21137238543aae5017c8584a47feaa11 100644 (file)
@@ -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 (file)
index 0000000..97d39d1
--- /dev/null
@@ -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;
+}
index 2c9135502ae5b66c0fcd64ffb1d64bb9fb2c323a..5dcbf1f1649dbfc72be643b512e62053349df3a1 100644 (file)
@@ -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[] =
@@ -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",
index fadbe3d4d361720adf96d7889ff47fbf5ccb3f37..da0052bade08d08233cd643b482375f0e7a7fcac 100644 (file)
@@ -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().
 /
index 20e852fbe882d8019c01a68d0592a885f3bb53d8..28e92be7f6719e09ca0d7d85ec9ebd9961e3ac54 100644 (file)
@@ -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 2014 Google, Inc
  * Simon Glass <sjg@chromium.org>
index 0e165cc11e1ebe65d58b588074de3b9239691cb7..0ce28a5532e8be3a7baa8fda9ea474d5fac117e2 100644 (file)
@@ -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 2014 Google, Inc
  * Simon Glass <sjg@chromium.org>
index 5582fa5bc5d8836756d8ae07e6999131e1b30428..db469e314f7ed8af865a571c2ece8970b3d2ecce 100644 (file)
@@ -1,10 +1,10 @@
 /*
- * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
+ * (C) Copyright 2014-2016 Leo C. <erbl259-lmu@yahoo.de>
  *
  * (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.