openocd: revert workarounds for 'expr' syntax change
[openocd.git] / src / helper / command.h
index 4d1928c5a9630ecd03d249e607bebaac7041978e..478e5c8adaed0bc015d577a6cda819d8ab298eb8 100644 (file)
@@ -1,22 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
 /***************************************************************************
  *   Copyright (C) 2005 by Dominic Rath                                    *
  *   Dominic.Rath@gmx.de                                                   *
  *                                                                         *
  *   Copyright (C) 2007,2008 Ã˜yvind Harboe                                 *
  *   oyvind.harboe@zylin.com                                               *
- *                                                                         *
- *   This program is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU General Public License as published by  *
- *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
- *                                                                         *
- *   This program is distributed in the hope that it will be useful,       *
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
- *   GNU General Public License for more details.                          *
- *                                                                         *
- *   You should have received a copy of the GNU General Public License     *
- *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
  ***************************************************************************/
 
 #ifndef OPENOCD_HELPER_COMMAND_H
@@ -24,8 +13,8 @@
 
 #include <stdint.h>
 #include <stdbool.h>
-#include <jim-nvp.h>
 
+#include <helper/jim-nvp.h>
 #include <helper/list.h>
 #include <helper/types.h>
 
 #define PRINTF_ATTRIBUTE_FORMAT printf
 #endif
 
+/**
+ * OpenOCD command mode is COMMAND_CONFIG at start, then switches to COMMAND_EXEC
+ * during the execution of command 'init'.
+ * The field 'mode' in struct command_registration specifies in which command mode
+ * the command can be executed:
+ * - during COMMAND_CONFIG only,
+ * - during COMMAND_EXEC only,
+ * - in both modes (COMMAND_ANY).
+ */
 enum command_mode {
        COMMAND_EXEC,
        COMMAND_CONFIG,
@@ -54,7 +52,6 @@ typedef int (*command_output_handler_t)(struct command_context *context,
 struct command_context {
        Jim_Interp *interp;
        enum command_mode mode;
-       struct command *commands;
        struct target *current_target;
                /* The target set by 'targets xx' command or the latest created */
        struct target *current_target_override;
@@ -85,6 +82,17 @@ struct command_invocation {
        Jim_Obj *output;
 };
 
+/**
+ * Return true if the command @c cmd is registered by OpenOCD.
+ */
+bool jimcmd_is_oocd_command(Jim_Cmd *cmd);
+
+/**
+ * Return the pointer to the command's private data specified during the
+ * registration of command @a cmd .
+ */
+void *jimcmd_privdata(Jim_Cmd *cmd);
+
 /**
  * Command handlers may be defined with more parameters than the base
  * set provided by command.c.  This macro uses C99 magic to allow
@@ -182,8 +190,6 @@ typedef __COMMAND_HANDLER((*command_handler_t));
 
 struct command {
        char *name;
-       struct command *parent;
-       struct command *children;
        command_handler_t handler;
        Jim_CmdProc *jim_handler;
        void *jim_handler_data;
@@ -191,7 +197,6 @@ struct command {
        struct target *jim_override_target;
                /* Used only for target of target-prefixed cmd */
        enum command_mode mode;
-       struct command *next;
 };
 
 /*
@@ -420,7 +425,7 @@ DECLARE_PARSE_WRAPPER(_target_addr, target_addr_t);
 #define COMMAND_PARSE_NUMBER(type, in, out) \
        do { \
                int retval_macro_tmp = parse_ ## type(in, &(out)); \
-               if (ERROR_OK != retval_macro_tmp) { \
+               if (retval_macro_tmp != ERROR_OK) { \
                        command_print(CMD, stringify(out) \
                                " option value ('%s') is not valid", in); \
                        return retval_macro_tmp; \
@@ -430,6 +435,48 @@ DECLARE_PARSE_WRAPPER(_target_addr, target_addr_t);
 #define COMMAND_PARSE_ADDRESS(in, out) \
        COMMAND_PARSE_NUMBER(target_addr, in, out)
 
+/**
+ * @brief parses the command argument at position @a argn into @a out
+ * as a @a type, or prints a command error referring to @a name_str
+ * and passes the error code to the caller. @a argn will be incremented
+ * if no error occurred. Otherwise the calling function will return
+ * the error code produced by the parsing function.
+ *
+ * This function may cause the calling function to return immediately,
+ * so it should be used carefully to avoid leaking resources.  In most
+ * situations, parsing should be completed in full before proceeding
+ * to allocate resources, and this strategy will most prevents leaks.
+ */
+#define COMMAND_PARSE_ADDITIONAL_NUMBER(type, argn, out, name_str) \
+       do { \
+               if (argn+1 >= CMD_ARGC || CMD_ARGV[argn+1][0] == '-') { \
+                       command_print(CMD, "no " name_str " given"); \
+                       return ERROR_FAIL; \
+               } \
+               ++argn; \
+               COMMAND_PARSE_NUMBER(type, CMD_ARGV[argn], out); \
+       } while (0)
+
+/**
+ * @brief parses the command argument at position @a argn into @a out
+ * as a @a type if the argument @a argn does not start with '-'.
+ * and passes the error code to the caller. @a argn will be incremented
+ * if no error occurred. Otherwise the calling function will return
+ * the error code produced by the parsing function.
+ *
+ * This function may cause the calling function to return immediately,
+ * so it should be used carefully to avoid leaking resources.  In most
+ * situations, parsing should be completed in full before proceeding
+ * to allocate resources, and this strategy will most prevents leaks.
+ */
+#define COMMAND_PARSE_OPTIONAL_NUMBER(type, argn, out) \
+       do { \
+               if (argn+1 < CMD_ARGC && CMD_ARGV[argn+1][0] != '-') { \
+                       ++argn; \
+                       COMMAND_PARSE_NUMBER(type, CMD_ARGV[argn], out); \
+               } \
+       } while (0)
+
 /**
  * Parse the string @c as a binary parameter, storing the boolean value
  * in @c out.  The strings @c on and @c off are used to match different
@@ -440,7 +487,7 @@ DECLARE_PARSE_WRAPPER(_target_addr, target_addr_t);
        do { \
                bool value; \
                int retval_macro_tmp = command_parse_bool_arg(in, &value); \
-               if (ERROR_OK != retval_macro_tmp) { \
+               if (retval_macro_tmp != ERROR_OK) { \
                        command_print(CMD, stringify(out) \
                                " option value ('%s') is not valid", in); \
                        command_print(CMD, "  choices are '%s' or '%s'", \
@@ -460,6 +507,4 @@ COMMAND_HELPER(handle_command_parse_bool, bool *out, const char *label);
 #define COMMAND_PARSE_ENABLE(in, out) \
        COMMAND_PARSE_BOOL(in, out, "enable", "disable")
 
-void script_debug(Jim_Interp *interp, unsigned int argc, Jim_Obj * const *argv);
-
 #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)