]> cloudbase.mooo.com Git - z180-stamp.git/blame_incremental - avr/cmd_run.c
add missing newlines
[z180-stamp.git] / avr / cmd_run.c
... / ...
CommitLineData
1/*
2 * (C) Copyright 2016, 2018 Leo C. <erbl259-lmu@yahoo.de>
3 *
4 * SPDX-License-Identifier: GPL-2.0
5 */
6
7#include "cmd_run.h"
8
9#include "ff.h"
10#include "cli_readline.h" /* console_buffer[] */
11#include "cli.h" /* run_command() */
12#include "env.h"
13
14
15command_ret_t do_run(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag, int argc, char * const argv[])
16{
17 if (argc < 2)
18 return CMD_RET_USAGE;
19
20 for (int i = 1; i < argc; ++i) {
21 char *arg;
22
23 arg = getenv_str(argv[i]);
24 if (arg == NULL) {
25 printf_P(PSTR("run: \"%s\" is not set\n"), argv[i]);
26 return CMD_RET_FAILURE;
27 }
28
29 if (run_command(arg, flag) != 0)
30 return CMD_RET_FAILURE;
31 }
32 return CMD_RET_SUCCESS;
33}
34
35command_ret_t do_bootd(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag, int argc UNUSED, char * const argv[] UNUSED)
36{
37 return run_command(getenv_str("bootcmd"), flag);
38}
39
40static int source(FIL *fp, uint_fast8_t flag, int argc UNUSED, char * const argv[] UNUSED)
41{
42 int lineno = 0;
43 int res = 0;
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
65command_ret_t do_source(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag, int argc, char * const argv[])
66{
67 FIL File;
68 int res;
69
70 if (argc < 2)
71 return CMD_RET_USAGE;
72
73 res = f_open(&File, argv[1], FA_READ );
74 if (res) {
75 printf_P(PSTR("Error: failed to open script '%s'\n"), argv[1]);
76 return CMD_RET_FAILURE;
77 }
78
79 printf_P(PSTR("Executing script: '%s'...\n"), argv[1]);
80 res = source(&File, flag, --argc, ++argv);
81 f_close(&File);
82 if (res != 0) {
83 return CMD_RET_FAILURE;
84 }
85 return CMD_RET_SUCCESS;
86}