28 #include "jim-eventloop.h"
31 #define __THIS__FILE__ "command.c"
38 const char *cmd_prefix,
const char *
name);
41 const char *cmd_name,
const char *help_text,
const char *usage_text);
48 #if defined(JIM_CMD_ISPROC)
50 return cmd->flags & JIM_CMD_ISPROC;
76 for (
unsigned int i = 0; i < argc; i++) {
77 const char *w = Jim_GetString(argv[i],
NULL);
113 Jim_Obj *jim_name = Jim_NewStringObj(interp,
name, -1);
114 Jim_IncrRefCount(jim_name);
115 Jim_Cmd *
cmd = Jim_GetCommand(interp, jim_name, JIM_NONE);
116 Jim_DecrRefCount(interp, jim_name);
136 LOG_ERROR(
"BUG: command '%s' does not have the "
137 "'.usage' field filled out",
172 if (!context || !cr->
name)
202 if (retval != JIM_OK) {
216 struct target *override_target)
220 for (i = 0; cmds[i].
name || cmds[i].
chain; i++) {
254 for (
unsigned int j = 0; j < i; j++)
261 int unregister_commands_match(struct
command_context *cmd_ctx, const
char *format, ...)
263 Jim_Interp *interp = cmd_ctx->interp;
266 va_start(ap, format);
272 char *query_cmd =
alloc_printf(
"info commands {%s}", query);
277 int retval = Jim_EvalSource(interp,
__THIS__FILE__, __LINE__, query_cmd);
279 if (retval != JIM_OK)
282 Jim_Obj *list = Jim_GetResult(interp);
283 Jim_IncrRefCount(list);
285 int len = Jim_ListLength(interp, list);
286 for (
int i = 0; i < len; i++) {
287 Jim_Obj *elem = Jim_ListGetIndex(interp, list, i);
288 Jim_IncrRefCount(elem);
290 const char *
name = Jim_GetString(elem,
NULL);
294 Jim_DecrRefCount(interp, elem);
299 #if JIM_VERSION >= 80
300 Jim_DeleteCommand(interp, elem);
302 Jim_DeleteCommand(interp,
name);
307 Jim_DecrRefCount(interp, elem);
310 Jim_DecrRefCount(interp, list);
315 const char *cmd_prefix)
320 if (!cmd_prefix || !*cmd_prefix)
321 return unregister_commands_match(context,
"*");
323 int retval = unregister_commands_match(context,
"%s *", cmd_prefix);
327 return unregister_commands_match(context,
"%s", cmd_prefix);
331 const char *cmd_prefix,
const char *
name)
333 if (!context || !
name)
336 if (!cmd_prefix || !*cmd_prefix)
337 return unregister_commands_match(context,
"%s",
name);
339 return unregister_commands_match(context,
"%s %s", cmd_prefix,
name);
353 va_start(ap, format);
362 Jim_AppendString(
cmd->ctx->interp,
cmd->output,
string, -1);
376 va_start(ap, format);
380 strcat(
string,
"\n");
387 Jim_AppendString(
cmd->ctx->interp,
cmd->output,
string, -1);
412 when =
"if Cthulhu is summoned by";
415 LOG_ERROR(
"The '%s' command must be used %s 'init'.",
421 struct command *c,
int argc, Jim_Obj *
const *argv)
424 const char **words = malloc(argc *
sizeof(
char *));
430 for (
int i = 0; i < argc; i++)
431 words[i] = Jim_GetString(argv[i],
NULL);
439 .jimtcl_argv =
argv + 1,
442 cmd.output = Jim_NewEmptyStringObj(context->
interp);
443 Jim_IncrRefCount(
cmd.output);
448 Jim_EvalObjPrefix(context->
interp, Jim_NewStringObj(context->
interp,
"usage", -1), 1,
argv);
453 LOG_DEBUG(
"Command '%s' failed with error code %d",
460 const char *output_txt = Jim_String(
cmd.output);
461 int len = strlen(output_txt);
462 if (len && output_txt[len - 1] ==
'\n')
464 Jim_SetResultString(context->
interp, output_txt, len);
466 Jim_DecrRefCount(context->
interp,
cmd.output);
477 Jim_Obj *error_code = Jim_NewListObj(context->
interp,
NULL, 0);
478 Jim_ListAppendElement(context->
interp, error_code, Jim_NewStringObj(context->
interp,
"OpenOCD", -1));
479 Jim_ListAppendElement(context->
interp, error_code, Jim_NewIntObj(context->
interp, retval));
480 Jim_SetGlobalVariableStr(context->
interp,
"errorCode", error_code);
501 Jim_Interp *interp = context->
interp;
503 Jim_DeleteAssocData(
interp,
"context");
504 retcode = Jim_SetAssocData(
interp,
"context",
NULL, context);
505 if (retcode == JIM_OK) {
506 retcode = Jim_Eval_Named(
interp, line,
NULL, 0);
507 Jim_DeleteAssocData(
interp,
"context");
508 int inner_retcode = Jim_SetAssocData(
interp,
"context",
NULL, old_context);
509 if (retcode == JIM_OK)
510 retcode = inner_retcode;
514 if (retcode == JIM_RETURN)
515 retcode =
interp->returnCode;
517 if (retcode == JIM_OK) {
521 result = Jim_GetString(Jim_GetResult(
interp), &reslen);
529 if (retcode == JIM_EXIT) {
534 Jim_MakeErrorMessage(
interp);
546 va_start(ap, format);
567 *copy_context = *context;
610 static void tcl_output(
void *privData,
const char *file,
unsigned int line,
611 const char *
function,
const char *
string)
614 char *old =
state->output;
644 const char *cmd_result = Jim_GetString(Jim_GetResult(
CMD_CTX->interp),
NULL);
653 if (cmd_result && *cmd_result)
669 bool show_help,
const char *cmd_match);
671 static COMMAND_HELPER(command_help_show_list,
bool show_help,
const char *cmd_match)
680 #define HELP_LINE_WIDTH(_n) (int)(76 - (2 * _n))
684 for (
unsigned int i = 0; i < n; i++)
691 const char *str,
unsigned int n,
unsigned int n2)
693 const char *cp = str, *last = str;
695 const char *next = last;
700 }
while (*next !=
' ' && *next !=
'\t' && *next !=
'\0');
714 bool show_help,
const char *cmd_match)
717 for (
const char *s = strchr(c->
cmd_name,
' '); s; s = strchr(s + 1,
' '))
722 bool is_match = strstr(c->
cmd_name, cmd_match) ||
724 (c->
help && strstr(c->
help, cmd_match));
736 if (is_match && show_help) {
743 const char *stage_msg =
"";
747 stage_msg =
" (configuration command)";
750 stage_msg =
" (command valid any time)";
754 stage_msg =
" (?mode error?)";
775 bool full = strcmp(
CMD_NAME,
"help") == 0;
780 cmd_match = strdup(
"");
785 for (
unsigned int i = 1; i <
CMD_ARGC && cmd_match; ++i) {
786 char *prev = cmd_match;
793 LOG_ERROR(
"unable to build search string");
806 char *all = strdup(argv[0]);
812 for (
int i = 1; i < argc; ++i) {
830 Jim_Obj *js = Jim_NewStringObj(interp, s, -1);
831 Jim_IncrRefCount(js);
833 Jim_Cmd *
cmd = Jim_GetCommand(interp, js, JIM_NONE);
835 int retval = Jim_EvalObjPrefix(interp, js, argc - 2, argv + 2);
836 Jim_DecrRefCount(interp, js);
839 Jim_DecrRefCount(interp, js);
846 Jim_EvalObjPrefix(interp, Jim_NewStringObj(interp,
"usage", -1), 1, argv);
884 Jim_Obj *s = Jim_NewStringObj(interp,
cmd_name, -1);
886 Jim_Cmd *
cmd = Jim_GetCommand(interp, s, JIM_NONE);
887 Jim_DecrRefCount(interp, s);
914 const char *mode_str;
927 mode_str =
"unknown";
967 const char *
cmd_name,
const char *help_text,
const char *usage_text)
980 entry = calloc(1,
sizeof(*entry));
997 char *text = strdup(help_text);
1007 char *text = strdup(usage_text);
1013 entry->
usage = text;
1041 if (strcmp(
CMD_ARGV[1],
"busy") == 0)
1045 }
else if (CMD_ARGC < 1 || CMD_ARGC > 2)
1070 .handler = handle_command_mode,
1071 .usage =
"[command_name ...]",
1072 .help =
"Returns the command modes allowed by a command: "
1073 "'any', 'config', or 'exec'. If no command is "
1074 "specified, returns the current command mode. "
1075 "Returns 'unknown' if an unknown command is given. "
1076 "Command can be multiple tokens.",
1085 .handler = handle_find,
1086 .help =
"find full path to file",
1092 .handler = handle_command_capture,
1093 .help =
"Capture progress output and return as tcl return value. If the "
1094 "progress output was empty, return tcl return value.",
1099 .handler = handle_echo,
1101 .help =
"Logs a message at \"user\" priority. "
1102 "Option \"-n\" suppresses trailing newline",
1103 .usage =
"[-n] string",
1106 .name =
"add_help_text",
1107 .handler = handle_help_add_command,
1109 .help =
"Add new command help text; "
1110 "Command can be multiple tokens.",
1111 .usage =
"command_name helptext_string",
1114 .name =
"add_usage_text",
1115 .handler = handle_help_add_command,
1117 .help =
"Add new command usage text; "
1118 "command can be multiple tokens.",
1119 .usage =
"command_name usage_string",
1123 .handler = handle_sleep_command,
1125 .help =
"Sleep for specified number of milliseconds. "
1126 "\"busy\" will busy wait instead (avoid this).",
1127 .usage =
"milliseconds ['busy']",
1131 .handler = handle_help_command,
1133 .help =
"Show full command help; "
1134 "command can be multiple tokens.",
1135 .usage =
"[command_name]",
1139 .handler = handle_help_command,
1141 .help =
"Show basic command usage; "
1142 "command can be multiple tokens.",
1143 .usage =
"[command_name]",
1148 .help =
"core command group (introspection)",
1168 interp = Jim_CreateInterp();
1170 Jim_RegisterCoreCommands(
interp);
1171 Jim_InitStaticExtensions(
interp);
1178 Jim_SetAssocData(
interp,
"context",
NULL, context);
1179 if (Jim_Eval_Named(
interp, startup_tcl,
"embedded:startup.tcl", 1) == JIM_ERR) {
1180 LOG_ERROR(
"Failed to run startup.tcl (embedded into OpenOCD)");
1181 Jim_MakeErrorMessage(
interp);
1185 Jim_DeleteAssocData(
interp,
"context");
1195 Jim_FreeInterp(context->
interp);
1211 static int recursion;
1216 Jim_ProcessEvents(cmd_ctx->
interp, JIM_ALL_EVENTS | JIM_DONT_WAIT);
1220 #define DEFINE_PARSE_NUM_TYPE(name, type, func, min, max) \
1221 int parse ## name(const char *str, type * ul) \
1224 LOG_ERROR("Invalid command argument"); \
1225 return ERROR_COMMAND_ARGUMENT_INVALID; \
1229 *ul = func(str, &end, 0); \
1231 LOG_ERROR("Invalid command argument"); \
1232 return ERROR_COMMAND_ARGUMENT_INVALID; \
1234 if ((max == *ul) && (errno == ERANGE)) { \
1235 LOG_ERROR("Argument overflow"); \
1236 return ERROR_COMMAND_ARGUMENT_OVERFLOW; \
1238 if (min && (min == *ul) && (errno == ERANGE)) { \
1239 LOG_ERROR("Argument underflow"); \
1240 return ERROR_COMMAND_ARGUMENT_UNDERFLOW; \
1249 #define DEFINE_PARSE_WRAPPER(name, type, min, max, functype, funcname) \
1250 int parse ## name(const char *str, type * ul) \
1253 int retval = parse ## funcname(str, &n); \
1254 if (retval != ERROR_OK) \
1257 return ERROR_COMMAND_ARGUMENT_OVERFLOW; \
1259 return ERROR_COMMAND_ARGUMENT_UNDERFLOW; \
1264 #define DEFINE_PARSE_ULONGLONG(name, type, min, max) \
1265 DEFINE_PARSE_WRAPPER(name, type, min, max, unsigned long long, _ullong)
1274 #define DEFINE_PARSE_LONGLONG(name, type, min, max) \
1275 DEFINE_PARSE_WRAPPER(name, type, min, max, long long, _llong)
1283 const
char *on, const
char *off)
1285 if (strcasecmp(in, on) == 0)
1287 else if (strcasecmp(in, off) == 0)
1309 COMMAND_HELPER(command_parse_str_to_buf,
const char *str,
void *buf,
unsigned int buf_len)
int str_to_buf(const char *str, void *_buf, unsigned int buf_bitsize)
Parse an unsigned number (provided as a zero-terminated string) into a bit buffer whose size is buf_l...
#define ERROR_NUMBER_EXCEEDS_BUFFER
#define ERROR_INVALID_NUMBER
struct command_context * current_command_context(Jim_Interp *interp)
static void command_free(struct Jim_Interp *interp, void *priv)
int command_parse_bool_arg(const char *in, bool *out)
static void script_debug(Jim_Interp *interp, unsigned int argc, Jim_Obj *const *argv)
int help_del_all_commands(struct command_context *cmd_ctx)
Unregisters the help for all commands.
static bool command_can_run(struct command_context *cmd_ctx, struct command *c, const char *full_name)
bool jimcmd_is_oocd_command(Jim_Cmd *cmd)
Return true if the command cmd is registered by OpenOCD.
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
static int command_parse_bool(const char *in, bool *out, const char *on, const char *off)
static enum command_mode get_command_mode(Jim_Interp *interp, const char *cmd_name)
static struct command * register_command(struct command_context *context, const char *cmd_prefix, const struct command_registration *cr)
struct command_context * global_cmd_ctx
static int help_add_command(struct command_context *cmd_ctx, const char *cmd_name, const char *help_text, const char *usage_text)
static void tcl_output(void *privData, const char *file, unsigned int line, const char *function, const char *string)
static struct command * command_new(struct command_context *cmd_ctx, const char *full_name, const struct command_registration *cr)
static COMMAND_HELPER(command_help_show, struct help_entry *c, bool show_help, const char *cmd_match)
static int jim_command_dispatch(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
void command_done(struct command_context *cmd_ctx)
Frees the resources associated with a command context.
void * jimcmd_privdata(Jim_Cmd *cmd)
Return the pointer to the command's private data specified during the registration of command cmd .
static bool jimcmd_is_proc(Jim_Cmd *cmd)
#define HELP_LINE_WIDTH(_n)
void command_print(struct command_invocation *cmd, const char *format,...)
static int unregister_command(struct command_context *context, const char *cmd_prefix, const char *name)
void process_jim_events(struct command_context *cmd_ctx)
static const struct command_registration command_subcommand_handlers[]
COMMAND_HANDLER(handle_find)
struct command_context * copy_command_context(struct command_context *context)
Creates a copy of an existing command context.
void command_exit(struct command_context *context)
Shutdown a command context.
static struct command * command_find_from_name(Jim_Interp *interp, const char *name)
Find a openocd command from fullname.
static char * alloc_concatenate_strings(int argc, const char **argv)
static int jim_exec_command(Jim_Interp *interp, struct command_context *context, struct command *c, int argc, Jim_Obj *const *argv)
static __attribute__((format(PRINTF_ATTRIBUTE_FORMAT, 2, 3)))
void command_output_text(struct command_context *context, const char *data)
#define DEFINE_PARSE_LONGLONG(name, type, min, max)
static int help_del_command(struct command_context *cmd_ctx, const char *cmd_name)
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,...
int command_context_mode(struct command_context *cmd_ctx, enum command_mode mode)
int command_run_linef(struct command_context *context, const char *format,...)
int __register_commands(struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds, void *data, struct target *override_target)
void command_set_output_handler(struct command_context *context, command_output_handler_t output_handler, void *priv)
static const struct command_registration command_builtin_handlers[]
int unregister_all_commands(struct command_context *context, const char *cmd_prefix)
Unregisters all commands from the specified context.
#define DEFINE_PARSE_ULONGLONG(name, type, min, max)
int command_run_line(struct command_context *context, char *line)
#define DEFINE_PARSE_NUM_TYPE(name, type, func, min, max)
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
#define CMD_NAME
Use this macro to access the name of the command being handled, rather than accessing the variable di...
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
int parse_ulong(const char *str, unsigned long *ul)
#define PRINTF_ATTRIBUTE_FORMAT
#define ERROR_COMMAND_SYNTAX_ERROR
#define ERROR_COMMAND_CLOSE_CONNECTION
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
#define CMD_JIMTCL_ARGV
Use this macro to access the jimtcl arguments for the command being handled, rather than accessing th...
int(* command_output_handler_t)(struct command_context *context, const char *line)
The type signature for command context's output handler.
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
static struct command * jim_to_command(Jim_Interp *interp)
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
#define ERROR_COMMAND_ARGUMENT_INVALID
static int register_commands(struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds)
Register one or more commands in the specified context, as children of parent (or top-level commends,...
command_mode
OpenOCD command mode is COMMAND_CONFIG at start, then switches to COMMAND_EXEC during the execution o...
char * find_file(const char *file)
static struct esp_usb_jtag * priv
void jtag_poll_unmask(bool saved)
Restore saved mask for polling.
bool jtag_poll_mask(void)
Mask (disable) polling and return the current mask status that should be feed to jtag_poll_unmask() t...
The JTAG interface can be implemented with a software or hardware fifo.
static void list_add(struct list_head *new, struct list_head *head)
#define list_for_each_entry_reverse(p, h, field)
#define list_for_each_entry_safe(p, n, h, field)
#define list_for_each_entry(p, h, field)
static void list_del(struct list_head *entry)
static void INIT_LIST_HEAD(struct list_head *list)
int log_remove_callback(log_callback_fn fn, void *priv)
int log_add_callback(log_callback_fn fn, void *priv)
char * alloc_vprintf(const char *fmt, va_list ap)
void busy_sleep(uint64_t ms)
char * alloc_printf(const char *format,...)
#define LOG_USER(expr ...)
#define LOG_USER_N(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_LEVEL_IS(FOO)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
static uint32_t lh(unsigned int rd, unsigned int base, int16_t offset) __attribute__((unused))
struct list_head * help_list
void * output_handler_priv
struct target * current_target_override
command_output_handler_t output_handler
When run_command is called, a new instance will be created on the stack, filled with the proper value...
command_handler_t handler
const struct command_registration * chain
If non-NULL, the commands in chain will be registered in the same context and scope of this registrat...
const char * usage
a string listing the options and arguments, required or optional
command_handler_t handler
struct target * jim_override_target
int target_call_timer_callbacks_now(void)
Invoke this to ensure that e.g.
int target_call_timer_callbacks(void)