VTYSH原始碼解析二

2021-09-24 19:57:23 字數 1813 閱讀 7802

上一節說了vtysh的main和命令的主要結構,但是怎樣加節點呢?細看下面。定義命令的巨集

/* defun for vty command interafce. little bit hacky ;-). 命令定義*/

#define defun(funcname, cmdname, cmdstr, helpstr) \

static int funcname (struct cmd_element *, struct vty *, int, char **); \

static struct cmd_element cmdname = \

; \static int funcname \

(struct cmd_element *self, struct vty *vty, int argc, char **ar**)

/* defun + defsh 這個巨集現在不需要理解,在多守護程序模式下才會用到,我們這裡都沒有守護程序*/

#define defunsh(daemon, funcname, cmdname, cmdstr, helpstr) \

static int funcname (struct cmd_element *, struct vty *, int, char **); \

static struct cmd_element cmdname = \

; \static int funcname \

(struct cmd_element *self, struct vty *vty, int argc, char **ar**)

/* alias macro which define existing command's alias. 命令別名*/

#define alias(funcname, cmdname, cmdstr, helpstr) \

static struct cmd_element cmdname = \

;void cmd_install_element (enum node_type ntype, struct cmd_element *cmd);//插入cmd

void cmd_install_node (struct cmd_node *node, int (*func) (struct vty *));//插入node

結合上節的命令結構,首先要建立乙個根vector(cmd_init()中可以看到),然後呼叫cmd_install_node插入node,呼叫cmd_install_element插入cmd。

原始碼的cmd目錄下,不需要的檔案可以刪掉,可以自定義新增我們自己的命令。

比如說在cmd目錄下建立乙個vtysh_debug.c修改command.**件,enum node_type部分

/* there are some command levels which called from command node. */

enum node_type

;

#include "command.h"

stduct cmd_node debug_node = ;

defun (config_debug,

config_debug_cmd,

"debug",

"進入debug模式")

void

vtysh_debug_init()

然後make clean;make,./vtysh測試執行程式,新增命令或者節點就是這麼簡單。

EventBus原始碼解析(二)

那麼我們就馬上進入主題吧,上篇文章分析到boolean sticky這個引數和registersticky object subscriber 這個方法,這個方法與之前我們註冊的方法是不是很像啊?public void register object subscriber public void r...

EventBus原始碼解析二

上次分析了 註冊 這次分析下 事件傳送 eventbus.getdefault post new anyeventtype setresult aaa public void post object event try finally eventqueue.add event 新增到事件佇列中,然後...

Junit 原始碼解析(二)

這裡對blockjunit4classrunner中幾個跟寫單元測試最相關的幾個方法進行分析 1 構造方法 public blockjunit4classrunner class klass throws initializationerror作用 是取得類的聲名描述,即class klass 2 ...