CNDP  22.08.0
cli.h File Reference
#include <libgen.h>
#include <sys/queue.h>
#include <cne_common.h>
#include <cne_log.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 <cne_strings.h>
#include <stdint.h>
#include <string.h>
#include "cne_stdio.h"

Go to the source code of this file.

Data Structures

struct  cli_cmd
 
struct  cli_alias
 
struct  cli_file
 
struct  cli_str
 

Macros

#define CLI_USE_TIMERS   (1 << 8)
 
#define CLI_NODES_UNLIMITED   (1 << 9)
 
#define c_dir(n)   { CLI_DIR_NODE, .dir = {.name = (n), .bin = 0} }
 

Typedefs

typedef int(* cli_cfunc_t) (int argc, char **argv)
 
typedef int(* cli_ffunc_t) (struct cli_node *node, char *buff, int len, uint32_t opt)
 
typedef int(* cli_prompt_t) (int continuation)
 
typedef int(* cli_tree_t) (void)
 

Enumerations

enum  {
  CLI_MAX_ARGVS = 64 , CLI_DEFAULT_NB_NODES = 256 , CLI_DEFAULT_HIST_LINES = 128 , CLI_MAX_PATH_LENGTH = 2048 ,
  CLI_MAX_SCRATCH_LENGTH = 4096 , CLI_NAME_LEN = 64 , CLI_MAX_LIST_NODES = 128 , CLI_MAX_BINS = 32 ,
  CLI_DEFAULT_NODES = 0 , CLI_SCREEN_WIDTH = 80
}
 
enum  node_type_t {
  CLI_UNK_NODE = 0x0000 , CLI_DIR_NODE = 0x0001 , CLI_CMD_NODE = 0x0002 , CLI_FILE_NODE = 0x0004 ,
  CLI_ALIAS_NODE = 0x0008 , CLI_STR_NODE = 0x0010
}
 

Functions

static struct cli_node * get_root (void)
 
static struct cli_node * get_cwd (void)
 
static void set_cwd (struct cli_node *node)
 
static int is_cli_valid (void)
 
static int is_match (const char *s1, const char *s2)
 
static int is_node (struct cli_node *node, uint32_t types)
 
static int is_command (struct cli_node *node)
 
static int is_alias (struct cli_node *node)
 
static int is_file (struct cli_node *node)
 
static int is_directory (struct cli_node *node)
 
static int is_executable (struct cli_node *node)
 
static int cli_usage (void)
 
static const char * cli_node_type (struct cli_node *node)
 
static char * cli_path_string (struct cli_node *node, char *path)
 
static char * cli_cwd_path (void)
 
static void cli_pwd (struct cli_node *node)
 
static int cli_set_history_size (uint32_t nb_hist)
 
static uint32_t cli_get_history_size (void)
 
static void cli_history_list (void)
 
static struct cli_node * cli_root_node (void)
 
CNDP_API int cli_create (struct cli_cfg *cfg)
 
CNDP_API int cli_create_with_defaults (struct cli_cfg *cfg)
 
CNDP_API int cli_setup (cli_prompt_t prompt, cli_tree_t default_func)
 
CNDP_API int cli_setup_with_defaults (void)
 
CNDP_API int cli_setup_with_tree (cli_tree_t tree)
 
CNDP_API cli_prompt_t cli_set_prompt (cli_prompt_t prompt)
 
CNDP_API struct cli_node * cli_create_root (const char *dirname)
 
CNDP_API int cli_default_tree_init (void)
 
CNDP_API void cli_destroy (void)
 
CNDP_API void cli_start (const char *msg)
 
CNDP_API void cli_start_with_timers (const char *msg)
 
CNDP_API int cli_execute (void)
 
CNDP_API int cli_add_bin (struct cli_node *node)
 
CNDP_API int cli_del_bin (struct cli_node *node)
 
CNDP_API int cli_add_bin_path (const char *path)
 
CNDP_API struct cli_node * cli_add_dir (const char *dirname, struct cli_node *parent)
 
CNDP_API struct cli_node * cli_add_cmd (const char *name, struct cli_node *dir, cli_cfunc_t func, const char *short_desc)
 
