+ nwords = i;
+
+ /* grab the command context from the associated data */
+ context = Jim_GetAssocData(interp, "context");
+ if (context == NULL)
+ {
+ /* Tcl can invoke commands directly instead of via command_run_line(). This would
+ * happen when the Jim Tcl interpreter is provided by eCos.
+ */
+ context = global_cmd_ctx;
+ }
+
+ /* capture log output and return it */
+ Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
+ /* a garbage collect can happen, so we need a reference count to this object */
+ Jim_IncrRefCount(tclOutput);
+
+ log_add_callback(tcl_output, tclOutput);
+
+ // turn words[0] into CMD_ARGV[-1] with this cast
+ retval = run_command(context, c, (const char **)words, nwords);
+
+ log_remove_callback(tcl_output, tclOutput);
+
+ /* We dump output into this local variable */
+ Jim_SetResult(interp, tclOutput);
+ Jim_DecrRefCount(interp, tclOutput);
+
+ for (i = 0; i < nwords; i++)
+ free(words[i]);
+ free(words);
+
+ int *return_retval = Jim_GetAssocData(interp, "retval");
+ if (return_retval != NULL)
+ {
+ *return_retval = retval;
+ }
+
+ return (retval == ERROR_OK)?JIM_OK:JIM_ERR;
+}
+
+static Jim_Obj *command_name_list(struct command *c)
+{
+ Jim_Obj *cmd_list = c->parent ?
+ command_name_list(c->parent) :
+ Jim_NewListObj(interp, NULL, 0);
+ Jim_ListAppendElement(interp, cmd_list,
+ Jim_NewStringObj(interp, c->name, -1));
+
+ return cmd_list;
+}
+
+static void command_helptext_add(Jim_Obj *cmd_list, const char *help)
+{
+ Jim_Obj *cmd_entry = Jim_NewListObj(interp, NULL, 0);
+ Jim_ListAppendElement(interp, cmd_entry, cmd_list);
+ Jim_ListAppendElement(interp, cmd_entry,
+ Jim_NewStringObj(interp, help ? : "", -1));
+
+ /* accumulate help text in Tcl helptext list. */
+ Jim_Obj *helptext = Jim_GetGlobalVariableStr(interp,
+ "ocd_helptext", JIM_ERRMSG);
+ if (Jim_IsShared(helptext))
+ helptext = Jim_DuplicateObj(interp, helptext);
+ Jim_ListAppendElement(interp, helptext, cmd_entry);
+}
+
+/* nice short description of source file */
+#define __THIS__FILE__ "command.c"
+
+/**
+ * Find a command by name from a list of commands.
+ * @returns The named command if found, or NULL.
+ */
+static struct command *command_find(struct command **head, const char *name)
+{
+ assert(head);
+ for (struct command *cc = *head; cc; cc = cc->next)
+ {
+ if (strcmp(cc->name, name) == 0)
+ return cc;
+ }
+ return NULL;