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>
43 /* Root of all show commands. */
45 VLIB_CLI_COMMAND (vlib_cli_show_command, static) = {
47 .short_help = "Show commands",
51 /* Root of all clear commands. */
53 VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = {
55 .short_help = "Clear commands",
59 /* Root of all set commands. */
61 VLIB_CLI_COMMAND (vlib_cli_set_command, static) = {
63 .short_help = "Set commands",
67 /* Root of all test commands. */
69 VLIB_CLI_COMMAND (vlib_cli_test_command, static) = {
71 .short_help = "Test commands",
75 /* Returns bitmap of commands which match key. */
77 vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input)
81 vlib_cli_parse_position_t *p;
83 unformat_skip_white_space (input);
89 k = unformat_get_input (input);
103 case UNFORMAT_END_OF_INPUT:
104 /* White space or end of input removes any non-white
105 matches that were before possible. */
106 if (i < vec_len (c->sub_command_positions)
107 && clib_bitmap_count_set_bits (match) > 1)
109 p = vec_elt_at_index (c->sub_command_positions, i);
110 for (n = 0; n < vec_len (p->bitmaps); n++)
111 match = clib_bitmap_andnot (match, p->bitmaps[n]);
116 unformat_put_input (input);
120 if (i >= vec_len (c->sub_command_positions))
123 clib_bitmap_free (match);
127 p = vec_elt_at_index (c->sub_command_positions, i);
128 if (vec_len (p->bitmaps) == 0)
132 if (n < 0 || n >= vec_len (p->bitmaps))
136 match = clib_bitmap_dup (p->bitmaps[n]);
138 match = clib_bitmap_and (match, p->bitmaps[n]);
140 if (clib_bitmap_is_zero (match))
148 /* Looks for string based sub-input formatted { SUB-INPUT }. */
150 unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
152 unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
158 c = unformat_get_input (i);
170 /* Put back paren. */
171 if (c != UNFORMAT_END_OF_INPUT)
172 unformat_put_input (i);
174 if (c == '{' && unformat (i, "%v", &s))
176 unformat_init_vector (sub_input, s);
185 static vlib_cli_command_t *
186 get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
188 vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
189 return vec_elt_at_index (cm->commands, s->index);
193 unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
195 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
196 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
197 vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
198 vlib_cli_main_t *cm = &vm->cli_main;
199 uword *match_bitmap, is_unique, index;
202 vlib_cli_sub_rule_t *sr;
203 vlib_cli_parse_rule_t *r;
204 vec_foreach (sr, c->sub_rules)
207 r = vec_elt_at_index (cm->parse_rules, sr->rule_index);
208 vec_add2 (cm->parse_rule_data, d, 1);
209 vec_reset_length (d[0]);
211 d[0] = _vec_resize (d[0],
212 /* length increment */ 1,
214 /* header_bytes */ 0,
215 /* data align */ sizeof (uword));
216 if (unformat_user (i, r->unformat_function, vm, d[0]))
218 *result = vec_elt_at_index (cm->commands, sr->command_index);
224 match_bitmap = vlib_cli_sub_command_match (c, i);
225 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
229 index = clib_bitmap_first_set (match_bitmap);
230 *result = get_sub_command (cm, c, index);
232 clib_bitmap_free (match_bitmap);
238 format_vlib_cli_command_help (u8 * s, va_list * args)
240 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
241 int is_long = va_arg (*args, int);
242 if (is_long && c->long_help)
243 s = format (s, "%s", c->long_help);
244 else if (c->short_help)
245 s = format (s, "%s", c->short_help);
247 s = format (s, "%v commands", c->path);
252 format_vlib_cli_parse_rule_name (u8 * s, va_list * args)
254 vlib_cli_parse_rule_t *r = va_arg (*args, vlib_cli_parse_rule_t *);
255 return format (s, "<%U>", format_c_identifier, r->name);
259 format_vlib_cli_path (u8 * s, va_list * args)
261 u8 *path = va_arg (*args, u8 *);
264 for (i = 0; i < vec_len (path); i++)
270 vec_add1 (s, '<'); /* start of <RULE> */
274 /* _ -> space in rules. */
275 vec_add1 (s, in_rule ? ' ' : '_');
281 vec_add1 (s, '>'); /* end of <RULE> */
288 vec_add1 (s, path[i]);
294 vec_add1 (s, '>'); /* terminate <RULE> */
299 static vlib_cli_command_t *
300 all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
302 vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
303 vlib_cli_sub_command_t *sc;
304 vlib_cli_sub_rule_t *sr;
307 vec_add1 (subs, c[0]);
309 vec_foreach (sr, c->sub_rules)
310 subs = all_subs (cm, subs, sr->command_index);
311 vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
317 vlib_cli_cmp_rule (void *a1, void *a2)
319 vlib_cli_sub_rule_t *r1 = a1;
320 vlib_cli_sub_rule_t *r2 = a2;
322 return vec_cmp (r1->name, r2->name);
326 vlib_cli_cmp_command (void *a1, void *a2)
328 vlib_cli_command_t *c1 = a1;
329 vlib_cli_command_t *c2 = a2;
331 return vec_cmp (c1->path, c2->path);
334 static clib_error_t *
335 vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
336 vlib_cli_main_t * cm,
337 unformat_input_t * input,
338 uword parent_command_index)
340 vlib_cli_command_t *parent, *c;
341 clib_error_t *error = 0;
342 unformat_input_t sub_input;
344 uword is_main_dispatch = cm == &vm->cli_main;
346 parent = vec_elt_at_index (cm->commands, parent_command_index);
347 if (is_main_dispatch && unformat (input, "help"))
349 uword help_at_end_of_line, i;
351 help_at_end_of_line =
352 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
357 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
360 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
367 /* help SUB-COMMAND => long format help.
368 "help" at end of line: show all commands. */
369 if (!help_at_end_of_line)
370 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
373 else if (vec_len (c->sub_commands) + vec_len (c->sub_rules) == 0)
374 vlib_cli_output (vm, "%v: no sub-commands", c->path);
378 vlib_cli_sub_command_t *sc;
379 vlib_cli_sub_rule_t *sr, *subs;
381 subs = vec_dup (c->sub_rules);
383 /* Add in rules if any. */
384 vec_foreach (sc, c->sub_commands)
386 vec_add2 (subs, sr, 1);
388 sr->command_index = sc->index;
392 vec_sort_with_function (subs, vlib_cli_cmp_rule);
394 for (i = 0; i < vec_len (subs); i++)
396 vlib_cli_command_t *d;
397 vlib_cli_parse_rule_t *r;
399 d = vec_elt_at_index (cm->commands, subs[i].command_index);
401 subs[i].rule_index != ~0 ? vec_elt_at_index (cm->parse_rules,
409 format_vlib_cli_parse_rule_name, r,
410 format_vlib_cli_command_help, d, /* is_long */ 0);
415 format_vlib_cli_command_help, d, /* is_long */ 0);
422 else if (is_main_dispatch
423 && (unformat (input, "choices") || unformat (input, "?")))
425 vlib_cli_command_t *sub, *subs;
427 subs = all_subs (cm, 0, parent_command_index);
428 vec_sort_with_function (subs, vlib_cli_cmp_command);
429 vec_foreach (sub, subs)
430 vlib_cli_output (vm, " %-40U %U",
431 format_vlib_cli_path, sub->path,
432 format_vlib_cli_command_help, sub, /* is_long */ 0);
436 else if (unformat (input, "comment %v", &string))
441 else if (unformat (input, "uncomment %U",
442 unformat_vlib_cli_sub_input, &sub_input))
445 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
446 parent_command_index);
447 unformat_free (&sub_input);
451 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
453 unformat_input_t *si;
454 uword has_sub_commands =
455 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
458 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
461 if (has_sub_commands)
462 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
464 if (has_sub_commands && !error)
465 /* Found valid sub-command. */ ;
467 else if (c->function)
469 clib_error_t *c_error;
471 /* Skip white space for benefit of called function. */
472 unformat_skip_white_space (si);
474 if (unformat (si, "?"))
476 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
482 vlib_worker_thread_barrier_sync (vm);
484 c_error = c->function (vm, si, c);
487 vlib_worker_thread_barrier_release (vm);
492 clib_error_return (0, "%v: %v", c->path, c_error->what);
493 clib_error_free (c_error);
494 /* Free sub input. */
502 /* Free any previous error. */
503 clib_error_free (error);
507 error = clib_error_return (0, "%v: no sub-commands", c->path);
509 /* Free sub input. */
521 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
522 format_unformat_error, input);
524 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
529 void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
530 __attribute__ ((weak));
533 vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
537 /* Process CLI input. */
539 vlib_cli_input (vlib_main_t * vm,
540 unformat_input_t * input,
541 vlib_cli_output_function_t * function, uword function_arg)
543 vlib_process_t *cp = vlib_get_current_process (vm);
544 vlib_cli_main_t *cm = &vm->cli_main;
546 vlib_cli_output_function_t *save_function;
547 uword save_function_arg;
549 save_function = cp->output_function;
550 save_function_arg = cp->output_function_arg;
552 cp->output_function = function;
553 cp->output_function_arg = function_arg;
557 vec_reset_length (cm->parse_rule_data);
558 error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input, /* parent */
561 while (!error && !unformat (input, "%U", unformat_eof));
565 vlib_cli_output (vm, "%v", error->what);
566 vlib_unix_error_report (vm, error);
567 clib_error_free (error);
570 cp->output_function = save_function;
571 cp->output_function_arg = save_function_arg;
574 /* Output to current CLI connection. */
576 vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
578 vlib_process_t *cp = vlib_get_current_process (vm);
583 s = va_format (0, fmt, &va);
586 /* Terminate with \n if not present. */
587 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
590 if ((!cp) || (!cp->output_function))
591 fformat (stdout, "%v", s);
593 cp->output_function (cp->output_function_arg, s, vec_len (s));
598 static clib_error_t *
599 show_memory_usage (vlib_main_t * vm,
600 unformat_input_t * input, vlib_cli_command_t * cmd)
606 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
608 if (unformat (input, "verbose"))
612 error = clib_error_return (0, "unknown input `%U'",
613 format_unformat_error, input);
621 vlib_cli_output (vm, "Thread %d %v\n", index, vlib_worker_threads[index].name);
622 vlib_cli_output (vm, "%U\n", format_mheap, clib_per_cpu_mheaps[index], verbose);
630 VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
631 .path = "show memory",
632 .short_help = "Show current memory usage",
633 .function = show_memory_usage,
637 static clib_error_t *
638 show_cpu (vlib_main_t * vm, unformat_input_t * input,
639 vlib_cli_command_t * cmd)
641 #define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
642 _("Model name", "%U", format_cpu_model_name);
643 _("Microarchitecture", "%U", format_cpu_uarch);
644 _("Flags", "%U", format_cpu_flags);
645 _("Base frequency", "%.2f GHz",
646 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
652 * Displays various information about the CPU.
655 * @cliexstart{show cpu}
656 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
657 * Microarchitecture: Broadwell (Broadwell-EP/EX)
658 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
659 * Base Frequency: 3.20 GHz
663 VLIB_CLI_COMMAND (show_cpu_command, static) = {
665 .short_help = "Show cpu information",
666 .function = show_cpu,
670 static clib_error_t *
671 enable_disable_memory_trace (vlib_main_t * vm,
672 unformat_input_t * input,
673 vlib_cli_command_t * cmd)
675 clib_error_t *error = 0;
678 if (!unformat_user (input, unformat_vlib_enable_disable, &enable))
680 error = clib_error_return (0, "expecting enable/on or disable/off");
684 clib_mem_trace (enable);
691 VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
692 .path = "memory-trace",
693 .short_help = "Enable/disable memory allocation trace",
694 .function = enable_disable_memory_trace,
699 static clib_error_t *
700 test_heap_validate (vlib_main_t * vm, unformat_input_t * input,
701 vlib_cli_command_t * cmd)
703 clib_error_t *error = 0;
707 if (unformat (input, "on"))
711 heap = clib_per_cpu_mheaps[this_vlib_main->cpu_index];
712 mheap = mheap_header(heap);
713 mheap->flags |= MHEAP_FLAG_VALIDATE;
714 // Turn off small object cache because it delays detection of errors
715 mheap->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
720 else if (unformat (input, "off"))
724 heap = clib_per_cpu_mheaps[this_vlib_main->cpu_index];
725 mheap = mheap_header(heap);
726 mheap->flags &= ~MHEAP_FLAG_VALIDATE;
727 mheap->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
731 else if (unformat (input, "now"))
735 heap = clib_per_cpu_mheaps[this_vlib_main->cpu_index];
736 mheap = mheap_header(heap);
737 mheap_validate(heap);
740 vlib_cli_output (vm, "heap validation complete");
745 return clib_error_return (0, "unknown input `%U'",
746 format_unformat_error, input);
753 VLIB_CLI_COMMAND (cmd_test_heap_validate,static) = {
754 .path = "test heap-validate",
755 .short_help = "<on/off/now> validate heap on future allocs/frees or right now",
756 .function = test_heap_validate,
762 * A trivial test harness to verify the per-process output_function
763 * is working correcty.
766 static clib_error_t *
767 sleep_ten_seconds (vlib_main_t * vm,
768 unformat_input_t * input, vlib_cli_command_t * cmd)
773 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
775 for (i = 0; i < 10; i++)
777 vlib_process_wait_for_event_or_clock (vm, 1.0);
778 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
780 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
785 VLIB_CLI_COMMAND (ping_command, static) = {
786 .path = "test sleep",
787 .function = sleep_ten_seconds,
788 .short_help = "Sleep for 10 seconds",
791 #endif /* ifdef TEST_CODE */
794 vlib_cli_normalize_path (char *input, char **result)
799 uword index_of_last_space = ~0;
804 /* Multiple white space -> single space. */
811 if (l > 0 && s[l - 1] != ' ')
819 if (l > 0 && s[l - 1] == ' ')
820 index_of_last_space = vec_len (s);
827 /* Remove any extra space at end. */
828 if (l > 0 && s[l - 1] == ' ')
832 return index_of_last_space;
836 parent_path_len (char *path)
839 for (i = vec_len (path) - 1; i >= 0; i--)
848 add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
850 vlib_cli_command_t *p, *c;
851 vlib_cli_sub_command_t *sub_c;
855 p = vec_elt_at_index (cm->commands, parent_index);
856 c = vec_elt_at_index (cm->commands, child_index);
858 l = parent_path_len (c->path);
860 sub_name = vec_dup ((u8 *) c->path);
863 ASSERT (l + 1 < vec_len (c->path));
865 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
868 if (sub_name[0] == '%')
871 vlib_cli_sub_rule_t *sr;
874 vec_delete (sub_name, 1, 0);
876 if (!p->sub_rule_index_by_name)
877 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
878 sizeof (sub_name[0]),
880 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
883 sr = vec_elt_at_index (p->sub_rules, q[0]);
884 ASSERT (sr->command_index == child_index);
888 q = hash_get_mem (cm->parse_rule_index_by_name, sub_name);
891 clib_error ("reference to unknown rule `%%%v' in path `%v'",
896 hash_set_mem (p->sub_rule_index_by_name, sub_name,
897 vec_len (p->sub_rules));
898 vec_add2 (p->sub_rules, sr, 1);
900 sr->rule_index = q[0];
901 sr->command_index = child_index;
905 if (!p->sub_command_index_by_name)
906 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
910 /* Check if sub-command has already been created. */
911 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
917 vec_add2 (p->sub_commands, sub_c, 1);
918 sub_c->index = child_index;
919 sub_c->name = sub_name;
920 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
921 sub_c - p->sub_commands);
923 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
924 for (i = 0; i < vec_len (sub_c->name); i++)
927 vlib_cli_parse_position_t *pos;
929 pos = vec_elt_at_index (p->sub_command_positions, i);
932 pos->min_char = sub_c->name[i];
934 n = sub_c->name[i] - pos->min_char;
937 pos->min_char = sub_c->name[i];
938 vec_insert (pos->bitmaps, -n, 0);
942 vec_validate (pos->bitmaps, n);
944 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
949 vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
953 vlib_cli_command_t *c, *parent;
955 /* Root command (index 0) should have already been added. */
956 ASSERT (vec_len (cm->commands) > 0);
958 c = vec_elt_at_index (cm->commands, ci);
959 p_len = parent_path_len (c->path);
961 /* No space? Parent is root command. */
964 add_sub_command (cm, 0, ci);
969 vec_add (p_path, c->path, p_len);
971 p = hash_get_mem (cm->command_index_by_path, p_path);
976 /* Parent does not exist; create it. */
977 vec_add2 (cm->commands, parent, 1);
978 parent->path = p_path;
979 hash_set_mem (cm->command_index_by_path, parent->path,
980 parent - cm->commands);
981 pi = parent - cm->commands;
989 add_sub_command (cm, pi, ci);
991 /* Create parent's parent. */
993 vlib_cli_make_parent (cm, pi);
997 vlib_cli_command_is_empty (vlib_cli_command_t * c)
999 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
1003 vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
1005 vlib_cli_main_t *cm = &vm->cli_main;
1006 clib_error_t *error = 0;
1008 char *normalized_path;
1010 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1013 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1015 if (!cm->command_index_by_path)
1016 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
1017 sizeof (c->path[0]),
1020 /* See if command already exists with given path. */
1021 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1024 vlib_cli_command_t *d;
1027 d = vec_elt_at_index (cm->commands, ci);
1029 /* If existing command was created via vlib_cli_make_parent
1030 replaced it with callers data. */
1031 if (vlib_cli_command_is_empty (d))
1033 vlib_cli_command_t save = d[0];
1035 ASSERT (!vlib_cli_command_is_empty (c));
1037 /* Copy callers fields. */
1040 /* Save internal fields. */
1041 d->path = save.path;
1042 d->sub_commands = save.sub_commands;
1043 d->sub_command_index_by_name = save.sub_command_index_by_name;
1044 d->sub_command_positions = save.sub_command_positions;
1045 d->sub_rules = save.sub_rules;
1049 clib_error_return (0, "duplicate command name with path %v",
1052 vec_free (normalized_path);
1058 /* Command does not exist: create it. */
1060 /* Add root command (index 0). */
1061 if (vec_len (cm->commands) == 0)
1063 /* Create command with index 0; path is empty string. */
1064 vec_resize (cm->commands, 1);
1067 ci = vec_len (cm->commands);
1068 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1069 vec_add1 (cm->commands, c[0]);
1071 c = vec_elt_at_index (cm->commands, ci);
1072 c->path = normalized_path;
1074 /* Don't inherit from registration. */
1075 c->sub_commands = 0;
1076 c->sub_command_index_by_name = 0;
1077 c->sub_command_positions = 0;
1080 vlib_cli_make_parent (cm, ci);
1085 vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1087 vlib_cli_main_t *cm = &vm->cli_main;
1088 vlib_cli_parse_rule_t *r;
1089 clib_error_t *error = 0;
1093 if (!cm->parse_rule_index_by_name)
1094 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1095 sizeof (r->name[0]),
1098 /* Make vector copy of name. */
1099 r_name = format (0, "%s", r_reg->name);
1101 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1104 return clib_error_return (0, "duplicate parse rule name `%s'",
1108 vec_add2 (cm->parse_rules, r, 1);
1110 r->name = (char *) r_name;
1111 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1117 /* $$$ turn back on again someday, maybe */
1118 static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1119 vlib_cli_parse_rule_t *
1121 vlib_cli_parse_rule_t *
1123 __attribute__ ((unused))
1125 clib_error_t *error = 0;
1126 vlib_cli_parse_rule_t *r;
1128 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1130 if (!r->name || strlen (r->name) == 0)
1132 error = clib_error_return (0, "parse rule with no name");
1136 error = vlib_cli_register_parse_rule (vm, r);
1146 static clib_error_t *
1147 vlib_cli_init (vlib_main_t * vm)
1149 vlib_cli_main_t *cm = &vm->cli_main;
1150 clib_error_t *error = 0;
1151 vlib_cli_command_t *cmd;
1153 cmd = cm->cli_command_registrations;
1157 error = vlib_cli_register (vm, cmd);
1160 cmd = cmd->next_cli_command;
1165 VLIB_INIT_FUNCTION (vlib_cli_init);
1168 * fd.io coding-style-patch-verification: ON
1171 * eval: (c-set-style "gnu")