2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 * cli.c: command line interface
18 * Copyright (c) 2008 Eliot Dresselhaus
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 #include <vlib/vlib.h>
41 #include <vppinfra/cpu.h>
44 /* Root of all show commands. */
46 VLIB_CLI_COMMAND (vlib_cli_show_command, static) = {
48 .short_help = "Show commands",
52 /* Root of all clear commands. */
54 VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = {
56 .short_help = "Clear commands",
60 /* Root of all set commands. */
62 VLIB_CLI_COMMAND (vlib_cli_set_command, static) = {
64 .short_help = "Set commands",
68 /* Root of all test commands. */
70 VLIB_CLI_COMMAND (vlib_cli_test_command, static) = {
72 .short_help = "Test commands",
76 /* Returns bitmap of commands which match key. */
78 vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input)
82 vlib_cli_parse_position_t *p;
84 unformat_skip_white_space (input);
90 k = unformat_get_input (input);
104 case UNFORMAT_END_OF_INPUT:
105 /* White space or end of input removes any non-white
106 matches that were before possible. */
107 if (i < vec_len (c->sub_command_positions)
108 && clib_bitmap_count_set_bits (match) > 1)
110 p = vec_elt_at_index (c->sub_command_positions, i);
111 for (n = 0; n < vec_len (p->bitmaps); n++)
112 match = clib_bitmap_andnot (match, p->bitmaps[n]);
117 unformat_put_input (input);
121 if (i >= vec_len (c->sub_command_positions))
124 clib_bitmap_free (match);
128 p = vec_elt_at_index (c->sub_command_positions, i);
129 if (vec_len (p->bitmaps) == 0)
133 if (n < 0 || n >= vec_len (p->bitmaps))
137 match = clib_bitmap_dup (p->bitmaps[n]);
139 match = clib_bitmap_and (match, p->bitmaps[n]);
141 if (clib_bitmap_is_zero (match))
149 /* Looks for string based sub-input formatted { SUB-INPUT }. */
151 unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
153 unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
159 c = unformat_get_input (i);
171 /* Put back paren. */
172 if (c != UNFORMAT_END_OF_INPUT)
173 unformat_put_input (i);
175 if (c == '{' && unformat (i, "%v", &s))
177 unformat_init_vector (sub_input, s);
186 static vlib_cli_command_t *
187 get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
189 vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
190 return vec_elt_at_index (cm->commands, s->index);
194 unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
196 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
197 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
198 vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
199 vlib_cli_main_t *cm = &vm->cli_main;
200 uword *match_bitmap, is_unique, index;
203 vlib_cli_sub_rule_t *sr;
204 vlib_cli_parse_rule_t *r;
205 vec_foreach (sr, c->sub_rules)
208 r = vec_elt_at_index (cm->parse_rules, sr->rule_index);
209 vec_add2 (cm->parse_rule_data, d, 1);
210 vec_reset_length (d[0]);
212 d[0] = _vec_resize (d[0],
213 /* length increment */ 1,
215 /* header_bytes */ 0,
216 /* data align */ sizeof (uword));
217 if (unformat_user (i, r->unformat_function, vm, d[0]))
219 *result = vec_elt_at_index (cm->commands, sr->command_index);
225 match_bitmap = vlib_cli_sub_command_match (c, i);
226 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
230 index = clib_bitmap_first_set (match_bitmap);
231 *result = get_sub_command (cm, c, index);
233 clib_bitmap_free (match_bitmap);
239 format_vlib_cli_command_help (u8 * s, va_list * args)
241 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
242 int is_long = va_arg (*args, int);
243 if (is_long && c->long_help)
244 s = format (s, "%s", c->long_help);
245 else if (c->short_help)
246 s = format (s, "%s", c->short_help);
248 s = format (s, "%v commands", c->path);
253 format_vlib_cli_parse_rule_name (u8 * s, va_list * args)
255 vlib_cli_parse_rule_t *r = va_arg (*args, vlib_cli_parse_rule_t *);
256 return format (s, "<%U>", format_c_identifier, r->name);
260 format_vlib_cli_path (u8 * s, va_list * args)
262 u8 *path = va_arg (*args, u8 *);
265 for (i = 0; i < vec_len (path); i++)
271 vec_add1 (s, '<'); /* start of <RULE> */
275 /* _ -> space in rules. */
276 vec_add1 (s, in_rule ? ' ' : '_');
282 vec_add1 (s, '>'); /* end of <RULE> */
289 vec_add1 (s, path[i]);
295 vec_add1 (s, '>'); /* terminate <RULE> */
300 static vlib_cli_command_t *
301 all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
303 vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
304 vlib_cli_sub_command_t *sc;
305 vlib_cli_sub_rule_t *sr;
308 vec_add1 (subs, c[0]);
310 vec_foreach (sr, c->sub_rules)
311 subs = all_subs (cm, subs, sr->command_index);
312 vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
318 vlib_cli_cmp_rule (void *a1, void *a2)
320 vlib_cli_sub_rule_t *r1 = a1;
321 vlib_cli_sub_rule_t *r2 = a2;
323 return vec_cmp (r1->name, r2->name);
327 vlib_cli_cmp_command (void *a1, void *a2)
329 vlib_cli_command_t *c1 = a1;
330 vlib_cli_command_t *c2 = a2;
332 return vec_cmp (c1->path, c2->path);
335 static clib_error_t *
336 vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
337 vlib_cli_main_t * cm,
338 unformat_input_t * input,
339 uword parent_command_index)
341 vlib_cli_command_t *parent, *c;
342 clib_error_t *error = 0;
343 unformat_input_t sub_input;
345 uword is_main_dispatch = cm == &vm->cli_main;
347 parent = vec_elt_at_index (cm->commands, parent_command_index);
348 if (is_main_dispatch && unformat (input, "help"))
350 uword help_at_end_of_line, i;
352 help_at_end_of_line =
353 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
358 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
361 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
368 /* help SUB-COMMAND => long format help.
369 "help" at end of line: show all commands. */
370 if (!help_at_end_of_line)
371 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
374 else if (vec_len (c->sub_commands) + vec_len (c->sub_rules) == 0)
375 vlib_cli_output (vm, "%v: no sub-commands", c->path);
379 vlib_cli_sub_command_t *sc;
380 vlib_cli_sub_rule_t *sr, *subs;
382 subs = vec_dup (c->sub_rules);
384 /* Add in rules if any. */
385 vec_foreach (sc, c->sub_commands)
387 vec_add2 (subs, sr, 1);
389 sr->command_index = sc->index;
393 vec_sort_with_function (subs, vlib_cli_cmp_rule);
395 for (i = 0; i < vec_len (subs); i++)
397 vlib_cli_command_t *d;
398 vlib_cli_parse_rule_t *r;
400 d = vec_elt_at_index (cm->commands, subs[i].command_index);
402 subs[i].rule_index != ~0 ? vec_elt_at_index (cm->parse_rules,
410 format_vlib_cli_parse_rule_name, r,
411 format_vlib_cli_command_help, d, /* is_long */ 0);
416 format_vlib_cli_command_help, d, /* is_long */ 0);
423 else if (is_main_dispatch
424 && (unformat (input, "choices") || unformat (input, "?")))
426 vlib_cli_command_t *sub, *subs;
428 subs = all_subs (cm, 0, parent_command_index);
429 vec_sort_with_function (subs, vlib_cli_cmp_command);
430 vec_foreach (sub, subs)
431 vlib_cli_output (vm, " %-40U %U",
432 format_vlib_cli_path, sub->path,
433 format_vlib_cli_command_help, sub, /* is_long */ 0);
437 else if (unformat (input, "comment %v", &string))
442 else if (unformat (input, "uncomment %U",
443 unformat_vlib_cli_sub_input, &sub_input))
446 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
447 parent_command_index);
448 unformat_free (&sub_input);
452 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
454 unformat_input_t *si;
455 uword has_sub_commands =
456 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
459 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
462 if (has_sub_commands)
463 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
465 if (has_sub_commands && !error)
466 /* Found valid sub-command. */ ;
468 else if (c->function)
470 clib_error_t *c_error;
472 /* Skip white space for benefit of called function. */
473 unformat_skip_white_space (si);
475 if (unformat (si, "?"))
477 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
483 vlib_worker_thread_barrier_sync (vm);
485 c_error = c->function (vm, si, c);
488 vlib_worker_thread_barrier_release (vm);
493 clib_error_return (0, "%v: %v", c->path, c_error->what);
494 clib_error_free (c_error);
495 /* Free sub input. */
503 /* Free any previous error. */
504 clib_error_free (error);
508 error = clib_error_return (0, "%v: no sub-commands", c->path);
510 /* Free sub input. */
522 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
523 format_unformat_error, input);
525 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
530 void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
531 __attribute__ ((weak));
534 vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
538 /* Process CLI input. */
540 vlib_cli_input (vlib_main_t * vm,
541 unformat_input_t * input,
542 vlib_cli_output_function_t * function, uword function_arg)
544 vlib_process_t *cp = vlib_get_current_process (vm);
545 vlib_cli_main_t *cm = &vm->cli_main;
547 vlib_cli_output_function_t *save_function;
548 uword save_function_arg;
550 save_function = cp->output_function;
551 save_function_arg = cp->output_function_arg;
553 cp->output_function = function;
554 cp->output_function_arg = function_arg;
558 vec_reset_length (cm->parse_rule_data);
559 error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input, /* parent */
562 while (!error && !unformat (input, "%U", unformat_eof));
566 vlib_cli_output (vm, "%v", error->what);
567 vlib_unix_error_report (vm, error);
568 clib_error_free (error);
571 cp->output_function = save_function;
572 cp->output_function_arg = save_function_arg;
575 /* Output to current CLI connection. */
577 vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
579 vlib_process_t *cp = vlib_get_current_process (vm);
584 s = va_format (0, fmt, &va);
587 /* Terminate with \n if not present. */
588 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
591 if ((!cp) || (!cp->output_function))
592 fformat (stdout, "%v", s);
594 cp->output_function (cp->output_function_arg, s, vec_len (s));
599 static clib_error_t *
600 show_memory_usage (vlib_main_t * vm,
601 unformat_input_t * input, vlib_cli_command_t * cmd)
607 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
609 if (unformat (input, "verbose"))
613 error = clib_error_return (0, "unknown input `%U'",
614 format_unformat_error, input);
622 vlib_cli_output (vm, "Thread %d %v\n", index, vlib_worker_threads[index].name);
623 vlib_cli_output (vm, "%U\n", format_mheap, clib_per_cpu_mheaps[index], verbose);
631 VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
632 .path = "show memory",
633 .short_help = "Show current memory usage",
634 .function = show_memory_usage,
638 static clib_error_t *
639 show_cpu (vlib_main_t * vm, unformat_input_t * input,
640 vlib_cli_command_t * cmd)
642 #define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
643 _("Model name", "%U", format_cpu_model_name);
644 _("Microarchitecture", "%U", format_cpu_uarch);
645 _("Flags", "%U", format_cpu_flags);
646 _("Base frequency", "%.2f GHz",
647 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
653 * Displays various information about the CPU.
656 * @cliexstart{show cpu}
657 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
658 * Microarchitecture: Broadwell (Broadwell-EP/EX)
659 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
660 * Base Frequency: 3.20 GHz
664 VLIB_CLI_COMMAND (show_cpu_command, static) = {
666 .short_help = "Show cpu information",
667 .function = show_cpu,
671 static clib_error_t *
672 enable_disable_memory_trace (vlib_main_t * vm,
673 unformat_input_t * input,
674 vlib_cli_command_t * cmd)
676 clib_error_t *error = 0;
679 if (!unformat_user (input, unformat_vlib_enable_disable, &enable))
681 error = clib_error_return (0, "expecting enable/on or disable/off");
685 clib_mem_trace (enable);
692 VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
693 .path = "memory-trace",
694 .short_help = "Enable/disable memory allocation trace",
695 .function = enable_disable_memory_trace,
700 static clib_error_t *
701 test_heap_validate (vlib_main_t * vm, unformat_input_t * input,
702 vlib_cli_command_t * cmd)
704 clib_error_t *error = 0;
708 if (unformat (input, "on"))
712 heap = clib_per_cpu_mheaps[this_vlib_main->cpu_index];
713 mheap = mheap_header(heap);
714 mheap->flags |= MHEAP_FLAG_VALIDATE;
715 // Turn off small object cache because it delays detection of errors
716 mheap->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
721 else if (unformat (input, "off"))
725 heap = clib_per_cpu_mheaps[this_vlib_main->cpu_index];
726 mheap = mheap_header(heap);
727 mheap->flags &= ~MHEAP_FLAG_VALIDATE;
728 mheap->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
732 else if (unformat (input, "now"))
736 heap = clib_per_cpu_mheaps[this_vlib_main->cpu_index];
737 mheap = mheap_header(heap);
738 mheap_validate(heap);
741 vlib_cli_output (vm, "heap validation complete");
746 return clib_error_return (0, "unknown input `%U'",
747 format_unformat_error, input);
754 VLIB_CLI_COMMAND (cmd_test_heap_validate,static) = {
755 .path = "test heap-validate",
756 .short_help = "<on/off/now> validate heap on future allocs/frees or right now",
757 .function = test_heap_validate,
761 static clib_error_t *
762 restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
763 vlib_cli_command_t * cmd)
765 char *newenviron[] = { NULL };
767 execve (vm->name, (char **) vm->argv, newenviron);
773 VLIB_CLI_COMMAND (restart_cmd,static) = {
775 .short_help = "restart process",
776 .function = restart_cmd_fn,
782 * A trivial test harness to verify the per-process output_function
783 * is working correcty.
786 static clib_error_t *
787 sleep_ten_seconds (vlib_main_t * vm,
788 unformat_input_t * input, vlib_cli_command_t * cmd)
793 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
795 for (i = 0; i < 10; i++)
797 vlib_process_wait_for_event_or_clock (vm, 1.0);
798 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
800 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
805 VLIB_CLI_COMMAND (ping_command, static) = {
806 .path = "test sleep",
807 .function = sleep_ten_seconds,
808 .short_help = "Sleep for 10 seconds",
811 #endif /* ifdef TEST_CODE */
814 vlib_cli_normalize_path (char *input, char **result)
819 uword index_of_last_space = ~0;
824 /* Multiple white space -> single space. */
831 if (l > 0 && s[l - 1] != ' ')
839 if (l > 0 && s[l - 1] == ' ')
840 index_of_last_space = vec_len (s);
847 /* Remove any extra space at end. */
848 if (l > 0 && s[l - 1] == ' ')
852 return index_of_last_space;
856 parent_path_len (char *path)
859 for (i = vec_len (path) - 1; i >= 0; i--)
868 add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
870 vlib_cli_command_t *p, *c;
871 vlib_cli_sub_command_t *sub_c;
875 p = vec_elt_at_index (cm->commands, parent_index);
876 c = vec_elt_at_index (cm->commands, child_index);
878 l = parent_path_len (c->path);
880 sub_name = vec_dup ((u8 *) c->path);
883 ASSERT (l + 1 < vec_len (c->path));
885 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
888 if (sub_name[0] == '%')
891 vlib_cli_sub_rule_t *sr;
894 vec_delete (sub_name, 1, 0);
896 if (!p->sub_rule_index_by_name)
897 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
898 sizeof (sub_name[0]),
900 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
903 sr = vec_elt_at_index (p->sub_rules, q[0]);
904 ASSERT (sr->command_index == child_index);
908 q = hash_get_mem (cm->parse_rule_index_by_name, sub_name);
911 clib_error ("reference to unknown rule `%%%v' in path `%v'",
916 hash_set_mem (p->sub_rule_index_by_name, sub_name,
917 vec_len (p->sub_rules));
918 vec_add2 (p->sub_rules, sr, 1);
920 sr->rule_index = q[0];
921 sr->command_index = child_index;
925 if (!p->sub_command_index_by_name)
926 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
930 /* Check if sub-command has already been created. */
931 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
937 vec_add2 (p->sub_commands, sub_c, 1);
938 sub_c->index = child_index;
939 sub_c->name = sub_name;
940 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
941 sub_c - p->sub_commands);
943 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
944 for (i = 0; i < vec_len (sub_c->name); i++)
947 vlib_cli_parse_position_t *pos;
949 pos = vec_elt_at_index (p->sub_command_positions, i);
952 pos->min_char = sub_c->name[i];
954 n = sub_c->name[i] - pos->min_char;
957 pos->min_char = sub_c->name[i];
958 vec_insert (pos->bitmaps, -n, 0);
962 vec_validate (pos->bitmaps, n);
964 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
969 vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
973 vlib_cli_command_t *c, *parent;
975 /* Root command (index 0) should have already been added. */
976 ASSERT (vec_len (cm->commands) > 0);
978 c = vec_elt_at_index (cm->commands, ci);
979 p_len = parent_path_len (c->path);
981 /* No space? Parent is root command. */
984 add_sub_command (cm, 0, ci);
989 vec_add (p_path, c->path, p_len);
991 p = hash_get_mem (cm->command_index_by_path, p_path);
996 /* Parent does not exist; create it. */
997 vec_add2 (cm->commands, parent, 1);
998 parent->path = p_path;
999 hash_set_mem (cm->command_index_by_path, parent->path,
1000 parent - cm->commands);
1001 pi = parent - cm->commands;
1009 add_sub_command (cm, pi, ci);
1011 /* Create parent's parent. */
1013 vlib_cli_make_parent (cm, pi);
1017 vlib_cli_command_is_empty (vlib_cli_command_t * c)
1019 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
1023 vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
1025 vlib_cli_main_t *cm = &vm->cli_main;
1026 clib_error_t *error = 0;
1028 char *normalized_path;
1030 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1033 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1035 if (!cm->command_index_by_path)
1036 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
1037 sizeof (c->path[0]),
1040 /* See if command already exists with given path. */
1041 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1044 vlib_cli_command_t *d;
1047 d = vec_elt_at_index (cm->commands, ci);
1049 /* If existing command was created via vlib_cli_make_parent
1050 replaced it with callers data. */
1051 if (vlib_cli_command_is_empty (d))
1053 vlib_cli_command_t save = d[0];
1055 ASSERT (!vlib_cli_command_is_empty (c));
1057 /* Copy callers fields. */
1060 /* Save internal fields. */
1061 d->path = save.path;
1062 d->sub_commands = save.sub_commands;
1063 d->sub_command_index_by_name = save.sub_command_index_by_name;
1064 d->sub_command_positions = save.sub_command_positions;
1065 d->sub_rules = save.sub_rules;
1069 clib_error_return (0, "duplicate command name with path %v",
1072 vec_free (normalized_path);
1078 /* Command does not exist: create it. */
1080 /* Add root command (index 0). */
1081 if (vec_len (cm->commands) == 0)
1083 /* Create command with index 0; path is empty string. */
1084 vec_resize (cm->commands, 1);
1087 ci = vec_len (cm->commands);
1088 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1089 vec_add1 (cm->commands, c[0]);
1091 c = vec_elt_at_index (cm->commands, ci);
1092 c->path = normalized_path;
1094 /* Don't inherit from registration. */
1095 c->sub_commands = 0;
1096 c->sub_command_index_by_name = 0;
1097 c->sub_command_positions = 0;
1100 vlib_cli_make_parent (cm, ci);
1105 vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1107 vlib_cli_main_t *cm = &vm->cli_main;
1108 vlib_cli_parse_rule_t *r;
1109 clib_error_t *error = 0;
1113 if (!cm->parse_rule_index_by_name)
1114 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1115 sizeof (r->name[0]),
1118 /* Make vector copy of name. */
1119 r_name = format (0, "%s", r_reg->name);
1121 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1124 return clib_error_return (0, "duplicate parse rule name `%s'",
1128 vec_add2 (cm->parse_rules, r, 1);
1130 r->name = (char *) r_name;
1131 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1137 /* $$$ turn back on again someday, maybe */
1138 static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1139 vlib_cli_parse_rule_t *
1141 vlib_cli_parse_rule_t *
1143 __attribute__ ((unused))
1145 clib_error_t *error = 0;
1146 vlib_cli_parse_rule_t *r;
1148 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1150 if (!r->name || strlen (r->name) == 0)
1152 error = clib_error_return (0, "parse rule with no name");
1156 error = vlib_cli_register_parse_rule (vm, r);
1166 static clib_error_t *
1167 vlib_cli_init (vlib_main_t * vm)
1169 vlib_cli_main_t *cm = &vm->cli_main;
1170 clib_error_t *error = 0;
1171 vlib_cli_command_t *cmd;
1173 cmd = cm->cli_command_registrations;
1177 error = vlib_cli_register (vm, cmd);
1180 cmd = cmd->next_cli_command;
1185 VLIB_INIT_FUNCTION (vlib_cli_init);
1188 * fd.io coding-style-patch-verification: ON
1191 * eval: (c-set-style "gnu")