BLOG | DOCUMENTATION | GITHUB

Home --> Documentations --> PJLIB-UTIL Reference

Command Line Interface Framework

Modules

 Command Line Interface Implementor's API
 

Data Structures

struct  pj_cli_cfg
 
struct  pj_cli_cmd_val
 
struct  pj_cli_hint_info
 
struct  pj_cli_exec_info
 
struct  pj_cli_arg_choice_val
 
struct  pj_cli_dyn_choice_param
 

Typedefs

typedef struct pj_cli_t pj_cli_t
 
typedef int pj_cli_cmd_id
 
typedef int pj_cli_arg_id
 
typedef struct pj_cli_cmd_spec pj_cli_cmd_spec
 
typedef struct pj_cli_arg_spec pj_cli_arg_spec
 
typedef void(* pj_cli_get_dyn_choice) (pj_cli_dyn_choice_param *param)
 
typedef pj_status_t(* pj_cli_cmd_handler) (pj_cli_cmd_val *cval)
 

Functions

void pj_cli_write_log (pj_cli_t *cli, int level, const char *buffer, int len)
 
pj_status_t pj_cli_create (pj_cli_cfg *cfg, pj_cli_t **p_cli)
 
pj_cli_cmd_id pj_cli_get_cmd_id (const pj_cli_cmd_spec *cmd)
 
pj_cli_cfgpj_cli_get_param (pj_cli_t *cli)
 
void pj_cli_quit (pj_cli_t *cli, pj_cli_sess *req, pj_bool_t restart)
 
pj_bool_t pj_cli_is_quitting (pj_cli_t *cli)
 
pj_bool_t pj_cli_is_restarting (pj_cli_t *cli)
 
void pj_cli_destroy (pj_cli_t *cli)
 
void pj_cli_cfg_default (pj_cli_cfg *param)
 
void pj_cli_register_front_end (pj_cli_t *cli, pj_cli_front_end *fe)
 
pj_status_t pj_cli_add_cmd_from_xml (pj_cli_t *cli, pj_cli_cmd_spec *group, const pj_str_t *xml, pj_cli_cmd_handler handler, pj_cli_cmd_spec **p_cmd, pj_cli_get_dyn_choice get_choice)
 
void pj_cli_exec_info_default (pj_cli_exec_info *param)
 
void pj_cli_sess_write_msg (pj_cli_sess *sess, const char *buffer, pj_size_t len)
 
pj_status_t pj_cli_sess_parse (pj_cli_sess *sess, char *cmdline, pj_cli_cmd_val *val, pj_pool_t *pool, pj_cli_exec_info *info)
 
void pj_cli_sess_end_session (pj_cli_sess *sess)
 
pj_status_t pj_cli_sess_exec (pj_cli_sess *sess, char *cmdline, pj_pool_t *pool, pj_cli_exec_info *info)
 

Detailed Description

A CLI framework features an interface for defining command specification, parsing, and executing a command. It also features an interface to communicate with various front-ends, such as console, telnet. Application normally needs only one CLI instance to be created. On special cases, application could also create multiple CLI instances, with each instance has specific command structure.

| vid help                  Show this help screen                             |
| vid enable|disable        Enable or disable video in next offer/answer      |
| vid call add              Add video stream for current call                 |
| vid call cap N ID         Set capture dev ID for stream #N in current call  |
| disable_codec g711|g722   Show this help screen                             |
<CMD name='vid' id='0' desc="">
       <CMD name='help' id='0' desc='' />
       <CMD name='enable' id='0' desc='' />
       <CMD name='disable' id='0' desc='' />
       <CMD name='call' id='0' desc='' >
                <CMD name='add' id='101' desc='...' />
                <CMD name='cap' id='102' desc='...' >
                   <ARG name='streamno' type='int' desc='...' id='1'/>
                   <ARG name='devid' type='int' optional='1' id='2'/>
                </CMD>
       </CMD>
</CMD>
<CMD name='disable_codec' id=0 desc="">
        <ARG name='codec_list' type='choice' id='3'>
            <CHOICE value='g711'/>
            <CHOICE value='g722'/>
        </ARG>
</CMD>

Typedef Documentation

◆ pj_cli_arg_id

typedef int pj_cli_arg_id

