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