diff --git a/sys/include/shell.h b/sys/include/shell.h index b0ef48e6016a6..17e6337154050 100644 --- a/sys/include/shell.h +++ b/sys/include/shell.h @@ -237,6 +237,20 @@ static inline void shell_run(const shell_command_t *commands, */ int shell_handle_input_line(const shell_command_t *commands, char *line); +/** + * @brief Read shell commands from a file and run them. + * + * @note This requires the `vfs` module. + * + * @param[in] commands ptr to array of command structs + * @param[in] filename file to read shell commands from + * + * @returns 0 if all commands were executed successful + * error return of failed command otherwise + */ +int shell_parse_file(const shell_command_t *commands, + const char *filename); + #ifndef __cplusplus /** * @brief Define shell command diff --git a/sys/shell/shell.c b/sys/shell/shell.c index fa0b03113bac9..b8e1405a2cbb5 100644 --- a/sys/shell/shell.c +++ b/sys/shell/shell.c @@ -41,6 +41,11 @@ #include "shell.h" #include "shell_lock.h" +#ifdef MODULE_VFS +#include +#include "vfs.h" +#endif + /* define shell command cross file array */ XFA_INIT_CONST(shell_command_xfa_t*, shell_commands_xfa); @@ -529,3 +534,33 @@ void shell_run_once(const shell_command_t *shell_commands, print_prompt(); } } + +#ifdef MODULE_VFS +int shell_parse_file(const shell_command_t *shell_commands, + const char *filename) +{ + char buffer[SHELL_DEFAULT_BUFSIZE]; + + int fd = vfs_open(filename, O_RDONLY, 0); + if (fd < 0) { + printf("Can't open %s\n", filename); + return fd; + } + + int res = 0; + while (vfs_readline(fd, buffer, sizeof(buffer)) > 0) { + /* skip comment lines */ + if (buffer[0] == '#') { + continue; + } + res = shell_handle_input_line(shell_commands, buffer); + if (res) { + break; + } + } + + vfs_close(fd); + + return 0; +} +#endif