hooks to enable experimentation with scripting language support. Reduces patch size...
authoroharboe <oharboe@b42882b7-edfa-0310-969c-e2dbd0fdcd60>
Fri, 27 Jun 2008 06:58:45 +0000 (06:58 +0000)
committeroharboe <oharboe@b42882b7-edfa-0310-969c-e2dbd0fdcd60>
Fri, 27 Jun 2008 06:58:45 +0000 (06:58 +0000)
git-svn-id: svn://svn.berlios.de/openocd/trunk@733 b42882b7-edfa-0310-969c-e2dbd0fdcd60

src/helper/command.c
src/helper/command.h
src/openocd.c

index 968a8e4b7eeb0c2dcd785e3cf41b0659919619f4..54315dd943ffaf4b09c9aa54c17799695dbadb2d 100644 (file)
@@ -337,10 +337,9 @@ void command_print(command_context_t *context, char *format, ...)
        va_end(ap);
 }
 
-int find_and_run_command(command_context_t *context, command_t *commands, char *words[], int num_words, int start_word)
+command_t *find_command(command_context_t *context, command_t *commands, char *words[], int num_words, int start_word, int *new_start_word)
 {
        command_t *c;
-       int retval = ERROR_COMMAND_SYNTAX_ERROR;
        
        if (unique_length_dirty)
        {
@@ -363,60 +362,67 @@ int find_and_run_command(command_context_t *context, command_t *commands, char *
                        {
                                if (!c->handler)
                                {
-                                       command_print(context, "No handler for command");
-                                       retval = ERROR_COMMAND_SYNTAX_ERROR;
-                                       break;
+                                       return NULL;
                                }
                                else
                                {
-                                       int retval = c->handler(context, c->name, words + start_word + 1, num_words - start_word - 1);
-                                       if (retval == ERROR_COMMAND_SYNTAX_ERROR)
-                                       {
-                                               command_print(context, "Syntax error:");
-                                               command_print_help_line(context, c, 0);
-                                       }
-                                       else if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
-                                       {
-                                               /* just fall through for a shutdown request */
-                                       }
-                                       else if (retval != ERROR_OK)
-                                       {
-                                               /* we do not print out an error message because the command *should*
-                                                * have printed out an error
-                                                */
-                                               LOG_DEBUG("Command failed with error code %d", retval); 
-                                       }
-                                       return retval; 
+                                       *new_start_word=start_word;
+                                       return c;
                                }
                        }
                        else
                        {
                                if (start_word == num_words - 1)
                                {
-                                       command_print(context, "Incomplete command");
-                                       break;
+                                       return NULL;
                                }
-                               return find_and_run_command(context, c->children, words, num_words, start_word + 1);
+                               return find_command(context, c->children, words, num_words, start_word + 1, new_start_word);
                        }
                }
        }
+       return NULL;
+}
+
+int find_and_run_command(command_context_t *context, command_t *commands, char *words[], int num_words)
+{
+       int start_word=0;
+       command_t *c;
+       c=find_command(context, commands, words, num_words, start_word, &start_word);
+       if (c==NULL)
+       {
+               command_print(context, "Command %s not found", words[start_word]);
+               return ERROR_COMMAND_SYNTAX_ERROR;
+       }
        
-       command_print(context, "Command %s not found", words[start_word]);
-       return retval;
+       int retval = c->handler(context, c->name, words + start_word + 1, num_words - start_word - 1);
+       if (retval == ERROR_COMMAND_SYNTAX_ERROR)
+       {
+               command_print(context, "Syntax error:");
+               command_print_help_line(context, c, 0);
+       }
+       else if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
+       {
+               /* just fall through for a shutdown request */
+       }
+       else if (retval != ERROR_OK)
+       {
+               /* we do not print out an error message because the command *should*
+                * have printed out an error
+                */
+               LOG_DEBUG("Command failed with error code %d", retval); 
+       }
+       return retval; 
 }
 
-int command_run_line(command_context_t *context, char *line)
+int command_run_line_internal_op(command_context_t *context, char *line, int run)
 {
+       LOG_USER_N("%s", ""); /* Keep GDB connection alive*/ 
+       
        int nwords;
        char *words[128] = {0};
        int retval;
        int i;
 
-       LOG_USER_N("%s", ""); /* Keep GDB connection alive*/ 
-       
-       if ((!context) || (!line))
-               return ERROR_INVALID_ARGUMENTS;
-       
        /* skip preceding whitespace */
        while (isspace(*line))
                line++;
@@ -434,7 +440,14 @@ int command_run_line(command_context_t *context, char *line)
        nwords = parse_line(line, words, sizeof(words) / sizeof(words[0]));
        
        if (nwords > 0)
-               retval = find_and_run_command(context, context->commands, words, nwords, 0);
+               if (run)
+               {
+                       retval = find_and_run_command(context, context->commands, words, nwords);
+               } else
+               {
+                       int t;
+                       return (find_command(context, context->commands, words, nwords, 0, &t)!=NULL)?ERROR_OK:ERROR_FAIL;
+               }
        else
                return ERROR_INVALID_ARGUMENTS;
        
@@ -444,6 +457,32 @@ int command_run_line(command_context_t *context, char *line)
        return retval;
 }
 
+int command_run_line_internal(command_context_t *context, char *line)
+{
+       return command_run_line_internal_op(context, line, 1);
+}
+
+int command_run_line(command_context_t *context, char *line)
+{
+       int retval;
+       if ((!context) || (!line))
+               return ERROR_INVALID_ARGUMENTS;
+
+       if (command_run_line_internal_op(context, line, 0)!=ERROR_OK)
+       {
+               /* If we can't find a command, then try the interpreter. 
+                * If there is no interpreter implemented, then this will
+                * simply print a syntax error.
+                * 
+                * These hooks were left in to reduce patch size for 
+                * wip to add scripting language.
+                */
+               return jim_command(context, line);
+       }
+
+       return command_run_line_internal(context, line);
+}
+
 int command_run_file(command_context_t *context, FILE *file, enum command_mode mode)
 {
        int retval = ERROR_OK;
@@ -652,7 +691,7 @@ int handle_time_command(struct command_context_s *cmd_ctx, char *cmd, char **arg
        
        duration_start_measure(&duration);
        
-       retval = find_and_run_command(cmd_ctx, cmd_ctx->commands, args, argc, 0);
+       retval = find_and_run_command(cmd_ctx, cmd_ctx->commands, args, argc);
        
        duration_stop_measure(&duration, &duration_text);
        
index ed6a96a70d259f95b0985e5d71bb784e75ac6896..c468247c6f4270e4f63c019ec768e40bbb98287f 100644 (file)
@@ -74,6 +74,7 @@ extern int command_done(command_context_t *context);
 extern void command_print(command_context_t *context, char *format, ...);
 extern void command_print_sameline(command_context_t *context, char *format, ...);
 extern int command_run_line(command_context_t *context, char *line);
+extern int command_run_line_internal(command_context_t *context, char *line);
 extern int command_run_file(command_context_t *context, FILE *file, enum command_mode mode);
 
 
index 946cd93c44842f1b507943f201ae21ecd3598490..a9d38c955014def9095fa8abc6d3677cfcdfe3d5 100644 (file)
@@ -154,6 +154,12 @@ void unlockBigLock()
 {
 }
 
+/* Hook to add scripting language */
+int jim_command(command_context_t *context, char *line)
+{
+       LOG_ERROR("Syntax error");
+       return ERROR_COMMAND_SYNTAX_ERROR;
+}
 
 int main(int argc, char *argv[])
 {

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)