diff --git a/examples/filesystem/Makefile b/examples/filesystem/Makefile index ca82326dac0f..9170e03547f1 100644 --- a/examples/filesystem/Makefile +++ b/examples/filesystem/Makefile @@ -21,6 +21,8 @@ QUIET ?= 1 # Modules to include: USEMODULE += shell +USEMODULE += shell_cmd_cat +USEMODULE += shell_cmd_tee USEMODULE += shell_cmds_default USEMODULE += ps diff --git a/examples/filesystem/main.c b/examples/filesystem/main.c index 69ec3e8262bd..e72a7c44c2cd 100644 --- a/examples/filesystem/main.c +++ b/examples/filesystem/main.c @@ -19,86 +19,9 @@ */ #include -#include -#include -#include -#include "shell.h" - -static int _cat(int argc, char **argv) -{ - if (argc < 2) { - printf("Usage: %s \n", argv[0]); - return 1; - } - /* With newlib or picolibc, low-level syscalls are plugged to RIOT vfs - * on native, open/read/write/close/... are plugged to RIOT vfs */ -#if defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC) - FILE *f = fopen(argv[1], "r"); - if (f == NULL) { - printf("file %s does not exist\n", argv[1]); - return 1; - } - char c; - while (fread(&c, 1, 1, f) != 0) { - putchar(c); - } - fclose(f); -#else - int fd = open(argv[1], O_RDONLY); - if (fd < 0) { - printf("file %s does not exist\n", argv[1]); - return 1; - } - char c; - while (read(fd, &c, 1) != 0) { - putchar(c); - } - close(fd); -#endif - fflush(stdout); - return 0; -} - -static int _tee(int argc, char **argv) -{ - if (argc != 3) { - printf("Usage: %s \n", argv[0]); - return 1; - } - -#if defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC) - FILE *f = fopen(argv[1], "w+"); - if (f == NULL) { - printf("error while trying to create %s\n", argv[1]); - return 1; - } - if (fwrite(argv[2], 1, strlen(argv[2]), f) != strlen(argv[2])) { - puts("Error while writing"); - } - fclose(f); -#else - int fd = open(argv[1], O_RDWR | O_CREAT, 00777); - if (fd < 0) { - printf("error while trying to create %s\n", argv[1]); - return 1; - } - if (write(fd, argv[2], strlen(argv[2])) != (ssize_t)strlen(argv[2])) { - puts("Error while writing"); - } - close(fd); -#endif - return 0; -} - -static const shell_command_t shell_commands[] = { - { "cat", "print the content of a file", _cat }, - { "tee", "write a string in a file", _tee }, - { NULL, NULL, NULL } -}; - -/* constfs example */ #include "fs/constfs.h" +#include "shell.h" #define HELLO_WORLD_CONTENT "Hello World!\n" #define HELLO_RIOT_CONTENT "Hello RIOT!\n" @@ -142,7 +65,7 @@ int main(void) } char line_buf[SHELL_DEFAULT_BUFSIZE]; - shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE); + shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE); return 0; } diff --git a/makefiles/pseudomodules.inc.mk b/makefiles/pseudomodules.inc.mk index 6b66fcca95ed..34c594637ad2 100644 --- a/makefiles/pseudomodules.inc.mk +++ b/makefiles/pseudomodules.inc.mk @@ -393,6 +393,7 @@ PSEUDOMODULES += servo_saul PSEUDOMODULES += shell_cmd_app_metadata PSEUDOMODULES += shell_cmd_at30tse75x PSEUDOMODULES += shell_cmd_benchmark_udp +PSEUDOMODULES += shell_cmd_cat PSEUDOMODULES += shell_cmd_ccn-lite-utils PSEUDOMODULES += shell_cmd_conn_can PSEUDOMODULES += shell_cmd_cord_ep @@ -441,6 +442,7 @@ PSEUDOMODULES += shell_cmd_sht1x PSEUDOMODULES += shell_cmd_sntp PSEUDOMODULES += shell_cmd_suit PSEUDOMODULES += shell_cmd_sys +PSEUDOMODULES += shell_cmd_tee PSEUDOMODULES += shell_cmd_udptty PSEUDOMODULES += shell_cmd_vfs PSEUDOMODULES += shell_cmds_default diff --git a/sys/shell/cmds/cat.c b/sys/shell/cmds/cat.c new file mode 100644 index 000000000000..8d50c7cf25e1 --- /dev/null +++ b/sys/shell/cmds/cat.c @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2018 OTA keys S.A. + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup sys_shell_commands + * @{ + * + * @file + * @brief Print the content of a file + * + * @author Vincent Dupont + * + * @} + */ + +#include +/*#include */ +#include +#include + +#include "shell.h" + +static int _cat(int argc, char **argv) +{ + if (argc < 2) { + printf("Usage: %s \n", argv[0]); + return 1; + } + /* With newlib or picolibc, low-level syscalls are plugged to RIOT vfs + * on native, open/read/write/close/... are plugged to RIOT vfs */ +#if defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC) + FILE *f = fopen(argv[1], "r"); + if (f == NULL) { + printf("file %s does not exist\n", argv[1]); + return 1; + } + char c; + while (fread(&c, 1, 1, f) != 0) { + putchar(c); + } + fclose(f); +#else + int fd = open(argv[1], O_RDONLY); + if (fd < 0) { + printf("file %s does not exist\n", argv[1]); + return 1; + } + char c; + while (read(fd, &c, 1) != 0) { + putchar(c); + } + close(fd); +#endif + fflush(stdout); + return 0; +} + +SHELL_COMMAND(cat, "print the content of a file", _cat); diff --git a/sys/shell/cmds/tee.c b/sys/shell/cmds/tee.c new file mode 100644 index 000000000000..27fcc5b0b584 --- /dev/null +++ b/sys/shell/cmds/tee.c @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2018 OTA keys S.A. + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup sys_shell_commands + * @{ + * + * @file + * @brief Write a string in a file + * + * @author Vincent Dupont + * + * @} + */ + +#include +#include +#include + +#include "shell.h" + +static int _tee(int argc, char **argv) +{ + if (argc != 3) { + printf("Usage: %s \n", argv[0]); + return 1; + } + +#if defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC) + FILE *f = fopen(argv[1], "w+"); + if (f == NULL) { + printf("error while trying to create %s\n", argv[1]); + return 1; + } + if (fwrite(argv[2], 1, strlen(argv[2]), f) != strlen(argv[2])) { + puts("Error while writing"); + } + fclose(f); +#else + int fd = open(argv[1], O_RDWR | O_CREAT, 00777); + if (fd < 0) { + printf("error while trying to create %s\n", argv[1]); + return 1; + } + if (write(fd, argv[2], strlen(argv[2])) != (ssize_t)strlen(argv[2])) { + puts("Error while writing"); + } + close(fd); +#endif + return 0; +} + +SHELL_COMMAND(tee, "write a string in a file", _tee);