注意,外部命令下的子命令,既可以直接在外部命令的线程上下文中执行,也可以单独创建执行线程,取决于开发者的判断。
需要注意的是,外部命令的字命令处理函数,必须以下列格式来定义:
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;
}
子命令处理函数必须返回一个DWORD类型的值,用来表示子命令的执行情况,比如成功或者是失败。同时,子命令处理函数的参数,也必须是__CMD_PARA_OBJ* 类型。这是个内部定义的参数传递数据结构,如下:
typedef struct tag__CMD_PARA_OBJ
{
BYTE byParameterNum; //How many parameters followed.
WORD wReserved;
CHAR* Parameter[CMD_PARAMETER_COUNT];
}__CMD_PARA_OBJ;
byParameterNum指明了这个结构体中包含的参数个数,而Parameter则是一个字符串数组,包含了每个字符串参数的首地址。这与标准的C入口函数main(intargc,char* argv[])的参数是一致的。其中byParameterNum与argc对应,而Parameter则与argv数组对应。需要注意的是,数组中的第一个参数,就是子命令字符串本身。这与C的argv数组中,第一个是应用程序文件名字符串的情况一致。
子命令函数就可以通过分析__CMD_PARA_OBJ对象,来获取每个参数。
第二步:在外部命令数组中,增加入口函数信息。
外部命令数组在kernel/shell/extcmd.c文件中,在这个数组中增加一项,如下:
__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.";