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>
45 /* Root of all show commands. */
47 VLIB_CLI_COMMAND (vlib_cli_show_command, static) = {
49 .short_help = "Show commands",
53 /* Root of all clear commands. */
55 VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = {
57 .short_help = "Clear commands",
61 /* Root of all set commands. */
63 VLIB_CLI_COMMAND (vlib_cli_set_command, static) = {
65 .short_help = "Set commands",
69 /* Root of all test commands. */
71 VLIB_CLI_COMMAND (vlib_cli_test_command, static) = {
73 .short_help = "Test commands",
77 /* Returns bitmap of commands which match key. */
79 vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input)
83 vlib_cli_parse_position_t *p;
85 unformat_skip_white_space (input);
91 k = unformat_get_input (input);
105 case UNFORMAT_END_OF_INPUT:
106 /* White space or end of input removes any non-white
107 matches that were before possible. */
108 if (i < vec_len (c->sub_command_positions)
109 && clib_bitmap_count_set_bits (match) > 1)
111 p = vec_elt_at_index (c->sub_command_positions, i);
112 for (n = 0; n < vec_len (p->bitmaps); n++)
113 match = clib_bitmap_andnot (match, p->bitmaps[n]);
118 unformat_put_input (input);
122 if (i >= vec_len (c->sub_command_positions))
125 clib_bitmap_free (match);
129 p = vec_elt_at_index (c->sub_command_positions, i);
130 if (vec_len (p->bitmaps) == 0)
134 if (n < 0 || n >= vec_len (p->bitmaps))
138 match = clib_bitmap_dup (p->bitmaps[n]);
140 match = clib_bitmap_and (match, p->bitmaps[n]);
142 if (clib_bitmap_is_zero (match))
150 /* Looks for string based sub-input formatted { SUB-INPUT }. */
152 unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
154 unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
160 c = unformat_get_input (i);
172 /* Put back paren. */
173 if (c != UNFORMAT_END_OF_INPUT)
174 unformat_put_input (i);
176 if (c == '{' && unformat (i, "%v", &s))
178 unformat_init_vector (sub_input, s);
187 static vlib_cli_command_t *
188 get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
190 vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
191 return vec_elt_at_index (cm->commands, s->index);
195 unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
197 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
198 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
199 vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
200 vlib_cli_main_t *cm = &vm->cli_main;
201 uword *match_bitmap, is_unique, index;
204 vlib_cli_sub_rule_t *sr;
205 vlib_cli_parse_rule_t *r;
206 vec_foreach (sr, c->sub_rules)
209 r = vec_elt_at_index (cm->parse_rules, sr->rule_index);
210 vec_add2 (cm->parse_rule_data, d, 1);
211 vec_reset_length (d[0]);
213 d[0] = _vec_resize (d[0],
214 /* length increment */ 1,
216 /* header_bytes */ 0,
217 /* data align */ sizeof (uword));
218 if (unformat_user (i, r->unformat_function, vm, d[0]))
220 *result = vec_elt_at_index (cm->commands, sr->command_index);
226 match_bitmap = vlib_cli_sub_command_match (c, i);
227 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
231 index = clib_bitmap_first_set (match_bitmap);
232 *result = get_sub_command (cm, c, index);
234 clib_bitmap_free (match_bitmap);
240 vlib_cli_cmp_strings (void *a1, void *a2)
242 u8 *c1 = *(u8 **) a1;
243 u8 *c2 = *(u8 **) a2;
245 return vec_cmp (c1, c2);
249 vlib_cli_get_possible_completions (u8 * str)
251 vlib_cli_command_t *c;
252 vlib_cli_sub_command_t *sc;
253 vlib_main_t *vm = vlib_get_main ();
254 vlib_cli_main_t *vcm = &vm->cli_main;
255 uword *match_bitmap = 0;
256 uword index, is_unique, help_next_level;
258 unformat_input_t input;
259 unformat_init_vector (&input, vec_dup (str));
260 c = vec_elt_at_index (vcm->commands, 0);
262 /* remove trailing whitespace, except for one of them */
263 while (vec_len (input.buffer) >= 2 &&
264 isspace (input.buffer[vec_len (input.buffer) - 1]) &&
265 isspace (input.buffer[vec_len (input.buffer) - 2]))
267 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
270 /* if input is empty, directly return list of root commands */
271 if (vec_len (input.buffer) == 0 ||
272 (vec_len (input.buffer) == 1 && isspace (input.buffer[0])))
274 vec_foreach (sc, c->sub_commands)
276 vec_add1 (result, (u8 *) sc->name);
281 /* add a trailing '?' so that vlib_cli_sub_command_match can find
282 * all commands starting with the input string */
283 vec_add1 (input.buffer, '?');
287 match_bitmap = vlib_cli_sub_command_match (c, &input);
288 /* no match: return no result */
289 if (match_bitmap == 0)
293 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
294 /* unique match: try to step one subcommand level further */
297 /* stop if no more input */
298 if (input.index >= vec_len (input.buffer) - 1)
303 index = clib_bitmap_first_set (match_bitmap);
304 c = get_sub_command (vcm, c, index);
305 clib_bitmap_free (match_bitmap);
308 /* multiple matches: stop here, return all matches */
312 /* remove trailing '?' */
313 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
315 /* if we have a space at the end of input, and a unique match,
316 * autocomplete the next level of subcommands */
317 help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]);
319 clib_bitmap_foreach(index, match_bitmap, {
320 if (help_next_level && is_unique) {
321 c = get_sub_command (vcm, c, index);
322 vec_foreach (sc, c->sub_commands) {
323 vec_add1 (result, (u8*) sc->name);
325 goto done; /* break doesn't work in this macro-loop */
327 sc = &c->sub_commands[index];
328 vec_add1(result, (u8*) sc->name);
333 clib_bitmap_free (match_bitmap);
334 unformat_free (&input);
337 vec_sort_with_function (result, vlib_cli_cmp_strings);
342 format_vlib_cli_command_help (u8 * s, va_list * args)
344 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
345 int is_long = va_arg (*args, int);
346 if (is_long && c->long_help)
347 s = format (s, "%s", c->long_help);
348 else if (c->short_help)
349 s = format (s, "%s", c->short_help);
351 s = format (s, "%v commands", c->path);
356 format_vlib_cli_parse_rule_name (u8 * s, va_list * args)
358 vlib_cli_parse_rule_t *r = va_arg (*args, vlib_cli_parse_rule_t *);
359 return format (s, "<%U>", format_c_identifier, r->name);
363 format_vlib_cli_path (u8 * s, va_list * args)
365 u8 *path = va_arg (*args, u8 *);
368 for (i = 0; i < vec_len (path); i++)
374 vec_add1 (s, '<'); /* start of <RULE> */
378 /* _ -> space in rules. */
379 vec_add1 (s, in_rule ? ' ' : '_');
385 vec_add1 (s, '>'); /* end of <RULE> */
392 vec_add1 (s, path[i]);
398 vec_add1 (s, '>'); /* terminate <RULE> */
403 static vlib_cli_command_t *
404 all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
406 vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
407 vlib_cli_sub_command_t *sc;
408 vlib_cli_sub_rule_t *sr;
411 vec_add1 (subs, c[0]);
413 vec_foreach (sr, c->sub_rules)
414 subs = all_subs (cm, subs, sr->command_index);
415 vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
421 vlib_cli_cmp_rule (void *a1, void *a2)
423 vlib_cli_sub_rule_t *r1 = a1;
424 vlib_cli_sub_rule_t *r2 = a2;
426 return vec_cmp (r1->name, r2->name);
430 vlib_cli_cmp_command (void *a1, void *a2)
432 vlib_cli_command_t *c1 = a1;
433 vlib_cli_command_t *c2 = a2;
435 return vec_cmp (c1->path, c2->path);
438 static clib_error_t *
439 vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
440 vlib_cli_main_t * cm,
441 unformat_input_t * input,
442 uword parent_command_index)
444 vlib_cli_command_t *parent, *c;
445 clib_error_t *error = 0;
446 unformat_input_t sub_input;
448 uword is_main_dispatch = cm == &vm->cli_main;
450 parent = vec_elt_at_index (cm->commands, parent_command_index);
451 if (is_main_dispatch && unformat (input, "help"))
453 uword help_at_end_of_line, i;
455 help_at_end_of_line =
456 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
461 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
464 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
471 /* help SUB-COMMAND => long format help.
472 "help" at end of line: show all commands. */
473 if (!help_at_end_of_line)
474 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
477 else if (vec_len (c->sub_commands) + vec_len (c->sub_rules) == 0)
478 vlib_cli_output (vm, "%v: no sub-commands", c->path);
482 vlib_cli_sub_command_t *sc;
483 vlib_cli_sub_rule_t *sr, *subs;
485 subs = vec_dup (c->sub_rules);
487 /* Add in rules if any. */
488 vec_foreach (sc, c->sub_commands)
490 vec_add2 (subs, sr, 1);
492 sr->command_index = sc->index;
496 vec_sort_with_function (subs, vlib_cli_cmp_rule);
498 for (i = 0; i < vec_len (subs); i++)
500 vlib_cli_command_t *d;
501 vlib_cli_parse_rule_t *r;
503 d = vec_elt_at_index (cm->commands, subs[i].command_index);
505 subs[i].rule_index != ~0 ? vec_elt_at_index (cm->parse_rules,
513 format_vlib_cli_parse_rule_name, r,
514 format_vlib_cli_command_help, d, /* is_long */ 0);
519 format_vlib_cli_command_help, d, /* is_long */ 0);
526 else if (is_main_dispatch
527 && (unformat (input, "choices") || unformat (input, "?")))
529 vlib_cli_command_t *sub, *subs;
531 subs = all_subs (cm, 0, parent_command_index);
532 vec_sort_with_function (subs, vlib_cli_cmp_command);
533 vec_foreach (sub, subs)
534 vlib_cli_output (vm, " %-40U %U",
535 format_vlib_cli_path, sub->path,
536 format_vlib_cli_command_help, sub, /* is_long */ 0);
540 else if (unformat (input, "comment %v", &string))
545 else if (unformat (input, "uncomment %U",
546 unformat_vlib_cli_sub_input, &sub_input))
549 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
550 parent_command_index);
551 unformat_free (&sub_input);
555 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
557 unformat_input_t *si;
558 uword has_sub_commands =
559 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
562 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
565 if (has_sub_commands)
566 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
568 if (has_sub_commands && !error)
569 /* Found valid sub-command. */ ;
571 else if (c->function)
573 clib_error_t *c_error;
575 /* Skip white space for benefit of called function. */
576 unformat_skip_white_space (si);
578 if (unformat (si, "?"))
580 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
586 vlib_worker_thread_barrier_sync (vm);
588 c_error = c->function (vm, si, c);
591 vlib_worker_thread_barrier_release (vm);
596 clib_error_return (0, "%v: %v", c->path, c_error->what);
597 clib_error_free (c_error);
598 /* Free sub input. */
606 /* Free any previous error. */
607 clib_error_free (error);
611 error = clib_error_return (0, "%v: no sub-commands", c->path);
613 /* Free sub input. */
625 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
626 format_unformat_error, input);
628 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
633 void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
634 __attribute__ ((weak));
637 vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
641 /* Process CLI input. */
643 vlib_cli_input (vlib_main_t * vm,
644 unformat_input_t * input,
645 vlib_cli_output_function_t * function, uword function_arg)
647 vlib_process_t *cp = vlib_get_current_process (vm);
648 vlib_cli_main_t *cm = &vm->cli_main;
650 vlib_cli_output_function_t *save_function;
651 uword save_function_arg;
653 save_function = cp->output_function;
654 save_function_arg = cp->output_function_arg;
656 cp->output_function = function;
657 cp->output_function_arg = function_arg;
661 vec_reset_length (cm->parse_rule_data);
662 error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input, /* parent */
665 while (!error && !unformat (input, "%U", unformat_eof));
669 vlib_cli_output (vm, "%v", error->what);
670 vlib_unix_error_report (vm, error);
671 clib_error_free (error);
674 cp->output_function = save_function;
675 cp->output_function_arg = save_function_arg;
678 /* Output to current CLI connection. */
680 vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
682 vlib_process_t *cp = vlib_get_current_process (vm);
687 s = va_format (0, fmt, &va);
690 /* Terminate with \n if not present. */
691 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
694 if ((!cp) || (!cp->output_function))
695 fformat (stdout, "%v", s);
697 cp->output_function (cp->output_function_arg, s, vec_len (s));
702 static clib_error_t *
703 show_memory_usage (vlib_main_t * vm,
704 unformat_input_t * input, vlib_cli_command_t * cmd)
710 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
712 if (unformat (input, "verbose"))
716 error = clib_error_return (0, "unknown input `%U'",
717 format_unformat_error, input);
725 vlib_cli_output (vm, "Thread %d %v\n", index, vlib_worker_threads[index].name);
726 vlib_cli_output (vm, "%U\n", format_mheap, clib_per_cpu_mheaps[index], verbose);
734 VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
735 .path = "show memory",
736 .short_help = "Show current memory usage",
737 .function = show_memory_usage,
741 static clib_error_t *
742 show_cpu (vlib_main_t * vm, unformat_input_t * input,
743 vlib_cli_command_t * cmd)
745 #define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
746 _("Model name", "%U", format_cpu_model_name);
747 _("Microarchitecture", "%U", format_cpu_uarch);
748 _("Flags", "%U", format_cpu_flags);
749 _("Base frequency", "%.2f GHz",
750 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
756 * Displays various information about the CPU.
759 * @cliexstart{show cpu}
760 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
761 * Microarchitecture: Broadwell (Broadwell-EP/EX)
762 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
763 * Base Frequency: 3.20 GHz
767 VLIB_CLI_COMMAND (show_cpu_command, static) = {
769 .short_help = "Show cpu information",
770 .function = show_cpu,
774 static clib_error_t *
775 enable_disable_memory_trace (vlib_main_t * vm,
776 unformat_input_t * input,
777 vlib_cli_command_t * cmd)
779 clib_error_t *error = 0;
782 if (!unformat_user (input, unformat_vlib_enable_disable, &enable))
784 error = clib_error_return (0, "expecting enable/on or disable/off");
788 clib_mem_trace (enable);
795 VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
796 .path = "memory-trace",
797 .short_help = "Enable/disable memory allocation trace",
798 .function = enable_disable_memory_trace,
803 static clib_error_t *
804 test_heap_validate (vlib_main_t * vm, unformat_input_t * input,
805 vlib_cli_command_t * cmd)
807 clib_error_t *error = 0;
811 if (unformat (input, "on"))
815 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
816 mheap = mheap_header(heap);
817 mheap->flags |= MHEAP_FLAG_VALIDATE;
818 // Turn off small object cache because it delays detection of errors
819 mheap->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
824 else if (unformat (input, "off"))
828 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
829 mheap = mheap_header(heap);
830 mheap->flags &= ~MHEAP_FLAG_VALIDATE;
831 mheap->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
835 else if (unformat (input, "now"))
839 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
840 mheap = mheap_header(heap);
841 mheap_validate(heap);
844 vlib_cli_output (vm, "heap validation complete");
849 return clib_error_return (0, "unknown input `%U'",
850 format_unformat_error, input);
857 VLIB_CLI_COMMAND (cmd_test_heap_validate,static) = {
858 .path = "test heap-validate",
859 .short_help = "<on/off/now> validate heap on future allocs/frees or right now",
860 .function = test_heap_validate,
864 static clib_error_t *
865 restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
866 vlib_cli_command_t * cmd)
868 char *newenviron[] = { NULL };
870 execve (vm->name, (char **) vm->argv, newenviron);
876 VLIB_CLI_COMMAND (restart_cmd,static) = {
878 .short_help = "restart process",
879 .function = restart_cmd_fn,
885 * A trivial test harness to verify the per-process output_function
886 * is working correcty.
889 static clib_error_t *
890 sleep_ten_seconds (vlib_main_t * vm,
891 unformat_input_t * input, vlib_cli_command_t * cmd)
896 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
898 for (i = 0; i < 10; i++)
900 vlib_process_wait_for_event_or_clock (vm, 1.0);
901 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
903 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
908 VLIB_CLI_COMMAND (ping_command, static) = {
909 .path = "test sleep",
910 .function = sleep_ten_seconds,
911 .short_help = "Sleep for 10 seconds",
914 #endif /* ifdef TEST_CODE */
917 vlib_cli_normalize_path (char *input, char **result)
922 uword index_of_last_space = ~0;
927 /* Multiple white space -> single space. */
934 if (l > 0 && s[l - 1] != ' ')
942 if (l > 0 && s[l - 1] == ' ')
943 index_of_last_space = vec_len (s);
950 /* Remove any extra space at end. */
951 if (l > 0 && s[l - 1] == ' ')
955 return index_of_last_space;
959 parent_path_len (char *path)
962 for (i = vec_len (path) - 1; i >= 0; i--)
971 add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
973 vlib_cli_command_t *p, *c;
974 vlib_cli_sub_command_t *sub_c;
978 p = vec_elt_at_index (cm->commands, parent_index);
979 c = vec_elt_at_index (cm->commands, child_index);
981 l = parent_path_len (c->path);
983 sub_name = vec_dup ((u8 *) c->path);
986 ASSERT (l + 1 < vec_len (c->path));
988 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
991 if (sub_name[0] == '%')
994 vlib_cli_sub_rule_t *sr;
997 vec_delete (sub_name, 1, 0);
999 if (!p->sub_rule_index_by_name)
1000 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1001 sizeof (sub_name[0]),
1003 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1006 sr = vec_elt_at_index (p->sub_rules, q[0]);
1007 ASSERT (sr->command_index == child_index);
1011 q = hash_get_mem (cm->parse_rule_index_by_name, sub_name);
1014 clib_error ("reference to unknown rule `%%%v' in path `%v'",
1019 hash_set_mem (p->sub_rule_index_by_name, sub_name,
1020 vec_len (p->sub_rules));
1021 vec_add2 (p->sub_rules, sr, 1);
1022 sr->name = sub_name;
1023 sr->rule_index = q[0];
1024 sr->command_index = child_index;
1028 if (!p->sub_command_index_by_name)
1029 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1030 sizeof (c->path[0]),
1033 /* Check if sub-command has already been created. */
1034 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1036 vec_free (sub_name);
1040 vec_add2 (p->sub_commands, sub_c, 1);
1041 sub_c->index = child_index;
1042 sub_c->name = sub_name;
1043 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
1044 sub_c - p->sub_commands);
1046 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1047 for (i = 0; i < vec_len (sub_c->name); i++)
1050 vlib_cli_parse_position_t *pos;
1052 pos = vec_elt_at_index (p->sub_command_positions, i);
1055 pos->min_char = sub_c->name[i];
1057 n = sub_c->name[i] - pos->min_char;
1060 pos->min_char = sub_c->name[i];
1061 vec_insert (pos->bitmaps, -n, 0);
1065 vec_validate (pos->bitmaps, n);
1067 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
1072 vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
1074 uword p_len, pi, *p;
1076 vlib_cli_command_t *c, *parent;
1078 /* Root command (index 0) should have already been added. */
1079 ASSERT (vec_len (cm->commands) > 0);
1081 c = vec_elt_at_index (cm->commands, ci);
1082 p_len = parent_path_len (c->path);
1084 /* No space? Parent is root command. */
1087 add_sub_command (cm, 0, ci);
1092 vec_add (p_path, c->path, p_len);
1094 p = hash_get_mem (cm->command_index_by_path, p_path);
1096 /* Parent exists? */
1099 /* Parent does not exist; create it. */
1100 vec_add2 (cm->commands, parent, 1);
1101 parent->path = p_path;
1102 hash_set_mem (cm->command_index_by_path, parent->path,
1103 parent - cm->commands);
1104 pi = parent - cm->commands;
1112 add_sub_command (cm, pi, ci);
1114 /* Create parent's parent. */
1116 vlib_cli_make_parent (cm, pi);
1120 vlib_cli_command_is_empty (vlib_cli_command_t * c)
1122 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
1126 vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
1128 vlib_cli_main_t *cm = &vm->cli_main;
1129 clib_error_t *error = 0;
1131 char *normalized_path;
1133 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1136 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1138 if (!cm->command_index_by_path)
1139 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
1140 sizeof (c->path[0]),
1143 /* See if command already exists with given path. */
1144 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1147 vlib_cli_command_t *d;
1150 d = vec_elt_at_index (cm->commands, ci);
1152 /* If existing command was created via vlib_cli_make_parent
1153 replaced it with callers data. */
1154 if (vlib_cli_command_is_empty (d))
1156 vlib_cli_command_t save = d[0];
1158 ASSERT (!vlib_cli_command_is_empty (c));
1160 /* Copy callers fields. */
1163 /* Save internal fields. */
1164 d->path = save.path;
1165 d->sub_commands = save.sub_commands;
1166 d->sub_command_index_by_name = save.sub_command_index_by_name;
1167 d->sub_command_positions = save.sub_command_positions;
1168 d->sub_rules = save.sub_rules;
1172 clib_error_return (0, "duplicate command name with path %v",
1175 vec_free (normalized_path);
1181 /* Command does not exist: create it. */
1183 /* Add root command (index 0). */
1184 if (vec_len (cm->commands) == 0)
1186 /* Create command with index 0; path is empty string. */
1187 vec_resize (cm->commands, 1);
1190 ci = vec_len (cm->commands);
1191 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1192 vec_add1 (cm->commands, c[0]);
1194 c = vec_elt_at_index (cm->commands, ci);
1195 c->path = normalized_path;
1197 /* Don't inherit from registration. */
1198 c->sub_commands = 0;
1199 c->sub_command_index_by_name = 0;
1200 c->sub_command_positions = 0;
1203 vlib_cli_make_parent (cm, ci);
1208 vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1210 vlib_cli_main_t *cm = &vm->cli_main;
1211 vlib_cli_parse_rule_t *r;
1212 clib_error_t *error = 0;
1216 if (!cm->parse_rule_index_by_name)
1217 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1218 sizeof (r->name[0]),
1221 /* Make vector copy of name. */
1222 r_name = format (0, "%s", r_reg->name);
1224 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1227 return clib_error_return (0, "duplicate parse rule name `%s'",
1231 vec_add2 (cm->parse_rules, r, 1);
1233 r->name = (char *) r_name;
1234 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1240 /* $$$ turn back on again someday, maybe */
1241 static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1242 vlib_cli_parse_rule_t *
1244 vlib_cli_parse_rule_t *
1246 __attribute__ ((unused))
1248 clib_error_t *error = 0;
1249 vlib_cli_parse_rule_t *r;
1251 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1253 if (!r->name || strlen (r->name) == 0)
1255 error = clib_error_return (0, "parse rule with no name");
1259 error = vlib_cli_register_parse_rule (vm, r);
1270 cli_path_compare (void *a1, void *a2)
1275 if ((vec_len (*s1) < vec_len (*s2)) &&
1276 memcmp ((char *) *s1, (char *) *s2, vec_len (*s1)) == 0)
1280 if ((vec_len (*s1) > vec_len (*s2)) &&
1281 memcmp ((char *) *s1, (char *) *s2, vec_len (*s2)) == 0)
1284 return vec_cmp (*s1, *s2);
1287 static clib_error_t *
1288 show_cli_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1289 vlib_cli_command_t * cmd)
1291 vlib_cli_main_t *cm = &vm->cli_main;
1292 vlib_cli_command_t *cli;
1293 u8 **paths = 0, **s;
1296 vec_foreach (cli, cm->commands)
1297 if (vec_len (cli->path) > 0)
1298 vec_add1 (paths, (u8 *) cli->path);
1300 vec_sort_with_function (paths, cli_path_compare);
1302 vec_foreach (s, paths)
1303 vlib_cli_output (vm, "%v", *s);
1311 VLIB_CLI_COMMAND (show_cli_command, static) = {
1313 .short_help = "Show cli commands",
1314 .function = show_cli_cmd_fn,
1318 static clib_error_t *
1319 vlib_cli_init (vlib_main_t * vm)
1321 vlib_cli_main_t *cm = &vm->cli_main;
1322 clib_error_t *error = 0;
1323 vlib_cli_command_t *cmd;
1325 cmd = cm->cli_command_registrations;
1329 error = vlib_cli_register (vm, cmd);
1332 cmd = cmd->next_cli_command;
1337 VLIB_INIT_FUNCTION (vlib_cli_init);
1340 * fd.io coding-style-patch-verification: ON
1343 * eval: (c-set-style "gnu")