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>
<Number>0</Number>
<Key>DLGUARM</Key>
<Name></Name>
<Name>(106=-1,-1,-1,-1,0)(107=-1,-1,-1,-1,0)</Name>
</SetRegEntry>
<SetRegEntry>
<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>
</SetRegEntry>
</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>
<Ww>
<count>0</count>
@ -188,28 +205,8 @@
<Ww>
<count>5</count>
<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>
</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>
<MemoryWindow1>
<Mm>
@ -390,7 +387,24 @@
<Name>-O975 -S0 -C0 -FO7 -FD10000000 -FC800 -FN2 -FF0LPC18xx43xx_512_BA -FS01A000000 -FL080000 -FF1LPC18xx43xx_512_BB -FS11B000000 -FL180000)</Name>
</SetRegEntry>
</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>
<Ww>
<count>0</count>
@ -446,7 +460,7 @@
<Focus>0</Focus>
<ColumnNumber>1</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>194</TopLine>
<TopLine>195</TopLine>
<CurrentLine>200</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>..\src\main.c</PathWithFileName>
@ -462,7 +476,7 @@
<Focus>0</Focus>
<ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>121</TopLine>
<TopLine>94</TopLine>
<CurrentLine>145</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>..\src\cdc_serial_app.c</PathWithFileName>
@ -478,7 +492,7 @@
<Focus>0</Focus>
<ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>163</TopLine>
<TopLine>136</TopLine>
<CurrentLine>180</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>..\src\keyboard_app.c</PathWithFileName>
@ -494,7 +508,7 @@
<Focus>0</Focus>
<ColumnNumber>44</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>122</TopLine>
<TopLine>120</TopLine>
<CurrentLine>127</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>..\src\mouse_app.c</PathWithFileName>
@ -524,10 +538,10 @@
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
<ColumnNumber>3</ColumnNumber>
<ColumnNumber>1</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>125</TopLine>
<CurrentLine>135</CurrentLine>
<TopLine>111</TopLine>
<CurrentLine>125</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>..\src\msc_app.c</PathWithFileName>
<FilenameWithoutPath>msc_app.c</FilenameWithoutPath>
@ -540,10 +554,10 @@
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
<ColumnNumber>30</ColumnNumber>
<ColumnNumber>24</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>69</TopLine>
<CurrentLine>75</CurrentLine>
<TopLine>1</TopLine>
<CurrentLine>11</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>..\src\cli.c</PathWithFileName>
<FilenameWithoutPath>cli.c</FilenameWithoutPath>
@ -582,7 +596,7 @@
<Focus>0</Focus>
<ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>138</TopLine>
<TopLine>139</TopLine>
<CurrentLine>142</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>..\..\bsp\boards\embedded_artists\board_ea4357.c</PathWithFileName>
@ -598,7 +612,7 @@
<Focus>0</Focus>
<ColumnNumber>6</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>122</TopLine>
<TopLine>95</TopLine>
<CurrentLine>135</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>..\..\bsp\boards\printf_retarget.c</PathWithFileName>
@ -734,7 +748,7 @@
<Focus>0</Focus>
<ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>52</TopLine>
<TopLine>25</TopLine>
<CurrentLine>76</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>..\..\..\tinyusb\tusb.c</PathWithFileName>
@ -798,7 +812,7 @@
<Focus>0</Focus>
<ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>495</TopLine>
<TopLine>483</TopLine>
<CurrentLine>501</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>..\..\..\tinyusb\host\usbh.c</PathWithFileName>
@ -814,7 +828,7 @@
<Focus>0</Focus>
<ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>601</TopLine>
<TopLine>602</TopLine>
<CurrentLine>605</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>..\..\..\tinyusb\host\ehci\ehci.c</PathWithFileName>
@ -998,7 +1012,7 @@
<Focus>0</Focus>
<ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>547</TopLine>
<TopLine>548</TopLine>
<CurrentLine>553</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>..\..\bsp\lpc43xx\CMSIS_LPC43xx_DriverLib\src\lpc43xx_uart.c</PathWithFileName>
@ -1100,10 +1114,10 @@
<FileType>2</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
<ColumnNumber>26</ColumnNumber>
<ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>144</TopLine>
<CurrentLine>152</CurrentLine>
<TopLine>140</TopLine>
<CurrentLine>158</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>..\..\bsp\lpc43xx\startup_keil\startup_LPC43xx.s</PathWithFileName>
<FilenameWithoutPath>startup_LPC43xx.s</FilenameWithoutPath>
@ -1126,7 +1140,7 @@
<Focus>0</Focus>
<ColumnNumber>46</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>16</TopLine>
<TopLine>17</TopLine>
<CurrentLine>30</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>..\..\..\vendor\fatfs\diskio.c</PathWithFileName>
@ -1150,6 +1164,22 @@
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</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>
</ProjectOpt>

