mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
sys/shell: add shell_parse_file()
This commit is contained in:
parent
138a94bae4
commit
471e257c7c
@ -237,6 +237,21 @@ 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
|
||||
* @param[out] line_nr line on which an error occurred, may be NULL
|
||||
*
|
||||
* @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, unsigned *line_nr);
|
||||
|
||||
#ifndef __cplusplus
|
||||
/**
|
||||
* @brief Define shell command
|
||||
|
@ -41,6 +41,11 @@
|
||||
#include "shell.h"
|
||||
#include "shell_lock.h"
|
||||
|
||||
#ifdef MODULE_VFS
|
||||
#include <fcntl.h>
|
||||
#include "vfs.h"
|
||||
#endif
|
||||
|
||||
/* define shell command cross file array */
|
||||
XFA_INIT_CONST(shell_command_xfa_t*, shell_commands_xfa);
|
||||
|
||||
@ -529,3 +534,44 @@ 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, unsigned *line_nr)
|
||||
{
|
||||
char buffer[SHELL_DEFAULT_BUFSIZE];
|
||||
|
||||
if (line_nr) {
|
||||
*line_nr = 0;
|
||||
}
|
||||
|
||||
int res, fd = vfs_open(filename, O_RDONLY, 0);
|
||||
if (fd < 0) {
|
||||
printf("Can't open %s\n", filename);
|
||||
return fd;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
res = vfs_readline(fd, buffer, sizeof(buffer));
|
||||
if (line_nr) {
|
||||
*line_nr += 1;
|
||||
}
|
||||
/* error reading line */
|
||||
if (res < 0) {
|
||||
break;
|
||||
}
|
||||
/* skip comment and empty lines */
|
||||
if (buffer[0] == '#') {
|
||||
continue;
|
||||
}
|
||||
res = shell_handle_input_line(shell_commands, buffer);
|
||||
if (res) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
vfs_close(fd);
|
||||
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user