File: cli_command.cc

package info (click to toggle)
xorp 1.8.5-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,560 kB
  • ctags: 54,995
  • sloc: cpp: 397,204; sh: 17,490; ansic: 17,029; python: 7,643; lex: 1,632; yacc: 1,474; awk: 956; makefile: 251; perl: 217; sed: 33
file content (911 lines) | stat: -rw-r--r-- 25,159 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
// -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-

// Copyright (c) 2001-2011 XORP, Inc and Others
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, Version 2, June
// 1991 as published by the Free Software Foundation. Redistribution
// and/or modification of this program under the terms of any other
// version of the GNU General Public License is not permitted.
// 
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
// see the GNU General Public License, Version 2, a copy of which can be
// found in the XORP LICENSE.gpl file.
// 
// XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
// http://xorp.net




//
// CLI (Command-Line Interface) commands structuring implementation
//


#include "cli_module.h"
#include "libxorp/xorp.h"
#include "libxorp/xlog.h"
#include "libxorp/debug.h"
#include "libxorp/ipvx.hh"
#include "libxorp/token.hh"
#include "libxorp/utils.hh"

#include "cli_command_pipe.hh"


//
// Exported variables
//

//
// Local constants definitions
//

//
// Local structures/classes, typedefs and macros
//


//
// Local variables
//

static string EXECUTE_THIS_COMMAND_STRING =
    "<[Enter]>            Execute this command\r\n";

//
// Local functions prototypes
//


CliCommand::CliCommand(CliCommand *init_parent_command,
		       const string& init_command_name,
		       const string& init_command_help)
    : _parent_command(init_parent_command),
      _name(init_command_name),
      _help(init_command_help),
      _default_nomore_mode(false),
      _is_command_argument(false),
      _is_argument_expected(false)
{
    if (_parent_command != NULL)
	_root_command = _parent_command->root_command();
    else
	_root_command = this;
    
    set_allow_cd(false, "");
    set_can_pipe(false);	// XXX: default
    set_cli_command_pipe(NULL);
    
    // Set the command-completion help string
    // TODO: parameterize the hard-coded number
    _help_completion = c_format(" %*s%s\r\n", (int)(20 - _name.size()), " ",
				_help.c_str());

    // XXX: set the CLI completion function to its default value
    set_cli_completion_func(cli_attempt_command_completion_byname);
    
    _has_dynamic_children = false;
}

CliCommand::~CliCommand()
{
    // Delete recursively all child commands
    delete_pointers_list(_child_command_list);
    delete_pipes();
}

//
// Enable/disable "cd" to this command, and set the "cd prompt"
//
void
CliCommand::set_allow_cd(bool v, const string& init_cd_prompt)
{
    _allow_cd = v;
    if (init_cd_prompt.size())
	_cd_prompt = init_cd_prompt;
}

//
// Return %XORP_OK, on success, otherwise %XORP_ERROR
//
int
CliCommand::add_command(CliCommand *child_command, string& error_msg)
{
    list<CliCommand *>::iterator iter, insert_pos;
    
    insert_pos = child_command_list().begin();
    
    // Check if command already installed, as well as find the
    // position to install the command (based on lexicographical ordering).
    for (iter = child_command_list().begin();
	 iter != child_command_list().end();
	 ++iter) {
	CliCommand *cli_command = *iter;
	if (cli_command->is_same_command(child_command->name())) {
	    // Command already installed
	    error_msg = c_format("Command '%s' already installed",
				 child_command->name().c_str());
	    XLOG_ERROR("%s", error_msg.c_str());
	    return (XORP_ERROR);
	}
	if (cli_command->name() < child_command->name()) {
	    insert_pos = iter;
	    ++insert_pos;
	}
    }
    
    if (insert_pos == child_command_list().end())
	_child_command_list.push_back(child_command);
    else
	_child_command_list.insert(insert_pos, child_command);
    child_command->set_root_command(this->root_command());
    
    return (XORP_OK);
}