CNDP_API struct cli_node * cli_add_alias (const char *name, struct cli_node *dir, const char *line, const char *short_desc)
 
CNDP_API struct cli_node * cli_add_file (const char *name, struct cli_node *dir, cli_ffunc_t func, const char *short_desc)
 
CNDP_API int cli_add_str (const char *name, cli_sfunc_t func, const char *str)
 
CNDP_API int cli_add_tree (struct cli_node *dir, struct cli_tree *tree)
 
static int cli_add_cmdfile (const char *filename)
 
CNDP_API int cli_execute_cmdfile (const char *path)
 
CNDP_API int cli_execute_cmdfiles (void)
 
CNDP_API int cli_remove_node (struct cli_node *node)
 
CNDP_API void cli_use_timers (void)
 
CNDP_API int cli_nodes_unlimited (void)
 
CNDP_API int cli_execute_cmd (int argc, char **argv)
 
CNDP_API int cli_get_quit_flag (void)
 
CNDP_API void cli_set_quit_flag (void)
 

Variables

struct cli_node __cne_cache_aligned
 
cli_files_t cli_cmd_files
 

Detailed Description

CNE Command line interface

Definition in file cli.h.

Macro Definition Documentation

◆ CLI_USE_TIMERS

#define CLI_USE_TIMERS   (1 << 8)

call timer_manage() on input

Definition at line 168 of file cli.h.

◆ CLI_NODES_UNLIMITED

#define CLI_NODES_UNLIMITED   (1 << 9)

Allocate nodes with no limit

Definition at line 169 of file cli.h.

◆ c_dir

#define c_dir (   n)    { CLI_DIR_NODE, .dir = {.name = (n), .bin = 0} }

< Used to help create a directory tree

Examples
examples/cli/main.c, and examples/timer/main.c.

Definition at line 239 of file cli.h.

Typedef Documentation

◆ cli_cfunc_t

typedef int(* cli_cfunc_t) (int argc, char **argv)

CLI function pointer type for a command/alias node

Definition at line 80 of file cli.h.

◆ cli_ffunc_t

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

Definition at line 82 of file cli.h.

◆ cli_prompt_t

typedef int(* cli_prompt_t) (int continuation)

CLI prompt routine

Definition at line 85 of file cli.h.

◆ cli_tree_t

typedef int(* cli_tree_t) (void)

CLI function pointer type for user initialization

Definition at line 86 of file cli.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
CLI_MAX_ARGVS 

Max number of args to support

CLI_DEFAULT_NB_NODES 

Default number of nodes

CLI_DEFAULT_HIST_LINES 

Default number of history lines

CLI_MAX_PATH_LENGTH 

Max path string length

CLI_MAX_SCRATCH_LENGTH 

Max scratch space length

CLI_NAME_LEN 

Max node name dir/cmd/file/..

CLI_MAX_LIST_NODES 

Max number of nodes to list

CLI_MAX_BINS 

Max number of bin directories

CLI_DEFAULT_NODES 

Use default node count

CLI_SCREEN_WIDTH 

Size of screen width

Definition at line 39 of file cli.h.

◆ node_type_t

bitmap for the node type

Enumerator
CLI_UNK_NODE 

Unknown node type

CLI_DIR_NODE 

Directory node type

CLI_CMD_NODE 

Command node type

CLI_FILE_NODE 

File node type

CLI_ALIAS_NODE 

Alias node type

CLI_STR_NODE 

String node type

Definition at line 56 of file cli.h.

Function Documentation

◆ get_root()

static struct cli_node* get_root ( void  )
inlinestatic

CLI root directory node.

Returns
Pointer to current working directory.

Definition at line 261 of file cli.h.

◆ get_cwd()

static struct cli_node* get_cwd ( void  )
inlinestatic

CLI current working directory.

Returns
Pointer to current working directory.

Definition at line 274 of file cli.h.

◆ set_cwd()

static void set_cwd ( struct cli_node *  node)
inlinestatic

set CLI current working directory.

Parameters
nodePointer to set as the current working directory

Definition at line 287 of file cli.h.

◆ is_cli_valid()

