jtag: linuxgpiod: drop extra parenthesis
[openocd.git] / src / helper / command.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 /***************************************************************************
4 * Copyright (C) 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
6 * *
7 * Copyright (C) 2007,2008 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
9 ***************************************************************************/
10
11 #ifndef OPENOCD_HELPER_COMMAND_H
12 #define OPENOCD_HELPER_COMMAND_H
13
14 #include <stdint.h>
15 #include <stdbool.h>
16
17 #include <helper/jim-nvp.h>
18 #include <helper/list.h>
19 #include <helper/types.h>
20
21 /* To achieve C99 printf compatibility in MinGW, gnu_printf should be
22 * used for __attribute__((format( ... ))), with GCC v4.4 or later
23 */
24 #if (defined(IS_MINGW) && (((__GNUC__ << 16) + __GNUC_MINOR__) >= 0x00040004))
25 #define PRINTF_ATTRIBUTE_FORMAT gnu_printf
26 #else
27 #define PRINTF_ATTRIBUTE_FORMAT printf
28 #endif
29
30 /**
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).
38 */
39 enum command_mode {
40 COMMAND_EXEC,
41 COMMAND_CONFIG,
42 COMMAND_ANY,
43 COMMAND_UNKNOWN = -1, /* error condition */
44 };
45
46 struct command_context;
47
48 /** The type signature for command context's output handler. */
49 typedef int (*command_output_handler_t)(struct command_context *context,
50 const char *line);
51
52 struct command_context {
53 Jim_Interp *interp;
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
61 * 2) an event handler
62 * Pay attention to reentrancy when setting override.
63 */
64 command_output_handler_t output_handler;
65 void *output_handler_priv;
66 struct list_head *help_list;
67 };
68
69 struct command;
70
71 /**
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.
75 */
76 struct command_invocation {
77 struct command_context *ctx;
78 struct command *current;
79 const char *name;
80 unsigned argc;
81 const char **argv;
82 Jim_Obj * const *jimtcl_argv;
83 Jim_Obj *output;
84 };
85
86 /**
87 * Return true if the command @c cmd is registered by OpenOCD.
88 */
89 bool jimcmd_is_oocd_command(Jim_Cmd *cmd);
90
91 /**
92 * Return the pointer to the command's private data specified during the
93 * registration of command @a cmd .
94 */
95 void *jimcmd_privdata(Jim_Cmd *cmd);
96
97 /**
98 * Command handlers may be defined with more parameters than the base
99 * set provided by command.c. This macro uses C99 magic to allow
100 * defining all such derivative types using this macro.
101 */
102 #define __COMMAND_HANDLER(name, extra ...) \
103 int name(struct command_invocation *cmd, ## extra)
104
105 /**
106 * Use this to macro to call a command helper (or a nested handler).
107 * It provides command handler authors protection against reordering or
108 * removal of unused parameters.
109 *
110 * @b Note: This macro uses lexical capture to provide some arguments.
111 * As a result, this macro should be used @b only within functions
112 * defined by the COMMAND_HANDLER or COMMAND_HELPER macros. Those
113 * macros provide the expected lexical context captured by this macro.
114 * Furthermore, it should be used only from the top-level of handler or
115 * helper function, or care must be taken to avoid redefining the same
116 * variables in intervening scope(s) by accident.
117 */
118 #define CALL_COMMAND_HANDLER(name, extra ...) \
119 name(cmd, ## extra)
120
121 /**
122 * Always use this macro to define new command handler functions.
123 * It ensures the parameters are ordered, typed, and named properly, so
124 * they be can be used by other macros (e.g. COMMAND_PARSE_NUMBER).
125 * All command handler functions must be defined as static in scope.
126 */
127 #define COMMAND_HANDLER(name) \
128 static __COMMAND_HANDLER(name)
129
130 /**
131 * Similar to COMMAND_HANDLER, except some parameters are expected.
132 * A helper is globally-scoped because it may be shared between several
133 * source files (e.g. the s3c24xx device command helper).
134 */
135 #define COMMAND_HELPER(name, extra ...) __COMMAND_HANDLER(name, extra)
136
137 /**
138 * Use this macro to access the command being handled,
139 * rather than accessing the variable directly. It may be moved.
140 */
141 #define CMD (cmd)
142 /**
143 * Use this macro to access the context of the command being handled,
144 * rather than accessing the variable directly. It may be moved.
145 */
146 #define CMD_CTX (cmd->ctx)
147 /**
148 * Use this macro to access the number of arguments for the command being
149 * handled, rather than accessing the variable directly. It may be moved.
150 */
151 #define CMD_ARGC (cmd->argc)
152 /**
153 * Use this macro to access the arguments for the command being handled,
154 * rather than accessing the variable directly. It may be moved.
155 */
156 #define CMD_ARGV (cmd->argv)
157 /**
158 * Use this macro to access the jimtcl arguments for the command being
159 * handled, rather than accessing the variable directly. It may be moved.
160 */
161 #define CMD_JIMTCL_ARGV (cmd->jimtcl_argv)
162 /**
163 * Use this macro to access the name of the command being handled,
164 * rather than accessing the variable directly. It may be moved.
165 */
166 #define CMD_NAME (cmd->name)
167 /**
168 * Use this macro to access the current command being handled,
169 * rather than accessing the variable directly. It may be moved.
170 */
171 #define CMD_CURRENT (cmd->current)
172 /**
173 * Use this macro to access the invoked command handler's data pointer,
174 * rather than accessing the variable directly. It may be moved.
175 */
176 #define CMD_DATA (CMD_CURRENT->jim_handler_data)
177
178 /**
179 * The type signature for command handling functions. They are
180 * usually registered as part of command_registration, providing
181 * a high-level means for executing a command.
182 *
183 * If the command fails, it *MUST* return a value != ERROR_OK
184 * (many commands break this rule, patches welcome!)
185 *
186 * This is *especially* important for commands such as writing
187 * to flash or verifying memory. The reason is that those commands
188 * can be used by programs to determine if the operation succeeded
189 * or not. If the operation failed, then a program can try
190 * an alternative approach.
191 *
192 * Returning ERROR_COMMAND_SYNTAX_ERROR will have the effect of
193 * printing out the syntax of the command.
194 */
195 typedef __COMMAND_HANDLER((*command_handler_t));
196
197 struct command {
198 char *name;
199 command_handler_t handler;
200 Jim_CmdProc *jim_handler;
201 void *jim_handler_data;
202 /* Command handlers can use it for any handler specific data */
203 struct target *jim_override_target;
204 /* Used only for target of target-prefixed cmd */
205 enum command_mode mode;
206 };
207
208 /*
209 * Return the struct command pointer kept in private data
210 * Used to enforce check on data type
211 */
212 static inline struct command *jim_to_command(Jim_Interp *interp)
213 {
214 return Jim_CmdPrivData(interp);
215 }
216
217 /*
218 * Commands should be registered by filling in one or more of these
219 * structures and passing them to [un]register_commands().
220 *
221 * A conventional format should be used for help strings, to provide both
222 * usage and basic information:
223 * @code
224 * "@<options@> ... - some explanation text"
225 * @endcode
226 *
227 * @param name The name of the command to register, which must not have
228 * been registered previously in the intended context.
229 * @param handler The callback function that will be called. If NULL,
230 * then the command serves as a placeholder for its children or a script.
231 * @param mode The command mode(s) in which this command may be run.
232 * @param help The help text that will be displayed to the user.
233 */
234 struct command_registration {
235 const char *name;
236 command_handler_t handler;
237 Jim_CmdProc *jim_handler;
238 enum command_mode mode;
239 const char *help;
240 /** a string listing the options and arguments, required or optional */
241 const char *usage;
242
243 /**
244 * If non-NULL, the commands in @c chain will be registered in
245 * the same context and scope of this registration record.
246 * This allows modules to inherit lists commands from other
247 * modules.
248 */
249 const struct command_registration *chain;
250 };
251
252 /** Use this as the last entry in an array of command_registration records. */
253 #define COMMAND_REGISTRATION_DONE { .name = NULL, .chain = NULL }
254
255 int __register_commands(struct command_context *cmd_ctx, const char *cmd_prefix,
256 const struct command_registration *cmds, void *data,
257 struct target *override_target);
258
259 /**
260 * Register one or more commands in the specified context, as children
261 * of @c parent (or top-level commends, if NULL). In a registration's
262 * record contains a non-NULL @c chain member and name is NULL, the
263 * commands on the chain will be registered in the same context.
264 * Otherwise, the chained commands are added as children of the command.
265 *
266 * @param cmd_ctx The command_context in which to register the command.
267 * @param cmd_prefix Register this command as a child of this, or NULL to
268 * register a top-level command.
269 * @param cmds Pointer to an array of command_registration records that
270 * contains the desired command parameters. The last record must have
271 * NULL for all fields.
272 * @returns ERROR_OK on success; ERROR_FAIL if any registration fails.
273 */
274 static inline int register_commands(struct command_context *cmd_ctx, const char *cmd_prefix,
275 const struct command_registration *cmds)
276 {
277 return __register_commands(cmd_ctx, cmd_prefix, cmds, NULL, NULL);
278 }
279
280 /**
281 * Register one or more commands, as register_commands(), plus specify
282 * that command should override the current target
283 *
284 * @param cmd_ctx The command_context in which to register the command.
285 * @param cmd_prefix Register this command as a child of this, or NULL to
286 * register a top-level command.
287 * @param cmds Pointer to an array of command_registration records that
288 * contains the desired command parameters. The last record must have
289 * NULL for all fields.
290 * @param target The target that has to override current target.
291 * @returns ERROR_OK on success; ERROR_FAIL if any registration fails.
292 */
293 static inline int register_commands_override_target(struct command_context *cmd_ctx,
294 const char *cmd_prefix, const struct command_registration *cmds,
295 struct target *target)
296 {
297 return __register_commands(cmd_ctx, cmd_prefix, cmds, NULL, target);
298 }
299
300 /**
301 * Register one or more commands, as register_commands(), plus specify
302 * a pointer to command private data that would be accessible through
303 * the macro CMD_DATA. The private data will not be freed when command
304 * is unregistered.
305 *
306 * @param cmd_ctx The command_context in which to register the command.
307 * @param cmd_prefix Register this command as a child of this, or NULL to
308 * register a top-level command.
309 * @param cmds Pointer to an array of command_registration records that
310 * contains the desired command parameters. The last record must have
311 * NULL for all fields.
312 * @param data The command private data.
313 * @returns ERROR_OK on success; ERROR_FAIL if any registration fails.
314 */
315 static inline int register_commands_with_data(struct command_context *cmd_ctx,
316 const char *cmd_prefix, const struct command_registration *cmds,
317 void *data)
318 {
319 return __register_commands(cmd_ctx, cmd_prefix, cmds, data, NULL);
320 }
321
322 /**
323 * Unregisters all commands from the specified context.
324 * @param cmd_ctx The context that will be cleared of registered commands.
325 * @param cmd_prefix If given, only clear commands from under this one command.
326 * @returns ERROR_OK on success, or an error code.
327 */
328 int unregister_all_commands(struct command_context *cmd_ctx,
329 const char *cmd_prefix);
330
331 /**
332 * Unregisters the help for all commands. Used at exit to remove the help
333 * added through the commands 'add_help_text' and 'add_usage_text'.
334 * @param cmd_ctx The context that will be cleared of registered helps.
335 * @returns ERROR_OK on success, or an error code.
336 */
337 int help_del_all_commands(struct command_context *cmd_ctx);
338
339 void command_set_output_handler(struct command_context *context,
340 command_output_handler_t output_handler, void *priv);
341
342
343 int command_context_mode(struct command_context *context, enum command_mode mode);
344
345 /* Return the current command context associated with the Jim interpreter or
346 * alternatively the global default command interpreter
347 */
348 struct command_context *current_command_context(Jim_Interp *interp);
349 /**
350 * Creates a new command context using the startup TCL provided and
351 * the existing Jim interpreter, if any. If interp == NULL, then command_init
352 * creates a command interpreter.
353 */
354 struct command_context *command_init(const char *startup_tcl, Jim_Interp *interp);
355 /**
356 * Shutdown a command context.
357 *
358 * Free the command context and the associated Jim interpreter.
359 *
360 * @param context The command_context that will be destroyed.
361 */
362 void command_exit(struct command_context *context);
363 /**
364 * Creates a copy of an existing command context. This does not create
365 * a deep copy of the command list, so modifications in one context will
366 * affect all shared contexts. The caller must track reference counting
367 * and ensure the commands are freed before destroying the last instance.
368 * @param cmd_ctx The command_context that will be copied.
369 * @returns A new command_context with the same state as the original.
370 */
371 struct command_context *copy_command_context(struct command_context *cmd_ctx);
372 /**
373 * Frees the resources associated with a command context. The commands
374 * are not removed, so unregister_all_commands() must be called first.
375 * @param context The command_context that will be destroyed.
376 */
377 void command_done(struct command_context *context);
378
379 /*
380 * command_print() and command_print_sameline() are used to produce the TCL
381 * output of OpenOCD commands. command_print() automatically adds a '\n' at
382 * the end or the format string. Use command_print_sameline() to avoid the
383 * trailing '\n', e.g. to concatenate the command output in the same line.
384 * The very last '\n' of the command is stripped away (see run_command()).
385 * For commands that strictly require a '\n' as last output character, add
386 * it explicitly with either an empty command_print() or with a '\n' in the
387 * last command_print() and add a comment to document it.
388 */
389 void command_print(struct command_invocation *cmd, const char *format, ...)
390 __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 2, 3)));
391 void command_print_sameline(struct command_invocation *cmd, const char *format, ...)
392 __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 2, 3)));
393
394 int command_run_line(struct command_context *context, char *line);
395 int command_run_linef(struct command_context *context, const char *format, ...)
396 __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 2, 3)));
397 void command_output_text(struct command_context *context, const char *data);
398
399 void process_jim_events(struct command_context *cmd_ctx);
400
401 #define ERROR_COMMAND_CLOSE_CONNECTION (-600)
402 #define ERROR_COMMAND_SYNTAX_ERROR (-601)
403 #define ERROR_COMMAND_NOTFOUND (-602)
404 #define ERROR_COMMAND_ARGUMENT_INVALID (-603)
405 #define ERROR_COMMAND_ARGUMENT_OVERFLOW (-604)
406 #define ERROR_COMMAND_ARGUMENT_UNDERFLOW (-605)
407
408 int parse_ulong(const char *str, unsigned long *ul);
409 int parse_ullong(const char *str, unsigned long long *ul);
410
411 int parse_long(const char *str, long *ul);
412 int parse_llong(const char *str, long long *ul);
413
414 #define DECLARE_PARSE_WRAPPER(name, type) \
415 int parse ## name(const char *str, type * ul)
416
417 DECLARE_PARSE_WRAPPER(_uint, unsigned);
418 DECLARE_PARSE_WRAPPER(_u64, uint64_t);
419 DECLARE_PARSE_WRAPPER(_u32, uint32_t);
420 DECLARE_PARSE_WRAPPER(_u16, uint16_t);
421 DECLARE_PARSE_WRAPPER(_u8, uint8_t);
422
423 DECLARE_PARSE_WRAPPER(_int, int);
424 DECLARE_PARSE_WRAPPER(_s64, int64_t);
425 DECLARE_PARSE_WRAPPER(_s32, int32_t);
426 DECLARE_PARSE_WRAPPER(_s16, int16_t);
427 DECLARE_PARSE_WRAPPER(_s8, int8_t);
428
429 DECLARE_PARSE_WRAPPER(_target_addr, target_addr_t);
430
431 /**
432 * @brief parses the string @a in into @a out as a @a type, or prints
433 * a command error and passes the error code to the caller. If an error
434 * does occur, the calling function will return the error code produced
435 * by the parsing function (one of ERROR_COMMAND_ARGUMENT_*).
436 *
437 * This function may cause the calling function to return immediately,
438 * so it should be used carefully to avoid leaking resources. In most
439 * situations, parsing should be completed in full before proceeding
440 * to allocate resources, and this strategy will most prevents leaks.
441 */
442 #define COMMAND_PARSE_NUMBER(type, in, out) \
443 do { \
444 int retval_macro_tmp = parse_ ## type(in, &(out)); \
445 if (retval_macro_tmp != ERROR_OK) { \
446 command_print(CMD, stringify(out) \
447 " option value ('%s') is not valid", in); \
448 return retval_macro_tmp; \
449 } \
450 } while (0)
451
452 #define COMMAND_PARSE_ADDRESS(in, out) \
453 COMMAND_PARSE_NUMBER(target_addr, in, out)
454
455 /**
456 * @brief parses the command argument at position @a argn into @a out
457 * as a @a type, or prints a command error referring to @a name_str
458 * and passes the error code to the caller. @a argn will be incremented
459 * if no error occurred. Otherwise the calling function will return
460 * the error code produced by the parsing function.
461 *
462 * This function may cause the calling function to return immediately,
463 * so it should be used carefully to avoid leaking resources. In most
464 * situations, parsing should be completed in full before proceeding
465 * to allocate resources, and this strategy will most prevents leaks.
466 */
467 #define COMMAND_PARSE_ADDITIONAL_NUMBER(type, argn, out, name_str) \
468 do { \
469 if (argn+1 >= CMD_ARGC || CMD_ARGV[argn+1][0] == '-') { \
470 command_print(CMD, "no " name_str " given"); \
471 return ERROR_FAIL; \
472 } \
473 ++argn; \
474 COMMAND_PARSE_NUMBER(type, CMD_ARGV[argn], out); \
475 } while (0)
476
477 /**
478 * @brief parses the command argument at position @a argn into @a out
479 * as a @a type if the argument @a argn does not start with '-'.
480 * and passes the error code to the caller. @a argn will be incremented
481 * if no error occurred. Otherwise the calling function will return
482 * the error code produced by the parsing function.
483 *
484 * This function may cause the calling function to return immediately,
485 * so it should be used carefully to avoid leaking resources. In most
486 * situations, parsing should be completed in full before proceeding
487 * to allocate resources, and this strategy will most prevents leaks.
488 */
489 #define COMMAND_PARSE_OPTIONAL_NUMBER(type, argn, out) \
490 do { \
491 if (argn+1 < CMD_ARGC && CMD_ARGV[argn+1][0] != '-') { \
492 ++argn; \
493 COMMAND_PARSE_NUMBER(type, CMD_ARGV[argn], out); \
494 } \
495 } while (0)
496
497 /**
498 * Parse the string @c as a binary parameter, storing the boolean value
499 * in @c out. The strings @c on and @c off are used to match different
500 * strings for true and false options (e.g. "on" and "off" or
501 * "enable" and "disable").
502 */
503 #define COMMAND_PARSE_BOOL(in, out, on, off) \
504 do { \
505 bool value; \
506 int retval_macro_tmp = command_parse_bool_arg(in, &value); \
507 if (retval_macro_tmp != ERROR_OK) { \
508 command_print(CMD, stringify(out) \
509 " option value ('%s') is not valid", in); \
510 command_print(CMD, " choices are '%s' or '%s'", \
511 on, off); \
512 return retval_macro_tmp; \
513 } \
514 out = value; \
515 } while (0)
516
517 int command_parse_bool_arg(const char *in, bool *out);
518 COMMAND_HELPER(handle_command_parse_bool, bool *out, const char *label);
519
520 /** parses an on/off command argument */
521 #define COMMAND_PARSE_ON_OFF(in, out) \
522 COMMAND_PARSE_BOOL(in, out, "on", "off")
523 /** parses an enable/disable command argument */
524 #define COMMAND_PARSE_ENABLE(in, out) \
525 COMMAND_PARSE_BOOL(in, out, "enable", "disable")
526
527 #endif /* OPENOCD_HELPER_COMMAND_H */

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)