//
// Create a new command.
// Return the new child command on success, otherwise NULL.
// XXX: By default, we CANNOT "cd" to this command.
// XXX: If @is_multilevel_command is true, then @init_command_name can
// include more than one command levels in the middle.
// E.g. "show version pim". However, commands "show" and "show version" must
// have been installed first.
//
CliCommand *
CliCommand::add_command(const string& init_command_name,
			const string& init_command_help,
			bool is_multilevel_command,
			string& error_msg)
{
    CliCommand *parent_cli_command = NULL;
    CliCommand *cli_command = NULL;
    vector<string> command_tokens;
    string token;
    string token_line = init_command_name;
    string command_name_string;
    
    if (is_multilevel_command) {
	// Create a vector of all takens in the command
	for (token = pop_token(token_line);
	     ! token.empty();
	     token = pop_token(token_line)) {
	    command_tokens.push_back(token);
	}
    } else {
	if (token_line.empty()) {
	    error_msg = c_format("Empty token line for command %s",
				 init_command_name.c_str());
	    return (NULL);
	}
	command_tokens.push_back(token_line);
    }

    if (command_tokens.empty()) {
	error_msg = c_format("Empty command tokens for command %s",
			     init_command_name.c_str());
	return (NULL);
    }
    command_name_string = command_tokens[command_tokens.size() - 1];
    
    // Traverse all tokens and find the parent command where to install
    // the new command
    parent_cli_command = this;
    for (size_t i = 0; i < command_tokens.size() - 1; i++) {
	parent_cli_command = parent_cli_command->command_find(command_tokens[i]);
	if (parent_cli_command == NULL)
	    break;
    }
    if (parent_cli_command == NULL) {
	error_msg = c_format("Cannot find parent command");
	goto error_label_missing;
    }
    
    cli_command = new CliCommand(parent_cli_command,
				 command_name_string,
				 init_command_help);
    
    if (parent_cli_command->add_command(cli_command, error_msg) != XORP_OK) {
	delete cli_command;
	goto error_label_failed;
    }
    
    cli_command->set_allow_cd(false, "");
    
    return (cli_command);
    
 error_label_missing:
    error_msg = c_format("Error installing '%s' on non-existent node '%s': %s",
			 init_command_name.c_str(),
			 (this->name().size() > 0) ? this->name().c_str() : "<ROOT>",
			 error_msg.c_str());
    XLOG_ERROR("%s", error_msg.c_str());
    return (NULL);		// Invalid path to the command
 error_label_failed:
    error_msg = c_format("Error installing '%s' on '%s': %s",
			 init_command_name.c_str(),
			 (this->name().size() > 0) ? this->name().c_str() : "<ROOT>",
			 error_msg.c_str());
    XLOG_ERROR("%s", error_msg.c_str());
    return (NULL);		// Invalid path to the command
}

//
// Create a command and assign a processing callback to it.
// Return the new child command on success, otherwise NULL.
//
CliCommand *
CliCommand::add_command(const string& init_command_name,
			const string& init_command_help,
			bool is_multilevel_command,
			const CLI_PROCESS_CALLBACK& init_cli_process_callback,
			string& error_msg)
{
    CliCommand *cli_command = add_command(init_command_name,
					  init_command_help,
					  is_multilevel_command,
					  error_msg);
    
    if (cli_command == NULL)
	return (NULL);
    cli_command->set_cli_process_callback(init_cli_process_callback);
    cli_command->set_allow_cd(false, "");
    if (! init_cli_process_callback.is_empty()) {
	// XXX: by default, enable pipe processing if there is a callback func
	cli_command->set_can_pipe(true);
    }
    
    return (cli_command);
}

//
// Create a command and assign a processing and an interrupt callbacks to it.
// Return the new child command on success, otherwise NULL.
//
CliCommand *
CliCommand::add_command(const string& init_command_name,
			const string& init_command_help,
			bool is_multilevel_command,
			const CLI_PROCESS_CALLBACK& init_cli_process_callback,
			const CLI_INTERRUPT_CALLBACK& init_cli_interrupt_callback,
			string& error_msg)
{
    CliCommand *cli_command = add_command(init_command_name,
					  init_command_help,
					  is_multilevel_command,
					  init_cli_process_callback,
					  error_msg);
    
    if (cli_command == NULL)
	return (NULL);
    cli_command->set_cli_interrupt_callback(init_cli_interrupt_callback);
    
    return (cli_command);
}

//
// Return the new child command on success, otherwise NULL.
// Setup a command we can "cd" to. If @cd_prompt is not-NULL,
// then the CLI prompt will be set to @cd_prompt;
// otherwise, it will remain unchanged.
//
CliCommand *
CliCommand::add_command(const string& init_command_name,
			const string& init_command_help,
			const string& init_cd_prompt,
			bool is_multilevel_command,
			string& error_msg)
{
    CliCommand *cli_command = add_command(init_command_name,
					  init_command_help,
					  is_multilevel_command,
					  error_msg);
    
    if (cli_command == NULL)
	return (NULL);
    cli_command->set_allow_cd(true, init_cd_prompt);
    
    return (cli_command);
}


