源代码来自于TI开发板
在ARM上实现shell命令解析
第一步:构建命令实现函数和命令表
1,定义结构体 和命令表
typedef int (*pfnCmdLine)(int argc, char *argv[]);
//*****************************************************************************
//
//! Structure for an entry in the command list table.
//
//*****************************************************************************
typedef struct
{
//
//! A pointer to a string containing the name of the command.
//
const char *pcCmd;
//
//! A function pointer to the implementation of the command.
//
pfnCmdLine pfnCmd;
//
//! A pointer to a string of brief help text for the command.
//
const char *pcHelp;
}
tCmdLineEntry;
//*****************************************************************************
//
//! This is the command table that must be provided by the application.
//
//*****************************************************************************
extern tCmdLineEntry g_sCmdTable[];
2,编写命令执行函数 实现命令表
int
Cmd_help(int argc, char *argv[])
{
tCmdLineEntry *pEntry;
//
// Print some header text.
//
UARTprintf("\nAvailable commands\n");
UARTprintf("------------------\n");
//
// Point at the beginning of the command table.
//
pEntry = &g_sCmdTable[0];
//
// Enter a loop to read each entry from the command table. The
// end of the table has been reached when the command name is NULL.
//
while(pEntry->pcCmd)
{
//
// Print the command name and the brief description.
//
UARTprintf("%s%s\n", pEntry->pcCmd, pEntry->pcHelp);
//
// Advance to the next entry in the table.
//
pEntry++;
}
//
// Return success.
//
return(0);
}
int
Cmd_ls(int argc, char *argv[])
{
unsigned long ulTotalSize;
unsigned long ulFileCount;
unsigned long ulDirCount;
FRESULT fresult;
FATFS *pFatFs;
//
// Open the current directory for access.
//
fresult = f_opendir(&g_sDirObject, g_cCwdBuf);
//
// Check for error and retur