-
Notifications
You must be signed in to change notification settings - Fork 11
/
cmd_hier.h
220 lines (165 loc) · 6.12 KB
/
cmd_hier.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* =====================================================================================
*
* Filename: cmd_hier.h
*
* Description: This file defines the structure for maintaining cmd hierarchy
*
* Version: 1.0
* Created: Thursday 03 August 2017 02:08:10 IST
* Revision: 1.0
* Compiler: gcc
*
* Author: Er. Abhishek Sagar, Networking Developer (AS), [email protected]
* Company: Brocade Communications(Jul 2012- Mar 2016), Current : Juniper Networks(Apr 2017 - Present)
*
* =====================================================================================
*/
#ifndef __CMD_HIER__
#define __CMD_HIER__
#include <string.h>
#include <assert.h>
#include <stdbool.h>
#include "libcliid.h"
#include "clistd.h"
#include "cliconst.h"
typedef struct serialized_buffer ser_buff_t;
typedef int (*cmd_callback)(param_t *param, ser_buff_t *tlv_buf, op_mode enable_or_diable);
typedef int (*user_validation_callback)(char *leaf_value);
typedef void (*display_possible_values_callback)(param_t *, ser_buff_t *);
typedef struct _param_t_ param_t;
typedef struct cmd{
char cmd_name[CMD_NAME_SIZE];
} cmd_t;
typedef struct leaf{
leaf_type_t leaf_type;
char value_holder[LEAF_VALUE_HOLDER_SIZE];
user_validation_callback user_validation_cb_fn;
char leaf_id[LEAF_ID_SIZE];/*Within a single command, it should be unique*/
} leaf_t;
typedef CLI_VAL_RC (*leaf_type_handler)(leaf_t *leaf, char *value_passed);
typedef enum{
CMD,
LEAF,
NO_CMD
} param_type_t;
typedef union _param_t{
cmd_t *cmd;
leaf_t *leaf;
} _param_t;
struct _param_t_{
param_type_t param_type;
_param_t cmd_type;
cmd_callback callback;
char ishidden;
char help[PARAM_HELP_STRING_SIZE];
param_t *options[MAX_OPTION_SIZE];
param_t *parent;
display_possible_values_callback disp_callback;
int CMDCODE;
};
char*
get_str_leaf_type(leaf_type_t leaf_type);
#define MIN(a,b) (a < b ? a : b)
#define GET_PARAM_CMD(param) (param->cmd_type.cmd)
#define GET_PARAM_LEAF(param) (param->cmd_type.leaf)
#define IS_PARAM_NO_CMD(param) (param->param_type == NO_CMD)
#define IS_PARAM_CMD(param) (param->param_type == CMD)
#define IS_PARAM_LEAF(param) (param->param_type == LEAF)
#define GET_LEAF_TYPE_STR(param) (get_str_leaf_type(GET_PARAM_LEAF(param)->leaf_type))
#define GET_LEAF_VALUE_PTR(param) (GET_PARAM_LEAF(param)->value_holder)
#define GET_LEAF_TYPE(param) (GET_PARAM_LEAF(param)->leaf_type)
#define GET_CMD_NAME(param) (GET_PARAM_CMD(param)->cmd_name)
#define GET_PARAM_HELP_STRING(param) (param->help)
#define GET_LEAF_ID(param) (GET_PARAM_LEAF(param)->leaf_id)
#define IS_LEAF_USER_VALIDATION_CALLBACK_REGISTERED(param) \
(param->cmd_type.leaf->user_validation_cb_fn)
#define IS_APPLICATION_CALLBACK_HANDLER_REGISTERED(param) (param->callback)
#define _INVOKE_LEAF_USER_VALIDATION_CALLBACK(param, arg) \
(param->cmd_type.leaf->user_validation_cb_fn(arg))
#define INVOKE_LEAF_LIB_VALIDATION_CALLBACK(param, arg) \
(leaf_handler_array[GET_LEAF_TYPE(param)](GET_PARAM_LEAF(param), arg))
#define INVOKE_APPLICATION_CALLBACK_HANDLER(param, arg, enable_or_disable) \
param->callback(param, arg, enable_or_disable);
#define IS_PARAM_MODE_ENABLE(param_ptr) (param_ptr->options[MODE_PARAM_INDEX] != NULL)
#define IS_PARAM_SUBOPTIONS_ENABLE(param_ptr) (param_ptr->options[SUBOPTIONS_INDEX] != NULL)
/*True if user is not operating in root level*/
int
is_user_in_cmd_mode();
param_t *
libcli_get_no_hook(void);
param_t *
libcli_get_do_hook(void);
param_t *
libcli_get_root(void);
param_t *
libcli_get_mode_param();
param_t *
libcli_get_suboptions_param();
param_t *
libcli_get_cmd_expansion_param();
param_t *
libcli_get_repeat_hook(void);
param_t *
libcli_get_show_brief_extension_param(void);
static inline param_t **
get_child_array_ptr(param_t *param){
return ¶m->options[0];
}
static inline int
INVOKE_LEAF_USER_VALIDATION_CALLBACK(param_t *param, char *leaf_value) {
assert(param);
assert(leaf_value);
/*If validation fn is not registered, then validation is assumed to be passed*/
if(!IS_LEAF_USER_VALIDATION_CALLBACK_REGISTERED(param))
return 0;
return _INVOKE_LEAF_USER_VALIDATION_CALLBACK(param, leaf_value);
}
#define PRINT_TABS(n) \
do{ \
unsigned short _i = 0; \
for(; _i < n; _i++) \
printf(" "); \
} while(0);
/*Command Mode implementation*/
param_t *
get_current_branch_hook(param_t *current_param);
#define IS_CURRENT_MODE_SHOW() (get_current_branch_hook(get_cmd_tree_cursor()) == libcli_get_show_hook())
#define IS_CURRENT_MODE_DEBUG() (get_current_branch_hook(get_cmd_tree_cursor()) == libcli_get_debug_hook())
#define IS_CURRENT_MODE_CONFIG() (get_current_branch_hook(get_cmd_tree_cursor()) == libcli_get_config_hook())
#define IS_CURRENT_MODE_CLEAR() (get_current_branch_hook(get_cmd_tree_cursor()) == libcli_get_clear_hook())
void
reset_cmd_tree_cursor();
void
goto_top_of_cmd_tree(param_t *curr_cmd_tree_cursor);
void
go_one_level_up_cmd_tree(param_t *curr_cmd_tree_cursor);
void
set_cmd_tree_cursor(param_t *param);
param_t *
get_cmd_tree_cursor();
param_t*
find_matching_param(param_t **options, const char *cmd_name);
void
build_mode_console_name(param_t *dst_param);
/*Source and Destination command MUST be in the same branch AND
* Source must be at higher level as compared to Destination*/
void
build_cmd_tree_leaves_data(ser_buff_t *tlv_buff,/*Output serialize buffer*/
param_t *src_param, /*Source command*/
param_t *dst_param);/*Destination command*/
static inline int
is_cmd_string_match(param_t *param,
const char *str,
bool *ex_match) {
*ex_match = false;
int str_len = strlen(str);
int str_len_param = strlen(param->cmd_type.cmd->cmd_name);
int rc = (strncmp(param->cmd_type.cmd->cmd_name,
str, str_len));
if ( !rc && (str_len == str_len_param )) {
*ex_match = true;
}
return rc;
}
#endif