OpenOCD
command.c File Reference
Include dependency graph for command.c:

Go to the source code of this file.

Data Structures

struct  help_entry
 
struct  log_capture_state
 

Macros

#define __THIS__FILE__   "command.c"
 
#define DEFINE_PARSE_LONGLONG(name, type, min, max)    DEFINE_PARSE_WRAPPER(name, type, min, max, long long, _llong)
 
#define DEFINE_PARSE_NUM_TYPE(name, type, func, min, max)
 
#define DEFINE_PARSE_ULONGLONG(name, type, min, max)    DEFINE_PARSE_WRAPPER(name, type, min, max, unsigned long long, _ullong)
 
#define DEFINE_PARSE_WRAPPER(name, type, min, max, functype, funcname)
 
#define HELP_LINE_WIDTH(_n)   (int)(76 - (2 * _n))
 
#define JIM_EMBEDDED
 

Functions

static __attribute__ ((format(PRINTF_ATTRIBUTE_FORMAT, 2, 3)))
 
int __register_commands (struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds, void *data, struct target *override_target)
 
static char * alloc_concatenate_strings (int argc, Jim_Obj *const *argv)
 
static bool command_can_run (struct command_context *cmd_ctx, struct command *c, const char *full_name)
 
int command_context_mode (struct command_context *cmd_ctx, enum command_mode mode)
 
void command_done (struct command_context *cmd_ctx)
 Frees the resources associated with a command context. More...
 
void command_exit (struct command_context *context)
 Shutdown a command context. More...
 
static struct commandcommand_find_from_name (Jim_Interp *interp, const char *name)
 Find a openocd command from fullname. More...
 
static void command_free (struct Jim_Interp *interp, void *priv)
 
 COMMAND_HANDLER (handle_echo)
 
 COMMAND_HANDLER (handle_help_add_command)
 
 COMMAND_HANDLER (handle_help_command)
 
 COMMAND_HANDLER (handle_sleep_command)
 
static void command_help_show_indent (unsigned n)
 
static void command_help_show_wrap (const char *str, unsigned n, unsigned n2)
 
static COMMAND_HELPER (command_help_show, struct help_entry *c, bool show_help, const char *cmd_match)
 
static COMMAND_HELPER (command_help_show_list, bool show_help, const char *cmd_match)
 
 COMMAND_HELPER (handle_command_parse_bool, bool *out, const char *label)
 
struct command_contextcommand_init (const char *startup_tcl, Jim_Interp *interp)
 Creates a new command context using the startup TCL provided and the existing Jim interpreter, if any. More...
 
static void command_log_capture_finish (struct log_capture_state *state)
 
static struct log_capture_statecommand_log_capture_start (Jim_Interp *interp)
 
static struct commandcommand_new (struct command_context *cmd_ctx, const char *full_name, const struct command_registration *cr)
 
void command_output_text (struct command_context *context, const char *data)
 
static int command_parse_bool (const char *in, bool *out, const char *on, const char *off)
 
int command_parse_bool_arg (const char *in, bool *out)
 
void command_print (struct command_invocation *cmd, const char *format,...)
 
void command_print_sameline (struct command_invocation *cmd, const char *format,...)
 
static int command_retval_set (Jim_Interp *interp, int retval)
 
int command_run_line (struct command_context *context, char *line)
 
int command_run_linef (struct command_context *context, const char *format,...)
 
void command_set_output_handler (struct command_context *context, command_output_handler_t output_handler, void *priv)
 
struct command_contextcopy_command_context (struct command_context *context)
 Creates a copy of an existing command context. More...
 
struct command_contextcurrent_command_context (Jim_Interp *interp)
 
static int exec_command (Jim_Interp *interp, struct command_context *cmd_ctx, struct command *c, int argc, Jim_Obj *const *argv)
 
static int help_add_command (struct command_context *cmd_ctx, const char *cmd_name, const char *help_text, const char *usage_text)
 
int help_del_all_commands (struct command_context *cmd_ctx)
 Unregisters the help for all commands. More...
 
static int help_del_command (struct command_context *cmd_ctx, const char *cmd_name)
 
static int jim_capture (Jim_Interp *interp, int argc, Jim_Obj *const *argv)
 
static int jim_command_dispatch (Jim_Interp *interp, int argc, Jim_Obj *const *argv)
 
