implement cat command

This commit is contained in:
hathach 2022-11-19 15:27:07 +07:00
parent 083ed364ba
commit 16e1838862
No known key found for this signature in database
GPG Key ID: F5D50C6D51D17CBA
2 changed files with 67 additions and 10 deletions

View File

@ -23,6 +23,7 @@
*
*/
#include <ctype.h>
#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<argc; i++)
{
FIL fi;
const char* fpath = embeddedCliGetToken(args, i+1); // token count from 1
if ( FR_OK != f_open(&fi, fpath, FA_READ) )
{
printf("%s: No such file or directory\r\n", fpath);
}else
{
uint8_t buf[64];
size_t count = 0;
while ( (FR_OK == f_read(&fi, buf, sizeof(buf), &count)) && (count > 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);
}
}

View File

@ -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