summaryrefslogtreecommitdiff
path: root/lib/librte_cli/cli.h
blob: bf5aba1bf48d41a9613447710638c5d44b1d07dc (plain)
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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
/*-
 *   BSD LICENSE
 *
 *   Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * Neither the name of Intel Corporation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _CLI_H_
#define _CLI_H_

/**
 * @file
 * RTE Command line interface
 *
 */
#include <libgen.h>
#include <sys/queue.h>

#include <rte_common.h>
#include <rte_debug.h>
#include <rte_memory.h>
#include <rte_per_lcore.h>

#include <cli_common.h>
#include <cli_env.h>
#include <cli_search.h>

#include <cli_file.h>
#include <cli_gapbuf.h>
#include <cli_help.h>
#include <cli_history.h>
#include <cli_map.h>
#include <cli_vt100.h>
#include <cli_vt100_keys.h>

#include <rte_string_fns.h>

#ifdef __cplusplus
extern "C" {
#endif

#define CLI_USE_TIMERS 0x0001      /**< call rte_timer_manager() on input */
#define CLI_NODES_UNLIMITED 0x0002 /**< Allocate nodes with no limit */
#define CLI_YIELD_IO 0x0004

#define CLI_ROOT_NAME "/"
#define CLI_BIN_NAME "bin"

enum {
	CLI_MAX_ARGVS = 64,            /**< Max number of args to support */
	CLI_DEFAULT_NB_NODES = 256,    /**< Default number of nodes */
	CLI_DEFAULT_HIST_LINES = 128,  /**< Default number of history lines */
	CLI_MAX_PATH_LENGTH = 2048,    /**< Max path string length */
	CLI_MAX_SCRATCH_LENGTH = 4096, /**< Max scratch space length */
	CLI_NAME_LEN = 64,             /**< Max node name dir/cmd/file/.. */
	CLI_MAX_LIST_NODES = 128,      /**< Max number of nodes to list */
	CLI_MAX_BINS = 32,             /**< Max number of bin directories */
	CLI_DEFAULT_NODES = 0,         /**< Use default node count */
	CLI_SCREEN_WIDTH = 80          /**< Size of screen width */
};

#define CLI_RECURSE_FLAG (1 << 0)
#define CLI_LONG_LIST_FLAG (1 << 1)

/* bitmap for the node type */
typedef enum {
	CLI_UNK_NODE = 0x0000,   /**< Unknown node type */
	CLI_DIR_NODE = 0x0001,   /**< Directory node type */
	CLI_CMD_NODE = 0x0002,   /**< Command node type */
	CLI_FILE_NODE = 0x0004,  /**< File node type */
	CLI_ALIAS_NODE = 0x0008, /**< Alias node type */
	CLI_STR_NODE = 0x0010,   /**< String node type */
} node_type_t;

/* Keep this list in sync with the node_type_t enum above */
#define CLI_NODE_TYPES                                                         \
  { "Unknown", "Directory", "Command", "File", "Alias", "String", NULL }

enum {
	CLI_EXE_TYPE = (CLI_CMD_NODE | CLI_ALIAS_NODE),
	CLI_ALL_TYPE = (CLI_EXE_TYPE | CLI_FILE_NODE | CLI_DIR_NODE),
	CLI_OTHER_TYPE = (CLI_DIR_NODE | CLI_FILE_NODE)
};

struct cli;
struct cli_node;

typedef int (*cli_cfunc_t)(int argc, char **argv);
/**< CLI function pointer type for a command/alias node  */
typedef int (*cli_ffunc_t)(struct cli_node *node, char *buff, int len,
                           uint32_t opt);
/**< CLI function pointer type for a file type node  */

typedef void (*cli_prompt_t)(int continuation);
/**< CLI prompt routine */
typedef int (*cli_tree_t)(void);
/**< CLI function pointer type for user initialization */

/* Generic node structure for all node types in the directory tree */
struct cli_node {
	TAILQ_ENTRY(cli_node) next; /**< link list of commands */
	struct cli_node *parent;    /**< Parent directory (NULL == ROOT) */
	char name[CLI_NAME_LEN];    /**< Name of Node */
	uint16_t name_sz;           /**< Number of bytes in name w/o null */
	uint16_t fstate;            /**< File State */
	uint16_t fflags;            /**< File flags */
	uint16_t pad0;
	node_type_t type; /**< Node Type Root, Dir or cmd */
	union {
		cli_cfunc_t cfunc; /**< Function pointer for commands */
		cli_ffunc_t ffunc; /**< Function pointer for files */
		cli_sfunc_t sfunc; /**< Function pointer for Strings */
	};
	const char *short_desc;       /**< Short description */
	const char *alias_str;        /**< Alias string */
	size_t foffset;               /**< Current offset in file */
	size_t file_size;             /**< Size of file */
	char *file_data;              /**< Pointer to file data */
	TAILQ_HEAD(, cli_node) items; /**< List of nodes for directory */
} __rte_cache_aligned;          /**< Structure for each node type */

#define MAX_CMD_FILES 16

typedef struct {
	char *filename[MAX_CMD_FILES];
	uint32_t idx;
} cli_files_t;

struct cli {
	TAILQ_HEAD(, cli_node) root;      /**< head of node entries or root */
	CIRCLEQ_HEAD(, cli_hist) hd_hist; /**< History circular queue */

	uint16_t flags;              /**< Flags about CLI */
	volatile uint16_t quit_flag; /**< When set to non-zero quit */
	uint32_t nb_nodes;           /**< total number of nodes */

	uint32_t nb_hist;           /**< total number of history lines */
	cli_files_t cmd_files;      /**< array of command filename pointers  */
	struct cli_hist *curr_hist; /**< Current history */
	struct cli_node *bins[CLI_MAX_BINS]; /**< Arrays of bin directories,
                                    first is the current working directory */
	struct cli_node *exe_node;           /**< Node currently being executed */

	struct cli_env *env;  /**< Environment variables */
	struct gapbuf *gb;    /**< Gap buffer information */
	struct cli_vt100 *vt; /**< vt100 information */
	char **argv;          /**< array of argument string pointers */

	cli_prompt_t prompt; /**< Prompt function pointer */
	char *scratch;       /**< Place to build the path string */
	char *kill;          /**< strdup() string of last kill data */

	struct cli_node *node_mem; /**< Base address of node memory */
	struct cli_hist *hist_mem; /**< Base address of history memory */

	TAILQ_HEAD(, help_node) help_nodes; /**< head of help */
	TAILQ_HEAD(, cli_node) free_nodes;  /**< Free list of nodes */
	CIRCLEQ_HEAD(, cli_hist) free_hist; /**< free list of history nodes */
	void *user_state;                   /**< Pointer to some state variable */
} __rte_cache_aligned;

RTE_DECLARE_PER_LCORE(struct cli *, cli);
#define this_cli RTE_PER_LCORE(cli)

typedef union {
	cli_cfunc_t cfunc; /**< Function pointer for commands */
	cli_ffunc_t ffunc; /**< Function pointer for files */
	cli_sfunc_t sfunc; /**< Function pointer for strings */
} cli_funcs_t;       /* Internal: Used in argument list for adding nodes */

struct cli_dir {
	const char *name; /**< directory name */
};

struct cli_cmd {
	const char *name;       /**< Name of command */
	cli_cfunc_t cfunc;      /**< Function pointer */
	const char *short_desc; /**< Short description */
};                        /**< List of commands for cli_add_cmds() */

struct cli_alias {
	const char *name;       /**< Name of command */
	const char *alias_atr;  /**< Alias string */
	const char *short_desc; /**< Short description */
};                        /**< List of alias for cli_add_aliases() */

struct cli_file {
	const char *name;       /**< Name of command */
	cli_ffunc_t ffunc;      /**< Read/Write function pointer */
	const char *short_desc; /**< Short description */
};                        /**< List of alias for cli_add_aliases() */

struct cli_str {
	const char *name;   /**< Name of command */
	cli_sfunc_t sfunc;  /**< Function pointer */
	const char *string; /**< Default string */
};                    /**< List of commands for cli_add_str() */

struct cli_tree {
	node_type_t type; /**< type of node to create */
	union {
		struct cli_dir dir;     /**< directory and bin directory */
		struct cli_cmd cmd;     /**< command nodes */
		struct cli_file file;   /**< file nodes */
		struct cli_alias alias; /**< alias nodes */
		struct cli_str str;     /**< string node */
	};
}; /**< Used to help create a directory tree */

#define c_dir(n)                                                               \
  {                                                                            \
    CLI_DIR_NODE, .dir = {(n) }                                                \
  }
#define c_cmd(n, f, h)                                                         \
  {                                                                            \
    CLI_CMD_NODE, .cmd = {(n), (f), (h) }                                      \
  }
#define c_file(n, rw, h)                                                       \
  {                                                                            \
    CLI_FILE_NODE, .file = {(n), (rw), (h) }                                   \
  }
#define c_alias(n, l, h)                                                       \
  {                                                                            \
    CLI_ALIAS_NODE, .alias = {(n), (l), (h) }                                  \
  }
#define c_str(n, f, s)                                                         \
  {                                                                            \
    CLI_STR_NODE, .str = {(n), (f), (s) }                                      \
  }
#define c_end()                                                                \
  {                                                                            \
    CLI_UNK_NODE, .dir = { NULL }                                              \
  }

/**
 * The CLI write routine, using write() call
 *
 * @note Uses thread variable this_cli.
 *
 * @param msg
 *   The string to be written
 * @param len
 *   Number of bytes to write or if -1 then strlen(msg) is used.
 * @return
 *   N/A
 */
void cli_write(const void *msg, int len);

static inline void cli_set_user_state(void *val)
{
	this_cli->user_state = val;
}

/**
 * CLI root directory node.
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   Pointer to current working directory.
 */
static inline struct cli_node *get_root(void)
{
	RTE_ASSERT(this_cli != NULL);
	return this_cli->root.tqh_first;
}

/**
 * CLI current working directory.
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   Pointer to current working directory.
 */
static inline struct cli_node *get_cwd(void)
{
	RTE_ASSERT(this_cli != NULL);
	return this_cli->bins[0];
}

/**
 * set CLI current working directory.
 *
 * @note Uses thread variable this_cli.
 *
 * @param node
 *   Pointer to set as the current working directory
 * @return
 *   None
 */
static inline void set_cwd(struct cli_node *node)
{
	RTE_ASSERT(this_cli != NULL);
	this_cli->bins[0] = node;
}

/**
 * Check if this_cli pointer is valid
 *
 * @return
 *    1 if true else 0
 */
static inline int is_cli_valid(void)
{
	return (this_cli) ? 1 : 0;
}

/**
 * Helper routine to compare two strings exactly
 *
 * @param s1
 *   Pointer to first string.
 * @param s2
 *   Pointer to second string.
 * @return
 *   0 failed to compare and 1 is equal.
 */
static inline int is_match(const char *s1, const char *s2)
{
	if (!s1 || !s2)
		return 0;

	while ((*s1 != '\0') && (*s2 != '\0')) {
		if (*s1++ != *s2++)
			return 0;
	}
	if (*s1 != *s2)
		return 0;

	return 1;
}

/**
 * Test if the node is of a given type(s)
 *
 * @param node
 *   Pointer the cli_node structure
 * @return
 *   True if node is one of the types given
 */
static inline int is_node(struct cli_node *node, uint32_t types)
{
	return node->type & types;
}

/**
 * Test if the node is a command
 *
 * @param node
 *   Pointer the cli_node structure
 * @return
 *   True if command else false if not
 */
static inline int is_command(struct cli_node *node)
{
	return is_node(node, CLI_CMD_NODE);
}

/**
 * Test if the node is alias
 *
 * @param node
 *   Pointer the cli_node structure
 * @return
 *   True if alias else false if not
 */
static inline int is_alias(struct cli_node *node)
{
	return is_node(node, CLI_ALIAS_NODE);
}

/**
 * Test if the node is a file
 *
 * @param node
 *   Pointer the cli_node structure
 * @return
 *   True if a file else false if not
 */
static inline int is_file(struct cli_node *node)
{
	return is_node(node, CLI_FILE_NODE);
}

/**
 * Test if the node is directory
 *
 * @param node
 *   Pointer the cli_node structure
 * @return
 *   True if directory else false if not
 */
static inline int is_directory(struct cli_node *node)
{
	return is_node(node, CLI_DIR_NODE);
}

/**
 * Test if the node is executable
 *
 * @param node
 *   Pointer the cli_node structure
 * @return
 *   True if executable else false if not
 */
static inline int is_executable(struct cli_node *node)
{
	return is_command(node) || is_alias(node);
}

/**
 * Print out the short description for commands.
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   -1 just to remove code having to return error anyway.
 */
static inline int cli_usage(void)
{
	if (this_cli && this_cli->exe_node) {
		const char *p = this_cli->exe_node->short_desc;

		cli_printf("  Usage: %s\n", (p) ? p : "No description found");
	}
	return -1;
}

/**
 * Return the string for the given node type
 *
 * @param node
 *   struct cli_node pointer
 * @return
 *   String for the node type.
 */
static inline const char *cli_node_type(struct cli_node *node)
{
	const char *node_str[] = CLI_NODE_TYPES;
	switch (node->type) {
	case CLI_UNK_NODE:
	default:
		break;
	case CLI_DIR_NODE:
		return node_str[1];
	case CLI_CMD_NODE:
		return node_str[2];
	case CLI_FILE_NODE:
		return node_str[3];
	case CLI_ALIAS_NODE:
		return node_str[4];
	}
	return node_str[0];
}

/**
 * Create the current working directory string, which is the complete
 * path to node. Uses CLI routines to output the string to the console.
 *
 * @note Uses thread variable this_cli.
 *
 * @param node
 *   Starting node or last file/dir to be printed
 * @param path
 *   Pointer to a path buffer string.
 * @return
 *   Return the pointer to the cli->scratch buffer or buf with path string.
 */
static inline char *cli_path_string(struct cli_node *node, char *path)
{
	if (!path)
		path = this_cli->scratch;

	if (!node)
		node = get_cwd();

	if (node->parent) {
		cli_path_string(node->parent, path);
		strcat(path, node->name);
		strcat(path, "/");
	} else
		strcpy(path, "/");

	return path;
}

/**
 * path string of current working directory
 *
 * @note Uses thread variable this_cli.
 *
 * @param entry
 *   The node to free.
 * @return
 *   N/A
 */
static inline char *cli_cwd_path(void)
{
	return cli_path_string(get_cwd(), NULL);
}

/**
 * Print the current working directory string, which is the complete
 * path to node. Uses CLI routines to output the string to the console.
 *
 * @note Uses thread variable this_cli.
 *
 * @param node
 *   Starting node or last file/dir to be printed
 * @return
 *   N/A.
 */
static inline void cli_pwd(struct cli_node *node)
{
	cli_printf("%s", cli_path_string(node, NULL));
}

/**
 * Move the vt100 cursor to the left one character
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   N/A
 */
static inline void cli_cursor_left(void)
{
	cli_write(vt100_left_arr, -1);
}

/**
 * Move the vt100 cursor to the right one character
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   N/A
 */
static inline void cli_cursor_right(void)
{
	cli_write(vt100_right_arr, -1);
}

/**
 * Save the vt100 cursor location
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   N/A
 */
static inline void cli_save_cursor(void)
{
	cli_write(vt100_save_cursor, -1);
}

/**
 * Restore the cursor to the saved location on the console
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   N/A
 */
static inline void cli_restore_cursor(void)
{
	cli_write(vt100_restore_cursor, -1);
}

/**
 * Print out the left side of the input in the Gap Buffer.
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   N/A
 */
static inline void cli_display_left(void)
{
	if (gb_left_data_size(this_cli->gb))
		cli_write(gb_start_of_buf(this_cli->gb),
		          gb_left_data_size(this_cli->gb));
}

/**
 * Print out the right side of the input in the Gap Buffer.
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   N/A
 */
static inline void cli_display_right(void)
{
	if (gb_right_data_size(this_cli->gb))
		cli_write(gb_end_of_gap(this_cli->gb),
		          gb_right_data_size(this_cli->gb));
}

/**
 * Print out the complete line in the Gap Buffer.
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   N/A
 */
static inline void cli_display_line(void)
{
	uint32_t i;

	this_cli->prompt(0);

	cli_display_left();
	cli_display_right();

	gb_move_gap_to_point(this_cli->gb);

	for (i = 0;
	     i < (gb_data_size(this_cli->gb) - gb_point_offset(this_cli->gb));
	     i++)
		cli_cursor_left();
}

/**
 * Clear the console screen
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   N/A
 */
static inline void cli_clear_screen(void)
{
	cli_write(vt100_clear_screen, -1);
}

/**
 * clear from cursor to end of line
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   N/A
 */
static inline void cli_clear_to_eol(void)
{
	cli_write(vt100_clear_right, -1);
}

/**
 * Clear the current line or the line given
 *
 * @note Uses thread variable this_cli.
 *
 * @param lineno
 *   if lineno is -1 then clear the current line else the lineno given.
 * @return
 *   N/A
 */
static inline void cli_clear_line(int lineno)
{
	if (lineno > 0)
		cli_printf(vt100_pos_cursor, lineno, 0);
	else
		cli_write("\r", 1);

	cli_write(vt100_clear_line, -1);
}

/**
 * Move the cursor up by the number of lines given
 *
 * @note Uses thread variable this_cli.
 *
 * @param lineno
 *   Number of lines to move the cursor
 * @return
 *   N/A
 */
static inline void cli_move_cursor_up(int lineno)
{
	while (lineno--)
		cli_printf(vt100_up_arr);
}

/**
 * Set the number of lines in history
 *
 * @note Uses thread variable this_cli.
 *
 * @param nb_hist
 *   Number of lines in history if zero disable history.
 * @return
 *   zero on success or -1 on error
 */
static inline int cli_set_history_size(uint32_t nb_hist)
{
	return cli_set_history(nb_hist);
}

/**
 * Get the total number of lines in history
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   total number of line for history
 */
static inline uint32_t cli_get_history_size(void)
{
	return this_cli->nb_hist;
}

/**
 * List the history lines
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   N/A
 */
static inline void cli_history_list(void)
{
	cli_history_dump();
}

/**
 * Return the CLI root node.
 *
 * @return
 *   Pointer to root node.
 */
static inline struct cli_node *cli_root_node(void)
{
	return this_cli->root.tqh_first;
}

/**
 * Add a input text string the cli input parser
 *
 * @note Uses thread variable this_cli.
 *
 * @param str
 *   Pointer to string to insert
 * @param n
 *   Number of bytes in string
 * @return
 *   N/A
 */
void cli_input(char *str, int n);

/**
 * Create the CLI engine
 *
 * @param prompt_func
 *   Function pointer to call for displaying the prompt.
 * @param tree_func
 *   The user supplied function to init the tree or can be NULL. If NULL then
 *   a default tree is initialized with basic commands.
 * @param nb_entries
 *   Total number of commands, files, aliases and directories. If 0 then use
 *   the default number of nodes. If -1 then unlimited number of nodes.
 * @param nb_hist
 *   The number of lines to keep in history. If zero then turn off history.
 *   If the value is CLI_DEFAULT_HISTORY use CLI_DEFAULT_HIST_LINES
 * @return
 *   0 on success or -1
 */
int cli_create(cli_prompt_t prompt_func, cli_tree_t tree_func, int nb_entries,
               uint32_t nb_hist);

/**
 * Create the CLI engine using system defaults.
 *
 * @return
 *   0 on success or -1
 */
int cli_create_with_defaults(void);

/**
 * Create the CLI engine using system defaults and supplied tree init function.
 *
 * @param tree
 *   The user supplied function to init the tree or can be NULL. If NULL then
 *   a default tree is initialized with default commands.
 * @return
 *   0 on success or -1
 */
int cli_create_with_tree(cli_tree_t tree);

/**
 * Set the CLI prompt function pointer
 *
 * @param prompt
 *   Function pointer to display the prompt
 * @return
 *   Return the old prompt function pointer or NULL if one does not exist
 */
cli_prompt_t cli_set_prompt(cli_prompt_t prompt);

/**
 * Create the root directory
 *
 * @note Uses thread variable this_cli.
 *
 * @param dirname
 *   Name of root directory, if null uses '/'
 * @return
 *   NULL on error or the root node on success.
 */
struct cli_node *cli_create_root(const char *dirname);

/**
 * Create the default directory tree
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   0 on success or non-zero on error
 */
int cli_default_tree_init(void);

/**
 * Destroy the CLI engine
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   N/A
 */
void cli_destroy(void);

/**
 * Start the CLI running
 *
 * @note Uses thread variable this_cli.
 *
 * @param msg
 *   User message to be displayed on startup
 * @return
 *   N/A
 */
void cli_start(const char *msg);

/**
 * Start the CLI running and use timerss
 *
 * @note Uses thread variable this_cli.
 *
 * @param msg
 *   User message to be displayed on startup
 * @return
 *   N/A
 */
void cli_start_with_timers(const char *msg);

/**
 * Execute command line string in cli->input
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   zero on success or -1 on error
 */
int cli_execute(void);

/**
 * Add a bin directory to the bin list
 *
 * @note Uses thread variable this_cli.
 *
 * @param node
 *   Directory to add to bin list
 * @return
 *   0 is ok, -1 is full
 */
int cli_add_bin(struct cli_node *node);

/**
 * Remove a bin directory from the bin list
 *
 * @note Uses thread variable this_cli.
 *
 * @param node
 *   Directory to add to bin list
 * @return
 *   0 is ok, -1 is not found
 */
int cli_del_bin(struct cli_node *node);

/**
 * Add a bin directory to the bin list using path
 *
 * @note Uses thread variable this_cli.
 *
 * @param path
 *   path to bin directory to add, must exist first.
 * @return
 *   0 is ok, -1 is full
 */
int cli_add_bin_path(const char *path);

/**
 * Add a cli directory
 *
 * @note Uses thread variable this_cli.
 *
 * @param dirname
 *   String pointing to the directory name
 * @param parent
 *   Parent node of the new directory
 * @return
 *   pointer to directory entry or NULL on error
 */
struct cli_node *cli_add_dir(const char *dirname, struct cli_node *parent);

/**
 * Add a command to a directory
 *
 * @note Uses thread variable this_cli.
 *
 * @param name
 *   Pointer to command name string
 * @param dir
 *   Directory node pointer
 * @param func
 *   Pointer to function to execute
 * @param short_desc
 *   Short string for help to display
 * @return
 *   NULL on error or the node address for the command
 */
struct cli_node *cli_add_cmd(const char *name, struct cli_node *dir,
                             cli_cfunc_t func, const char *short_desc);

/**
 * Add an alias string or special command type
 *
 * @note Uses thread variable this_cli.
 *
 * @param name
 *   Pointer to command name string
 * @param dir
 *   Directory node pointer
 * @param line
 *   Pointer to alias string
 * @param short_desc
 *   Short string for help to display
 * @return
 *   NULL on error or the node address for the command
 */
struct cli_node *cli_add_alias(const char *name, struct cli_node *dir,
                               const char *line, const char *short_desc);

/**
 * Add an file to a directory
 *
 * @note Uses thread variable this_cli.
 *
 * @param name
 *   Pointer to command name string
 * @param dir
 *   Directory node pointer
 * @param func
 *   Pointer to a function attached to the file.
 * @param short_desc
 *   Short string for help to display
 * @return
 *   NULL on error or the node pointer.
 */
struct cli_node *cli_add_file(const char *name, struct cli_node *dir,
                              cli_ffunc_t func, const char *short_desc);

/**
 * Add a string to the system.
 *
 * @note Uses thread variable this_cli.
 *
 * @param name
 *   Pointer to command name string
 * @param dir
 *   Directory node pointer
 * @param func
 *   Pointer to a function attached to the string.
 * @param str
 *   Value of string if no function defined.
 * @return
 *   NULL on error or the node pointer.
 */
int cli_add_str(const char *name, cli_sfunc_t func, const char *str);

/**
 * Add a list of nodes to a directory
 *
 * @note Uses thread variable this_cli.
 *
 * @param dir
 *   Node pointer to directory for add commands
 * @param treee
 *   Pointer to list of nodes to add to the tree
 * @return
 *   -1 on error or 0 for OK
 */
int cli_add_tree(struct cli_node *dir, struct cli_tree *tree);

/**
 * Set the I/O file descriptors
 *
 * @note Uses thread variable this_cli.
 *
 * @param in
 *   File descriptor for input
 * @param out
 *   File descriptor for output
 * @return
 *   N/A
 */
void cli_set_io(FILE *in, FILE *out);

/**
 * Set the I/O to use stdin/stdout
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   0 on success or non-0 on error
 */
int cli_stdin_setup(void);

/**
 * Restore the stdin/stdout tty params from setup
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   N/A
 */
void cli_stdin_restore(void);

/**
 * Pause and wait for input character
 *
 * @note Uses thread variable this_cli.
 *
 * @param keys
 *   List of keys to force return, if NULL defaults to ESC and q/Q
 * @return
 *   character that terminated the pause or zero.
 */
char cli_pause(const char *msg, const char *keys);

/**
 * Add filenames to the CLI command list.
 *
 * @param filename
 *    Path of command file.
 * @return
 *    0 is OK and -1 if error
 */
static inline int cli_add_cmdfile(const char *filename)
{
	if (this_cli->cmd_files.idx >= MAX_CMD_FILES)
		return -1;

	this_cli->cmd_files.filename[this_cli->cmd_files.idx++] =
	        strdup(filename);

	return 0;
}

/**
 * execute a command file
 *
 * @note Uses thread variable this_cli.
 *
 * @param path
 *   Pointer to path to file
 * @return
 *   0 on OK or -1 on error
 */
int cli_execute_cmdfile(const char *path);

/**
 * execute a list for command files
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   0 on OK or -1 on error
 */
int cli_execute_cmdfiles(void);

/**
 * Remove a node from the directory tree
 *
 * @note Uses thread variable this_cli.
 *
 * @param node
 *   The pointer to the node to remove
 * @return
 *   0 on OK and -1 on error
 */
int cli_remove_node(struct cli_node *node);

/**
 * return true if timers are enabled.
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   non-zero if true else 0
 */
int cli_use_timers(void);

/**
 * return true if allocating unlimited nodes are enabled.
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   non-zero if true else 0
 */
int cli_nodes_unlimited(void);

/**
 * return true if calling yield should are enabled.
 *
 * @note Uses thread variable this_cli.
 *
 * @return
 *   non-zero if true else 0
 */
int cli_yield_io(void);

/**
 * shutdown the CLI command interface.
 *
 */
static inline void
cli_quit(void)
{
	this_cli->quit_flag = 1;
}

#ifdef __cplusplus
}
#endif

#endif /* _CLI_H_ */