static int jim_command_mode (Jim_Interp *interp, int argc, Jim_Obj *const *argv)
 
static int jim_find (Jim_Interp *interp, int argc, Jim_Obj *const *argv)
 
bool jimcmd_is_oocd_command (Jim_Cmd *cmd)
 Return true if the command cmd is registered by OpenOCD. More...
 
static bool jimcmd_is_proc (Jim_Cmd *cmd)
 
void * jimcmd_privdata (Jim_Cmd *cmd)
 Return the pointer to the command's private data specified during the registration of command cmd . More...
 
void process_jim_events (struct command_context *cmd_ctx)
 
static struct commandregister_command (struct command_context *context, const char *cmd_prefix, const struct command_registration *cr)
 
static int run_command (struct command_context *context, struct command *c, const char **words, unsigned num_words)
 
static char ** script_command_args_alloc (unsigned argc, Jim_Obj *const *argv, unsigned *nwords)
 
static void script_command_args_free (char **words, unsigned nwords)
 
static void script_debug (Jim_Interp *interp, unsigned int argc, Jim_Obj *const *argv)
 
static void tcl_output (void *privData, const char *file, unsigned line, const char *function, const char *string)
 
int unregister_all_commands (struct command_context *context, const char *cmd_prefix)
 Unregisters all commands from the specified context. More...
 
static int unregister_command (struct command_context *context, const char *cmd_prefix, const char *name)
 

Variables

static const struct command_registration command_builtin_handlers []
 
static const struct command_registration command_subcommand_handlers []
 
struct command_contextglobal_cmd_ctx
 

Macro Definition Documentation

◆ __THIS__FILE__

#define __THIS__FILE__   "command.c"

Definition at line 34 of file command.c.

◆ DEFINE_PARSE_LONGLONG

#define DEFINE_PARSE_LONGLONG (   name,
  type,
  min,
  max 
)     DEFINE_PARSE_WRAPPER(name, type, min, max, long long, _llong)

Definition at line 1364 of file command.c.

◆ DEFINE_PARSE_NUM_TYPE

#define DEFINE_PARSE_NUM_TYPE (   name,
  type,
  func,
  min,
  max 
)
Value:
int parse ## name(const char *str, type * ul) \
{ \
if (!*str) { \
LOG_ERROR("Invalid command argument"); \
} \
char *end; \
errno = 0; \
*ul = func(str, &end, 0); \
if (*end) { \
LOG_ERROR("Invalid command argument"); \
} \
if ((max == *ul) && (errno == ERANGE)) { \
LOG_ERROR("Argument overflow"); \
} \
if (min && (min == *ul) && (errno == ERANGE)) { \
LOG_ERROR("Argument underflow"); \
} \
return ERROR_OK; \
}
const char * name
Definition: armv4_5.c:76
#define ERROR_COMMAND_ARGUMENT_UNDERFLOW
Definition: command.h:389
#define ERROR_COMMAND_ARGUMENT_OVERFLOW
Definition: command.h:388
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:387
uint8_t type
Definition: esp_usb_jtag.c:0
#define ERROR_OK
Definition: log.h:155

Definition at line 1310 of file command.c.

◆ DEFINE_PARSE_ULONGLONG

#define DEFINE_PARSE_ULONGLONG (   name,
  type,
  min,
  max 
)     DEFINE_PARSE_WRAPPER(name, type, min, max, unsigned long long, _ullong)

Definition at line 1354 of file command.c.

◆ DEFINE_PARSE_WRAPPER

#define DEFINE_PARSE_WRAPPER (   name,
  type,
  min,
  max,
  functype,
  funcname 
)
Value:
int parse ## name(const char *str, type * ul) \
{ \
functype n; \
int retval = parse ## funcname(str, &n); \
if (retval != ERROR_OK) \
return retval; \
if (n > max) \
if (min) \
*ul = n; \
return ERROR_OK; \
}

Definition at line 1339 of file command.c.

◆ HELP_LINE_WIDTH

#define HELP_LINE_WIDTH (   _n)    (int)(76 - (2 * _n))

Definition at line 736 of file command.c.

◆ JIM_EMBEDDED

#define JIM_EMBEDDED

Definition at line 22 of file command.c.

Function Documentation

◆ __attribute__()

static __attribute__ ( (format(PRINTF_ATTRIBUTE_FORMAT, 2, 3))  )
static