//
// Delete a command, and all sub-commands below it
// Return: %XORP_OK on success, otherwise %XORP_ERROR
//
int
CliCommand::delete_command(CliCommand *child_command)
{
    list<CliCommand *>::iterator iter;
    
    iter = find(_child_command_list.begin(),
		_child_command_list.end(),
		child_command);
    if (iter == _child_command_list.end())
	return (XORP_ERROR);
    
    _child_command_list.erase(iter);
    delete child_command;
    
    return (XORP_OK);
}

//
// Delete a command, and all sub-commands below it
// Return: %XORP_OK on success, otherwise %XORP_ERROR
// XXX: @delete_command_name can be the full path-name for that command
//
int
CliCommand::delete_command(const string& delete_command_name)
{
    CliCommand *parent_cli_command = NULL;
    CliCommand *delete_cli_command = NULL;
    vector<string> command_tokens;
    string token;
    string token_line = delete_command_name;
    
    // Create a vector of all takens in the command
    for (token = pop_token(token_line);
	 ! token.empty();
	 token = pop_token(token_line)) {
	command_tokens.push_back(token);
    }
    if (command_tokens.empty())
	return (XORP_ERROR);
    
    // Traverse all tokens and find the command to delete
    parent_cli_command = this;
    delete_cli_command = NULL;
    for (size_t i = 0; i < command_tokens.size(); i++) {
	if (delete_cli_command != NULL)
	    parent_cli_command = delete_cli_command;
	delete_cli_command = parent_cli_command->command_find(command_tokens[i]);
	if (delete_cli_command == NULL)
	    break;
    }
    if (delete_cli_command == NULL)
	goto error_label;
    
    if (parent_cli_command->delete_command(delete_cli_command) != XORP_OK)
	goto error_label;
    return (XORP_OK);
    
 error_label:
    XLOG_ERROR("Error deleting %s on %s", delete_command_name.c_str(),
	       this->name().c_str());
    return (XORP_ERROR);
}

// Recursively delete all the children of this command.
void
CliCommand::delete_all_commands() 
{
    list <CliCommand*>::iterator iter;
    for (iter = _child_command_list.begin();
	 iter != _child_command_list.end(); ++iter) {
	(*iter)->delete_all_commands();
	delete *iter;
    }
    while (! _child_command_list.empty())
	_child_command_list.pop_front();
}

/**
 * CliCommand::create_default_cli_commands:
 * @: 
 * 
 * Create the default CLI commands at each level of the command tree.
 * 
 * Return value: %XORP_OK on success, otherwise %XORP_ERROR.
 **/
int
CliCommand::create_default_cli_commands()
{
    // TODO: add commands like "help", "list" (all available subcommands, etc
    
    return (XORP_OK);
}

/**
 * CliCommand::add_pipes:
 * @: 
 * 
 * Create and add the default CLI pipe commands.
 * 
 * Return value: %XORP_OK on success, otherwise %XORP_ERROR.
 **/
int
CliCommand::add_pipes(string& error_msg)
{
    CliPipe *cli_pipe;
    CliCommand *com0;
    
    com0 = new CliCommand(this, "|", "Pipe through a command");
    // com0 = add_command("|", "Pipe through a command", false, error_msg);
    if (com0 == NULL) {
	return (XORP_ERROR);
    }
    delete_pipes(); // be sure to not leak memory if one is already set.
    set_cli_command_pipe(com0);
    
    cli_pipe = new CliPipe("count");
    if (com0->add_command(cli_pipe, error_msg) != XORP_OK) {
	delete_pipes();
	return (XORP_ERROR);
    }
    cli_pipe = new CliPipe("except");
    if (com0->add_command(cli_pipe, error_msg) != XORP_OK) {
	delete_pipes();
	return (XORP_ERROR);
    }
    cli_pipe = new CliPipe("find");
    if (com0->add_command(cli_pipe, error_msg) != XORP_OK) {
	delete_pipes();
	return (XORP_ERROR);
    }
    cli_pipe = new CliPipe("hold");
    if (com0->add_command(cli_pipe, error_msg) != XORP_OK) {
	delete_pipes();
	return (XORP_ERROR);
    }
    cli_pipe = new CliPipe("match");
    if (com0->add_command(cli_pipe, error_msg) != XORP_OK) {
	delete_pipes();
	return (XORP_ERROR);
    }
    cli_pipe = new CliPipe("no-more");
    if (com0->add_command(cli_pipe, error_msg) != XORP_OK) {
	delete_pipes();
	return (XORP_ERROR);
    }
    cli_pipe = new CliPipe("resolve");
    if (com0->add_command(cli_pipe, error_msg) != XORP_OK) {
	delete_pipes();
	return (XORP_ERROR);
    }
    cli_pipe = new CliPipe("save");
    if (com0->add_command(cli_pipe, error_msg) != XORP_OK) {
	delete_pipes();
	return (XORP_ERROR);
    }
    cli_pipe = new CliPipe("trim");
    if (com0->add_command(cli_pipe, error_msg) != XORP_OK) {
	delete_pipes();
	return (XORP_ERROR);
    }
    
    return (XORP_OK);
}

