refractor cli error handling

add cat command
This commit is contained in:
hathach 2013-09-26 11:27:48 +07:00
parent f827750120
commit 4bdede53eb
3 changed files with 165 additions and 94 deletions

View File

@ -135,7 +135,7 @@
<SetRegEntry> <SetRegEntry>
<Number>0</Number> <Number>0</Number>
<Key>DLGUARM</Key> <Key>DLGUARM</Key>
<Name></Name> <Name>(106=-1,-1,-1,-1,0)(107=-1,-1,-1,-1,0)</Name>
</SetRegEntry> </SetRegEntry>
<SetRegEntry> <SetRegEntry>
<Number>0</Number> <Number>0</Number>
@ -158,7 +158,24 @@
<Name>-O975 -S0 -C0 -FO7 -FD10000000 -FC800 -FN2 -FF0LPC18xx43xx_512_BA -FS01A000000 -FL080000 -FF1LPC18xx43xx_512_BB -FS11B000000 -FL180000)</Name> <Name>-O975 -S0 -C0 -FO7 -FD10000000 -FC800 -FN2 -FF0LPC18xx43xx_512_BA -FS01A000000 -FL080000 -FF1LPC18xx43xx_512_BB -FS11B000000 -FL180000)</Name>
</SetRegEntry> </SetRegEntry>
</TargetDriverDllRegistry> </TargetDriverDllRegistry>
<Breakpoint/> <Breakpoint>
<Bp>
<Number>0</Number>
<Type>0</Type>
<LineNumber>269</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>0</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
<SizeOfObject>0</SizeOfObject>
<BreakByAccess>0</BreakByAccess>
<BreakIfRCount>0</BreakIfRCount>
<Filename>C:\Users\hathach\Dropbox\tinyusb\workspace\tinyusb\demos\host\src\cli.c</Filename>
<ExecCommand></ExecCommand>
<Expression></Expression>
</Bp>
</Breakpoint>
<WatchWindow1> <WatchWindow1>
<Ww> <Ww>
<count>0</count> <count>0</count>
@ -188,28 +205,8 @@
<Ww> <Ww>
<count>5</count> <count>5</count>
<WinNumber>1</WinNumber> <WinNumber>1</WinNumber>
<ItemText>ehci_data.device[0].qhd[0]</ItemText>
</Ww>
<Ww>
<count>6</count>
<WinNumber>1</WinNumber>
<ItemText>ehci_data.device[0].qhd[1]</ItemText>
</Ww>
<Ww>
<count>7</count>
<WinNumber>1</WinNumber>
<ItemText>msch_semaphore</ItemText> <ItemText>msch_semaphore</ItemText>
</Ww> </Ww>
<Ww>
<count>8</count>
<WinNumber>1</WinNumber>
<ItemText>ehci_data.device[0].qhd[0].p_qtd_list_head</ItemText>
</Ww>
<Ww>
<count>9</count>
<WinNumber>1</WinNumber>
<ItemText>ehci_data.device[0].qhd[1].p_qtd_list_head</ItemText>
</Ww>
</WatchWindow1> </WatchWindow1>
<MemoryWindow1> <MemoryWindow1>
<Mm> <Mm>
@ -390,7 +387,24 @@
<Name>-O975 -S0 -C0 -FO7 -FD10000000 -FC800 -FN2 -FF0LPC18xx43xx_512_BA -FS01A000000 -FL080000 -FF1LPC18xx43xx_512_BB -FS11B000000 -FL180000)</Name> <Name>-O975 -S0 -C0 -FO7 -FD10000000 -FC800 -FN2 -FF0LPC18xx43xx_512_BA -FS01A000000 -FL080000 -FF1LPC18xx43xx_512_BB -FS11B000000 -FL180000)</Name>
</SetRegEntry> </SetRegEntry>
</TargetDriverDllRegistry> </TargetDriverDllRegistry>
<Breakpoint/> <Breakpoint>
<Bp>
<Number>0</Number>
<Type>0</Type>
<LineNumber>269</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>0</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
<SizeOfObject>0</SizeOfObject>
<BreakByAccess>0</BreakByAccess>
<BreakIfRCount>0</BreakIfRCount>
<Filename>C:\Users\hathach\Dropbox\tinyusb\workspace\tinyusb\demos\host\src\cli.c</Filename>
<ExecCommand></ExecCommand>
<Expression></Expression>
</Bp>
</Breakpoint>
<WatchWindow1> <WatchWindow1>
<Ww> <Ww>
<count>0</count> <count>0</count>
@ -446,7 +460,7 @@
<Focus>0</Focus> <Focus>0</Focus>
<ColumnNumber>1</ColumnNumber> <ColumnNumber>1</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg> <tvExpOptDlg>0</tvExpOptDlg>
<TopLine>194</TopLine> <TopLine>195</TopLine>
<CurrentLine>200</CurrentLine> <CurrentLine>200</CurrentLine>
<bDave2>0</bDave2> <bDave2>0</bDave2>
<PathWithFileName>..\src\main.c</PathWithFileName> <PathWithFileName>..\src\main.c</PathWithFileName>
@ -462,7 +476,7 @@
<Focus>0</Focus> <Focus>0</Focus>
<ColumnNumber>0</ColumnNumber> <ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg> <tvExpOptDlg>0</tvExpOptDlg>
<TopLine>121</TopLine> <TopLine>94</TopLine>
<CurrentLine>145</CurrentLine> <CurrentLine>145</CurrentLine>
<bDave2>0</bDave2> <bDave2>0</bDave2>
<PathWithFileName>..\src\cdc_serial_app.c</PathWithFileName> <PathWithFileName>..\src\cdc_serial_app.c</PathWithFileName>
@ -478,7 +492,7 @@
<Focus>0</Focus> <Focus>0</Focus>
<ColumnNumber>0</ColumnNumber> <ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg> <tvExpOptDlg>0</tvExpOptDlg>
<TopLine>163</TopLine> <TopLine>136</TopLine>
<CurrentLine>180</CurrentLine> <CurrentLine>180</CurrentLine>
<bDave2>0</bDave2> <bDave2>0</bDave2>
<PathWithFileName>..\src\keyboard_app.c</PathWithFileName> <PathWithFileName>..\src\keyboard_app.c</PathWithFileName>
@ -494,7 +508,7 @@
<Focus>0</Focus> <Focus>0</Focus>
<ColumnNumber>44</ColumnNumber> <ColumnNumber>44</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg> <tvExpOptDlg>0</tvExpOptDlg>
<TopLine>122</TopLine> <TopLine>120</TopLine>
<CurrentLine>127</CurrentLine> <CurrentLine>127</CurrentLine>
<bDave2>0</bDave2> <bDave2>0</bDave2>
<PathWithFileName>..\src\mouse_app.c</PathWithFileName> <PathWithFileName>..\src\mouse_app.c</PathWithFileName>
@ -524,10 +538,10 @@
<FileType>1</FileType> <FileType>1</FileType>
<tvExp>0</tvExp> <tvExp>0</tvExp>
<Focus>0</Focus> <Focus>0</Focus>
<ColumnNumber>3</ColumnNumber> <ColumnNumber>1</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg> <tvExpOptDlg>0</tvExpOptDlg>
<TopLine>125</TopLine> <TopLine>111</TopLine>
<CurrentLine>135</CurrentLine> <CurrentLine>125</CurrentLine>
<bDave2>0</bDave2> <bDave2>0</bDave2>
<PathWithFileName>..\src\msc_app.c</PathWithFileName> <PathWithFileName>..\src\msc_app.c</PathWithFileName>
<FilenameWithoutPath>msc_app.c</FilenameWithoutPath> <FilenameWithoutPath>msc_app.c</FilenameWithoutPath>
@ -540,10 +554,10 @@
<FileType>1</FileType> <FileType>1</FileType>
<tvExp>0</tvExp> <tvExp>0</tvExp>
<Focus>0</Focus> <Focus>0</Focus>
<ColumnNumber>30</ColumnNumber> <ColumnNumber>24</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg> <tvExpOptDlg>0</tvExpOptDlg>
<TopLine>69</TopLine> <TopLine>1</TopLine>
<CurrentLine>75</CurrentLine> <CurrentLine>11</CurrentLine>
<bDave2>0</bDave2> <bDave2>0</bDave2>
<PathWithFileName>..\src\cli.c</PathWithFileName> <PathWithFileName>..\src\cli.c</PathWithFileName>
<FilenameWithoutPath>cli.c</FilenameWithoutPath> <FilenameWithoutPath>cli.c</FilenameWithoutPath>
@ -582,7 +596,7 @@
<Focus>0</Focus> <Focus>0</Focus>
<ColumnNumber>0</ColumnNumber> <ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg> <tvExpOptDlg>0</tvExpOptDlg>
<TopLine>138</TopLine> <TopLine>139</TopLine>
<CurrentLine>142</CurrentLine> <CurrentLine>142</CurrentLine>
<bDave2>0</bDave2> <bDave2>0</bDave2>
<PathWithFileName>..\..\bsp\boards\embedded_artists\board_ea4357.c</PathWithFileName> <PathWithFileName>..\..\bsp\boards\embedded_artists\board_ea4357.c</PathWithFileName>
@ -598,7 +612,7 @@
<Focus>0</Focus> <Focus>0</Focus>
<ColumnNumber>6</ColumnNumber> <ColumnNumber>6</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg> <tvExpOptDlg>0</tvExpOptDlg>
<TopLine>122</TopLine> <TopLine>95</TopLine>
<CurrentLine>135</CurrentLine> <CurrentLine>135</CurrentLine>
<bDave2>0</bDave2> <bDave2>0</bDave2>
<PathWithFileName>..\..\bsp\boards\printf_retarget.c</PathWithFileName> <PathWithFileName>..\..\bsp\boards\printf_retarget.c</PathWithFileName>
@ -734,7 +748,7 @@
<Focus>0</Focus> <Focus>0</Focus>
<ColumnNumber>0</ColumnNumber> <ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg> <tvExpOptDlg>0</tvExpOptDlg>
<TopLine>52</TopLine> <TopLine>25</TopLine>
<CurrentLine>76</CurrentLine> <CurrentLine>76</CurrentLine>
<bDave2>0</bDave2> <bDave2>0</bDave2>
<PathWithFileName>..\..\..\tinyusb\tusb.c</PathWithFileName> <PathWithFileName>..\..\..\tinyusb\tusb.c</PathWithFileName>
@ -798,7 +812,7 @@
<Focus>0</Focus> <Focus>0</Focus>
<ColumnNumber>0</ColumnNumber> <ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg> <tvExpOptDlg>0</tvExpOptDlg>
<TopLine>495</TopLine> <TopLine>483</TopLine>
<CurrentLine>501</CurrentLine> <CurrentLine>501</CurrentLine>
<bDave2>0</bDave2> <bDave2>0</bDave2>
<PathWithFileName>..\..\..\tinyusb\host\usbh.c</PathWithFileName> <PathWithFileName>..\..\..\tinyusb\host\usbh.c</PathWithFileName>
@ -814,7 +828,7 @@
<Focus>0</Focus> <Focus>0</Focus>
<ColumnNumber>0</ColumnNumber> <ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg> <tvExpOptDlg>0</tvExpOptDlg>
<TopLine>601</TopLine> <TopLine>602</TopLine>
<CurrentLine>605</CurrentLine> <CurrentLine>605</CurrentLine>
<bDave2>0</bDave2> <bDave2>0</bDave2>
<PathWithFileName>..\..\..\tinyusb\host\ehci\ehci.c</PathWithFileName> <PathWithFileName>..\..\..\tinyusb\host\ehci\ehci.c</PathWithFileName>
@ -998,7 +1012,7 @@
<Focus>0</Focus> <Focus>0</Focus>
<ColumnNumber>0</ColumnNumber> <ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg> <tvExpOptDlg>0</tvExpOptDlg>
<TopLine>547</TopLine> <TopLine>548</TopLine>
<CurrentLine>553</CurrentLine> <CurrentLine>553</CurrentLine>
<bDave2>0</bDave2> <bDave2>0</bDave2>
<PathWithFileName>..\..\bsp\lpc43xx\CMSIS_LPC43xx_DriverLib\src\lpc43xx_uart.c</PathWithFileName> <PathWithFileName>..\..\bsp\lpc43xx\CMSIS_LPC43xx_DriverLib\src\lpc43xx_uart.c</PathWithFileName>
@ -1100,10 +1114,10 @@
<FileType>2</FileType> <FileType>2</FileType>
<tvExp>0</tvExp> <tvExp>0</tvExp>
<Focus>0</Focus> <Focus>0</Focus>
<ColumnNumber>26</ColumnNumber> <ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg> <tvExpOptDlg>0</tvExpOptDlg>
<TopLine>144</TopLine> <TopLine>140</TopLine>
<CurrentLine>152</CurrentLine> <CurrentLine>158</CurrentLine>
<bDave2>0</bDave2> <bDave2>0</bDave2>
<PathWithFileName>..\..\bsp\lpc43xx\startup_keil\startup_LPC43xx.s</PathWithFileName> <PathWithFileName>..\..\bsp\lpc43xx\startup_keil\startup_LPC43xx.s</PathWithFileName>
<FilenameWithoutPath>startup_LPC43xx.s</FilenameWithoutPath> <FilenameWithoutPath>startup_LPC43xx.s</FilenameWithoutPath>
@ -1126,7 +1140,7 @@
<Focus>0</Focus> <Focus>0</Focus>
<ColumnNumber>46</ColumnNumber> <ColumnNumber>46</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg> <tvExpOptDlg>0</tvExpOptDlg>
<TopLine>16</TopLine> <TopLine>17</TopLine>
<CurrentLine>30</CurrentLine> <CurrentLine>30</CurrentLine>
<bDave2>0</bDave2> <bDave2>0</bDave2>
<PathWithFileName>..\..\..\vendor\fatfs\diskio.c</PathWithFileName> <PathWithFileName>..\..\..\vendor\fatfs\diskio.c</PathWithFileName>
@ -1150,6 +1164,22 @@
<RteFlg>0</RteFlg> <RteFlg>0</RteFlg>
<bShared>0</bShared> <bShared>0</bShared>
</File> </File>
<File>
<GroupNumber>6</GroupNumber>
<FileNumber>43</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
<ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>0</TopLine>
<CurrentLine>0</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>..\..\..\vendor\fatfs\ccsbcs.c</PathWithFileName>
<FilenameWithoutPath>ccsbcs.c</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
</Group> </Group>
</ProjectOpt> </ProjectOpt>