◆ __register_commands()

int __register_commands ( struct command_context cmd_ctx,
const char *  cmd_prefix,
const struct command_registration cmds,
void *  data,
struct target override_target 
)

◆ alloc_concatenate_strings()

static char* alloc_concatenate_strings ( int  argc,
Jim_Obj *const *  argv 
)
static

Definition at line 866 of file command.c.

References alloc_printf(), LOG_ERROR, and NULL.

Referenced by jim_command_mode().

◆ command_can_run()

static bool command_can_run ( struct command_context cmd_ctx,
struct command c,
const char *  full_name 
)
static

◆ command_context_mode()

int command_context_mode ( struct command_context cmd_ctx,
enum command_mode  mode 
)

Definition at line 1290 of file command.c.

References ERROR_COMMAND_SYNTAX_ERROR, ERROR_OK, command_context::mode, and mode.

Referenced by COMMAND_HANDLER(), and openocd_main().

◆ command_done()

void command_done ( struct command_context context)

Frees the resources associated with a command context.

The commands are not removed, so unregister_all_commands() must be called first.

Parameters
contextThe command_context that will be destroyed.

Definition at line 651 of file command.c.

Referenced by add_connection(), command_exit(), remove_connection(), and setup_command_handler().

◆ command_exit()

void command_exit ( struct command_context context)

Shutdown a command context.

Free the command context and the associated Jim interpreter.

Parameters
contextThe command_context that will be destroyed.

Definition at line 1280 of file command.c.

References command_done(), command_context::help_list, and command_context::interp.

Referenced by openocd_main().

◆ command_find_from_name()

static struct command* command_find_from_name ( Jim_Interp *  interp,
const char *  name 
)
static

Find a openocd command from fullname.

Returns
Returns the named command if it is registred in interp. Returns NULL otherwise.

Definition at line 209 of file command.c.

References cmd, jimcmd_is_oocd_command(), jimcmd_is_proc(), jimcmd_privdata(), name, and NULL.

Referenced by __attribute__(), and register_command().

◆ command_free()

static void command_free ( struct Jim_Interp *  interp,
void *  priv 
)
static

Definition at line 261 of file command.c.

References command::name, and priv.

Referenced by register_command().

◆ COMMAND_HANDLER() [1/4]

COMMAND_HANDLER ( handle_echo  )

Definition at line 675 of file command.c.

References CMD_ARGC, CMD_ARGV, ERROR_FAIL, ERROR_OK, LOG_USER, and LOG_USER_N.

◆ COMMAND_HANDLER() [2/4]

COMMAND_HANDLER ( handle_help_add_command  )

◆ COMMAND_HANDLER() [3/4]

COMMAND_HANDLER ( handle_help_command  )

Definition at line 837 of file command.c.

References alloc_printf(), CALL_COMMAND_HANDLER, CMD_ARGC, CMD_ARGV, CMD_NAME, and LOG_ERROR.

◆ COMMAND_HANDLER() [4/4]

COMMAND_HANDLER ( handle_sleep_command  )

Definition at line 1127 of file command.c.

◆ command_help_show_indent()

static void command_help_show_indent ( unsigned  n)
static

Definition at line 738 of file command.c.

References LOG_USER_N.

Referenced by command_help_show_wrap().

◆ command_help_show_wrap()

static void command_help_show_wrap ( const char *  str,
unsigned  n,
unsigned  n2 
)
static

Definition at line 743 of file command.c.

References command_help_show_indent(), HELP_LINE_WIDTH, and LOG_USER.

Referenced by COMMAND_HELPER().

◆ COMMAND_HELPER() [1/3]

static COMMAND_HELPER ( command_help_show  ,
struct help_entry c,
bool  show_help,
const char *  cmd_match 
)
static

◆ COMMAND_HELPER() [2/3]

static COMMAND_HELPER ( command_help_show_list  ,
bool  show_help,
const char *  cmd_match 
)
static

Definition at line 727 of file command.c.

References CALL_COMMAND_HANDLER, CMD_CTX, ERROR_OK, lh(), and list_for_each_entry.

◆ COMMAND_HELPER() [3/3]

COMMAND_HELPER ( handle_command_parse_bool  ,
bool *  out,
const char *  label 
)

◆ command_init()

