]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_run.c
Merge branch 'chan-fatfs' into fatfs-integration
[z180-stamp.git] / avr / cmd_run.c
1 /*
2 * (C) Copyright 2016 Leo C. <erbl259-lmu@yahoo.de>
3 *
4 * SPDX-License-Identifier: GPL-2.0
5 */
6
7 #include "common.h"
8 #include <string.h>
9 #include <stdio.h>
10
11 #include "ff.h"
12 #include "config.h"
13 #include "command.h"
14 #include "cli_readline.h" /* console_buffer[] */
15 #include "cli.h" /* run_command() */
16 #include "env.h"
17
18
19 command_ret_t do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
20 {
21 int i;
22 (void) cmdtp;
23
24 if (argc < 2)
25 return CMD_RET_USAGE;
26
27 for (i = 1; i < argc; ++i) {
28 char *arg;
29
30 arg = getenv_str(argv[i]);
31 if (arg == NULL) {
32 printf_P(PSTR("## Error: \"%s\" is not set\n"), argv[i]);
33 return CMD_RET_FAILURE;
34 }
35
36 if (run_command(arg, flag) != 0)
37 return CMD_RET_FAILURE;
38 }
39 return CMD_RET_SUCCESS;
40 }
41
42 static int source(FIL *fp, int flag, int argc, char * const argv[])
43 {
44 int lineno = 0;
45 int res = 0;
46
47 (void)argc; (void)argv;
48
49 while (!f_eof(fp) && !f_error(fp) && !res) {
50 lineno++;
51 if (f_gets(console_buffer, CONFIG_SYS_CBSIZE, fp)) {
52 int i = strlen(console_buffer) - 1;
53 if (i != 0) {
54 if (i > 0) {
55 if (console_buffer[i] != '\n') {
56 printf_P(PSTR("Error: line %d to long\n"), lineno);
57 res = -1;
58 break;
59 }
60 }
61 console_buffer[i] = 0;
62 res = run_command(console_buffer, flag);
63 }
64 }
65 }
66 return !f_eof(fp) || res;
67 }
68
69 command_ret_t do_source(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
70 {
71 FIL File;
72 int res;
73
74 (void) cmdtp;
75
76 if (argc < 2)
77 return CMD_RET_USAGE;
78
79 res = f_open(&File, argv[1], FA_READ );
80 if (res) {
81 printf_P(PSTR("Error: failed to open script '%s'\n"), argv[1]);
82 return CMD_RET_FAILURE;
83 }
84
85 printf_P(PSTR("Executing script: '%s'...\n"), argv[1]);
86 res = source(&File, flag, --argc, ++argv);
87 f_close(&File);
88 if (res != 0) {
89 return CMD_RET_FAILURE;
90 }
91 return CMD_RET_SUCCESS;
92 }