在项目中cmd_node的形式如下
/* Node which has some commands and prompt string and configuration
function pointer . */
struct cmd_node
{
/* Node index. */
enum node_type node;
/* Prompt character at vty interface. */
char *prompt;
/* Prompt function if prompt is NULL */
char *(*prompt_func)(struct vty *);
/* Is this node's configuration goes to vtysh ? */
int vtysh;
/* Node's configuration write function */
int (*func) (struct vty *);
/* Vector of this node's command list. */
vector cmd_vector;
};
在这里总结用函数指针char *(*prompt_func)(struct vty *);来定义cmd_node。以下以interface_node为例进行讲解,可供参考
1. 在cmd_interface.c 的2355行定义
static struct cmd_node vlan_interface_node = {VLAN_INTERFACE_NODE, NULL, NULL, 1 };
2. 在command.h的node_type中加入VLAN_INTERFACE_NODE
3. 在cmd_interface.c中的2373行定义指针函数
static char *cmd_vlan_interface_node_prompt(struct vty *vty)
{
static char prompt[40];
sprintf(prompt, "%%s(Vlan-interface%d)# ", vty->para.para_integer);
return prompt;
}
4. 在cmd_interface.c 的7806行的void cmd_interface_init(void)中添加
vlan_interface_node.prompt_func = cmd_vlan_interface_node_prompt;
install_node(&vlan_interface_node, NULL);
5.在cmd_interface.c的2380行中添加触发命令
DEFUN (vlan_interface,
access_vlan_interface_cmd,
"interface vlan-interface <1-4094>",
INTERFACE_DESC_STR
"VLAN interface\n"
"VLAN interface\n")
{
char szifname[64];//added by wanghuanyu for 129
#if 0 //add by zhouguanhua 2013/5/31
sys_network_t info;
int iRet = IPC_STATUS_OK;
memset(&info, 0x0, sizeof(sys_network_t));
iRet = ipc_get_sys_networking(&info);
if(IPC_STATUS_OK != iRet)
{
vty_out(vty, " ERROR: get system networking failed!%s", VTY_NEWLINE);
return CMD_WARNING;
}
if(info.m_vlan != strtoul(argv[0], NULL, 0))
{
vty_out(vty, " ERROR: Interface-vlan should be same with management-vlan!%s", VTY_NEWLINE);
return CMD_WARNING;
}
vty->para.para_integer = strtoul(argv[0], NULL, 0);
vty->node = VLAN_INTERFACE_NODE;
return CMD_SUCCESS;
#endif //add by zhouguanhua 2013/5/31
sys_mvlan_t info;
int iRet = IPC_STATUS_OK;
memset(&info, 0x0, sizeof(sys_mvlan_t));
iRet = get_sys_mvlan_value(&info);
if(IPC_STATUS_OK != iRet)
{
vty_out(vty, " ERROR: get system mvlan failed!%s", VTY_NEWLINE);
return CMD_WARNING;
}
if(info.m_vlan != strtoul(argv[0], NULL, 0))
{
vty_out(vty, " ERROR: Interface-vlan should be same with management-vlan!%s", VTY_NEWLINE);
return CMD_WARNING;
}
/*begin modified by wanghuanyu */
vty->para.para_integer = strtoul(argv[0], NULL, 0);
sprintf(szifname,"%s%d",IF_L3VLAN_NAMEPREFIX,vty->para.para_integer);
IF_GetByIFName(szifname,&(vty->ifindex));
vty->node = VLAN_INTERFACE_NODE;
/*end modified by wanghuanyu */
return CMD_SUCCESS;
}
6. 在cmd_interface_init(void)的7813行安装触发命令
install_element(CONFIG_NODE, &access_vlan_interface_cmd);
至此安装节点完毕,以下是在该节点下添加命令
7. 在void cmd_interface_init(void)中添加命令(以ip_dynamic_cmd 命令为例)
install_element(VLAN_INTERFACE_NODE, &ip_dynamic_cmd);
至此可正常使用
(注:在source insight 的install_element(CONFIG_NODE, &access_vlan_interface_cmd);中,选中access_vlan_interface_cmd按下shift+F3可跳转到DEFUN(触发命令))