struct command_context* command_init ( const char *  startup_tcl,
Jim_Interp *  interp 
)

Creates a new command context using the startup TCL provided and the existing Jim interpreter, if any.

If interp == NULL, then command_init creates a command interpreter.

Definition at line 1245 of file command.c.

References command_builtin_handlers, COMMAND_EXEC, command_context::help_list, INIT_LIST_HEAD(), command_context::interp, LOG_ERROR, LOG_USER_N, command_context::mode, NULL, and register_commands().

Referenced by setup_command_handler().

◆ command_log_capture_finish()

static void command_log_capture_finish ( struct log_capture_state state)
static

Definition at line 108 of file command.c.

References length, log_remove_callback(), state, and tcl_output().

Referenced by jim_capture().

◆ command_log_capture_start()

static struct log_capture_state* command_log_capture_start ( Jim_Interp *  interp)
static

Definition at line 71 of file command.c.

References log_capture_state::interp, log_add_callback(), LOG_ERROR, NULL, state, and tcl_output().

Referenced by jim_capture().

◆ command_new()

◆ command_output_text()

void command_output_text ( struct command_context context,
const char *  data 
)

Definition at line 444 of file command.c.

References command_context::output_handler.

Referenced by command_run_line(), target_asciimsg(), and target_hexmsg().

◆ command_parse_bool()

static int command_parse_bool ( const char *  in,
bool *  out,
const char *  on,
const char *  off 
)
static

Definition at line 1372 of file command.c.

References ERROR_COMMAND_SYNTAX_ERROR, and ERROR_OK.

Referenced by command_parse_bool_arg().

◆ command_parse_bool_arg()

int command_parse_bool_arg ( const char *  in,
bool *  out 
)

Definition at line 1384 of file command.c.

References command_parse_bool(), ERROR_COMMAND_SYNTAX_ERROR, and ERROR_OK.

Referenced by COMMAND_HELPER().

◆ command_print()

◆ command_print_sameline()

◆ command_retval_set()

static int command_retval_set ( Jim_Interp *  interp,
int  retval 
)
static

Definition at line 129 of file command.c.

References ERROR_OK, and log_capture_state::interp.

Referenced by exec_command().

◆ command_run_line()

◆ command_run_linef()

int command_run_linef ( struct command_context context,
const char *  format,
  ... 
)

◆ command_set_output_handler()

void command_set_output_handler ( struct command_context context,
command_output_handler_t  output_handler,
void *  priv 
)

◆ copy_command_context()

struct command_context* copy_command_context ( struct command_context cmd_ctx)

Creates a copy of an existing command context.

This does not create a deep copy of the command list, so modifications in one context will affect all shared contexts. The caller must track reference counting and ensure the commands are freed before destroying the last instance.

Parameters
cmd_ctxThe command_context that will be copied.
Returns
A new command_context with the same state as the original.

Definition at line 642 of file command.c.

Referenced by add_connection().

◆ current_command_context()

◆ exec_command()

static int exec_command ( Jim_Interp *  interp,
struct command_context cmd_ctx,
struct command c,
int  argc,
Jim_Obj *const *  argv 
)
static

◆ help_add_command()

static int help_add_command ( struct command_context cmd_ctx,
const char *  cmd_name,
const char *  help_text,
const char *  usage_text 
)
static

◆ help_del_all_commands()

int help_del_all_commands ( struct command_context cmd_ctx)

Unregisters the help for all commands.

Used at exit to remove the help added through the commands 'add_help_text' and 'add_usage_text'.

Parameters
cmd_ctxThe context that will be cleared of registered helps.
Returns
ERROR_OK on success, or an error code.

Definition at line 1024 of file command.c.

References help_entry::cmd_name, ERROR_OK, help_entry::help, command_context::help_list, help_entry::lh, lh(), list_del(), list_for_each_entry_safe, and help_entry::usage.

Referenced by openocd_main().

◆ help_del_command()

static int help_del_command ( struct command_context cmd_ctx,
const char *  cmd_name 
)
static

◆ jim_capture()

static int jim_capture ( Jim_Interp *  interp,
int  argc,
Jim_Obj *const *  argv 
)
static

◆ jim_command_dispatch()

◆ jim_command_mode()

static int jim_command_mode ( Jim_Interp *  interp,
int  argc,
Jim_Obj *const *  argv 
)
static