static int is_cli_valid ( void  )
inlinestatic

Check if this_cli pointer is valid

Returns
1 if true else 0

Definition at line 300 of file cli.h.

◆ is_match()

static int is_match ( const char *  s1,
const char *  s2 
)
inlinestatic

Helper routine to compare two strings exactly

Parameters
s1Pointer to first string.
s2Pointer to second string.
Returns
0 failed to compare and 1 is equal.

Definition at line 316 of file cli.h.

◆ is_node()

static int is_node ( struct cli_node *  node,
uint32_t  types 
)
inlinestatic

Test if the node is of a given type(s)

Parameters
nodePointer the cli_node structure
typesTODO
Returns
True if node is one of the types given

Definition at line 342 of file cli.h.

◆ is_command()

static int is_command ( struct cli_node *  node)
inlinestatic

Test if the node is a command

Parameters
nodePointer the cli_node structure
Returns
True if command else false if not

Definition at line 356 of file cli.h.

◆ is_alias()

static int is_alias ( struct cli_node *  node)
inlinestatic

Test if the node is alias

Parameters
nodePointer the cli_node structure
Returns
True if alias else false if not

Definition at line 370 of file cli.h.

◆ is_file()

static int is_file ( struct cli_node *  node)
inlinestatic

Test if the node is a file

Parameters
nodePointer the cli_node structure
Returns
True if a file else false if not

Definition at line 384 of file cli.h.

◆ is_directory()

static int is_directory ( struct cli_node *  node)
inlinestatic

Test if the node is directory

Parameters
nodePointer the cli_node structure
Returns
True if directory else false if not

Definition at line 398 of file cli.h.

◆ is_executable()

static int is_executable ( struct cli_node *  node)
inlinestatic

Test if the node is executable

Parameters
nodePointer the cli_node structure
Returns
True if executable else false if not

Definition at line 412 of file cli.h.

◆ cli_usage()

static int cli_usage ( void  )
inlinestatic

Print out the short description for commands.

Returns
-1 just to remove code having to return error anyway.

Definition at line 424 of file cli.h.

◆ cli_node_type()

static const char* cli_node_type ( struct cli_node *  node)
inlinestatic

Return the string for the given node type

Parameters
nodestruct cli_node pointer
Returns
String for the node type.

Definition at line 443 of file cli.h.

◆ cli_path_string()

static char* cli_path_string ( struct cli_node *  node,
char *  path 
)
inlinestatic

Create the current working directory string, which is the complete path to node. Uses CLI routines to output the string to the console.

Parameters
nodeStarting node or last file/dir to be printed
pathPointer to a path buffer string.
Returns
Return the pointer to the cli->scratch buffer or buf with path string.
Examples
examples/cli/main.c, and examples/timer/main.c.

Definition at line 474 of file cli.h.

◆ cli_cwd_path()

static char* cli_cwd_path ( void  )
inlinestatic

path string of current working directory

Returns
The pointer the current working directory or NULL

Definition at line 499 of file cli.h.

◆ cli_pwd()

static void cli_pwd ( struct cli_node *  node)
inlinestatic

Print the current working directory string, which is the complete path to node. Uses CLI routines to output the string to the console.

Parameters
nodeStarting node or last file/dir to be printed

Definition at line 512 of file cli.h.

◆ cli_set_history_size()

static int cli_set_history_size ( uint32_t  nb_hist)
inlinestatic

Set the number of lines in history

Parameters
nb_histNumber of lines in history if zero disable history.
Returns
zero on success or -1 on error

Definition at line 526 of file cli.h.

◆ cli_get_history_size()

static uint32_t cli_get_history_size ( void  )
inlinestatic

Get the total number of lines in history

Returns
total number of line for history

Definition at line 538 of file cli.h.

◆ cli_history_list()

static void cli_history_list ( void  )
inlinestatic

List the history lines

Definition at line 548 of file cli.h.

◆ cli_root_node()

static struct cli_node* cli_root_node ( void  )
inlinestatic

Return the CLI root node.

Returns
Pointer to root node.
Examples
examples/cli/main.c, and examples/timer/main.c.

Definition at line 560 of file cli.h.

◆ cli_create()

