注意,外部命令下的子命令,既可以直接在外部命令的線程上下文中執(zhí)行,也可以單獨(dú)創(chuàng)建執(zhí)行線程,取決于開發(fā)者的判斷。
需要注意的是,外部命令的字命令處理函數(shù),必須以下列格式來定義:
static DWORD ping(__CMD_PARA_OBJ* lpCmdObj)
{
__PING_PARAM PingParam;
ip_addr_t ipAddr;
int count = 3; //Ping counter.
int size = 64; //Ping packet size.
BYTE index = 1;
DWORD dwRetVal = SHELL_CMD_PARSER_FAILED;
__CMD_PARA_OBJ* pCurCmdObj = lpCmdObj;
if(pCurCmdObj->byParameterNum<= 1)
{
return dwRetVal;
}
while(index byParameterNum)
{
if(strcmp(pCurCmdObj->Parameter[index],"/c")== 0)
{
index++;
if(index>= lpCmdObj->byParameterNum)
{
break;
}
count = atoi(pCurCmdObj->Parameter[index]);
}
elseif(strcmp(pCurCmdObj->Parameter[index],"/l") == 0)
{
index++;
if(index>= lpCmdObj->byParameterNum)
{
break;
}
size = atoi(pCurCmdObj->Parameter[index]);
}
else
{
ipAddr.addr= inet_addr(pCurCmdObj->Parameter[index]);
}
index ++;
}
if(ipAddr.addr != 0)
{
dwRetVal = SHELL_CMD_PARSER_SUCCESS;
}
PingParam.count = count;
PingParam.targetAddr =ipAddr;
PingParam.size = size;
//Call ping entry routine.
ping_Entry((void*)&PingParam);
return dwRetVal;
}
子命令處理函數(shù)必須返回一個DWORD類型的值,用來表示子命令的執(zhí)行情況,比如成功或者是失敗。同時,子命令處理函數(shù)的參數(shù),也必須是__CMD_PARA_OBJ* 類型。這是個內(nèi)部定義的參數(shù)傳遞數(shù)據(jù)結(jié)構(gòu),如下:
typedef struct tag__CMD_PARA_OBJ
{
BYTE byParameterNum; //How many parameters followed.
WORD wReserved;
CHAR* Parameter[CMD_PARAMETER_COUNT];
}__CMD_PARA_OBJ;
byParameterNum指明了這個結(jié)構(gòu)體中包含的參數(shù)個數(shù),而Parameter則是一個字符串?dāng)?shù)組,包含了每個字符串參數(shù)的首地址。這與標(biāo)準(zhǔn)的C入口函數(shù)main(intargc,char* argv[])的參數(shù)是一致的。其中byParameterNum與argc對應(yīng),而Parameter則與argv數(shù)組對應(yīng)。需要注意的是,數(shù)組中的第一個參數(shù),就是子命令字符串本身。這與C的argv數(shù)組中,第一個是應(yīng)用程序文件名字符串的情況一致。
子命令函數(shù)就可以通過分析__CMD_PARA_OBJ對象,來獲取每個參數(shù)。
第二步:在外部命令數(shù)組中,增加入口函數(shù)信息。
外部命令數(shù)組在kernel/shell/extcmd.c文件中,在這個數(shù)組中增加一項(xiàng),如下:
__EXTERNAL_COMMAND ExtCmdArray[] = {
{"fs",NULL,FALSE,fsEntry},
{"fdisk",NULL,FALSE,fdiskEntry},
{"hedit",NULL,FALSE,heditEntry},
{"fibonacci",NULL,FALSE,Fibonacci},
{"hypertrm",NULL,FALSE,Hypertrm},
{"hyptrm2",NULL,FALSE,Hyptrm2},
#if defined(__CFG_NET_IPv4) || defined(__CFG_NET_IPv6)
{"network",NULL,FALSE,networkEntry},
#endif
//Add your externalcommand/application entry here.
//{"yourcmd",NULL,FALSE,cmdentry},
//The last entry of thisarray must be the following;
LPSTR strSysName = " sysname : Change the systemhost name.";
LPSTR strHelpHelp = " help : Print out thisscreen.";
LPSTR strSupport = " support : Print out technicalsupport information.";
LPSTR strTime = " time : Show system date and time.";
LPSTR strRunTime = " runtime : Display the totalrun time since last reboot.";
LPSTR strIoCtrlApp =" ioctrl : Start IO control application.";
LPSTR strSysDiagApp = " sysdiag : System or hardwarediag application.";
LPSTR strFsApp = " fs : File system operating application.";
LPSTR strFdiskApp = " fdisk : Hard disk operating application.";
LPSTR strNetApp = " network : Network diagnostic application.";
LPSTR strLoadappApp = " loadapp : Load applicationmodule and execute it.";
LPSTR strGUIApp = " gui : Load GUI module and enter GUI mode.";
#ifdef __CFG_APP_JVM
LPSTR strJvmApp = " jvm : Start Java VM to run Java Application.";
#endif //__CFG_APP_JVM
LPSTR strReboot = " reboot : Reboot the system.";
LPSTR strCls = " cls : Clear the whole screen.";