Type of argument id.

◆ pj_cli_arg_spec

Forward declaration for CLI argument spec structure.

◆ pj_cli_cmd_handler

typedef pj_status_t(* pj_cli_cmd_handler) (pj_cli_cmd_val *cval)

This specifies the callback type for command handlers, which will be executed when the specified command is invoked.

Parameters
cvalThe command that is specified by the user.
Returns
Return the status of the command execution.

◆ pj_cli_cmd_id

typedef int pj_cli_cmd_id

Type of command id.

◆ pj_cli_cmd_spec

Forward declaration of pj_cli_cmd_spec structure.

◆ pj_cli_get_dyn_choice

typedef void(* pj_cli_get_dyn_choice) (pj_cli_dyn_choice_param *param)

This specifies the callback type for argument handlers, which will be called to get the valid values of the choice type arguments.

◆ pj_cli_t

typedef struct pj_cli_t pj_cli_t

This opaque structure represents a CLI application. A CLI application is the root placeholder of other CLI objects. In an application, one (and normally only one) CLI application instance needs to be created.

Function Documentation

◆ pj_cli_add_cmd_from_xml()

pj_status_t pj_cli_add_cmd_from_xml ( pj_cli_t cli,
pj_cli_cmd_spec group,
const pj_str_t xml,
pj_cli_cmd_handler  handler,
pj_cli_cmd_spec **  p_cmd,
pj_cli_get_dyn_choice  get_choice 
)

Create a new complete command specification from an XML node text and register it to the CLI application.

Note that the input string MUST be NULL terminated.

Parameters
cliThe CLI application.
groupOptional group to which this command will be added to, or specify NULL if this command is a root command.
xmlInput string containing XML node text for the command, MUST be NULL terminated.
handlerFunction handler for the command. This must be NULL if the command specifies a command group.
p_cmdOptional pointer to store the newly created specification.
get_choiceFunction handler for the argument. Specify this for dynamic choice type arguments.
Returns
PJ_SUCCESS on success, or the appropriate error code.

◆ pj_cli_cfg_default()

void pj_cli_cfg_default ( pj_cli_cfg param)

Initialize a pj_cli_cfg with its default values.

Parameters
paramThe instance to be initialized.

◆ pj_cli_create()

pj_status_t pj_cli_create ( pj_cli_cfg cfg,
pj_cli_t **  p_cli 
)

Create a new CLI application instance.

Parameters
cfgCLI application creation parameters.
p_cliPointer to receive the returned instance.
Returns
PJ_SUCCESS on success, or the appropriate error code.

◆ pj_cli_destroy()

void pj_cli_destroy ( pj_cli_t cli)

Destroy a CLI application instance. This would also close all sessions currently running for this CLI application.

Parameters
cliThe CLI application.

◆ pj_cli_exec_info_default()

void pj_cli_exec_info_default ( pj_cli_exec_info param)

Initialize pj_cli_exec_info with its default values.

Parameters
paramThe param to be initialized.

◆ pj_cli_get_cmd_id()

pj_cli_cmd_id pj_cli_get_cmd_id ( const pj_cli_cmd_spec cmd)

This specifies the function to get the id of the specified command

Parameters
cmdThe specified command.
Returns
The command id

◆ pj_cli_get_param()

pj_cli_cfg * pj_cli_get_param ( pj_cli_t cli)

Get the internal parameter of the CLI instance.

Parameters
cliThe CLI application instance.
Returns
CLI parameter instance.

◆ pj_cli_is_quitting()

pj_bool_t pj_cli_is_quitting ( pj_cli_t cli)

Check if application shutdown or restart has been requested.

Parameters
cliThe CLI application instance.
Returns
PJ_TRUE if pj_cli_quit() has been called.

◆ pj_cli_is_restarting()

pj_bool_t pj_cli_is_restarting ( pj_cli_t cli)

Check if application restart has been requested.

Parameters
cliThe CLI application instance.
Returns
PJ_TRUE if pj_cli_quit() has been called with restart parameter set.

◆ pj_cli_quit()

void pj_cli_quit ( pj_cli_t cli,
pj_cli_sess req,
pj_bool_t  restart 
)

Call this to signal application shutdown. Typically application would call this from it's "Quit" menu or similar command to quit the application.

