-
Notifications
You must be signed in to change notification settings - Fork 5
/
icli.c
1293 lines (1053 loc) · 34.4 KB
/
icli.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2019-2020 Iguazio.io Systems Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License") with
* an addition restriction as set forth herein. You may not use this
* file except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* In addition, you may not use the software for any purposes that are
* illegal under applicable law, and the grant of the foregoing license
* under the Apache 2.0 license is conditioned upon your compliance with
* such restriction.
*/
#include "icli.h"
#define _GNU_SOURCE
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <sys/queue.h>
#include <termios.h>
#include <errno.h>
#include <editline/readline.h>
#define ANSI_BLACK_NORMAL "\x1b[30m"
#define ANSI_RED_NORMAL "\x1b[31m"
#define ANSI_GREEN_NORMAL "\x1b[32m"
#define ANSI_YELLOW_NORMAL "\x1b[33m"
#define ANSI_BLUE_NORMAL "\x1b[34m"
#define ANSI_MAGENTA_NORMAL "\x1b[35m"
#define ANSI_CYAN_NORMAL "\x1b[36m"
#define ANSI_WHITE_NORMAL "\x1b[37m"
#define ANSI_RESET "\x1b[0m"
/* A structure which contains information on the commands this program
can understand. */
struct icli_command {
LIST_ENTRY(icli_command) cmd_list_entry;
char *name; /* User printable name of the function. */
char *short_name;
icli_cmd_func_t func; /* Function to call to do the job. */
char *doc; /* Documentation for this function. */
LIST_HEAD(, icli_command) cmd_list;
size_t n_cmds;
struct icli_command *parent;
int argc;
struct icli_arg *argv;
int max_name_len;
int name_len;
char *prompt_line;
bool internal;
};
struct icli {
void *user_data;
/* When non-zero, this means the user is done using this program. */
bool done;
struct icli_command *root_cmd;
struct icli_command *curr_cmd;
char *curr_prompt;
const char *prompt;
const char *hist_file;
int rows;
int cols;
int curr_row;
bool skip_output;
bool error_printed;
struct icli_command *curr_completion_cmd;
int curr_completion_arg;
icli_cmd_hook_t cmd_hook;
icli_output_hook_t out_hook;
icli_output_hook_t err_hook;
};
static struct icli icli;
#define array_len(_array) (sizeof(_array) / sizeof((_array)[0]))
#define UNUSED __attribute__((__unused__))
void icli_api_printf(const char *format, ...) __attribute__((__format__(__printf__, 1, 2)));
/* Strip whitespace from the start and end of STRING. Return a pointer
into STRING. */
static char *stripwhite(char *string)
{
register char *s, *t;
for (s = string; isspace(*s); s++)
;
if (*s == 0)
return (s);
t = s + strlen(s) - 1;
while (t > s && isspace(*t))
t--;
*++t = '\0';
return s;
}
void icli_api_printf(const char *format, ...)
{
va_list args;
printf("ERR: ");
va_start(args, format);
vprintf(format, args);
va_end(args);
}
/* Look up NAME as the name of a command, and return a pointer to that
command. Return a NULL pointer if NAME isn't a command name. */
static struct icli_command *icli_find_command(char *name)
{
struct icli_command *it;
LIST_FOREACH(it, &icli.curr_cmd->cmd_list, cmd_list_entry)
{
if (strcmp(name, it->name) == 0)
return it;
}
return ((struct icli_command *)NULL);
}
static void icli_cat_command(struct icli_command *curr)
{
if (!curr->parent)
return;
icli_cat_command(curr->parent);
if (curr->name) {
strcat(icli.curr_prompt, "(");
if (curr->prompt_line) {
strcat(icli.curr_prompt, curr->prompt_line);
} else if (curr->short_name) {
strcat(icli.curr_prompt, curr->short_name);
} else if (curr->name) {
strcat(icli.curr_prompt, curr->name);
}
strcat(icli.curr_prompt, ")");
}
}
static void icli_build_prompt(struct icli_command *command)
{
/*'\0' + prompt + '>' + ' ' */
size_t buf_sz = 1 + strlen(icli.prompt) + 2;
struct icli_command *curr = command;
while (curr) {
if (curr->prompt_line) {
/*command + '(' + ')' */
buf_sz += 2 + strlen(curr->prompt_line);
} else if (curr->short_name) {
/*command + '(' + ')' */
buf_sz += 2 + strlen(curr->short_name);
} else if (curr->name) {
/*command + '(' + ')' */
buf_sz += 2 + strlen(curr->name);
}
curr = curr->parent;
}
free((void *)icli.curr_prompt);
icli.curr_prompt = malloc(buf_sz);
strncpy(icli.curr_prompt, icli.prompt, buf_sz);
icli_cat_command(command);
strcat(icli.curr_prompt, "> ");
}
static int icli_parse_line(char *line, char **cmd, char *argv[], int argc)
{
int n_args = 0;
int i = 0;
char *tmp;
/* Isolate the command word. */
while (line[i] && isspace(line[i]))
i++;
tmp = &line[i];
while (line[i] && !isspace(line[i]))
i++;
if (line[i])
line[i++] = '\0';
*cmd = tmp;
/* Get argument to command, if any. */
while (line[i]) {
while (isspace(line[i]))
i++;
if (line[i] && n_args < argc) {
argv[n_args++] = &line[i];
}
while (line[i] && !isspace(line[i]))
i++;
if (line[i])
line[i++] = '\0';
}
return n_args;
}
static int icli_set_command_prompt(struct icli_command *cmd, char *argv[], int argc)
{
size_t bufsz = 1;
const char *name = NULL;
free(cmd->prompt_line);
cmd->prompt_line = NULL;
if (cmd->short_name) {
bufsz += strlen(cmd->short_name);
name = cmd->short_name;
} else if (cmd->name) {
bufsz += strlen(cmd->name);
name = cmd->name;
}
for (int i = 0; i < argc; ++i) {
bufsz += strlen(argv[i]) + 1;
}
cmd->prompt_line = malloc(bufsz);
if (!cmd->prompt_line)
return -1;
strcpy(cmd->prompt_line, name);
for (int i = 0; i < argc; ++i) {
strcat(cmd->prompt_line, " ");
strcat(cmd->prompt_line, argv[i]);
}
return 0;
}
static void icli_print_command_help(struct icli_command *cmd)
{
icli_printf("%s %s\n", cmd->name, cmd->doc);
icli_printf("Arguments:\n");
if (cmd->argc > 0) {
for (int i = 0; i < cmd->argc; ++i) {
icli_printf("---------------------------------------------------------------\n");
if (cmd->argv) {
switch (cmd->argv[i].type) {
case AT_Val:
if (cmd->argv[i].help)
icli_printf("%s\n", cmd->argv[i].help);
for (struct icli_arg_val *vals = cmd->argv[i].vals; vals && vals->val; ++vals) {
if (vals->help)
icli_printf("%s (%s)\n", vals->val, vals->help);
else
icli_printf("%s\n", vals->val);
}
break;
case AT_File:
if (cmd->argv[i].help)
icli_printf("filename (%s)\n", cmd->argv[i].help);
else
icli_printf("filename\n");
break;
default:
if (cmd->argv[i].help)
icli_printf("arg%d (%s)\n", i, cmd->argv[i].help);
else
icli_printf("arg%d\n", i);
}
} else {
icli_printf("arg%d\n", i);
}
}
} else if (ICLI_ARGS_DYNAMIC == cmd->argc) {
icli_printf("Variable number arguments accepted\n");
} else if (0 == cmd->argc) {
icli_printf("None\n");
}
}
int icli_execute_line(char *line)
{
struct icli_command *command;
static char *argv[ICLI_ARGS_MAX];
char *cmd;
int argc = icli_parse_line(line, &cmd, argv, array_len(argv));
command = icli_find_command(cmd);
if (!command) {
icli_err_printf("%s: No such command\n", cmd);
return -1;
}
if (!command->func && argc) {
icli_err_printf("Command %s does not accept arguments\n", cmd);
return -1;
}
if (command->func) {
if (command->argc != ICLI_ARGS_DYNAMIC) {
if (command->argc != argc) {
icli_err_printf("Command %s accepts exactly %d arguments. %d were provided\n",
cmd,
command->argc,
argc);
icli_print_command_help(command);
return -1;
}
if (command->argv) {
for (int i = 0; i < command->argc; ++i) {
if (AT_Val == command->argv[i].type) {
struct icli_arg_val *vals = command->argv[i].vals;
bool found = false;
if (!vals)
continue;
while (vals->val) {
if (strcmp(vals->val, argv[i]) == 0) {
found = true;
break;
}
++vals;
}
if (!found) {
icli_err_printf("Command %s %d argument invalid: %s\n", cmd, i, argv[i]);
icli_print_command_help(command);
return -1;
}
}
}
}
}
icli_set_command_prompt(command, argv, argc);
icli.curr_row = 0;
icli.error_printed = false;
if (icli.cmd_hook)
icli.cmd_hook(command->name, argv, argc, icli.user_data);
/* Call the function. */
enum icli_ret ret = command->func(argv, argc, icli.user_data);
icli.skip_output = false;
switch (ret) {
case ICLI_OK:
break;
case ICLI_ERR_ARG:
if (!icli.error_printed)
icli_err_printf("Argument error\n");
free(command->prompt_line);
command->prompt_line = NULL;
return -1;
break;
case ICLI_ERR:
if (!icli.error_printed)
icli_err_printf("Error\n");
free(command->prompt_line);
command->prompt_line = NULL;
return -1;
break;
}
} else {
if (icli.cmd_hook)
icli.cmd_hook(command->name, argv, argc, icli.user_data);
}
if (command->n_cmds) {
icli.curr_cmd = command;
icli_build_prompt(command);
}
return 0;
}
/* Generator function for command argument completion. STATE lets us
know whether to start from scratch; without any state
(i.e. STATE == 0), then we start at the top of the list. */
static char *icli_command_arg_generator(const char *text, int state)
{
static size_t len;
static struct icli_arg_val *vals;
static struct icli_arg *arg;
const char *name;
/* If this is a new word to complete, initialize now. This
includes saving the length of TEXT for efficiency, and
initializing the index variable to 0. */
if (!state) {
if (text)
len = strlen(text);
else
len = 0;
if (len) {
arg = &icli.curr_completion_cmd->argv[icli.curr_completion_arg - 1];
vals = icli.curr_completion_cmd->argv[icli.curr_completion_arg - 1].vals;
} else {
arg = &icli.curr_completion_cmd->argv[icli.curr_completion_arg];
vals = icli.curr_completion_cmd->argv[icli.curr_completion_arg].vals;
}
}
switch (arg->type) {
case AT_Val:
/* Return the next name which partially matches from the
argument list. */
while (vals && vals->val) {
name = vals->val;
++vals;
if (strncmp(name, text, len) == 0)
return strdup(name);
}
break;
case AT_File:
/* make readline attempt to complete with file name */
rl_attempted_completion_over = 0;
break;
default:
break;
}
/* If no names matched, then return NULL. */
return (char *)NULL;
}
/* Generator function for command completion. STATE lets us
know whether to start from scratch; without any state
(i.e. STATE == 0), then we start at the top of the list. */
static char *icli_command_generator(const char *text, int state)
{
static struct icli_command *list_entry;
static size_t len;
char *name;
/* If this is a new word to complete, initialize now. This
includes saving the length of TEXT for efficiency, and
initializing the index variable to 0. */
if (!state) {
list_entry = LIST_FIRST(&icli.curr_cmd->cmd_list);
if (text)
len = strlen(text);
else
len = 0;
}
/* Return the next name which partially matches from the
command list. */
while (list_entry) {
name = list_entry->name;
list_entry = LIST_NEXT(list_entry, cmd_list_entry);
if (strncmp(name, text, len) == 0)
return strdup(name);
}
/* If no names matched, then return NULL. */
return (char *)NULL;
}
/* Attempt to complete on the contents of TEXT. START and END
bound the region of rl_line_buffer that contains the word to
complete. TEXT is the word to complete. We can use the entire
contents of rl_line_buffer in case we want to do some simple
parsing. Return the array of matches, or NULL if there aren't any. */
static char **icli_completion(const char *text, int start, int end UNUSED)
{
char **matches;
static char *argv[ICLI_ARGS_MAX];
char *cmd;
static char tmp[4096];
memcpy(tmp, rl_line_buffer, (size_t)rl_end);
tmp[rl_end] = '\0';
int argc = icli_parse_line(tmp, &cmd, argv, array_len(argv));
/* Don't do filename completion even if our generator finds no matches. */
rl_attempted_completion_over = 1;
matches = (char **)NULL;
icli.curr_completion_cmd = NULL;
icli.curr_completion_arg = 0;
/* If this word is at the start of the line, then it is a command
* to complete */
if (start == 0) {
matches = completion_matches((char *)text, icli_command_generator);
/* matches = rl_completion_matches (text, icli_command_generator); */
} else {
struct icli_command *command = icli_find_command(cmd);
if (command && command->argc != ICLI_ARGS_DYNAMIC && command->argc) {
if (argc <= command->argc && command->argv) {
icli.curr_completion_cmd = command;
icli.curr_completion_arg = argc;
matches = completion_matches((char *)text, icli_command_arg_generator);
}
}
}
return matches;
}
static enum icli_ret icli_history(char *argv[] UNUSED, int argc UNUSED, void *context UNUSED)
{
HISTORY_STATE *hist_state = history_get_history_state();
HIST_ENTRY **mylist = history_list();
for (int i = 0; i < hist_state->length; i++) {
icli_printf("%d %s\n", history_base + i, mylist[i]->line);
}
free(hist_state);
return ICLI_OK;
}
static enum icli_ret icli_end(char *argv[], int argc, void *context UNUSED)
{
if (argc > 1) {
icli_err_printf("end supports either 0 or 1 numeric argument\n");
return ICLI_ERR_ARG;
}
int level = 1;
if (1 == argc) {
level = atoi(argv[0]);
if (0 == level) {
icli_err_printf("end argument must be a positive integer value\n");
return ICLI_ERR_ARG;
}
}
for (int i = 0; i < level; ++i) {
icli.curr_cmd = icli.curr_cmd->parent;
if (NULL == icli.curr_cmd) {
icli.curr_cmd = icli.root_cmd;
break;
}
}
icli_build_prompt(icli.curr_cmd);
return ICLI_OK;
}
/* Print out help for ARG, or for all of the commands if ARG is
not present. */
static enum icli_ret icli_help(char *argv[], int argc, void *context UNUSED)
{
int printed = 0;
struct icli_command *it;
if (argc > 1)
return ICLI_ERR_ARG;
icli_printf("Available commands:\n");
LIST_FOREACH(it, &icli.curr_cmd->cmd_list, cmd_list_entry)
{
if (0 == argc || (strcmp(argv[0], it->name) == 0)) {
if (argc > 0)
icli_print_command_help(it);
else
icli_printf(" %-*s : %s\n", icli.curr_cmd->max_name_len, it->name, it->doc);
printed++;
}
}
if (!printed) {
icli_err_printf("No commands match '%s'. Possibilities are:\n", argv[0]);
LIST_FOREACH(it, &icli.curr_cmd->cmd_list, cmd_list_entry)
{
/* Print in six columns. */
if (printed == 6) {
printed = 0;
icli_err_printf("\n");
}
icli_err_printf("%s\t", it->name);
printed++;
}
if (printed)
icli_err_printf("\n");
}
return ICLI_OK;
}
static enum icli_ret icli_quit(char *argv[] UNUSED, int argc UNUSED, void *context UNUSED)
{
icli.done = true;
return ICLI_OK;
}
int icli_exec_script(const char *fname)
{
FILE *input = fopen(fname, "r");
if (!input) {
icli_err_printf("Unable to open file %s:%m\n", fname);
return -1;
}
ssize_t read;
char *line = NULL;
char *stripped_line;
size_t len = 0;
int ret = 0;
while ((read = getline(&line, &len, input)) != -1) {
stripped_line = stripwhite(line);
if (*stripped_line && *stripped_line != '#') {
icli_printf("Executing: \"%s\"\n", stripped_line);
ret = icli_execute_line(stripped_line);
if (ret)
goto out;
}
}
out:
free(line);
fclose(input);
return ret;
}
static enum icli_ret icli_execute(char *argv[], int argc UNUSED, void *context UNUSED)
{
int ret = icli_exec_script(argv[0]);
if (ret)
return ICLI_ERR;
return ICLI_OK;
}
static int icli_init_default_cmds(struct icli_command *parent)
{
struct icli_command_params params[] =
{{.parent = parent,
.name = "help",
.command = icli_help,
.argc = ICLI_ARGS_DYNAMIC,
.help = "Show available commands or show help of a specific command. args: [command]"},
{.parent = parent, .name = "?", .command = icli_help, .argc = ICLI_ARGS_DYNAMIC, .help = "Synonym for 'help'"},
{.parent = parent,
.name = "history",
.command = icli_history,
.help = "Show a list of previously run commands"}};
struct icli_command *out_commands[array_len(params)];
int ret = icli_register_commands(params, out_commands, array_len(params));
if (ret)
return ret;
for (size_t i = 0; i < array_len(params); ++i) {
out_commands[i]->internal = true;
}
return ret;
}
static void icli_clean_command_argv(struct icli_command *cmd)
{
if (cmd->argc && cmd->argv) {
for (int j = 0; j < cmd->argc; ++j) {
if (AT_Val == cmd->argv[j].type) {
for (struct icli_arg_val *val = cmd->argv[j].vals; val && val->val; ++val) {
free((void *)val->val);
val->val = NULL;
free((void *)val->help);
val->help = NULL;
}
free(cmd->argv[j].vals);
cmd->argv[j].vals = NULL;
}
free((void *)cmd->argv[j].help);
cmd->argv[j].help = NULL;
}
free(cmd->argv);
cmd->argv = NULL;
}
}
static void icli_clean_command(struct icli_command *cmd)
{
while (!LIST_EMPTY(&cmd->cmd_list)) {
struct icli_command *it = LIST_FIRST(&cmd->cmd_list);
LIST_REMOVE(it, cmd_list_entry);
icli_clean_command(it);
}
free(cmd->name);
cmd->name = NULL;
free(cmd->short_name);
cmd->short_name = NULL;
free(cmd->doc);
cmd->doc = NULL;
free(cmd->prompt_line);
cmd->prompt_line = NULL;
icli_clean_command_argv(cmd);
cmd->argc = 0;
free(cmd);
}
int icli_register_commands(struct icli_command_params *params, struct icli_command *out_commads[], int n_commands)
{
int ret = 0;
for (int i = 0; i < n_commands; ++i) {
if (out_commads)
ret = icli_register_command(¶ms[i], &out_commads[i]);
else
ret = icli_register_command(¶ms[i], NULL);
if (ret)
return ret;
}
return ret;
}
static int icli_init_command_argv(struct icli_command *cmd, struct icli_arg *argv)
{
int ret = 0;
if (argv) {
cmd->argv = calloc((size_t)cmd->argc, sizeof(struct icli_arg));
if (!cmd->argv) {
icli_api_printf("Unable to allocate memory for argv in command:%s\n", cmd->name);
ret = -1;
goto out;
}
for (int i = 0; i < cmd->argc; ++i) {
cmd->argv[i].type = argv[i].type;
if (argv[i].help) {
cmd->argv[i].help = strdup(argv[i].help);
if (!cmd->argv[i].help) {
icli_api_printf("Unable to allocate help string for arg %d (%s)\n", i, argv[i].help);
ret = -1;
goto out;
}
}
if (AT_Val == argv[i].type) {
int n_vals = 0;
for (struct icli_arg_val *val = argv[i].vals; val && val->val; ++val, ++n_vals)
;
if (n_vals) {
struct icli_arg_val *vals = calloc((size_t)(n_vals + 1), sizeof(struct icli_arg_val));
if (!vals) {
icli_api_printf("Unable to allocate memory for vals of size %d in command:%s\n",
n_vals + 1,
cmd->name);
ret = -1;
goto out;
}
cmd->argv[i].vals = vals;
for (int j = 0; argv[i].vals[j].val; ++j) {
vals[j].val = strdup(argv[i].vals[j].val);
if (!vals[j].val) {
icli_api_printf("Unable to allocate memory for val %s in command:%s\n",
argv[i].vals[j].val,
cmd->name);
ret = -1;
goto out;
}
if (argv[i].vals[j].help) {
vals[j].help = strdup(argv[i].vals[j].help);
if (!vals[j].help) {
icli_api_printf("Unable to allocate memory for val %s help %s in command:%s\n",
argv[i].vals[j].val,
argv[i].vals[j].help,
cmd->name);
ret = -1;
goto out;
}
}
}
}
}
}
}
out:
return ret;
}
int icli_register_command(struct icli_command_params *params, struct icli_command **out_command)
{
bool need_end = true;
struct icli_command *parent, *it;
int ret = 0;
if (out_command)
*out_command = NULL;
if (!params) {
icli_api_printf("NULL params specified\n");
return -1;
}
if (!params->name || !(*params->name) || !params->help || !(*params->help)) {
icli_api_printf("name or help argument not provided\n");
return -1;
}
if (!params->command && params->argc != 0) {
icli_api_printf("command callback not provided while argc != 0 (%d)\n", params->argc);
return -1;
}
if (params->argv && 0 == params->argc) {
icli_api_printf("argv provided while argc = 0\n");
return -1;
}
parent = params->parent;
if (NULL == parent) {
need_end = false;
parent = icli.root_cmd;
}
LIST_FOREACH(it, &parent->cmd_list, cmd_list_entry)
{
if (strcmp(it->name, params->name) == 0) {
icli_api_printf("command %s already registered\n", params->name);
return -1;
}
}
struct icli_command *cmd = calloc(1, sizeof(struct icli_command));
if (NULL == cmd) {
icli_api_printf("unable to allocate memory for command %s\n", params->name);
return -1;
}
LIST_INIT(&cmd->cmd_list);
cmd->name = strdup(params->name);
cmd->name_len = (int)strlen(cmd->name);
cmd->doc = strdup(params->help);
cmd->func = params->command;
cmd->parent = parent;
cmd->argc = params->argc;
if (params->short_name)
cmd->short_name = strdup(params->short_name);
ret = icli_init_command_argv(cmd, params->argv);
if (ret) {
icli_clean_command(cmd);
goto out;
}
if (cmd->name_len > parent->max_name_len)
parent->max_name_len = cmd->name_len;
++parent->n_cmds;
if (1 == parent->n_cmds && need_end) {
struct icli_command *end;
struct icli_command_params param = {.parent = parent,
.name = "end",
.command = icli_end,
.argc = ICLI_ARGS_DYNAMIC,
.help = "Exit to upper level. args: [number of levels]"};
ret = icli_register_command(¶m, &end);
if (ret) {
--parent->n_cmds;
icli_clean_command(cmd);
goto out;
}
end->internal = true;
ret = icli_init_default_cmds(parent);
if (ret) {
--parent->n_cmds;
icli_clean_command(cmd);
goto out;
}
}
LIST_INSERT_HEAD(&parent->cmd_list, cmd, cmd_list_entry);
if (out_command)
*out_command = cmd;
out:
return ret;
}
int icli_init(struct icli_params *params)
{
memset(&icli, 0, sizeof(icli));
int ret = 0;
icli.root_cmd = calloc(1, sizeof(struct icli_command));
if (!icli.root_cmd) {
icli_api_printf("Unable to allocate memory for root command\n");
return -1;
}
LIST_INIT(&icli.root_cmd->cmd_list);
icli.root_cmd->internal = true;
icli.curr_cmd = icli.root_cmd;
icli.user_data = params->user_data;
icli.prompt = strdup(params->prompt);
if (!icli.prompt) {
icli_api_printf("Unable to allocate memory for prompt\n");
ret = -1;
goto err;
}
if (params->hist_file) {
icli.hist_file = strdup(params->hist_file);
if (!icli.hist_file) {
icli_api_printf("Unable to allocate memory for hist_file\n");
ret = -1;
goto err;
}
}
icli.cmd_hook = params->cmd_hook;
icli.out_hook = params->out_hook;
icli.err_hook = params->err_hook;
/* Allow conditional parsing of the ~/.inputrc file. */
rl_readline_name = strdup(params->app_name);
if (!rl_readline_name) {
icli_api_printf("Unable to allocate memory for rl_readline_name\n");
ret = -1;
goto err;
}
/* Tell the completer that we want a crack first. */
rl_attempted_completion_function = icli_completion;
using_history();
stifle_history(params->history_size);
if (icli.hist_file) {
ret = read_history(icli.hist_file);
if (ret && ret != ENOENT) {