|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 ML!PA Consulting GmbH |
| 3 | + * |
| 4 | + * This file is subject to the terms and conditions of the GNU Lesser |
| 5 | + * General Public License v2.1. See the file LICENSE in the top level |
| 6 | + * directory for more details. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * @ingroup tests |
| 11 | + * @{ |
| 12 | + * |
| 13 | + * @file |
| 14 | + * @brief Shell Scripting Test Application |
| 15 | + * |
| 16 | + * @author Benjamin Valentin <[email protected]> |
| 17 | + * @} |
| 18 | + */ |
| 19 | + |
| 20 | +#include <stdlib.h> |
| 21 | + |
| 22 | +#include "shell.h" |
| 23 | +#include "vfs_default.h" |
| 24 | +#include "vfs_util.h" |
| 25 | +#include "ztimer.h" |
| 26 | + |
| 27 | +#ifndef SCRIPT_FILE |
| 28 | +#define SCRIPT_FILE VFS_DEFAULT_DATA "/script.sh" |
| 29 | +#endif |
| 30 | + |
| 31 | +static int cmd_msleep(int argc, char **argv) |
| 32 | +{ |
| 33 | + if (argc != 2) { |
| 34 | + return -1; |
| 35 | + } |
| 36 | + |
| 37 | + ztimer_sleep(ZTIMER_MSEC, atoi(argv[1])); |
| 38 | + return 0; |
| 39 | +} |
| 40 | + |
| 41 | +static int cmd_echo(int argc, char **argv) |
| 42 | +{ |
| 43 | + for (int i = 1; i < argc; ++i) { |
| 44 | + printf("%s ", argv[i]); |
| 45 | + } |
| 46 | + puts(""); |
| 47 | + |
| 48 | + return 0; |
| 49 | +} |
| 50 | + |
| 51 | +static int cmd_add(int argc, char **argv) |
| 52 | +{ |
| 53 | + int sum = 0; |
| 54 | + |
| 55 | + for (int i = 1; i < argc; ++i) { |
| 56 | + sum += atoi(argv[i]); |
| 57 | + } |
| 58 | + |
| 59 | + printf("%d\n", sum); |
| 60 | + return 0; |
| 61 | +} |
| 62 | + |
| 63 | +static const shell_command_t shell_commands[] = { |
| 64 | + { "msleep", "sleep for a number of ms", cmd_msleep }, |
| 65 | + { "echo", "echo parameters to console", cmd_echo }, |
| 66 | + { "add", "add up a list of numbers", cmd_add }, |
| 67 | + { NULL, NULL, NULL } |
| 68 | +}; |
| 69 | + |
| 70 | +static int _create_script(void) |
| 71 | +{ |
| 72 | + const char file[] = { |
| 73 | + "# this is a comment\n" |
| 74 | + "msleep 500\n" |
| 75 | + "echo Hello RIOT!\n" |
| 76 | + "\n" |
| 77 | + "add 10 23 9\n" |
| 78 | + "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" |
| 79 | + }; |
| 80 | + |
| 81 | + return vfs_file_from_buffer(SCRIPT_FILE, file, sizeof(file) - 1); |
| 82 | +} |
| 83 | + |
| 84 | +int main(void) |
| 85 | +{ |
| 86 | + _create_script(); |
| 87 | + |
| 88 | + unsigned line; |
| 89 | + int res = shell_parse_file(shell_commands, SCRIPT_FILE, &line); |
| 90 | + if (res) { |
| 91 | + fprintf(stderr, "%s:%u: error %d\n", SCRIPT_FILE, line, res); |
| 92 | + } |
| 93 | + return res; |
| 94 | +} |
0 commit comments