1 /******************************************************************************
3 * Copyright (c) 2020 ICT/CAS.
5 * Licensed under the O-RAN Software License, Version 1.0 (the "Software License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * https://www.o-ran.org/software
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 *******************************************************************************/
23 #include "vos_types.h"
28 #endif /* __cplusplus */
31 #define VTY_MAXHIST 20
34 #define VTY_CMD_PROMPT_MAX 64
37 #define VTY_NODE_HIS_MAX 8
40 #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\r\n")
43 #define SHOW_STR "Show running system information\n"
46 #define CONFIG_STR "Config system's setting\n"
49 #define NO_STR "Negate a command or set its defaults\n"
52 #define DEBUG_STR "Debugging functions\n"
54 /** 命令回调函数返回值 命令执行 OK */
55 #define CMD_RET_OK (0)
57 /** 命令回调函数返回值 命令执行 ERROR */
58 #define CMD_RET_ERROR (1)
65 VOS_CLI_VIEW_NODE, ///< View node. Default mode of vty interface.
66 VOS_CLI_CONFIG_NODE, ///< Config node. Default mode of config file.
67 VOS_CLI_DEBUG_HIDDEN_NODE, ///< Debug hidden node.
68 VOS_CLI_CONFIRM_ACTION_NODE, ///< Confirm action.
69 VOS_CLI_RESV_MAX_NODE ///< Vos max reserved node ID
75 int node_in_use; ///< 使用标记
76 enum node_type node; ///< 节点ID
77 void *context_data; ///< 记录命令行上下文数据,如进入端口节点可以记录端口号
78 INT context_data_need_free; ///< 标记context_data 是否需要free,在exit 命令中会执行VOS_free
79 CHAR vty_cmd_prompt[ VTY_CMD_PROMPT_MAX ]; ///< 自定义提示符
83 /** VTY 结构体,只需关心fd之前的成员. */
86 char vty_cmd_prompt[VTY_CMD_PROMPT_MAX]; ///< 自定义提示符
88 VOID *context_data; ///< 记录命令行上下文数据,如进入端口节点可以记录端口号
90 INT context_data_need_free; ///< 标记context_data 是否需要free,在exit 命令中会执行VOS_free
92 int (*action_func) (struct vty *vty); ///< confirm action 的回调函数
94 int (*no_action_func) (struct vty *vty); ///< refuse action 的回调函数
103 enum node_type prev_node;
106 enum { VTY_TERM, }type;
130 char *hist[VTY_MAXHIST];
138 struct exit_use_node node_his_save[VTY_NODE_HIS_MAX];
141 unsigned char escape;
144 enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_START, VTY_CONTINUE, VTY_IOREDIRECT }status;
146 /* telnet IAC handling */
149 /* telnet IAC SB handling */
150 unsigned char iac_sb_in_progress;
151 /* telnet IAC SB buffer */
152 struct buffer *sb_buffer;
156 /* 窗口高度,lines为0时有效 */
162 /* 已进入config节点,用于限制多用户进入config */
166 struct cl_lib_thread *t_read;
168 struct cl_lib_thread *t_write;
171 unsigned long v_timeout;
174 struct cl_lib_thread *t_timeout;
177 unsigned short cl_vty_exit;
179 /* vty 接入类型console,telnet, telnet IPv6 */
186 struct cl_lib_thread_master *sub_thread_master;
189 VOS_HANDLE sub_task_id;
192 unsigned short debug_monitor;
194 /************************************
195 frozen vty in and(or) out
196 used for if a command can't return immediately
197 then call frozen to freeze vty's in and(or) out
198 call unfrozen when finished.
201 ***********************************/
202 unsigned short frozen;
204 //VOS_SYSLOG_MONITOR_CONF monitor_conf;
205 int veryBigOutLineCount;
206 int veryBigOutNoFirst;
208 /* vty 引用计数,当引用计数为0的时候调用vty_close释放内存 */
213 /** cl_vector 动态数组指针 */
216 unsigned int max; ///< 当前最大索引加1
217 unsigned int alloced; ///< 数组大小 */
218 void **index; ///< 指针数组
220 typedef struct _cl_vector *cl_vector;
226 char *string; ///< 命令字符串,多个用空格分隔的单词组成的句子
227 int (*func) (struct cmd_element *, struct vty *, int, char **); ///< 命令回调函数
228 char *doc; ///< 命令说明,对命令字符串中的每个单词进行说明
229 int msg_flag; ///< 命令类型标识,当前只有 DEFUN_FLAG 类型
230 cl_vector strvec; ///< 将 string和doc 拆分后放入该数组中 ,存放struct desc
231 int cmdsize; ///< string 中单词个数
232 cl_vector subconfig; ///< 将 string中可以重复的单词的重复位置和重复次数等
239 enum node_type node_id; ///< 节点ID
240 char prompt[VTY_CMD_PROMPT_MAX]; ///< 节点提示符
241 cl_vector cmd_vector; ///< 节点命令数组
249 * @param[in] funcname 回调函数的函数名
250 * @param[in] cmdname 命令变量的变量名
251 * @param[in] cmdstr 命令字符串
252 * @param[in] helpstr 命令字符串中各个单词的注解
255 #define DEFUN(funcname, cmdname, cmdstr, helpstr) \
256 int funcname (struct cmd_element *, struct vty *, int, char **);\
257 struct cmd_element cmdname = {cmdstr, funcname, helpstr , DEFUN_FLAG};\
258 int funcname (struct cmd_element *self, struct vty *vty, int argc, char **argv)
263 * 注意 LONG 输出时,要用%ld,或%lx
265 * @param[in] format 格式字符串
266 * @param[in] ... 要输出的变量
269 INT vty_out ( struct vty * vty, const CHAR * format, ... );
273 * @param[in] prompt 节点提示符,用于命令行提示符的显示
274 * @return 成功返回 一个新节点的指针,失败返回NULL
276 struct cmd_node *VOS_cli_create_node(char prompt[VTY_CMD_PROMPT_MAX]);
281 * @param[in] node_id 节点ID
283 * @return 成功返回 VOS_OK,失败返回其他
285 VOID VOS_cli_install_element( enum node_type node_id, struct cmd_element * cmd );
291 * @param[in] newnode 要进入的节点ID
292 * @return 成功返回 VOS_OK,失败返回其他
294 LONG vty_enter_node(struct vty *vty,enum node_type newnode);
298 * 在进入节点函数中调用,用于自定义要进入节点的命令行提示符,
299 * 调用vty_enter_node后调用
301 * @param[in] cpPrompt 提示符
303 VOID vty_set_prompt( struct vty * vty, CHAR cpPrompt[VTY_CMD_PROMPT_MAX] );
309 * Completion match types. 匹配度,数值越大匹配度越高,命令行模块内部使用无需关心
318 one_element_two_partly_match,
320 almost_exact_match, /* exactmatch except last word */
342 typedef struct _cmd_notify_register_tag
344 CHAR *match_str ; ///< 匹配的格式,如<H.H.H>
345 enum match_type (*pnotify_call) (CHAR *str); ///< 格式解析的回调函数,解析成功返回exact_match,解析失败返回no_match
346 CHAR IsCapital ; ///< 是否包含大写字母 0 - 全小写; 1 - 含有大写字母
347 }CMD_NOTIFY_REFISTER_S;
351 * @param[in] pNotifyRegister 自定义解析函数信息
352 * @return 成功返回 register_match,失败返回no_match
354 enum match_type cmd_rugular_register(CMD_NOTIFY_REFISTER_S * pNotifyRegister);
358 * @param[in ] vty vty
359 * @param[in ] newTime 新的超时时间,大于等于0时有效,0表示不会超时
360 * @param[out] oldTime 如果不为NULL 返回配置前的超时时间
361 * @return 成功返回 VOS_OK,失败则返回其他
363 LONG vty_timeout_config ( struct vty * vty, LONG newTime, LONG *oldTime);
367 * 要使用vty_very_big_out 函数必须先调用此初始化函数
368 * @param[in ] vty vty
370 void vty_very_big_out_init ( struct vty *vty );
375 * @param[in] format 格式字符串
376 * @param[in] ... 要输出的变量
378 int vty_very_big_out (struct vty *vty, const char *format, ...);
383 #endif /* __cplusplus */
384 #endif /* __VOS_CLI_H__ */