From 16e18388624bbb83f582c016cf4768875ed44b3d Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 19 Nov 2022 15:27:07 +0700 Subject: [PATCH] implement cat command --- examples/host/msc_file_explorer/src/msc_app.c | 75 ++++++++++++++++--- lib/fatfs/source/ffconf.h | 2 +- 2 files changed, 67 insertions(+), 10 deletions(-) diff --git a/examples/host/msc_file_explorer/src/msc_app.c b/examples/host/msc_file_explorer/src/msc_app.c index 76308cf5b..83074d89a 100644 --- a/examples/host/msc_file_explorer/src/msc_app.c +++ b/examples/host/msc_file_explorer/src/msc_app.c @@ -23,6 +23,7 @@ * */ +#include #include "tusb.h" #include "ff.h" @@ -32,6 +33,7 @@ #define EMBEDDED_CLI_IMPL #include "embedded_cli.h" + //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM DECLARATION //--------------------------------------------------------------------+ @@ -58,6 +60,7 @@ static scsi_inquiry_resp_t inquiry_resp; void cli_cmd_ls(EmbeddedCli *cli, char *args, void *context); void cli_cmd_cd(EmbeddedCli *cli, char *args, void *context); +void cli_cmd_cat(EmbeddedCli *cli, char *args, void *context); void cli_write_char(EmbeddedCli *cli, char c) { @@ -91,6 +94,14 @@ bool msc_app_init(void) _cli->writeChar = cli_write_char; + embeddedCliAddBinding(_cli, (CliCommandBinding) { + "cat", + "Usage: cat [FILE]...\r\n\tConcatenate FILE(s) to standard output..", + true, + NULL, + cli_cmd_cat + }); + embeddedCliAddBinding(_cli, (CliCommandBinding) { "cd", "Usage: cd [DIR]...\r\n\tChange the current directory to DIR.", @@ -115,13 +126,16 @@ void msc_app_task(void) { if (!_cli) return; - int ch; - while( (ch = getchar()) > 0 ) + int ch = getchar(); + if ( ch > 0 ) { - embeddedCliReceiveChar(_cli, (char) ch); + while( ch > 0 ) + { + embeddedCliReceiveChar(_cli, (char) ch); + ch = getchar(); + } + embeddedCliProcess(_cli); } - - embeddedCliProcess(_cli); } //--------------------------------------------------------------------+ @@ -308,8 +322,7 @@ DRESULT disk_ioctl ( void cli_cmd_ls(EmbeddedCli *cli, char *args, void *context) { - (void) cli; - (void) context; + (void) cli; (void) context; uint16_t argc = embeddedCliGetTokenCount(args); @@ -352,8 +365,7 @@ void cli_cmd_ls(EmbeddedCli *cli, char *args, void *context) void cli_cmd_cd(EmbeddedCli *cli, char *args, void *context) { - (void) cli; - (void) context; + (void) cli; (void) context; uint16_t argc = embeddedCliGetTokenCount(args); @@ -373,3 +385,48 @@ void cli_cmd_cd(EmbeddedCli *cli, char *args, void *context) return; } } + +void cli_cmd_cat(EmbeddedCli *cli, char *args, void *context) +{ + (void) cli; (void) context; + + uint16_t argc = embeddedCliGetTokenCount(args); + + // need at least 1 argument + if ( argc == 0 ) + { + printf("invalid arguments\r\n"); + return; + } + + for(uint16_t i=0; i 0) ) + { + for(size_t c = 0; c < count; c++) + { + const char ch = buf[c]; + if (isprint(ch) || iscntrl(ch)) + { + putchar(ch); + }else + { + putchar('.'); + } + } + } + } + + f_close(&fi); + } +} diff --git a/lib/fatfs/source/ffconf.h b/lib/fatfs/source/ffconf.h index c99498cda..9298ef897 100644 --- a/lib/fatfs/source/ffconf.h +++ b/lib/fatfs/source/ffconf.h @@ -237,7 +237,7 @@ / Note that enabling exFAT discards ANSI C (C89) compatibility. */ -#define FF_FS_NORTC 0 +#define FF_FS_NORTC 1 #define FF_NORTC_MON 1 #define FF_NORTC_MDAY 1 #define FF_NORTC_YEAR 2022