Przeglądaj źródła

implement cat command

hathach 3 lat temu
rodzic
commit
16e1838862

+ 66 - 9
examples/host/msc_file_explorer/src/msc_app.c

@@ -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);
+  }
+}

+ 1 - 1
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