◆ jim_find()

static int jim_find ( Jim_Interp *  interp,
int  argc,
Jim_Obj *const *  argv 
)
static

Definition at line 660 of file command.c.

References find_file(), command_context::interp, and NULL.

◆ jimcmd_is_oocd_command()

bool jimcmd_is_oocd_command ( Jim_Cmd *  cmd)

Return true if the command cmd is registered by OpenOCD.

Definition at line 54 of file command.c.

References cmd, and jim_command_dispatch().

Referenced by command_find_from_name(), jim_command_mode(), and telnet_auto_complete().

◆ jimcmd_is_proc()

static bool jimcmd_is_proc ( Jim_Cmd *  cmd)
inlinestatic

Definition at line 49 of file command.c.

References cmd.

Referenced by command_find_from_name(), and jim_command_mode().

◆ jimcmd_privdata()

void* jimcmd_privdata ( Jim_Cmd *  cmd)

Return the pointer to the command's private data specified during the registration of command cmd .

Definition at line 59 of file command.c.

References cmd, and NULL.

Referenced by command_find_from_name(), jim_command_mode(), and telnet_auto_complete().

◆ process_jim_events()

void process_jim_events ( struct command_context cmd_ctx)

Definition at line 1299 of file command.c.

References command_context::interp.

Referenced by server_loop().

◆ register_command()

static struct command* register_command ( struct command_context context,
const char *  cmd_prefix,
const struct command_registration cr 
)
static

◆ run_command()

static int run_command ( struct command_context context,
struct command c,
const char **  words,
unsigned  num_words 
)
static

◆ script_command_args_alloc()

static char** script_command_args_alloc ( unsigned  argc,
Jim_Obj *const *  argv,
unsigned *  nwords 
)
static

Definition at line 166 of file command.c.

References NULL, and script_command_args_free().

Referenced by exec_command().

◆ script_command_args_free()

static void script_command_args_free ( char **  words,
unsigned  nwords 
)
static

Definition at line 159 of file command.c.

Referenced by exec_command(), and script_command_args_alloc().

◆ script_debug()

static void script_debug ( Jim_Interp *  interp,
unsigned int  argc,
Jim_Obj *const *  argv 
)
static

Definition at line 142 of file command.c.

References alloc_printf(), debug_level, LOG_DEBUG, and LOG_LVL_DEBUG.

Referenced by jim_command_dispatch().

◆ tcl_output()

static void tcl_output ( void *  privData,
const char *  file,
unsigned  line,
const char *  function,
const char *  string 
)
static

Definition at line 64 of file command.c.

References state.

Referenced by command_log_capture_finish(), and command_log_capture_start().

◆ unregister_all_commands()

int unregister_all_commands ( struct command_context cmd_ctx,
const char *  cmd_prefix 
)

Unregisters all commands from the specified context.

Parameters
cmd_ctxThe context that will be cleared of registered commands.
cmd_prefixIf given, only clear commands from under this one command.
Returns
ERROR_OK on success, or an error code.

Definition at line 416 of file command.c.

References ERROR_OK.

Referenced by openocd_main().

◆ unregister_command()

static int unregister_command ( struct command_context context,
const char *  cmd_prefix,
const char *  name 
)
static

Definition at line 432 of file command.c.

References ERROR_COMMAND_SYNTAX_ERROR, and name.

Referenced by __register_commands().

Variable Documentation

◆ command_builtin_handlers

const struct command_registration command_builtin_handlers[]
static

Definition at line 1127 of file command.c.

Referenced by command_init().

◆ command_subcommand_handlers

const struct command_registration command_subcommand_handlers[]
static
Initial value:
= {
{
.name = "mode",
.mode = COMMAND_ANY,
.jim_handler = jim_command_mode,
.usage = "[command_name ...]",
.help = "Returns the command modes allowed by a command: "
"'any', 'config', or 'exec'. If no command is "
"specified, returns the current command mode. "
"Returns 'unknown' if an unknown command is given. "
"Command can be multiple tokens.",
},
}
static int jim_command_mode(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
Definition: command.c:975
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:247
@ COMMAND_ANY
Definition: command.h:42

Definition at line 1127 of file command.c.

◆ global_cmd_ctx

struct command_context* global_cmd_ctx
extern

Definition at line 292 of file openocd.c.

Referenced by current_command_context().