View File

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

View File

@ -43,19 +43,40 @@
#include "diskio.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
#define CLI_COMMAND_TABLE(ENTRY) \
ENTRY(unknow, cli_cmd_unknow , NULL) \
ENTRY(help , cli_cmd_help , NULL) \
ENTRY(ls , cli_cmd_list , "list items in current directory") \
ENTRY(cd , cli_cmd_changedir, "change current directory") \
ENTRY(cat , cli_cmd_cat , "display contents of a text file") \
ENTRY(unknown , cli_cmd_unknown , NULL) \
ENTRY(help , cli_cmd_help , NULL) \
ENTRY(ls , cli_cmd_list , "list items in current directory") \
ENTRY(cd , cli_cmd_changedir, "change current directory") \
ENTRY(cat , cli_cmd_cat , "display contents of a text file") \
//--------------------------------------------------------------------+
// Expands the function to have the standard function signature
//--------------------------------------------------------------------+
#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);
@ -95,21 +116,25 @@ char const* const cli_description_tbl[] =
#define CMD_LOOKUP_EXPAND(command, function, description)\
[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[] =
{
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
//--------------------------------------------------------------------+
#define CLI_MAX_BUFFER 256
#define CLI_FILE_READ_BUFFER (4*1024)
enum {
ASCII_BACKSPACE = 8,
};
static char cli_buffer[CLI_MAX_BUFFER];
@ -146,13 +171,15 @@ void cli_poll(char ch)
}
else if ( ch == '\r')
{ // execute command
//------------- Separate Command & Parameter -------------//
putchar('\n');
char* p_space = strchr(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;
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) )
{
@ -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);
printf("\nMSC %c%s\n$ ",
'E'+cli_buffer[0]-'0',
@ -177,16 +207,16 @@ void cli_poll(char ch)
//--------------------------------------------------------------------+
// 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\"");
return TUSB_ERROR_NONE;
puts("unknown command, please type \"help\" for list of supported commands");
return CLI_ERROR_NONE;
}
//--------------------------------------------------------------------+
// 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:");
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]);
}
return TUSB_ERROR_NONE;
return CLI_ERROR_NONE;
}
//--------------------------------------------------------------------+
// 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 ( (p_para == NULL) || (strlen(p_para) == 0) ) // list current directory
if ( 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];
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
{
printf("%s%c\n",
(dir_entry.lfname[0] != 0) ? dir_entry.lfname : dir_entry.fname,
dir_entry.fattrib & AM_DIR ? '/' : ' ');
TCHAR const * const p_name = (dir_entry.lfname[0] != 0) ? dir_entry.lfname : dir_entry.fname;
if ( dir_entry.fattrib & AM_DIR ) // directory
{
printf("/%s", p_name);
}else
{
printf("%-50s%d KB", p_name, dir_entry.fsize / 1000);
}
putchar('\n');
}
}
}
else
{
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
//--------------------------------------------------------------------+
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) )
{
printf("%s : No such file or directory\n", p_para);
return TUSB_ERROR_INVALID_PARA;
return CLI_ERROR_INVALID_PATH;
}
return TUSB_ERROR_NONE;
return CLI_ERROR_NONE;
}
//--------------------------------------------------------------------+
// 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;
@ -262,18 +297,18 @@ tusb_error_t cli_cmd_cat(const char *p_para)
case FR_OK:
{
uint32_t 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');
for(uint32_t i=0; i<bytes_read; i++)
{
putchar( fileread_buffer[i] );
}
do {
for(uint32_t i=0; i<bytes_read; i++) putchar( fileread_buffer[i] );
}while( (FR_OK == f_read(&file, fileread_buffer, CLI_FILE_READ_BUFFER, &bytes_read)) && (bytes_read > 0) );
}else
{
printf("%s 's contents is not printable\n", p_para);
{ // not display file contents if first character is not printable (high chance of binary file)
printf("%s 's contents is too large\n", p_para);
}
}
f_close(&file);
@ -281,17 +316,13 @@ tusb_error_t cli_cmd_cat(const char *p_para)
break;
case FR_INVALID_NAME:
printf("%s : No such file or directory\n", p_para);
return TUSB_ERROR_INVALID_PARA;
break;
return CLI_ERROR_INVALID_PATH;
default :
printf("failed to open %s\n", p_para);
return TUSB_ERROR_FAILED;
break;
return CLI_ERROR_FAILED;
}
return TUSB_ERROR_NONE;
return CLI_ERROR_NONE;
}
#endif