/**
 * CliCommand::delete_pipes:
 * @: 
 * 
 * Delete the default CLI pipe commands.
 * 
 * Return value: %XORP_OK on success, otherwise %XORP_ERROR.
 **/
int
CliCommand::delete_pipes()
{
    if (_cli_command_pipe != NULL)
	delete _cli_command_pipe;
    
    return (XORP_OK);
    // return (delete_command("|"));
}

//
// Attempts to complete the current command.
// Return: true if the attempt was successful, otherwise false.
//
bool
CliCommand::cli_attempt_command_completion_byname(void *obj,
						  WordCompletion *cpl,
						  void *data,
						  const char *line,
						  int word_end,
						  list<CliCommand *>& cli_command_match_list)
{
    CliCommand *cli_command = reinterpret_cast<CliCommand*>(obj);
    int word_start = 0;		// XXX: complete from the beginning of 'line'
    const char *type_suffix = NULL;
    const char *cont_suffix = " "; // XXX: a space after a command is completed
    string token, token_line;
    const string name_string = cli_command->name();
    bool is_command_completed;	// 'true' if complete command typed
    
    if ((cpl == NULL) || (line == NULL) || (word_end < 0)) {
	return (false);
    }
    
    token_line = string(line, word_end);
    token = pop_token(token_line);
    if ((! cli_command->is_same_prefix(token))
	&& (! cli_command->has_type_match_cb())) {
	    return (false);
    }
    
    if (token_line.length()
	&& (is_token_separator(token_line[0]) || (token == "|")))
	is_command_completed = true;
    else
	is_command_completed = false;
    
    // Check if a potential sub-prefix
    if (! is_command_completed) {
	string name_complete;

	if (cli_command->has_type_match_cb()) {
	    //
	    // XXX: Nothing to complete, we just need to print
	    // the help with the command type.
	    //
	    cli_command_match_list.push_back(cli_command);
	    return (true);
	}

	name_complete = name_string.substr(token.length());
	if (cli_command->help_completion().size() > 0)
	    type_suffix = cli_command->help_completion().c_str();
	
	// Add two empty spaces in front
	string line_string = "  ";
	if (token.empty()) {
	    // XXX: ignore the rest of the empty spaces
	    word_end = 0;
	} else {
	    line_string += line;
	}
	cpl_add_completion(cpl, line_string.c_str(), word_start, word_end + 2,
			   name_complete.c_str(),
			   type_suffix, cont_suffix);
	cli_command_match_list.push_back(cli_command);
	return (true);
    }
    
    // Must be a complete command

    bool is_token_match = false;
    if (cli_command->has_type_match_cb()) {
	string errmsg;
	is_token_match = cli_command->type_match_cb()->dispatch(token, errmsg);
    } else {
	is_token_match = cli_command->is_same_command(token);
    }
    if (! is_token_match) {
	return (false);
    }
    
    bool is_child_completion = false;
    
    if (cli_command->can_complete()
	&& (! has_more_tokens(token_line))
	&& (! cli_command->is_argument_expected())) {
	// Add the appropriate completion info if the command can be run
	string line_string1 = "  ";
	type_suffix = EXECUTE_THIS_COMMAND_STRING.c_str();
	cpl_add_completion(cpl, line_string1.c_str(), word_start,
			   line_string1.size(),
			   "",
			   type_suffix, cont_suffix);
	is_child_completion = true;
    }
    
    if (cli_command->can_pipe() && (cli_command->cli_command_pipe() != NULL)) {
	// Add the pipe completions
	if (cli_command->_cli_completion_func(cli_command->cli_command_pipe(),
					      cpl,
					      data,
					      token_line.c_str(),
					      token_line.length(),
					      cli_command_match_list)) {
	    is_child_completion = true;
	}
    }

    // Add completions for the sub-commands (if any)
    list<CliCommand *>::iterator iter;
    for (iter = cli_command->child_command_list().begin();
	 iter != cli_command->child_command_list().end();
	 ++iter) {
	CliCommand *cli_command_child = *iter;
	if (! cli_command_child->has_cli_completion_func())
	    continue;
	if (cli_command_child->_cli_completion_func(cli_command_child,
						    cpl,
						    data,
						    token_line.c_str(),
						    token_line.length(),
						    cli_command_match_list)) {
	    is_child_completion = true;
	}
    }
    
    return (is_child_completion);
}