View File

@ -626,6 +626,11 @@
<FileType>1</FileType> <FileType>1</FileType>
<FilePath>..\..\..\vendor\fatfs\ff.c</FilePath> <FilePath>..\..\..\vendor\fatfs\ff.c</FilePath>
</File> </File>
<File>
<FileName>ccsbcs.c</FileName>
<FileType>1</FileType>
<FilePath>..\..\..\vendor\fatfs\ccsbcs.c</FilePath>
</File>
</Files> </Files>
</Group> </Group>
</Groups> </Groups>
@ -1290,6 +1295,11 @@
<FileType>1</FileType> <FileType>1</FileType>
<FilePath>..\..\..\vendor\fatfs\ff.c</FilePath> <FilePath>..\..\..\vendor\fatfs\ff.c</FilePath>
</File> </File>
<File>
<FileName>ccsbcs.c</FileName>
<FileType>1</FileType>
<FilePath>..\..\..\vendor\fatfs\ccsbcs.c</FilePath>
</File>
</Files> </Files>
</Group> </Group>
</Groups> </Groups>

View File

@ -43,19 +43,40 @@
#include "diskio.h" #include "diskio.h"
#include "boards/ansi_escape.h" #include "boards/ansi_escape.h"
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+
#define CLI_MAX_BUFFER 256
#define CLI_FILE_READ_BUFFER (4*1024)
enum {
ASCII_BACKSPACE = 8,
};
typedef enum {
CLI_ERROR_NONE = 0,
CLI_ERROR_INVALID_PARA,
CLI_ERROR_INVALID_PATH,
CLI_ERROR_FAILED
}cli_error_t;
//--------------------------------------------------------------------+
// CLI Database definition
//--------------------------------------------------------------------+
// command, function, description // command, function, description
#define CLI_COMMAND_TABLE(ENTRY) \ #define CLI_COMMAND_TABLE(ENTRY) \
ENTRY(unknow, cli_cmd_unknow , NULL) \ ENTRY(unknown , cli_cmd_unknown , NULL) \
ENTRY(help , cli_cmd_help , NULL) \ ENTRY(help , cli_cmd_help , NULL) \
ENTRY(ls , cli_cmd_list , "list items in current directory") \ ENTRY(ls , cli_cmd_list , "list items in current directory") \
ENTRY(cd , cli_cmd_changedir, "change current directory") \ ENTRY(cd , cli_cmd_changedir, "change current directory") \
ENTRY(cat , cli_cmd_cat , "display contents of a text file") \ ENTRY(cat , cli_cmd_cat , "display contents of a text file") \
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Expands the function to have the standard function signature // Expands the function to have the standard function signature
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
#define CLI_PROTOTYPE_EXPAND(command, function, description) \ #define CLI_PROTOTYPE_EXPAND(command, function, description) \
tusb_error_t function(char const *);\ cli_error_t function(char const *);
CLI_COMMAND_TABLE(CLI_PROTOTYPE_EXPAND); CLI_COMMAND_TABLE(CLI_PROTOTYPE_EXPAND);
@ -95,21 +116,25 @@ char const* const cli_description_tbl[] =
#define CMD_LOOKUP_EXPAND(command, function, description)\ #define CMD_LOOKUP_EXPAND(command, function, description)\
[CLI_CMDTYPE_##command] = function,\ [CLI_CMDTYPE_##command] = function,\
typedef tusb_error_t (* const cli_cmdfunc_t)(char const *); typedef cli_error_t (* const cli_cmdfunc_t)(char const *);
static cli_cmdfunc_t cli_command_tbl[] = static cli_cmdfunc_t cli_command_tbl[] =
{ {
CLI_COMMAND_TABLE(CMD_LOOKUP_EXPAND) CLI_COMMAND_TABLE(CMD_LOOKUP_EXPAND)
}; };
static char const * const cli_error_message[] =
{
[CLI_ERROR_NONE ] = 0,
[CLI_ERROR_INVALID_PARA ] = "Invalid parameter(s)",
[CLI_ERROR_INVALID_PATH ] = "No such file or directory",
[CLI_ERROR_FAILED ] = "failed to execute"
};
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// INTERNAL OBJECT & FUNCTION DECLARATION // INTERNAL OBJECT & FUNCTION DECLARATION
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
#define CLI_MAX_BUFFER 256
#define CLI_FILE_READ_BUFFER (4*1024)
enum {
ASCII_BACKSPACE = 8,
};
static char cli_buffer[CLI_MAX_BUFFER]; static char cli_buffer[CLI_MAX_BUFFER];
@ -146,13 +171,15 @@ void cli_poll(char ch)
} }
else if ( ch == '\r') else if ( ch == '\r')
{ // execute command { // execute command
//------------- Separate Command & Parameter -------------//
putchar('\n'); putchar('\n');
char* p_space = strchr(cli_buffer, ' '); char* p_space = strchr(cli_buffer, ' ');
uint32_t command_len = (p_space == NULL) ? strlen(cli_buffer) : (p_space - cli_buffer); uint32_t command_len = (p_space == NULL) ? strlen(cli_buffer) : (p_space - cli_buffer);
char* p_para = (p_space == NULL) ? NULL : (p_space+1); char* p_para = (p_space == NULL) ? (cli_buffer+command_len) : (p_space+1); // point to NULL-character or after space
//------------- Find entered command in lookup table & execute it -------------//
cli_cmdtype_t cmd_id; cli_cmdtype_t cmd_id;
for(cmd_id = CLI_CMDTYPE_COUNT - 1; cmd_id > 0; cmd_id--) for(cmd_id = CLI_CMDTYPE_COUNT - 1; cmd_id > CLI_CMDTYPE_unknown; cmd_id--)
{ {
if( 0 == strncmp(cli_buffer, cli_string_tbl[cmd_id], command_len) ) if( 0 == strncmp(cli_buffer, cli_string_tbl[cmd_id], command_len) )
{ {
@ -160,8 +187,11 @@ void cli_poll(char ch)
} }
} }
cli_command_tbl[cmd_id]( p_para ); cli_error_t error = cli_command_tbl[cmd_id]( p_para ); // command execution, (unknown command if cannot find)
if (CLI_ERROR_NONE != error) puts(cli_error_message[error]); // error message output if any
//------------- print out current path -------------//
f_getcwd(cli_buffer, CLI_MAX_BUFFER); f_getcwd(cli_buffer, CLI_MAX_BUFFER);
printf("\nMSC %c%s\n$ ", printf("\nMSC %c%s\n$ ",
'E'+cli_buffer[0]-'0', 'E'+cli_buffer[0]-'0',
@ -177,16 +207,16 @@ void cli_poll(char ch)
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// UNKNOWN Command // UNKNOWN Command
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
tusb_error_t cli_cmd_unknow(char const * para) cli_error_t cli_cmd_unknown(char const * para)
{ {
puts("unknown command, please type \"help\""); puts("unknown command, please type \"help\" for list of supported commands");
return TUSB_ERROR_NONE; return CLI_ERROR_NONE;
} }
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// HELP command // HELP command
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
tusb_error_t cli_cmd_help(char const * para) cli_error_t cli_cmd_help(char const * para)
{ {
puts("current supported commands are:"); puts("current supported commands are:");
for(cli_cmdtype_t cmd_id = CLI_CMDTYPE_help+1; cmd_id < CLI_CMDTYPE_COUNT; cmd_id++) for(cli_cmdtype_t cmd_id = CLI_CMDTYPE_help+1; cmd_id < CLI_CMDTYPE_COUNT; cmd_id++)
@ -194,19 +224,18 @@ tusb_error_t cli_cmd_help(char const * para)
printf("%s\t%s\n", cli_string_tbl[cmd_id], cli_description_tbl[cmd_id]); printf("%s\t%s\n", cli_string_tbl[cmd_id], cli_description_tbl[cmd_id]);
} }
return TUSB_ERROR_NONE; return CLI_ERROR_NONE;
} }
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// LS Command // LS Command
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
tusb_error_t cli_cmd_list(const char * p_para) cli_error_t cli_cmd_list(const char * p_para)
{ {
DIR target_dir; if ( strlen(p_para) == 0 ) // list current directory
if ( (p_para == NULL) || (strlen(p_para) == 0) ) // list current directory
{ {
ASSERT_INT( FR_OK, f_opendir(&target_dir, "."), TUSB_ERROR_FAILED) ; DIR target_dir;
if ( FR_OK != f_opendir(&target_dir, ".") ) return CLI_ERROR_FAILED;
TCHAR long_filename[_MAX_LFN]; TCHAR long_filename[_MAX_LFN];
FILINFO dir_entry = FILINFO dir_entry =
@ -218,42 +247,48 @@ tusb_error_t cli_cmd_list(const char * p_para)
{ {
if ( dir_entry.fname[0] != '.' ) // ignore . and .. entry if ( dir_entry.fname[0] != '.' ) // ignore . and .. entry
{ {
printf("%s%c\n", TCHAR const * const p_name = (dir_entry.lfname[0] != 0) ? dir_entry.lfname : dir_entry.fname;
(dir_entry.lfname[0] != 0) ? dir_entry.lfname : dir_entry.fname, if ( dir_entry.fattrib & AM_DIR ) // directory
dir_entry.fattrib & AM_DIR ? '/' : ' '); {
printf("/%s", p_name);
}else
{
printf("%-50s%d KB", p_name, dir_entry.fsize / 1000);
}
putchar('\n');
} }
} }
} }
else else
{ {
puts("ls only supports list current directory only, try to cd to that folder first"); puts("ls only supports list current directory only, try to cd to that folder first");
return CLI_ERROR_INVALID_PARA;
} }
return TUSB_ERROR_NONE; return CLI_ERROR_NONE;
} }
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// CD Command // CD Command
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
tusb_error_t cli_cmd_changedir(const char * p_para) cli_error_t cli_cmd_changedir(const char * p_para)
{ {
if ( (p_para == NULL) || (strlen(p_para) == 0) ) return TUSB_ERROR_INVALID_PARA; if ( strlen(p_para) == 0 ) return CLI_ERROR_INVALID_PARA;
if ( FR_OK != f_chdir(p_para) ) if ( FR_OK != f_chdir(p_para) )
{ {
printf("%s : No such file or directory\n", p_para); return CLI_ERROR_INVALID_PATH;
return TUSB_ERROR_INVALID_PARA;
} }
return TUSB_ERROR_NONE; return CLI_ERROR_NONE;
} }
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// CAT Command // CAT Command
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
tusb_error_t cli_cmd_cat(const char *p_para) cli_error_t cli_cmd_cat(const char *p_para)
{ {
if ( (p_para == NULL) || (strlen(p_para) == 0) ) return TUSB_ERROR_INVALID_PARA; if ( strlen(p_para) == 0 ) return CLI_ERROR_INVALID_PARA;
FIL file; FIL file;
@ -262,18 +297,18 @@ tusb_error_t cli_cmd_cat(const char *p_para)
case FR_OK: case FR_OK:
{ {
uint32_t bytes_read = 0; uint32_t bytes_read = 0;
if ( (FR_OK == f_read(&file, fileread_buffer, CLI_FILE_READ_BUFFER, &bytes_read)) && (bytes_read > 0) ) if ( (FR_OK == f_read(&file, fileread_buffer, CLI_FILE_READ_BUFFER, &bytes_read)) && (bytes_read > 0) )
{ {
if ( isprint( fileread_buffer[0] ) ) if ( file.fsize < 0x80000 ) // ~ 500KB
{ {
putchar('\n'); putchar('\n');
for(uint32_t i=0; i<bytes_read; i++) do {
{ for(uint32_t i=0; i<bytes_read; i++) putchar( fileread_buffer[i] );
putchar( fileread_buffer[i] ); }while( (FR_OK == f_read(&file, fileread_buffer, CLI_FILE_READ_BUFFER, &bytes_read)) && (bytes_read > 0) );
}
}else }else
{ { // not display file contents if first character is not printable (high chance of binary file)
printf("%s 's contents is not printable\n", p_para); printf("%s 's contents is too large\n", p_para);
} }
} }
f_close(&file); f_close(&file);
@ -281,17 +316,13 @@ tusb_error_t cli_cmd_cat(const char *p_para)
break; break;
case FR_INVALID_NAME: case FR_INVALID_NAME:
printf("%s : No such file or directory\n", p_para); return CLI_ERROR_INVALID_PATH;
return TUSB_ERROR_INVALID_PARA;
break;
default : default :
printf("failed to open %s\n", p_para); return CLI_ERROR_FAILED;
return TUSB_ERROR_FAILED;
break;
} }
return TUSB_ERROR_NONE; return CLI_ERROR_NONE;
} }
#endif #endif