1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /***************************************************************************
4 * Copyright (C) 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
7 * Copyright (C) 2007,2008 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
9 ***************************************************************************/
11 #ifndef OPENOCD_HELPER_COMMAND_H
12 #define OPENOCD_HELPER_COMMAND_H
17 #include <helper/jim-nvp.h>
18 #include <helper/list.h>
19 #include <helper/types.h>
21 /* To achieve C99 printf compatibility in MinGW, gnu_printf should be
22 * used for __attribute__((format( ... ))), with GCC v4.4 or later
24 #if (defined(IS_MINGW) && (((__GNUC__ << 16) + __GNUC_MINOR__) >= 0x00040004))
25 #define PRINTF_ATTRIBUTE_FORMAT gnu_printf
27 #define PRINTF_ATTRIBUTE_FORMAT printf
31 * OpenOCD command mode is COMMAND_CONFIG at start, then switches to COMMAND_EXEC
32 * during the execution of command 'init'.
33 * The field 'mode' in struct command_registration specifies in which command mode
34 * the command can be executed:
35 * - during COMMAND_CONFIG only,
36 * - during COMMAND_EXEC only,
37 * - in both modes (COMMAND_ANY).
43 COMMAND_UNKNOWN = -1, /* error condition */
46 struct command_context;
48 /** The type signature for command context's output handler. */
49 typedef int (*command_output_handler_t)(struct command_context *context,
52 struct command_context {
54 enum command_mode mode;
55 struct target *current_target;
56 /* The target set by 'targets xx' command or the latest created */
57 struct target *current_target_override;
58 /* If set overrides current_target
59 * It happens during processing of
60 * 1) a target prefixed command
62 * Pay attention to reentrancy when setting override.
64 command_output_handler_t output_handler;
65 void *output_handler_priv;
66 struct list_head *help_list;
72 * When run_command is called, a new instance will be created on the
73 * stack, filled with the proper values, and passed by reference to the
74 * required COMMAND_HANDLER routine.
76 struct command_invocation {
77 struct command_context *ctx;
78 struct command *current;
86 * Return true if the command @c cmd is registered by OpenOCD.
88 bool jimcmd_is_oocd_command(Jim_Cmd *cmd);
91 * Return the pointer to the command's private data specified during the
92 * registration of command @a cmd .
94 void *jimcmd_privdata(Jim_Cmd *cmd);
97 * Command handlers may be defined with more parameters than the base
98 * set provided by command.c. This macro uses C99 magic to allow
99 * defining all such derivative types using this macro.
101 #define __COMMAND_HANDLER(name, extra ...) \
102 int name(struct command_invocation *cmd, ## extra)
105 * Use this to macro to call a command helper (or a nested handler).
106 * It provides command handler authors protection against reordering or
107 * removal of unused parameters.
109 * @b Note: This macro uses lexical capture to provide some arguments.
110 * As a result, this macro should be used @b only within functions
111 * defined by the COMMAND_HANDLER or COMMAND_HELPER macros. Those
112 * macros provide the expected lexical context captured by this macro.
113 * Furthermore, it should be used only from the top-level of handler or
114 * helper function, or care must be taken to avoid redefining the same
115 * variables in intervening scope(s) by accident.
117 #define CALL_COMMAND_HANDLER(name, extra ...) \
121 * Always use this macro to define new command handler functions.
122 * It ensures the parameters are ordered, typed, and named properly, so
123 * they be can be used by other macros (e.g. COMMAND_PARSE_NUMBER).
124 * All command handler functions must be defined as static in scope.
126 #define COMMAND_HANDLER(name) \
127 static __COMMAND_HANDLER(name)
130 * Similar to COMMAND_HANDLER, except some parameters are expected.
131 * A helper is globally-scoped because it may be shared between several
132 * source files (e.g. the s3c24xx device command helper).
134 #define COMMAND_HELPER(name, extra ...) __COMMAND_HANDLER(name, extra)
137 * Use this macro to access the command being handled,
138 * rather than accessing the variable directly. It may be moved.
142 * Use this macro to access the context of the command being handled,
143 * rather than accessing the variable directly. It may be moved.
145 #define CMD_CTX (cmd->ctx)
147 * Use this macro to access the number of arguments for the command being
148 * handled, rather than accessing the variable directly. It may be moved.
150 #define CMD_ARGC (cmd->argc)
152 * Use this macro to access the arguments for the command being handled,
153 * rather than accessing the variable directly. It may be moved.
155 #define CMD_ARGV (cmd->argv)
157 * Use this macro to access the name of the command being handled,
158 * rather than accessing the variable directly. It may be moved.
160 #define CMD_NAME (cmd->name)
162 * Use this macro to access the current command being handled,
163 * rather than accessing the variable directly. It may be moved.
165 #define CMD_CURRENT (cmd->current)
167 * Use this macro to access the invoked command handler's data pointer,
168 * rather than accessing the variable directly. It may be moved.
170 #define CMD_DATA (CMD_CURRENT->jim_handler_data)
173 * The type signature for command handling functions. They are
174 * usually registered as part of command_registration, providing
175 * a high-level means for executing a command.
177 * If the command fails, it *MUST* return a value != ERROR_OK
178 * (many commands break this rule, patches welcome!)
180 * This is *especially* important for commands such as writing
181 * to flash or verifying memory. The reason is that those commands
182 * can be used by programs to determine if the operation succeeded
183 * or not. If the operation failed, then a program can try
184 * an alternative approach.
186 * Returning ERROR_COMMAND_SYNTAX_ERROR will have the effect of
187 * printing out the syntax of the command.
189 typedef __COMMAND_HANDLER((*command_handler_t));
193 command_handler_t handler;
194 Jim_CmdProc *jim_handler;
195 void *jim_handler_data;
196 /* Command handlers can use it for any handler specific data */
197 struct target *jim_override_target;
198 /* Used only for target of target-prefixed cmd */
199 enum command_mode mode;
203 * Return the struct command pointer kept in private data
204 * Used to enforce check on data type
206 static inline struct command *jim_to_command(Jim_Interp *interp)
208 return Jim_CmdPrivData(interp);
212 * Commands should be registered by filling in one or more of these
213 * structures and passing them to [un]register_commands().
215 * A conventional format should be used for help strings, to provide both
216 * usage and basic information:
218 * "@<options@> ... - some explanation text"
221 * @param name The name of the command to register, which must not have
222 * been registered previously in the intended context.
223 * @param handler The callback function that will be called. If NULL,
224 * then the command serves as a placeholder for its children or a script.
225 * @param mode The command mode(s) in which this command may be run.
226 * @param help The help text that will be displayed to the user.
228 struct command_registration {
230 command_handler_t handler;
231 Jim_CmdProc *jim_handler;
232 enum command_mode mode;
234 /** a string listing the options and arguments, required or optional */
238 * If non-NULL, the commands in @c chain will be registered in
239 * the same context and scope of this registration record.
240 * This allows modules to inherit lists commands from other
243 const struct command_registration *chain;
246 /** Use this as the last entry in an array of command_registration records. */
247 #define COMMAND_REGISTRATION_DONE { .name = NULL, .chain = NULL }
249 int __register_commands(struct command_context *cmd_ctx, const char *cmd_prefix,
250 const struct command_registration *cmds, void *data,
251 struct target *override_target);
254 * Register one or more commands in the specified context, as children
255 * of @c parent (or top-level commends, if NULL). In a registration's
256 * record contains a non-NULL @c chain member and name is NULL, the
257 * commands on the chain will be registered in the same context.
258 * Otherwise, the chained commands are added as children of the command.
260 * @param cmd_ctx The command_context in which to register the command.
261 * @param cmd_prefix Register this command as a child of this, or NULL to
262 * register a top-level command.
263 * @param cmds Pointer to an array of command_registration records that
264 * contains the desired command parameters. The last record must have
265 * NULL for all fields.
266 * @returns ERROR_OK on success; ERROR_FAIL if any registration fails.
268 static inline int register_commands(struct command_context *cmd_ctx, const char *cmd_prefix,
269 const struct command_registration *cmds)
271 return __register_commands(cmd_ctx, cmd_prefix, cmds, NULL, NULL);
275 * Register one or more commands, as register_commands(), plus specify
276 * that command should override the current target
278 * @param cmd_ctx The command_context in which to register the command.
279 * @param cmd_prefix Register this command as a child of this, or NULL to
280 * register a top-level command.
281 * @param cmds Pointer to an array of command_registration records that
282 * contains the desired command parameters. The last record must have
283 * NULL for all fields.
284 * @param target The target that has to override current target.
285 * @returns ERROR_OK on success; ERROR_FAIL if any registration fails.
287 static inline int register_commands_override_target(struct command_context *cmd_ctx,
288 const char *cmd_prefix, const struct command_registration *cmds,
289 struct target *target)
291 return __register_commands(cmd_ctx, cmd_prefix, cmds, NULL, target);
295 * Register one or more commands, as register_commands(), plus specify
296 * a pointer to command private data that would be accessible through
297 * the macro CMD_DATA. The private data will not be freed when command
300 * @param cmd_ctx The command_context in which to register the command.
301 * @param cmd_prefix Register this command as a child of this, or NULL to
302 * register a top-level command.
303 * @param cmds Pointer to an array of command_registration records that
304 * contains the desired command parameters. The last record must have
305 * NULL for all fields.
306 * @param data The command private data.
307 * @returns ERROR_OK on success; ERROR_FAIL if any registration fails.
309 static inline int register_commands_with_data(struct command_context *cmd_ctx,
310 const char *cmd_prefix, const struct command_registration *cmds,
313 return __register_commands(cmd_ctx, cmd_prefix, cmds, data, NULL);
317 * Unregisters all commands from the specified context.
318 * @param cmd_ctx The context that will be cleared of registered commands.
319 * @param cmd_prefix If given, only clear commands from under this one command.
320 * @returns ERROR_OK on success, or an error code.
322 int unregister_all_commands(struct command_context *cmd_ctx,
323 const char *cmd_prefix);
326 * Unregisters the help for all commands. Used at exit to remove the help
327 * added through the commands 'add_help_text' and 'add_usage_text'.
328 * @param cmd_ctx The context that will be cleared of registered helps.
329 * @returns ERROR_OK on success, or an error code.
331 int help_del_all_commands(struct command_context *cmd_ctx);
333 void command_set_output_handler(struct command_context *context,
334 command_output_handler_t output_handler, void *priv);
337 int command_context_mode(struct command_context *context, enum command_mode mode);
339 /* Return the current command context associated with the Jim interpreter or
340 * alternatively the global default command interpreter
342 struct command_context *current_command_context(Jim_Interp *interp);
344 * Creates a new command context using the startup TCL provided and
345 * the existing Jim interpreter, if any. If interp == NULL, then command_init
346 * creates a command interpreter.
348 struct command_context *command_init(const char *startup_tcl, Jim_Interp *interp);
350 * Shutdown a command context.
352 * Free the command context and the associated Jim interpreter.
354 * @param context The command_context that will be destroyed.
356 void command_exit(struct command_context *context);
358 * Creates a copy of an existing command context. This does not create
359 * a deep copy of the command list, so modifications in one context will
360 * affect all shared contexts. The caller must track reference counting
361 * and ensure the commands are freed before destroying the last instance.
362 * @param cmd_ctx The command_context that will be copied.
363 * @returns A new command_context with the same state as the original.
365 struct command_context *copy_command_context(struct command_context *cmd_ctx);
367 * Frees the resources associated with a command context. The commands
368 * are not removed, so unregister_all_commands() must be called first.
369 * @param context The command_context that will be destroyed.
371 void command_done(struct command_context *context);
373 void command_print(struct command_invocation *cmd, const char *format, ...)
374 __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 2, 3)));
375 void command_print_sameline(struct command_invocation *cmd, const char *format, ...)
376 __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 2, 3)));
377 int command_run_line(struct command_context *context, char *line);
378 int command_run_linef(struct command_context *context, const char *format, ...)
379 __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 2, 3)));
380 void command_output_text(struct command_context *context, const char *data);
382 void process_jim_events(struct command_context *cmd_ctx);
384 #define ERROR_COMMAND_CLOSE_CONNECTION (-600)
385 #define ERROR_COMMAND_SYNTAX_ERROR (-601)
386 #define ERROR_COMMAND_NOTFOUND (-602)
387 #define ERROR_COMMAND_ARGUMENT_INVALID (-603)
388 #define ERROR_COMMAND_ARGUMENT_OVERFLOW (-604)
389 #define ERROR_COMMAND_ARGUMENT_UNDERFLOW (-605)
391 int parse_ulong(const char *str, unsigned long *ul);
392 int parse_ullong(const char *str, unsigned long long *ul);
394 int parse_long(const char *str, long *ul);
395 int parse_llong(const char *str, long long *ul);
397 #define DECLARE_PARSE_WRAPPER(name, type) \
398 int parse ## name(const char *str, type * ul)
400 DECLARE_PARSE_WRAPPER(_uint, unsigned);
401 DECLARE_PARSE_WRAPPER(_u64, uint64_t);
402 DECLARE_PARSE_WRAPPER(_u32, uint32_t);
403 DECLARE_PARSE_WRAPPER(_u16, uint16_t);
404 DECLARE_PARSE_WRAPPER(_u8, uint8_t);
406 DECLARE_PARSE_WRAPPER(_int, int);
407 DECLARE_PARSE_WRAPPER(_s64, int64_t);
408 DECLARE_PARSE_WRAPPER(_s32, int32_t);
409 DECLARE_PARSE_WRAPPER(_s16, int16_t);
410 DECLARE_PARSE_WRAPPER(_s8, int8_t);
412 DECLARE_PARSE_WRAPPER(_target_addr, target_addr_t);
415 * @brief parses the string @a in into @a out as a @a type, or prints
416 * a command error and passes the error code to the caller. If an error
417 * does occur, the calling function will return the error code produced
418 * by the parsing function (one of ERROR_COMMAND_ARGUMENT_*).
420 * This function may cause the calling function to return immediately,
421 * so it should be used carefully to avoid leaking resources. In most
422 * situations, parsing should be completed in full before proceeding
423 * to allocate resources, and this strategy will most prevents leaks.
425 #define COMMAND_PARSE_NUMBER(type, in, out) \
427 int retval_macro_tmp = parse_ ## type(in, &(out)); \
428 if (retval_macro_tmp != ERROR_OK) { \
429 command_print(CMD, stringify(out) \
430 " option value ('%s') is not valid", in); \
431 return retval_macro_tmp; \
435 #define COMMAND_PARSE_ADDRESS(in, out) \
436 COMMAND_PARSE_NUMBER(target_addr, in, out)
439 * @brief parses the command argument at position @a argn into @a out
440 * as a @a type, or prints a command error referring to @a name_str
441 * and passes the error code to the caller. @a argn will be incremented
442 * if no error occurred. Otherwise the calling function will return
443 * the error code produced by the parsing function.
445 * This function may cause the calling function to return immediately,
446 * so it should be used carefully to avoid leaking resources. In most
447 * situations, parsing should be completed in full before proceeding
448 * to allocate resources, and this strategy will most prevents leaks.
450 #define COMMAND_PARSE_ADDITIONAL_NUMBER(type, argn, out, name_str) \
452 if (argn+1 >= CMD_ARGC || CMD_ARGV[argn+1][0] == '-') { \
453 command_print(CMD, "no " name_str " given"); \
457 COMMAND_PARSE_NUMBER(type, CMD_ARGV[argn], out); \
461 * @brief parses the command argument at position @a argn into @a out
462 * as a @a type if the argument @a argn does not start with '-'.
463 * and passes the error code to the caller. @a argn will be incremented
464 * if no error occurred. Otherwise the calling function will return
465 * the error code produced by the parsing function.
467 * This function may cause the calling function to return immediately,
468 * so it should be used carefully to avoid leaking resources. In most
469 * situations, parsing should be completed in full before proceeding
470 * to allocate resources, and this strategy will most prevents leaks.
472 #define COMMAND_PARSE_OPTIONAL_NUMBER(type, argn, out) \
474 if (argn+1 < CMD_ARGC && CMD_ARGV[argn+1][0] != '-') { \
476 COMMAND_PARSE_NUMBER(type, CMD_ARGV[argn], out); \
481 * Parse the string @c as a binary parameter, storing the boolean value
482 * in @c out. The strings @c on and @c off are used to match different
483 * strings for true and false options (e.g. "on" and "off" or
484 * "enable" and "disable").
486 #define COMMAND_PARSE_BOOL(in, out, on, off) \
489 int retval_macro_tmp = command_parse_bool_arg(in, &value); \
490 if (retval_macro_tmp != ERROR_OK) { \
491 command_print(CMD, stringify(out) \
492 " option value ('%s') is not valid", in); \
493 command_print(CMD, " choices are '%s' or '%s'", \
495 return retval_macro_tmp; \
500 int command_parse_bool_arg(const char *in, bool *out);
501 COMMAND_HELPER(handle_command_parse_bool, bool *out, const char *label);
503 /** parses an on/off command argument */
504 #define COMMAND_PARSE_ON_OFF(in, out) \
505 COMMAND_PARSE_BOOL(in, out, "on", "off")
506 /** parses an enable/disable command argument */
507 #define COMMAND_PARSE_ENABLE(in, out) \
508 COMMAND_PARSE_BOOL(in, out, "enable", "disable")
510 #endif /* OPENOCD_HELPER_COMMAND_H */