CliCommand *
CliCommand::command_find(const string& token)
{
    list<CliCommand *>::iterator iter;
    
    for (iter = child_command_list().begin();
	 iter != child_command_list().end();
	 ++iter) {
	CliCommand *cli_command = *iter;
	if (cli_command->has_type_match_cb()) {
	    string errmsg;
	    if (cli_command->type_match_cb()->dispatch(token, errmsg))
		return (cli_command);
	    continue;
	}
	if (cli_command->is_same_command(token)) {
	    // XXX: assume that no two subcommands have the same name
	    return (cli_command);
	}
    }
    
    return (NULL);
}

//
// Test if the command line is a prefix for a multi-level command.
// Note that if there is an exact match, then the return value is false.
//
bool
CliCommand::is_multi_command_prefix(const string& command_line)
{
    string token;
    string token_line = command_line;
    CliCommand *parent_cli_command = this;
    CliCommand *child_cli_command = NULL;

    for (token = pop_token(token_line);
	 ! token.empty();
	 token = pop_token(token_line)) {
	child_cli_command = parent_cli_command->command_find(token);
	if (child_cli_command != NULL) {
	    parent_cli_command = child_cli_command;
	    continue;
	}

	// Test if the token is a prefix for a child command
	list<CliCommand *>::const_iterator iter;
	for (iter = parent_cli_command->child_command_list().begin();
	     iter != parent_cli_command->child_command_list().end();
	     ++iter) {
	    child_cli_command = *iter;
	    if (child_cli_command->is_same_prefix(token))
		return true;
	}

	break;
    }

    return (false);
}

// Tests if the string in @token can be a prefix for this command
bool
CliCommand::is_same_prefix(const string& token)
{
    size_t s = token.length();
    
    if (s > _name.length())
	return (false);
    
    return (token.substr(0, s) == _name.substr(0, s));
}

// Tests if the string in @token matches this command
bool
CliCommand::is_same_command(const string& token)
{
    return (token == _name);
}

bool
CliCommand::find_command_help(const char *line, int word_end,
			      set<string>& help_strings)
{
    string token, token_line;
    bool ret_value = false;
    bool is_no_space_at_end;
    
    if ((line == NULL) || (word_end < 0)) {
        return (false);
    }
    
    token_line = string(line, word_end);
    token = pop_token(token_line);

    if ((! is_same_prefix(token)) && (! has_type_match_cb()))
	return (false);

    //
    // Test if the token matches the command
    //
    bool is_token_match = false;
    if (has_type_match_cb()) {
	string errmsg;
	is_token_match = type_match_cb()->dispatch(token, errmsg);
    } else {
	is_token_match = is_same_command(token);
    }

    if (token_line.length()
	&& is_token_separator(token_line[0])
	&& (! is_token_match)) {
	// Not a match
	return (false);
    }
    
    is_no_space_at_end = (token_line.empty()) ? (true) : (false);
    
    // Get the token for the child's command (if any)
    token = pop_token(token_line);
    
    if ((token.length() == 0) && is_no_space_at_end) {
	// The last token, and there is no space, so print my help.
	help_strings.insert(c_format("  %-19s  %s\r\n",
				     name().c_str(), help().c_str()));
	return (true);
    }
    
    if ((token.length() == 0)
	&& can_complete()
	&& (! is_argument_expected())) {
	// The last token, and there is space at the end,
	// so print the "default" help.
	help_strings.insert(c_format("  %-19s  %s\r\n",
				     "<[Enter]>", "Execute this command"));
	ret_value = true;
    }
    
    // Not the last token, so search down for help
    list<CliCommand *>::iterator iter;    
    for (iter = child_command_list().begin();
	 iter != child_command_list().end();
	 ++iter) {
	CliCommand *cli_command = *iter;
	string tmp_token_line = copy_token(token) + token_line;
	ret_value |= cli_command->find_command_help(tmp_token_line.c_str(),
						    tmp_token_line.length(),
						    help_strings);
    }
    
    if (can_pipe() && (cli_command_pipe() != NULL)) {
	// Add the pipe completions
	string tmp_token_line = copy_token(token) + token_line;
	ret_value |= cli_command_pipe()->find_command_help(
	    tmp_token_line.c_str(),
	    tmp_token_line.length(),
	    help_strings);
    }
    
    return (ret_value);
}