CNDP_API int cli_create ( struct cli_cfg *  cfg)

Create the CLI engine

Parameters
cfgThe struyct cli_cfg pointer, if NULL use all default values
Returns
0 on success or -1
Examples
examples/cli/main.c, examples/cnet-graph/cnet-graph.c, examples/cnet-quic/cnet-quic.c, and examples/timer/main.c.

◆ cli_create_with_defaults()

CNDP_API int cli_create_with_defaults ( struct cli_cfg *  cfg)

Create the CLI and add the default set of commands.

Parameters
cfgThe struct cli_cfg pointer, can be NULL will use defaults.
Returns
-1 on error or 0 on success
Examples
examples/cnet-graph/cnet-graph.c, and examples/cnet-quic/cnet-quic.c.

◆ cli_setup()

CNDP_API int cli_setup ( cli_prompt_t  prompt,
cli_tree_t  default_func 
)

Setup the CLI prompt callback and default setup function

Parameters
promptThe function to call when the prompt needs to be displayed
default_funcThe function pointer to setup the CLI files, directories and executables.
Returns
0 on success or -1 on error

◆ cli_setup_with_defaults()

CNDP_API int cli_setup_with_defaults ( void  )

Create the CLI engine using system defaults.

Returns
0 on success or -1

◆ cli_setup_with_tree()

CNDP_API int cli_setup_with_tree ( cli_tree_t  tree)

Create the CLI engine using system defaults and supplied tree init function.

Parameters
treeThe user supplied function to init the tree or can be NULL. If NULL then a default tree is initialized with default commands.
Returns
0 on success or -1
Examples
examples/cli/main.c, examples/cnet-graph/cnet-graph.c, examples/cnet-quic/cnet-quic.c, and examples/timer/main.c.

◆ cli_set_prompt()

CNDP_API cli_prompt_t cli_set_prompt ( cli_prompt_t  prompt)

Set the CLI prompt function pointer

Parameters
promptFunction pointer to display the prompt
Returns
Return the old prompt function pointer or NULL if one does not exist
Examples
examples/cli/main.c, and examples/timer/main.c.

◆ cli_create_root()

CNDP_API struct cli_node* cli_create_root ( const char *  dirname)

Create the root directory

Note
Uses thread variable this_cli.
Parameters
dirnameName of root directory, if null uses '/'
Returns
NULL on error or the root node on success.

◆ cli_default_tree_init()

CNDP_API int cli_default_tree_init ( void  )

Create the default directory tree

Returns
0 on success or non-zero on error
Examples
examples/cli/main.c, and examples/timer/main.c.

◆ cli_destroy()

CNDP_API void cli_destroy ( void  )

◆ cli_start()

CNDP_API void cli_start ( const char *  msg)

Start the CLI running

Parameters
msgUser message to be displayed on startup
Examples
examples/cli/main.c, examples/cnet-graph/cnet-graph.c, examples/cnet-quic/cnet-quic.c, and examples/timer/main.c.

◆ cli_start_with_timers()

CNDP_API void cli_start_with_timers ( const char *  msg)

Start the CLI running and use timerss

Parameters
msgUser message to be displayed on startup

◆ cli_execute()

CNDP_API int cli_execute ( void  )

Execute command line string in cli->input

Returns
zero on success or -1 on error

◆ cli_add_bin()

CNDP_API int cli_add_bin ( struct cli_node *  node)

Add a bin directory to the bin list

Parameters
nodeDirectory to add to bin list
Returns
0 is ok, -1 is full

◆ cli_del_bin()

CNDP_API int cli_del_bin ( struct cli_node *  node)

Remove a bin directory from the bin list

Parameters
nodeDirectory to add to bin list
Returns
0 is ok, -1 is not found

◆ cli_add_bin_path()

CNDP_API int cli_add_bin_path ( const char *  path)

Add a bin directory to the bin list using path

Parameters
pathpath to bin directory to add, must exist first.
Returns
0 is ok, -1 is full
Examples
examples/cli/main.c, and examples/timer/main.c.

◆ cli_add_dir()

CNDP_API struct cli_node* cli_add_dir ( const char *  dirname,
struct cli_node *  parent 
)

