improve jtag_tap_configure

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.
This commit is contained in:
Zachary T Welch 2009-11-26 13:52:04 -08:00
parent 4ff5eda576
commit 7124be8247
1 changed files with 89 additions and 78 deletions

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