-
Notifications
You must be signed in to change notification settings - Fork 11
/
clistd.c
465 lines (355 loc) · 13.2 KB
/
clistd.c
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
* =====================================================================================
*
* Filename: clistd.h
*
* Description: All Validation functions for leaves and standard default callbacks are defined in this file
*
* Version: 1.0
* Created: Sunday 06 August 2017 05:56:03 IST
* Revision: 1.0
* Compiler: gcc
*
* Author: Er. Abhishek Sagar, Networking Developer (AS), sachinites@gmail.com
* Company: Brocade Communications(Jul 2012- Mar 2016), Current : Juniper Networks(Apr 2017 - Present)
*
* =====================================================================================
*/
#include <signal.h>
#include "cmd_hier.h"
#include "clistd.h"
#include "cmdtlv.h"
#include "libcli.h"
#include "css.h"
#include "clicbext.h"
#include "string_util.h"
int GL_FD_OUT = STDOUT_FILENO;
extern CMD_PARSE_STATUS
parse_input_cmd(char *input, unsigned int len, bool *is_repeat_cmd);
extern char *
get_last_command();
extern char temp[ LEAF_ID_SIZE + 2];
static void
dump_all_commands(param_t *root, unsigned int index){
if(!root)
return;
if(IS_PARAM_NO_CMD(root))
return;
if(IS_PARAM_CMD(root)){
untokenize(index);
tokenize(GET_CMD_NAME(root), strlen(GET_CMD_NAME(root)), index);
}
else if(IS_PARAM_LEAF(root)){
untokenize(index);
memset(temp, 0, sizeof(temp));
sprintf(temp, "<%s>", GET_LEAF_ID(root));
tokenize(temp, strlen(GET_LEAF_ID(root)) + 2, index);
}
unsigned int i = CHILDREN_START_INDEX;
for( ; i <= CHILDREN_END_INDEX; i++)
dump_all_commands(root->options[i], index+1);
if(IS_APPLICATION_CALLBACK_HANDLER_REGISTERED(root)){
print_tokens(index + 1);
printf("\n");
}
}
/*Default validation handlers for Data types*/
CLI_VAL_RC
int_validation_handler(leaf_t *leaf, char *value_passed){
/*printf("%s is called for leaf type = %s, leaf value = %s\n", __FUNCTION__,
* get_str_leaf_type(leaf->leaf_type), value_passed);*/
return VALIDATION_SUCCESS;
}
CLI_VAL_RC
string_validation_handler(leaf_t *leaf, char *value_passed){
/*printf("%s is called for leaf type = %s, leaf value = %s\n", __FUNCTION__,
* get_str_leaf_type(leaf->leaf_type), value_passed);*/
return VALIDATION_SUCCESS;
}
CLI_VAL_RC
ipv4_validation_handler(leaf_t *leaf, char *value_passed){
/*printf("%s is called for leaf type = %s, leaf value = %s\n", __FUNCTION__,
* get_str_leaf_type(leaf->leaf_type), value_passed);*/
return VALIDATION_SUCCESS;
}
CLI_VAL_RC
ipv6_validation_handler(leaf_t *leaf, char *value_passed){
/*printf("%s is called for leaf type = %s, leaf value = %s\n", __FUNCTION__,
* get_str_leaf_type(leaf->leaf_type), value_passed);*/
return VALIDATION_SUCCESS;
}
CLI_VAL_RC
float_validation_handler(leaf_t *leaf, char *value_passed){
/*printf("%s is called for leaf type = %s, leaf value = %s\n", __FUNCTION__,
* get_str_leaf_type(leaf->leaf_type), value_passed);*/
return VALIDATION_SUCCESS;
}
CLI_VAL_RC
boolean_validation_handler(leaf_t *leaf, char *value_passed){
if((strncmp(value_passed, "true", strlen("true")) == 0) ||
(strncmp(value_passed, "false", strlen("false")) ==0))
return VALIDATION_SUCCESS;
return VALIDATION_FAILED;
}
int
enable_disable_validation_handler(char *value_passed){
if((strncmp(value_passed, "enable", strlen("enable")) == 0) ||
(strncmp(value_passed, "disable", strlen("disable")) ==0))
return VALIDATION_SUCCESS;
return VALIDATION_FAILED;
}
/* Default command handlers */
/*config console name <cons name>*/
extern char console_name[TERMINAL_NAME_SIZE];
int
config_console_name_handler(param_t *param, ser_buff_t *b, op_mode enable_or_disable){
tlv_struct_t *tlv = NULL;
TLV_LOOP_BEGIN(b, tlv){
if(enable_or_disable == CONFIG_ENABLE)
set_device_name(tlv->value);
else{
if(strncmp(tlv->value, console_name, strlen(tlv->value)) == 0)
set_device_name(DEFAULT_DEVICE_NAME);
else
printf("Error : Incorrect device name\n");
}
}TLV_LOOP_END;
return 0;
}
/*repeat*/
int
repeat_last_command(param_t *param, ser_buff_t *b, op_mode enable_or_disable){
static char new_line_consume[2];
char *last_cmd = get_last_command();
printf("prev : %s", last_cmd);
scanf("%c", new_line_consume);;
parse_input_cmd(last_cmd, strlen(last_cmd), NULL);
return 0;
}
int
mode_enter_callback(param_t *param, ser_buff_t *b, op_mode enable_or_disable){
if(param == libcli_get_root()){
printf(ANSI_COLOR_YELLOW "Info : Mode not supported at root level\n" ANSI_COLOR_RESET);
return 0;
}
set_cmd_tree_cursor(param);
build_mode_console_name(param);
if(IS_APPLICATION_CALLBACK_HANDLER_REGISTERED(param))
INVOKE_APPLICATION_CALLBACK_HANDLER(param, b, enable_or_disable);
return 0;
}
int
display_sub_options_callback(param_t *param, ser_buff_t *b, op_mode enable_or_disable){
int i = 0;
tlv_struct_t dummy;
if(IS_APPLICATION_CALLBACK_HANDLER_REGISTERED(param))
printf("<Enter>\n");
for(i = CHILDREN_START_INDEX; i <= CHILDREN_END_INDEX; i++){
if(param->options[i]){
if(IS_PARAM_HIDDEN(param->options[i]))
continue;
if(IS_PARAM_CMD(param->options[i]) || IS_PARAM_NO_CMD(param->options[i])){
printf(ANSI_COLOR_MAGENTA "nxt cmd -> %-31s | %s\n" ANSI_COLOR_RESET, GET_CMD_NAME(param->options[i]), GET_PARAM_HELP_STRING(param->options[i]));
continue;
}
printf(ANSI_COLOR_CYAN "nxt leaf -> %-32s | %s\n" ANSI_COLOR_RESET, GET_LEAF_TYPE_STR(param->options[i]), GET_PARAM_HELP_STRING(param->options[i]));
continue;
}
break;
}
/*Means param is a leaf*/
if(param->disp_callback){
/*Add a dummy TLV to compensate for the cmd code TLV*/
memset(&dummy, 0, sizeof(tlv_struct_t));
collect_tlv(b, &dummy);
printf(ANSI_COLOR_YELLOW "possible values :\n");
param->disp_callback(param, b);
printf(ANSI_COLOR_RESET);
}
return 0;
}
int
display_cmd_expansion_callback(param_t *param, ser_buff_t *b, op_mode enable_or_disable){
re_init_tokens(MAX_CMD_TREE_DEPTH);
unsigned int index = 0;
dump_all_commands(param, index);
return 0;
}
/* show history calback*/
static char file_cmsd_size[FILE_CMD_SIZE_MAX];
int
show_history_callback(param_t *param, ser_buff_t *b, op_mode enable_or_disable){
unsigned int cmd_counter = 0;
unsigned long cmd_offset[MAX_SAVED_CMDS];
memset(&cmd_offset, 0, MAX_SAVED_CMDS * sizeof(unsigned long));
FILE *f = fopen(CMD_HIST_RECORD_FILE, "r");
if(!f){
printf("Error : History file could not be read\n");
return 0;
}
memset(file_cmsd_size, 0, FILE_CMD_SIZE_MAX);
cmd_offset[cmd_counter++] = ftell(f);
while(fgets(file_cmsd_size, FILE_CMD_SIZE_MAX, f) != NULL) {
printf("%d. %s", cmd_counter - 1, file_cmsd_size);
cmd_offset[cmd_counter++] = ftell(f);
memset(file_cmsd_size, 0, FILE_CMD_SIZE_MAX);
}
int cmd_choice;
printf("Enter command no to trigger : ");
scanf("%d", &cmd_choice);
if(!(cmd_choice >= 0 && cmd_choice < cmd_counter)){
printf("Invalid choice\n");
fclose(f) ;
return 0;
}
fseek(f, cmd_offset[cmd_choice], SEEK_SET);
memset(file_cmsd_size, 0, FILE_CMD_SIZE_MAX);
fgets(file_cmsd_size, FILE_CMD_SIZE_MAX, f);
file_cmsd_size[FILE_CMD_SIZE_MAX -1] = '\0';
printf("Command to be triggered : %s", file_cmsd_size);
parse_input_cmd(file_cmsd_size, strlen(file_cmsd_size), NULL);
fclose(f) ;
return 0;
}
void
record_command(char *hist_file, char *cons_input_buffer, unsigned int cmd_len){
assert(hist_file || cons_input_buffer || !cmd_len);
static unsigned int cmd_counter = 0;
if(cmd_counter == MAX_SAVED_CMDS){
return;
}
FILE *f = fopen(CMD_HIST_RECORD_FILE, "a");
fwrite(cons_input_buffer, cmd_len, 1, f);
fwrite("\n", 1, 1, f);
cmd_counter++;
fclose(f);
}
int
clear_screen_handler(param_t *param, ser_buff_t *b, op_mode enable_or_disable){
system("clear");
return 0;
}
int
exit_cmd_handler(param_t *param, ser_buff_t *b, op_mode enable_or_disable){
go_one_level_up_cmd_tree(get_cmd_tree_cursor());
return 0;
}
int
end_cmd_handler(param_t *param, ser_buff_t *b, op_mode enable_or_disable){
goto_top_of_cmd_tree(get_cmd_tree_cursor());
return 0;
}
int
config_mode_enter_handler(param_t *param, ser_buff_t *b, op_mode enable_or_disable){
set_cmd_tree_cursor(param);
build_mode_console_name(param);
mark_checkpoint_serialize_buffer(b);
return 0;
}
extern void
parse_file(char *file_name);
int
load_file_handler(param_t *param, ser_buff_t *b, op_mode enable_or_disable){
char *file_name = NULL;
tlv_struct_t *tlv = NULL;
printf("No Op - Full type the command\n");
return 0;
TLV_LOOP_BEGIN(b, tlv) {
if (strncmp(tlv->leaf_id, "file-name",
strlen("file-name")) == 0) {
file_name = tlv->value;
}
} TLV_LOOP_END;
assert(file_name);
parse_file(file_name);
return 0;
}
int
negate_callback(param_t *param, ser_buff_t *b, op_mode enable_or_disable){
printf("Command Negation - Type the cmd following to Negate\n");
return 0;
}
int
supportsave_handler(param_t *param, ser_buff_t *b, op_mode enable_or_disable){
switch(enable_or_disable){
case CONFIG_ENABLE:
//signal(SIGTERM, terminate_signal_handler);/*When process is killed*/
//signal(SIGSEGV, terminate_signal_handler);/*When process access illegal memory*/
//signal(SIGABRT, terminate_signal_handler);/*when process abort itself*/
break;
case CONFIG_DISABLE:
signal(SIGTERM, SIG_DFL);/*When process is killed*/
signal(SIGSEGV, SIG_DFL);/*When process access illegal memory*/
signal(SIGABRT, SIG_DFL);/*when process abort itself*/
break;
default:
assert(0);
}
return 0;
}
int
cli_terminate_handler(param_t *param, ser_buff_t *b, op_mode enable_or_disable){
printf("Bye Bye\n");
exit(0);
}
int
show_help_handler(param_t *param, ser_buff_t *b, op_mode enable_or_disable){
printf("Welcome to Help Wizard\n");
printf("========================\n");
printf("1. Use '%s' Character after the command to enter command mode\n", MODE_CHARACTER);
printf("2. Use '%s' Character after the command to see possible follow up suboptions\n", SUBOPTIONS_CHARACTER);
printf("3. Use '%s' from within the config branch to directly trigger operational commands\n", DO);
printf("4. Use '%s' Character after the command to see possible complete command completions\n", CMD_EXPANSION_CHARACTER);
printf("5. Built-in commands:\n");
printf(" a. %s - clear screen\n", CLEAR_SCR_STRING);
printf(" b. %s - jump to top of cmd tree\n", GOTO_TOP_STRING);
printf(" c. %s - jump one level up of command tree\n", GOTO_ONE_LVL_UP_STRING);
printf(" d. config [%s] console name <console name> - set/unset new console name\n", NEGATE_CHARACTER);
printf(" e. config [%s] supportsave enable - enable/disable supportsave facility\n", NEGATE_CHARACTER);
printf(" f. debug show cmdtree - Show entire command tree\n");
printf(" g. show history - show history of commands triggered\n");
printf(" h. repeat - repeat the last command\n");
printf(ANSI_COLOR_YELLOW " Author : Abhishek Sagar, Juniper Networks\n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_YELLOW " Visit : www.csepracticals.com for more courses and projects\n" ANSI_COLOR_RESET);
return 0;
}
int
show_resgistered_cmd_handler(param_t *param, ser_buff_t *b, op_mode enable_or_disable){
/*Implement DFS and dump all complete commands*/
re_init_tokens(MAX_CMD_TREE_DEPTH);
param_t *root = libcli_get_root();
unsigned int index = 0;
dump_all_commands(root, index);
return 0;
}
int
show_cmd_tree(param_t *param, ser_buff_t *tlv_buf, op_mode enable_or_disable){
dump_cmd_tree();
return 0;
}
int
show_extension_param_handler(param_t *param, ser_buff_t *b, op_mode enable_or_disable){
tlv_struct_t tlv;
memset(&tlv, 0, sizeof(tlv_struct_t));
if(param == libcli_get_show_brief_extension_param()){
put_value_in_tlv((&tlv), SHOW_EXTENSION_PARAM_BRIEF);
tlv.leaf_type = INVALID;
strncpy(tlv.leaf_id, SHOW_EXTENSION_PARAM, strlen("SHOW_EXTENSION_PARAM"));
collect_tlv(b, &tlv);
}
return 0;
}
int
grep_pattern_validation(char *value_passed){
int i = 0;
while (value_passed[i] != '\0') {
if (value_passed[i] == ' ' ) return VALIDATION_FAILED;
i++;
}
return VALIDATION_SUCCESS;
}
int
pipe_handler (param_t *param, ser_buff_t *b, op_mode enable_or_disable){
printf ("%s() called \n", __FUNCTION__);
return 0;
}