Add a cli directory

Parameters
dirnameString pointing to the directory name
parentParent node of the new directory
Returns
pointer to directory entry or NULL on error

◆ cli_add_cmd()

CNDP_API struct cli_node* cli_add_cmd ( const char *  name,
struct cli_node *  dir,
cli_cfunc_t  func,
const char *  short_desc 
)

Add a command to a directory

Parameters
namePointer to command name string
dirDirectory node pointer
funcPointer to function to execute
short_descShort string for help to display
Returns
NULL on error or the node address for the command

◆ cli_add_alias()

CNDP_API struct cli_node* cli_add_alias ( const char *  name,
struct cli_node *  dir,
const char *  line,
const char *  short_desc 
)

Add an alias string or special command type

Parameters
namePointer to command name string
dirDirectory node pointer
linePointer to alias string
short_descShort string for help to display
Returns
NULL on error or the node address for the command

◆ cli_add_file()

CNDP_API struct cli_node* cli_add_file ( const char *  name,
struct cli_node *  dir,
cli_ffunc_t  func,
const char *  short_desc 
)

Add an file to a directory

Parameters
namePointer to command name string
dirDirectory node pointer
funcPointer to a function attached to the file.
short_descShort string for help to display
Returns
NULL on error or the node pointer.

◆ cli_add_str()

CNDP_API int cli_add_str ( const char *  name,
cli_sfunc_t  func,
const char *  str 
)

Add a string to the system.

Parameters
namePointer to command name string
funcPointer to a function attached to the string.
strValue of string if no function defined.
Returns
NULL on error or the node pointer.

◆ cli_add_tree()

CNDP_API int cli_add_tree ( struct cli_node *  dir,
struct cli_tree *  tree 
)

Add a list of nodes to a directory

Parameters
dirNode pointer to directory for add commands
treePointer to list of nodes to add to the tree
Returns
-1 on error or 0 for OK
Examples
examples/cli/main.c, and examples/timer/main.c.

◆ cli_add_cmdfile()

static int cli_add_cmdfile ( const char *  filename)
inlinestatic

Add filenames to the CLI command list.

Parameters
filenamePath of command file.
Returns
0 is OK and -1 if error
Examples
examples/cnet-graph/parse-args.c, and examples/cnet-quic/parse-args.c.

Definition at line 804 of file cli.h.

◆ cli_execute_cmdfile()

CNDP_API int cli_execute_cmdfile ( const char *  path)

execute a command file

Parameters
pathPointer to path to file
Returns
0 on OK or -1 on error

◆ cli_execute_cmdfiles()

CNDP_API int cli_execute_cmdfiles ( void  )

execute a list for command files

Returns
0 on OK or -1 on error

◆ cli_remove_node()

CNDP_API int cli_remove_node ( struct cli_node *  node)

Remove a node from the directory tree

Parameters
nodeThe pointer to the node to remove
Returns
0 on OK and -1 on error

◆ cli_use_timers()

CNDP_API void cli_use_timers ( void  )

Handle calling the timer_manage routine if trimers are enabled

◆ cli_nodes_unlimited()

CNDP_API int cli_nodes_unlimited ( void  )

return true if allocating unlimited nodes are enabled.

Returns
non-zero if true else 0

◆ cli_execute_cmd()

CNDP_API int cli_execute_cmd ( int  argc,
char **  argv 
)

Execute a command file from the CLI directories

Parameters
argcThe number of arguments in the argv array
argvThe array of string pointers to the arguments of the command.
Returns
0 on success or -1 on error

◆ cli_get_quit_flag()

CNDP_API int cli_get_quit_flag ( void  )

Return the state of the CLI quit flag

Returns
0 - still running or 1 - has quit.

◆ cli_set_quit_flag()

CNDP_API void cli_set_quit_flag ( void  )

Set the quit flag to true to allow CLI to quit.

Examples
examples/cli/main.c.

Variable Documentation

◆ __cne_cache_aligned

struct cli __cne_cache_aligned

Structure for each node type

◆ cli_cmd_files

cli_files_t cli_cmd_files
extern

array of command filename pointers