bool
CliCommand::can_complete()
{
    if (has_cli_process_callback() || allow_cd())
	return true;
    return false;
}

CliCommand *
CliCommand::cli_command_pipe()
{
    if (_root_command != this)
	return (_root_command->cli_command_pipe());
    else
	return (_cli_command_pipe);
}

void
CliCommand::set_dynamic_children_callback(DYNAMIC_CHILDREN_CALLBACK v)
{
    XLOG_ASSERT(!_global_name.empty());
    _dynamic_children_callback = v;
    _has_dynamic_children = true;
}

bool
CliCommand::has_dynamic_process_callback()
{
    return (!_dynamic_process_callback.is_empty());
}

bool
CliCommand::has_dynamic_interrupt_callback()
{
    return (!_dynamic_interrupt_callback.is_empty());
}

bool
CliCommand::has_cli_process_callback()
{
    if (_has_dynamic_children) {
	//
	// Force the children to be evaluated, which forces the
	// cli_process_callback to be set.
	//
	child_command_list();
    }
    return (!_cli_process_callback.is_empty());
}

bool
CliCommand::has_cli_interrupt_callback()
{
    return (!_cli_interrupt_callback.is_empty());
}

bool
CliCommand::has_type_match_cb() const
{
    return (! _type_match_cb.is_empty());
}

list<CliCommand *>&
CliCommand::child_command_list()
{
    string error_msg;

    if (_has_dynamic_children)
	XLOG_ASSERT(_child_command_list.empty());

    // Handle dynamic children generation
    if (_child_command_list.empty() && _has_dynamic_children) {
	// Now we've run this, we won't need to run it again.
	_has_dynamic_children = false;

	// Add dynamic children
	XLOG_ASSERT(global_name().size() > 0);
	map<string, CliCommandMatch> dynamic_children;
	map<string, CliCommandMatch>::iterator iter;
	dynamic_children = _dynamic_children_callback->dispatch(global_name());
	CliCommand *new_cmd;
	for (iter = dynamic_children.begin();
	     iter != dynamic_children.end();
	     ++iter) {
	    const CliCommandMatch& ccm = iter->second;
	    const string& command_name = ccm.command_name();
	    const string& help_string = ccm.help_string();
	    bool is_executable = ccm.is_executable();
	    bool can_pipe = ccm.can_pipe();
	    bool default_nomore_mode = ccm.default_nomore_mode();
	    bool is_command_argument = ccm.is_command_argument();
	    bool is_argument_expected = ccm.is_argument_expected();
	    new_cmd = add_command(command_name, help_string, false, error_msg);
	    if (new_cmd == NULL) {
		XLOG_FATAL("Cannot add command '%s' to parent '%s': %s",
			   command_name.c_str(), name().c_str(),
			   error_msg.c_str());
	    }
	    vector<string> child_global_name = global_name();
	    child_global_name.push_back(command_name);
	    new_cmd->set_global_name(child_global_name);
	    new_cmd->set_can_pipe(can_pipe);
	    new_cmd->set_default_nomore_mode(default_nomore_mode);
	    new_cmd->set_is_command_argument(is_command_argument);
	    new_cmd->set_is_argument_expected(is_argument_expected);
	    new_cmd->set_type_match_cb(ccm.type_match_cb());
	    new_cmd->set_dynamic_children_callback(_dynamic_children_callback);
	    new_cmd->set_dynamic_process_callback(_dynamic_process_callback);
	    new_cmd->set_dynamic_interrupt_callback(_dynamic_interrupt_callback);
	    if (is_executable) {
		new_cmd->set_cli_process_callback(_dynamic_process_callback);
		new_cmd->set_cli_interrupt_callback(_dynamic_interrupt_callback);
	    }
	}
    }

    return (_child_command_list); 
}