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 <vlib/unix/unix.h>
42 #include <vppinfra/cpu.h>
46 /* Root of all show commands. */
48 VLIB_CLI_COMMAND (vlib_cli_show_command, static) = {
50 .short_help = "Show commands",
54 /* Root of all clear commands. */
56 VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = {
58 .short_help = "Clear commands",
62 /* Root of all set commands. */
64 VLIB_CLI_COMMAND (vlib_cli_set_command, static) = {
66 .short_help = "Set commands",
70 /* Root of all test commands. */
72 VLIB_CLI_COMMAND (vlib_cli_test_command, static) = {
74 .short_help = "Test commands",
78 /* Returns bitmap of commands which match key. */
80 vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input)
84 vlib_cli_parse_position_t *p;
86 unformat_skip_white_space (input);
92 k = unformat_get_input (input);
106 case UNFORMAT_END_OF_INPUT:
107 /* White space or end of input removes any non-white
108 matches that were before possible. */
109 if (i < vec_len (c->sub_command_positions)
110 && clib_bitmap_count_set_bits (match) > 1)
112 p = vec_elt_at_index (c->sub_command_positions, i);
113 for (n = 0; n < vec_len (p->bitmaps); n++)
114 match = clib_bitmap_andnot (match, p->bitmaps[n]);
119 unformat_put_input (input);
123 if (i >= vec_len (c->sub_command_positions))
126 clib_bitmap_free (match);
130 p = vec_elt_at_index (c->sub_command_positions, i);
131 if (vec_len (p->bitmaps) == 0)
135 if (n < 0 || n >= vec_len (p->bitmaps))
139 match = clib_bitmap_dup (p->bitmaps[n]);
141 match = clib_bitmap_and (match, p->bitmaps[n]);
143 if (clib_bitmap_is_zero (match))
151 /* Looks for string based sub-input formatted { SUB-INPUT }. */
153 unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
155 unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
161 c = unformat_get_input (i);
173 /* Put back paren. */
174 if (c != UNFORMAT_END_OF_INPUT)
175 unformat_put_input (i);
177 if (c == '{' && unformat (i, "%v", &s))
179 unformat_init_vector (sub_input, s);
188 static vlib_cli_command_t *
189 get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
191 vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
192 return vec_elt_at_index (cm->commands, s->index);
196 unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
198 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
199 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
200 vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
201 vlib_cli_main_t *cm = &vm->cli_main;
202 uword *match_bitmap, is_unique, index;
205 vlib_cli_sub_rule_t *sr;
206 vlib_cli_parse_rule_t *r;
207 vec_foreach (sr, c->sub_rules)
210 r = vec_elt_at_index (cm->parse_rules, sr->rule_index);
211 vec_add2 (cm->parse_rule_data, d, 1);
212 vec_reset_length (d[0]);
214 d[0] = _vec_resize (d[0],
215 /* length increment */ 1,
217 /* header_bytes */ 0,
218 /* data align */ sizeof (uword));
219 if (unformat_user (i, r->unformat_function, vm, d[0]))
221 *result = vec_elt_at_index (cm->commands, sr->command_index);
227 match_bitmap = vlib_cli_sub_command_match (c, i);
228 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
232 index = clib_bitmap_first_set (match_bitmap);
233 *result = get_sub_command (cm, c, index);
235 clib_bitmap_free (match_bitmap);
241 vlib_cli_cmp_strings (void *a1, void *a2)
243 u8 *c1 = *(u8 **) a1;
244 u8 *c2 = *(u8 **) a2;
246 return vec_cmp (c1, c2);
250 vlib_cli_get_possible_completions (u8 * str)
252 vlib_cli_command_t *c;
253 vlib_cli_sub_command_t *sc;
254 vlib_main_t *vm = vlib_get_main ();
255 vlib_cli_main_t *vcm = &vm->cli_main;
256 uword *match_bitmap = 0;
257 uword index, is_unique, help_next_level;
259 unformat_input_t input;
260 unformat_init_vector (&input, vec_dup (str));
261 c = vec_elt_at_index (vcm->commands, 0);
263 /* remove trailing whitespace, except for one of them */
264 while (vec_len (input.buffer) >= 2 &&
265 isspace (input.buffer[vec_len (input.buffer) - 1]) &&
266 isspace (input.buffer[vec_len (input.buffer) - 2]))
268 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
271 /* if input is empty, directly return list of root commands */
272 if (vec_len (input.buffer) == 0 ||
273 (vec_len (input.buffer) == 1 && isspace (input.buffer[0])))
275 vec_foreach (sc, c->sub_commands)
277 vec_add1 (result, (u8 *) sc->name);
282 /* add a trailing '?' so that vlib_cli_sub_command_match can find
283 * all commands starting with the input string */
284 vec_add1 (input.buffer, '?');
288 match_bitmap = vlib_cli_sub_command_match (c, &input);
289 /* no match: return no result */
290 if (match_bitmap == 0)
294 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
295 /* unique match: try to step one subcommand level further */
298 /* stop if no more input */
299 if (input.index >= vec_len (input.buffer) - 1)
304 index = clib_bitmap_first_set (match_bitmap);
305 c = get_sub_command (vcm, c, index);
306 clib_bitmap_free (match_bitmap);
309 /* multiple matches: stop here, return all matches */
313 /* remove trailing '?' */
314 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
316 /* if we have a space at the end of input, and a unique match,
317 * autocomplete the next level of subcommands */
318 help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]);
320 clib_bitmap_foreach(index, match_bitmap, {
321 if (help_next_level && is_unique) {
322 c = get_sub_command (vcm, c, index);
323 vec_foreach (sc, c->sub_commands) {
324 vec_add1 (result, (u8*) sc->name);
326 goto done; /* break doesn't work in this macro-loop */
328 sc = &c->sub_commands[index];
329 vec_add1(result, (u8*) sc->name);
334 clib_bitmap_free (match_bitmap);
335 unformat_free (&input);
338 vec_sort_with_function (result, vlib_cli_cmp_strings);
343 format_vlib_cli_command_help (u8 * s, va_list * args)
345 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
346 int is_long = va_arg (*args, int);
347 if (is_long && c->long_help)
348 s = format (s, "%s", c->long_help);
349 else if (c->short_help)
350 s = format (s, "%s", c->short_help);
352 s = format (s, "%v commands", c->path);
357 format_vlib_cli_parse_rule_name (u8 * s, va_list * args)
359 vlib_cli_parse_rule_t *r = va_arg (*args, vlib_cli_parse_rule_t *);
360 return format (s, "<%U>", format_c_identifier, r->name);
364 format_vlib_cli_path (u8 * s, va_list * args)
366 u8 *path = va_arg (*args, u8 *);
369 for (i = 0; i < vec_len (path); i++)
375 vec_add1 (s, '<'); /* start of <RULE> */
379 /* _ -> space in rules. */
380 vec_add1 (s, in_rule ? ' ' : '_');
386 vec_add1 (s, '>'); /* end of <RULE> */
393 vec_add1 (s, path[i]);
399 vec_add1 (s, '>'); /* terminate <RULE> */
404 static vlib_cli_command_t *
405 all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
407 vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
408 vlib_cli_sub_command_t *sc;
409 vlib_cli_sub_rule_t *sr;
412 vec_add1 (subs, c[0]);
414 vec_foreach (sr, c->sub_rules)
415 subs = all_subs (cm, subs, sr->command_index);
416 vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
422 vlib_cli_cmp_rule (void *a1, void *a2)
424 vlib_cli_sub_rule_t *r1 = a1;
425 vlib_cli_sub_rule_t *r2 = a2;
427 return vec_cmp (r1->name, r2->name);
431 vlib_cli_cmp_command (void *a1, void *a2)
433 vlib_cli_command_t *c1 = a1;
434 vlib_cli_command_t *c2 = a2;
436 return vec_cmp (c1->path, c2->path);
439 static clib_error_t *
440 vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
441 vlib_cli_main_t * cm,
442 unformat_input_t * input,
443 uword parent_command_index)
445 vlib_cli_command_t *parent, *c;
446 clib_error_t *error = 0;
447 unformat_input_t sub_input;
449 uword is_main_dispatch = cm == &vm->cli_main;
451 parent = vec_elt_at_index (cm->commands, parent_command_index);
452 if (is_main_dispatch && unformat (input, "help"))
454 uword help_at_end_of_line, i;
456 help_at_end_of_line =
457 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
462 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
465 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
472 /* help SUB-COMMAND => long format help.
473 "help" at end of line: show all commands. */
474 if (!help_at_end_of_line)
475 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
478 else if (vec_len (c->sub_commands) + vec_len (c->sub_rules) == 0)
479 vlib_cli_output (vm, "%v: no sub-commands", c->path);
483 vlib_cli_sub_command_t *sc;
484 vlib_cli_sub_rule_t *sr, *subs;
486 subs = vec_dup (c->sub_rules);
488 /* Add in rules if any. */
489 vec_foreach (sc, c->sub_commands)
491 vec_add2 (subs, sr, 1);
493 sr->command_index = sc->index;
497 vec_sort_with_function (subs, vlib_cli_cmp_rule);
499 for (i = 0; i < vec_len (subs); i++)
501 vlib_cli_command_t *d;
502 vlib_cli_parse_rule_t *r;
504 d = vec_elt_at_index (cm->commands, subs[i].command_index);
506 subs[i].rule_index != ~0 ? vec_elt_at_index (cm->parse_rules,
514 format_vlib_cli_parse_rule_name, r,
515 format_vlib_cli_command_help, d, /* is_long */ 0);
520 format_vlib_cli_command_help, d, /* is_long */ 0);
527 else if (is_main_dispatch
528 && (unformat (input, "choices") || unformat (input, "?")))
530 vlib_cli_command_t *sub, *subs;
532 subs = all_subs (cm, 0, parent_command_index);
533 vec_sort_with_function (subs, vlib_cli_cmp_command);
534 vec_foreach (sub, subs)
535 vlib_cli_output (vm, " %-40U %U",
536 format_vlib_cli_path, sub->path,
537 format_vlib_cli_command_help, sub, /* is_long */ 0);
541 else if (unformat (input, "comment %v", &string))
546 else if (unformat (input, "uncomment %U",
547 unformat_vlib_cli_sub_input, &sub_input))
550 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
551 parent_command_index);
552 unformat_free (&sub_input);
556 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
558 unformat_input_t *si;
559 uword has_sub_commands =
560 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
563 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
566 if (has_sub_commands)
567 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
569 if (has_sub_commands && !error)
570 /* Found valid sub-command. */ ;
572 else if (c->function)
574 clib_error_t *c_error;
576 /* Skip white space for benefit of called function. */
577 unformat_skip_white_space (si);
579 if (unformat (si, "?"))
581 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
587 vlib_worker_thread_barrier_sync (vm);
589 c_error = c->function (vm, si, c);
592 vlib_worker_thread_barrier_release (vm);
597 clib_error_return (0, "%v: %v", c->path, c_error->what);
598 clib_error_free (c_error);
599 /* Free sub input. */
607 /* Free any previous error. */
608 clib_error_free (error);
612 error = clib_error_return (0, "%v: no sub-commands", c->path);
614 /* Free sub input. */
626 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
627 format_unformat_error, input);
629 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
634 void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
635 __attribute__ ((weak));
638 vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
642 /* Process CLI input. */
644 vlib_cli_input (vlib_main_t * vm,
645 unformat_input_t * input,
646 vlib_cli_output_function_t * function, uword function_arg)
648 vlib_process_t *cp = vlib_get_current_process (vm);
649 vlib_cli_main_t *cm = &vm->cli_main;
651 vlib_cli_output_function_t *save_function;
652 uword save_function_arg;
654 save_function = cp->output_function;
655 save_function_arg = cp->output_function_arg;
657 cp->output_function = function;
658 cp->output_function_arg = function_arg;
662 vec_reset_length (cm->parse_rule_data);
663 error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input, /* parent */
666 while (!error && !unformat (input, "%U", unformat_eof));
670 vlib_cli_output (vm, "%v", error->what);
671 vlib_unix_error_report (vm, error);
672 clib_error_free (error);
675 cp->output_function = save_function;
676 cp->output_function_arg = save_function_arg;
679 /* Output to current CLI connection. */
681 vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
683 vlib_process_t *cp = vlib_get_current_process (vm);
688 s = va_format (0, fmt, &va);
691 /* Terminate with \n if not present. */
692 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
695 if ((!cp) || (!cp->output_function))
696 fformat (stdout, "%v", s);
698 cp->output_function (cp->output_function_arg, s, vec_len (s));
703 void *vl_msg_push_heap (void) __attribute__ ((weak));
704 void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak));
706 static clib_error_t *
707 show_memory_usage (vlib_main_t * vm,
708 unformat_input_t * input, vlib_cli_command_t * cmd)
710 int verbose __attribute__ ((unused)) = 0, api_segment = 0;
714 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
716 if (unformat (input, "verbose"))
718 else if (unformat (input, "api-segment"))
722 error = clib_error_return (0, "unknown input `%U'",
723 format_unformat_error, input);
730 void *oldheap = vl_msg_push_heap ();
732 format (0, "%U\n", format_mheap, clib_mem_get_heap (), 1);
733 vl_msg_pop_heap (oldheap);
734 u8 *s = vec_dup (s_in_svm);
736 oldheap = vl_msg_push_heap ();
738 vl_msg_pop_heap (oldheap);
739 vlib_cli_output (vm, "API segment start:");
740 vlib_cli_output (vm, "%v", s);
741 vlib_cli_output (vm, "API segment end:");
745 #if USE_DLMALLOC == 0
749 mheap_t *h = mheap_header (clib_per_cpu_mheaps[index]);
750 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
751 vlib_worker_threads[index].name);
752 vlib_cli_output (vm, " %U\n", format_page_map, pointer_to_uword (h) -
753 h->vm_alloc_offset_from_header,
755 vlib_cli_output (vm, " %U\n", format_mheap, clib_per_cpu_mheaps[index],
762 uword clib_mem_trace_enable_disable (uword enable);
766 * Note: the foreach_vlib_main cause allocator traffic,
767 * so shut off tracing before we go there...
769 was_enabled = clib_mem_trace_enable_disable (0);
776 mspace = clib_per_cpu_mheaps[index];
778 mi = mspace_mallinfo (mspace);
779 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
780 vlib_worker_threads[index].name);
781 vlib_cli_output (vm, " %U\n", format_page_map,
782 pointer_to_uword (mspace_least_addr(mspace)),
784 vlib_cli_output (vm, " %U\n", format_mheap, clib_per_cpu_mheaps[index],
790 /* Restore the trace flag */
791 clib_mem_trace_enable_disable (was_enabled);
793 #endif /* USE_DLMALLOC */
798 VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
799 .path = "show memory",
800 .short_help = "[verbose | api-segment] Show current memory usage",
801 .function = show_memory_usage,
805 static clib_error_t *
806 show_cpu (vlib_main_t * vm, unformat_input_t * input,
807 vlib_cli_command_t * cmd)
809 #define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
810 _("Model name", "%U", format_cpu_model_name);
811 _("Microarchitecture", "%U", format_cpu_uarch);
812 _("Flags", "%U", format_cpu_flags);
813 _("Base frequency", "%.2f GHz",
814 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
820 * Displays various information about the CPU.
823 * @cliexstart{show cpu}
824 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
825 * Microarchitecture: Broadwell (Broadwell-EP/EX)
826 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
827 * Base Frequency: 3.20 GHz
831 VLIB_CLI_COMMAND (show_cpu_command, static) = {
833 .short_help = "Show cpu information",
834 .function = show_cpu,
839 static clib_error_t *
840 enable_disable_memory_trace (vlib_main_t * vm,
841 unformat_input_t * input,
842 vlib_cli_command_t * cmd)
844 unformat_input_t _line_input, *line_input = &_line_input;
850 if (!unformat_user (input, unformat_line_input, line_input))
853 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
855 if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
857 else if (unformat (line_input, "api-segment"))
861 unformat_free (line_input);
862 return clib_error_return (0, "invalid input");
865 unformat_free (line_input);
868 oldheap = vl_msg_push_heap ();
869 clib_mem_trace (enable);
871 vl_msg_pop_heap (oldheap);
877 VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
878 .path = "memory-trace",
879 .short_help = "on|off [api-segment] Enable/disable memory allocation trace",
880 .function = enable_disable_memory_trace,
885 static clib_error_t *
886 test_heap_validate (vlib_main_t * vm, unformat_input_t * input,
887 vlib_cli_command_t * cmd)
889 #if USE_DLMALLOC == 0
890 clib_error_t *error = 0;
894 if (unformat (input, "on"))
898 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
899 mheap = mheap_header(heap);
900 mheap->flags |= MHEAP_FLAG_VALIDATE;
901 // Turn off small object cache because it delays detection of errors
902 mheap->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
907 else if (unformat (input, "off"))
911 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
912 mheap = mheap_header(heap);
913 mheap->flags &= ~MHEAP_FLAG_VALIDATE;
914 mheap->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
918 else if (unformat (input, "now"))
922 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
923 mheap = mheap_header(heap);
924 mheap_validate(heap);
927 vlib_cli_output (vm, "heap validation complete");
932 return clib_error_return (0, "unknown input `%U'",
933 format_unformat_error, input);
938 return clib_error_return (0, "unimplemented...");
939 #endif /* USE_DLMALLOC */
943 VLIB_CLI_COMMAND (cmd_test_heap_validate,static) = {
944 .path = "test heap-validate",
945 .short_help = "<on/off/now> validate heap on future allocs/frees or right now",
946 .function = test_heap_validate,
950 static clib_error_t *
951 restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
952 vlib_cli_command_t * cmd)
954 clib_file_main_t *fm = &file_main;
957 /* environ(7) does not indicate a header for this */
958 extern char **environ;
960 /* Close all known open files */
962 pool_foreach(f, fm->file_pool,
964 if (f->file_descriptor > 2)
965 close(f->file_descriptor);
970 execve (vm->name, (char **) vm->argv, environ);
976 VLIB_CLI_COMMAND (restart_cmd,static) = {
978 .short_help = "restart process",
979 .function = restart_cmd_fn,
985 * A trivial test harness to verify the per-process output_function
986 * is working correcty.
989 static clib_error_t *
990 sleep_ten_seconds (vlib_main_t * vm,
991 unformat_input_t * input, vlib_cli_command_t * cmd)
996 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
998 for (i = 0; i < 10; i++)
1000 vlib_process_wait_for_event_or_clock (vm, 1.0);
1001 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
1003 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
1008 VLIB_CLI_COMMAND (ping_command, static) = {
1009 .path = "test sleep",
1010 .function = sleep_ten_seconds,
1011 .short_help = "Sleep for 10 seconds",
1014 #endif /* ifdef TEST_CODE */
1017 vlib_cli_normalize_path (char *input, char **result)
1022 uword index_of_last_space = ~0;
1027 /* Multiple white space -> single space. */
1034 if (l > 0 && s[l - 1] != ' ')
1042 if (l > 0 && s[l - 1] == ' ')
1043 index_of_last_space = vec_len (s);
1050 /* Remove any extra space at end. */
1051 if (l > 0 && s[l - 1] == ' ')
1055 return index_of_last_space;
1059 parent_path_len (char *path)
1062 for (i = vec_len (path) - 1; i >= 0; i--)
1071 add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
1073 vlib_cli_command_t *p, *c;
1074 vlib_cli_sub_command_t *sub_c;
1078 p = vec_elt_at_index (cm->commands, parent_index);
1079 c = vec_elt_at_index (cm->commands, child_index);
1081 l = parent_path_len (c->path);
1083 sub_name = vec_dup ((u8 *) c->path);
1086 ASSERT (l + 1 < vec_len (c->path));
1088 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
1091 if (sub_name[0] == '%')
1094 vlib_cli_sub_rule_t *sr;
1097 vec_delete (sub_name, 1, 0);
1099 if (!p->sub_rule_index_by_name)
1100 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1101 sizeof (sub_name[0]),
1103 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1106 sr = vec_elt_at_index (p->sub_rules, q[0]);
1107 ASSERT (sr->command_index == child_index);
1111 q = hash_get_mem (cm->parse_rule_index_by_name, sub_name);
1114 clib_error ("reference to unknown rule `%%%v' in path `%v'",
1119 hash_set_mem (p->sub_rule_index_by_name, sub_name,
1120 vec_len (p->sub_rules));
1121 vec_add2 (p->sub_rules, sr, 1);
1122 sr->name = sub_name;
1123 sr->rule_index = q[0];
1124 sr->command_index = child_index;
1128 if (!p->sub_command_index_by_name)
1129 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1130 sizeof (c->path[0]),
1133 /* Check if sub-command has already been created. */
1134 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1136 vec_free (sub_name);
1140 vec_add2 (p->sub_commands, sub_c, 1);
1141 sub_c->index = child_index;
1142 sub_c->name = sub_name;
1143 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
1144 sub_c - p->sub_commands);
1146 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1147 for (i = 0; i < vec_len (sub_c->name); i++)
1150 vlib_cli_parse_position_t *pos;
1152 pos = vec_elt_at_index (p->sub_command_positions, i);
1155 pos->min_char = sub_c->name[i];
1157 n = sub_c->name[i] - pos->min_char;
1160 pos->min_char = sub_c->name[i];
1161 vec_insert (pos->bitmaps, -n, 0);
1165 vec_validate (pos->bitmaps, n);
1167 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
1172 vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
1174 uword p_len, pi, *p;
1176 vlib_cli_command_t *c, *parent;
1178 /* Root command (index 0) should have already been added. */
1179 ASSERT (vec_len (cm->commands) > 0);
1181 c = vec_elt_at_index (cm->commands, ci);
1182 p_len = parent_path_len (c->path);
1184 /* No space? Parent is root command. */
1187 add_sub_command (cm, 0, ci);
1192 vec_add (p_path, c->path, p_len);
1194 p = hash_get_mem (cm->command_index_by_path, p_path);
1196 /* Parent exists? */
1199 /* Parent does not exist; create it. */
1200 vec_add2 (cm->commands, parent, 1);
1201 parent->path = p_path;
1202 hash_set_mem (cm->command_index_by_path, parent->path,
1203 parent - cm->commands);
1204 pi = parent - cm->commands;
1212 add_sub_command (cm, pi, ci);
1214 /* Create parent's parent. */
1216 vlib_cli_make_parent (cm, pi);
1220 vlib_cli_command_is_empty (vlib_cli_command_t * c)
1222 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
1226 vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
1228 vlib_cli_main_t *cm = &vm->cli_main;
1229 clib_error_t *error = 0;
1231 char *normalized_path;
1233 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1236 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1238 if (!cm->command_index_by_path)
1239 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
1240 sizeof (c->path[0]),
1243 /* See if command already exists with given path. */
1244 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1247 vlib_cli_command_t *d;
1250 d = vec_elt_at_index (cm->commands, ci);
1252 /* If existing command was created via vlib_cli_make_parent
1253 replaced it with callers data. */
1254 if (vlib_cli_command_is_empty (d))
1256 vlib_cli_command_t save = d[0];
1258 ASSERT (!vlib_cli_command_is_empty (c));
1260 /* Copy callers fields. */
1263 /* Save internal fields. */
1264 d->path = save.path;
1265 d->sub_commands = save.sub_commands;
1266 d->sub_command_index_by_name = save.sub_command_index_by_name;
1267 d->sub_command_positions = save.sub_command_positions;
1268 d->sub_rules = save.sub_rules;
1272 clib_error_return (0, "duplicate command name with path %v",
1275 vec_free (normalized_path);
1281 /* Command does not exist: create it. */
1283 /* Add root command (index 0). */
1284 if (vec_len (cm->commands) == 0)
1286 /* Create command with index 0; path is empty string. */
1287 vec_resize (cm->commands, 1);
1290 ci = vec_len (cm->commands);
1291 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1292 vec_add1 (cm->commands, c[0]);
1294 c = vec_elt_at_index (cm->commands, ci);
1295 c->path = normalized_path;
1297 /* Don't inherit from registration. */
1298 c->sub_commands = 0;
1299 c->sub_command_index_by_name = 0;
1300 c->sub_command_positions = 0;
1303 vlib_cli_make_parent (cm, ci);
1308 vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1310 vlib_cli_main_t *cm = &vm->cli_main;
1311 vlib_cli_parse_rule_t *r;
1312 clib_error_t *error = 0;
1316 if (!cm->parse_rule_index_by_name)
1317 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1318 sizeof (r->name[0]),
1321 /* Make vector copy of name. */
1322 r_name = format (0, "%s", r_reg->name);
1324 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1327 return clib_error_return (0, "duplicate parse rule name `%s'",
1331 vec_add2 (cm->parse_rules, r, 1);
1333 r->name = (char *) r_name;
1334 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1340 /* $$$ turn back on again someday, maybe */
1341 static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1342 vlib_cli_parse_rule_t *
1344 vlib_cli_parse_rule_t *
1346 __attribute__ ((unused))
1348 clib_error_t *error = 0;
1349 vlib_cli_parse_rule_t *r;
1351 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1353 if (!r->name || strlen (r->name) == 0)
1355 error = clib_error_return (0, "parse rule with no name");
1359 error = vlib_cli_register_parse_rule (vm, r);
1370 cli_path_compare (void *a1, void *a2)
1375 if ((vec_len (*s1) < vec_len (*s2)) &&
1376 memcmp ((char *) *s1, (char *) *s2, vec_len (*s1)) == 0)
1380 if ((vec_len (*s1) > vec_len (*s2)) &&
1381 memcmp ((char *) *s1, (char *) *s2, vec_len (*s2)) == 0)
1384 return vec_cmp (*s1, *s2);
1387 static clib_error_t *
1388 show_cli_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1389 vlib_cli_command_t * cmd)
1391 vlib_cli_main_t *cm = &vm->cli_main;
1392 vlib_cli_command_t *cli;
1393 u8 **paths = 0, **s;
1396 vec_foreach (cli, cm->commands)
1397 if (vec_len (cli->path) > 0)
1398 vec_add1 (paths, (u8 *) cli->path);
1400 vec_sort_with_function (paths, cli_path_compare);
1402 vec_foreach (s, paths)
1403 vlib_cli_output (vm, "%v", *s);
1411 VLIB_CLI_COMMAND (show_cli_command, static) = {
1413 .short_help = "Show cli commands",
1414 .function = show_cli_cmd_fn,
1418 static clib_error_t *
1419 vlib_cli_init (vlib_main_t * vm)
1421 vlib_cli_main_t *cm = &vm->cli_main;
1422 clib_error_t *error = 0;
1423 vlib_cli_command_t *cmd;
1425 cmd = cm->cli_command_registrations;
1429 error = vlib_cli_register (vm, cmd);
1432 cmd = cmd->next_cli_command;
1437 VLIB_INIT_FUNCTION (vlib_cli_init);
1440 * fd.io coding-style-patch-verification: ON
1443 * eval: (c-set-style "gnu")