improve jtag_tap_configure
authorZachary T Welch <zw@superlucidity.net>
Thu, 26 Nov 2009 21:52:04 +0000 (13:52 -0800)
committerZachary T Welch <zw@superlucidity.net>
Sat, 28 Nov 2009 21:00:39 +0000 (13:00 -0800)
Splits bulk of the jtag_tap_configure into jtag_tap_configure_event,
removing three or four levels of indentation in the process.
The resulting code was stylistically improved in other ways, but it
should be functionally identical.

src/jtag/tcl.c

index 83719671194f9344131571894acb48ef803b2605..bb86a325b0da2de65925e3efdf18671168b3bddb 100644 (file)
@@ -293,94 +293,105 @@ static Jim_Nvp nvp_config_opts[] = {
        { .name = NULL,          .value = -1 }
 };
 
        { .name = NULL,          .value = -1 }
 };
 
-static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap * tap)
+static int jtag_tap_configure_event(Jim_GetOptInfo *goi, struct jtag_tap * tap)
 {
 {
+       if (goi->argc == 0)
+       {
+               Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
+               return JIM_ERR;
+       }
+
        Jim_Nvp *n;
        Jim_Nvp *n;
-       Jim_Obj *o;
-       int e;
+       int e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
+       if (e != JIM_OK)
+       {
+               Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
+               return e;
+       }
+
+       if (goi->isconfigure) {
+               if (goi->argc != 1) {
+                       Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> <event-body>");
+                       return JIM_ERR;
+               }
+       } else {
+               if (goi->argc != 0) {
+                       Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name>");
+                       return JIM_ERR;
+               }
+       }
+
+       struct jtag_tap_event_action *jteap  = tap->event_action;
+       /* replace existing event body */
+       bool found = false;
+       while (jteap)
+       {
+               if (jteap->event == (enum jtag_event)n->value)
+               {
+                       found = true;
+                       break;
+               }
+               jteap = jteap->next;
+       }
+
+       Jim_SetEmptyResult(goi->interp);
+
+       if (goi->isconfigure)
+       {
+               if (!found)
+                       jteap = calloc(1, sizeof(*jteap));
+               else if (NULL != jteap->body)
+                       Jim_DecrRefCount(interp, jteap->body);
+
+               jteap->event = n->value;
+
+               Jim_Obj *o;
+               Jim_GetOpt_Obj(goi, &o);
+               jteap->body = Jim_DuplicateObj(goi->interp, o);
+               Jim_IncrRefCount(jteap->body);
+
+               if (!found)
+               {
+                       /* add to head of event list */
+                       jteap->next = tap->event_action;
+                       tap->event_action = jteap;
+               }
+       }
+       else if (found)
+       {
+               Jim_SetResult(goi->interp,
+                       Jim_DuplicateObj(goi->interp, jteap->body));
+       }
+       return JIM_OK;
+}
 
 
+static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap * tap)
+{
        /* parse config or cget options */
        /* parse config or cget options */
-       while (goi->argc > 0) {
+       while (goi->argc > 0)
+       {
                Jim_SetEmptyResult (goi->interp);
 
                Jim_SetEmptyResult (goi->interp);
 
-               e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
-               if (e != JIM_OK) {
+               Jim_Nvp *n;
+               int e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
+               if (e != JIM_OK)
+               {
                        Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
                        return e;
                }
 
                        Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
                        return e;
                }
 
-               switch (n->value) {
-                       case JCFG_EVENT:
-                               if (goi->argc == 0) {
-                                       Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
-                                       return JIM_ERR;
-                               }
-
-                               e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
-                               if (e != JIM_OK) {
-                                       Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
-                                       return e;
-                               }
-
-                               if (goi->isconfigure) {
-                                       if (goi->argc != 1) {
-                                               Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
-                                               return JIM_ERR;
-                                       }
-                               } else {
-                                       if (goi->argc != 0) {
-                                               Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
-                                               return JIM_ERR;
-                                       }
-                               }
-
-                               {
-                                       struct jtag_tap_event_action *jteap;
-
-                                       jteap = tap->event_action;
-                                       /* replace existing? */
-                                       while (jteap) {
-                                               if (jteap->event == (enum jtag_event)n->value) {
-                                                       break;
-                                               }
-                                               jteap = jteap->next;
-                                       }
-
-                                       if (goi->isconfigure) {
-                                               bool replace = true;
-                                               if (jteap == NULL) {
-                                                       /* create new */
-                                                       jteap = calloc(1, sizeof (*jteap));
-                                                       replace = false;
-                                               }
-                                               jteap->event = n->value;
-                                               Jim_GetOpt_Obj(goi, &o);
-                                               if (jteap->body) {
-                                                       Jim_DecrRefCount(interp, jteap->body);
-                                               }
-                                               jteap->body = Jim_DuplicateObj(goi->interp, o);
-                                               Jim_IncrRefCount(jteap->body);
-
-                                               if (!replace)
-                                               {
-                                                       /* add to head of event list */
-                                                       jteap->next = tap->event_action;
-                                                       tap->event_action = jteap;
-                                               }
-                                               Jim_SetEmptyResult(goi->interp);
-                                       } else {
-                                               /* get */
-                                               if (jteap == NULL) {
-                                                       Jim_SetEmptyResult(goi->interp);
-                                               } else {
-                                                       Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, jteap->body));
-                                               }
-                                       }
-                               }
-                               /* loop for more */
-                               break;
+               switch (n->value)
+               {
+               case JCFG_EVENT:
+                       e = jtag_tap_configure_event(goi, tap);
+                       if (e != JIM_OK)
+                               return e;
+                       break;
+               default:
+                       Jim_SetResult_sprintf(goi->interp, "unknown event: %s", n->name);
+                       return JIM_ERR;
                }
                }
-       } /* while (goi->argc) */
+       }
 
        return JIM_OK;
 }
 
        return JIM_OK;
 }

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)