See also pj_cli_sess_end_session() to end a session instead of quitting the whole application.

Parameters
cliThe CLI application instance.
reqThe session on which the shutdown request is received.
restartIndicate whether application restart is wanted.

◆ pj_cli_register_front_end()

void pj_cli_register_front_end ( pj_cli_t cli,
pj_cli_front_end fe 
)

Register a front end to the CLI application.

Parameters
cliThe CLI application.
feThe CLI front end to be registered.

◆ pj_cli_sess_end_session()

void pj_cli_sess_end_session ( pj_cli_sess sess)

End the specified session, and destroy it to release all resources used by the session.

See also pj_cli_sess and pj_cli_front_end for more info regarding the creation process. See also pj_cli_quit() to quit the whole application instead.

Parameters
sessThe CLI session to be destroyed.

◆ pj_cli_sess_exec()

pj_status_t pj_cli_sess_exec ( pj_cli_sess sess,
char *  cmdline,
pj_pool_t pool,
pj_cli_exec_info info 
)

Execute a command line. This function will parse the input string to find the appropriate command and verify whether the string matches the command specifications. If matches, the command will be executed, and the return value of the command will be set in the cmd_ret field of the info argument, if specified.

See also pj_cli_sess_parse() for more info regarding the cmdline format.

Parameters
sessThe CLI session.
cmdlineThe command line string to be executed.
poolThe pool to allocate memory from.
infoOptional pointer to receive additional information related to the execution of the command (such as the command return value).
Returns
This function returns the status of the command parsing and execution (note that the return value of the handler itself will be returned in info argument, if specified). Please see the return value of pj_cli_sess_parse() for possible return values.

References PJ_END_DECL.

◆ pj_cli_sess_parse()

pj_status_t pj_cli_sess_parse ( pj_cli_sess sess,
char *  cmdline,
pj_cli_cmd_val val,
pj_pool_t pool,
pj_cli_exec_info info 
)

Parse an input cmdline string. The first word of the command line is the command itself, which will be matched against command specifications registered in the CLI application.

Zero or more arguments follow the command name. Arguments are separated by one or more whitespaces. Argument may be placed inside a pair of quotes, double quotes, '{' and '}', or '[' and ']' pairs. This is useful when the argument itself contains whitespaces or other restricted characters. If the quote character itself is to appear in the argument, the argument then must be quoted with different quote characters. There is no character escaping facility provided by this function (such as the use of backslash '\' character).

The cmdline may be followed by an extra newline (LF or CR-LF characters), which will be removed by the function. However any more characters following this newline will cause an error to be returned.

Parameters
sessThe CLI session.
cmdlineThe command line string to be parsed.
valStructure to store the parsing result.
poolThe pool to allocate memory from.
infoAdditional info to be returned regarding the parsing.
Returns
This function returns the status of the parsing, which can be one of the following :
  • PJ_SUCCESS: a command was executed successfully.
  • PJ_EINVAL: invalid parameter to this function.
  • PJ_ENOTFOUND: command is not found.
  • PJ_CLI_EAMBIGUOUS: command/argument is ambiguous.
  • PJ_CLI_EMISSINGARG: missing argument.
  • PJ_CLI_EINVARG: invalid command argument.
  • PJ_CLI_EEXIT: "exit" has been called to end the current session. This is a signal for the application to end it's main loop.

◆ pj_cli_sess_write_msg()

void pj_cli_sess_write_msg ( pj_cli_sess sess,
const char *  buffer,
pj_size_t  len 
)

Write a log message to the specific CLI session.

Parameters
sessThe CLI active session.
bufferThe message itself.
lenLength of this message.

◆ pj_cli_write_log()

void pj_cli_write_log ( pj_cli_t cli,
int  level,
const char *  buffer,
int  len 
)

Write a log message to the CLI application. The CLI application will send the log message to all the registered front-ends.

Parameters
cliThe CLI application instance.
levelVerbosity level of this message message.
bufferThe message itself.
lenLength of this message.

 


PJLIB-UTIL Open Source, small footprint, and portable asynchronous/caching DNS resolver, text scanner, STUN client, and XML library
Copyright (C) 2006-2009 Teluu Inc.