1 /******************************************************************************
2 ###############################################################################
3 # Copyright (c) [2017-2020] [ICT/CAS] #
4 # Licensed under the ORAN Software License v1.0 (License) #
5 ###############################################################################
6 ******************************************************************************/
11 #include "vos_types.h"
16 #endif /* __cplusplus */
19 #define VTY_MAXHIST 20
22 #define VTY_CMD_PROMPT_MAX 64
25 #define VTY_NODE_HIS_MAX 8
28 #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\r\n")
31 #define SHOW_STR "Show running system information\n"
34 #define CONFIG_STR "Config system's setting\n"
37 #define NO_STR "Negate a command or set its defaults\n"
40 #define DEBUG_STR "Debugging functions\n"
42 /** 命令回调函数返回值 命令执行 OK */
43 #define CMD_RET_OK (0)
45 /** 命令回调函数返回值 命令执行 ERROR */
46 #define CMD_RET_ERROR (1)
53 VOS_CLI_VIEW_NODE, ///< View node. Default mode of vty interface.
54 VOS_CLI_CONFIG_NODE, ///< Config node. Default mode of config file.
55 VOS_CLI_DEBUG_HIDDEN_NODE, ///< Debug hidden node.
56 VOS_CLI_CONFIRM_ACTION_NODE, ///< Confirm action.
57 VOS_CLI_RESV_MAX_NODE ///< Vos max reserved node ID
63 int node_in_use; ///< 使用标记
64 enum node_type node; ///< 节点ID
65 void *context_data; ///< 记录命令行上下文数据,如进入端口节点可以记录端口号
66 INT context_data_need_free; ///< 标记context_data 是否需要free,在exit 命令中会执行VOS_free
67 CHAR vty_cmd_prompt[ VTY_CMD_PROMPT_MAX ]; ///< 自定义提示符
71 /** VTY 结构体,只需关心fd之前的成员. */
74 char vty_cmd_prompt[VTY_CMD_PROMPT_MAX]; ///< 自定义提示符
76 VOID *context_data; ///< 记录命令行上下文数据,如进入端口节点可以记录端口号
78 INT context_data_need_free; ///< 标记context_data 是否需要free,在exit 命令中会执行VOS_free
80 int (*action_func) (struct vty *vty); ///< confirm action 的回调函数
82 int (*no_action_func) (struct vty *vty); ///< refuse action 的回调函数
91 enum node_type prev_node;
94 enum { VTY_TERM, }type;
118 char *hist[VTY_MAXHIST];
126 struct exit_use_node node_his_save[VTY_NODE_HIS_MAX];
129 unsigned char escape;
132 enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_START, VTY_CONTINUE, VTY_IOREDIRECT }status;
134 /* telnet IAC handling */
137 /* telnet IAC SB handling */
138 unsigned char iac_sb_in_progress;
139 /* telnet IAC SB buffer */
140 struct buffer *sb_buffer;
144 /* 窗口高度,lines为0时有效 */
150 /* 已进入config节点,用于限制多用户进入config */
154 struct cl_lib_thread *t_read;
156 struct cl_lib_thread *t_write;
159 unsigned long v_timeout;
162 struct cl_lib_thread *t_timeout;
165 unsigned short cl_vty_exit;
167 /* vty 接入类型console,telnet, telnet IPv6 */
174 struct cl_lib_thread_master *sub_thread_master;
177 VOS_HANDLE sub_task_id;
180 unsigned short debug_monitor;
182 /************************************
183 frozen vty in and(or) out
184 used for if a command can't return immediately
185 then call frozen to freeze vty's in and(or) out
186 call unfrozen when finished.
189 ***********************************/
190 unsigned short frozen;
192 //VOS_SYSLOG_MONITOR_CONF monitor_conf;
193 int veryBigOutLineCount;
194 int veryBigOutNoFirst;
196 /* vty 引用计数,当引用计数为0的时候调用vty_close释放内存 */
201 /** cl_vector 动态数组指针 */
204 unsigned int max; ///< 当前最大索引加1
205 unsigned int alloced; ///< 数组大小 */
206 void **index; ///< 指针数组
208 typedef struct _cl_vector *cl_vector;
214 char *string; ///< 命令字符串,多个用空格分隔的单词组成的句子
215 int (*func) (struct cmd_element *, struct vty *, int, char **); ///< 命令回调函数
216 char *doc; ///< 命令说明,对命令字符串中的每个单词进行说明
217 int msg_flag; ///< 命令类型标识,当前只有 DEFUN_FLAG 类型
218 cl_vector strvec; ///< 将 string和doc 拆分后放入该数组中 ,存放struct desc
219 int cmdsize; ///< string 中单词个数
220 cl_vector subconfig; ///< 将 string中可以重复的单词的重复位置和重复次数等
227 enum node_type node_id; ///< 节点ID
228 char prompt[VTY_CMD_PROMPT_MAX]; ///< 节点提示符
229 cl_vector cmd_vector; ///< 节点命令数组
237 * @param[in] funcname 回调函数的函数名
238 * @param[in] cmdname 命令变量的变量名
239 * @param[in] cmdstr 命令字符串
240 * @param[in] helpstr 命令字符串中各个单词的注解
243 #define DEFUN(funcname, cmdname, cmdstr, helpstr) \
244 int funcname (struct cmd_element *, struct vty *, int, char **);\
245 struct cmd_element cmdname = {cmdstr, funcname, helpstr , DEFUN_FLAG};\
246 int funcname (struct cmd_element *self, struct vty *vty, int argc, char **argv)
251 * 注意 LONG 输出时,要用%ld,或%lx
253 * @param[in] format 格式字符串
254 * @param[in] ... 要输出的变量
257 INT vty_out ( struct vty * vty, const CHAR * format, ... );
261 * @param[in] prompt 节点提示符,用于命令行提示符的显示
262 * @return 成功返回 一个新节点的指针,失败返回NULL
264 struct cmd_node *VOS_cli_create_node(char prompt[VTY_CMD_PROMPT_MAX]);
269 * @param[in] node_id 节点ID
271 * @return 成功返回 VOS_OK,失败返回其他
273 VOID VOS_cli_install_element( enum node_type node_id, struct cmd_element * cmd );
279 * @param[in] newnode 要进入的节点ID
280 * @return 成功返回 VOS_OK,失败返回其他
282 LONG vty_enter_node(struct vty *vty,enum node_type newnode);
286 * 在进入节点函数中调用,用于自定义要进入节点的命令行提示符,
287 * 调用vty_enter_node后调用
289 * @param[in] cpPrompt 提示符
291 VOID vty_set_prompt( struct vty * vty, CHAR cpPrompt[VTY_CMD_PROMPT_MAX] );
297 * Completion match types. 匹配度,数值越大匹配度越高,命令行模块内部使用无需关心
306 one_element_two_partly_match,
308 almost_exact_match, /* exactmatch except last word */
330 typedef struct _cmd_notify_register_tag
332 CHAR *match_str ; ///< 匹配的格式,如<H.H.H>
333 enum match_type (*pnotify_call) (CHAR *str); ///< 格式解析的回调函数,解析成功返回exact_match,解析失败返回no_match
334 CHAR IsCapital ; ///< 是否包含大写字母 0 - 全小写; 1 - 含有大写字母
335 }CMD_NOTIFY_REFISTER_S;
339 * @param[in] pNotifyRegister 自定义解析函数信息
340 * @return 成功返回 register_match,失败返回no_match
342 enum match_type cmd_rugular_register(CMD_NOTIFY_REFISTER_S * pNotifyRegister);
346 * @param[in ] vty vty
347 * @param[in ] newTime 新的超时时间,大于等于0时有效,0表示不会超时
348 * @param[out] oldTime 如果不为NULL 返回配置前的超时时间
349 * @return 成功返回 VOS_OK,失败则返回其他
351 LONG vty_timeout_config ( struct vty * vty, LONG newTime, LONG *oldTime);
355 * 要使用vty_very_big_out 函数必须先调用此初始化函数
356 * @param[in ] vty vty
358 void vty_very_big_out_init ( struct vty *vty );
363 * @param[in] format 格式字符串
364 * @param[in] ... 要输出的变量
366 int vty_very_big_out (struct vty *vty, const char *format, ...);
371 #endif /* __cplusplus */
372 #endif /* __VOS_CLI_H__ */