From e7106b54c4ea103c5e46496614ae653f14be09b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Mon, 27 Apr 2020 21:52:52 +0200 Subject: [PATCH] Add cat command --- stm-firmware/shell.c | 55 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/stm-firmware/shell.c b/stm-firmware/shell.c index bf6a583..f913884 100644 --- a/stm-firmware/shell.c +++ b/stm-firmware/shell.c @@ -329,6 +329,49 @@ static shellmatta_retCode_t shell_cmd_reset(const shellmatta_handle_t handle, co return SHELLMATTA_BUSY; } + +static shellmatta_retCode_t shell_cmd_cat(const shellmatta_handle_t handle, const char *arguments, + uint32_t length) +{ + FIL file; + char path_buff[256]; + const char *path; + UINT bytes_read; + FRESULT res; + + strncpy(path_buff, arguments, MIN(sizeof(path_buff), length)); + path_buff[MIN(length, sizeof(path_buff)-1)] = 0; + path = strtok(path_buff, " "); + path = strtok(NULL, " "); + + if (strlen(path) == 0) { + shellmatta_printf(handle, "Specify path!\r\n"); + return SHELLMATTA_OK; + } + + res = f_open(&file, path, FA_READ); + if (res == FR_OK) { + shellmatta_write(handle, "\r\n", 2U); + do { + res = f_read(&file, path_buff, sizeof(path_buff), &bytes_read); + if (bytes_read > 0) + shellmatta_write(handle, path_buff, bytes_read); + else + break; + } while (res == FR_OK); + + shellmatta_write(handle, "\r\n", 2U); + } + + if (res != FR_OK) { + shellmatta_printf(handle, "Error reading file\r\n"); + } + + f_close(&file); + + return SHELLMATTA_OK; +} + //typedef struct shellmatta_cmd //{ // char *cmd; /**< command name */ @@ -339,7 +382,7 @@ static shellmatta_retCode_t shell_cmd_reset(const shellmatta_handle_t handle, co // struct shellmatta_cmd *next; /**< pointer to next command or NULL */ //} shellmatta_cmd_t; -static shellmatta_cmd_t cmd[12] = { +static shellmatta_cmd_t cmd[13] = { { .cmd = "version", .cmdAlias = "ver", @@ -432,8 +475,16 @@ static shellmatta_cmd_t cmd[12] = { .cmd = "reset", .cmdAlias = NULL, .helpText = "Reset controller", - .usageText = "Resets the controller", + .usageText = "reset", .cmdFct = shell_cmd_reset, + .next = &cmd[12], + }, + { + .cmd = "cat", + .cmdAlias = NULL, + .helpText = "Print file contents", + .usageText = "cat ", + .cmdFct = shell_cmd_cat, .next = NULL, }