+ *nwords = i;
+ return words;
+}
+
+struct command_context *current_command_context(Jim_Interp *interp)
+{
+ /* grab the command context from the associated data */
+ struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
+ if (NULL == cmd_ctx)
+ {
+ /* Tcl can invoke commands directly instead of via command_run_line(). This would
+ * happen when the Jim Tcl interpreter is provided by eCos or if we are running
+ * commands in a startup script.
+ *
+ * A telnet or gdb server would provide a non-default command context to
+ * handle piping of error output, have a separate current target, etc.
+ */
+ cmd_ctx = global_cmd_ctx;
+ }
+ return cmd_ctx;
+}
+
+static int script_command_run(Jim_Interp *interp,
+ int argc, Jim_Obj *const *argv, struct command *c, bool capture)
+{
+ target_call_timer_callbacks_now();
+ LOG_USER_N("%s", ""); /* Keep GDB connection alive*/
+
+ unsigned nwords;
+ const char **words = script_command_args_alloc(argc, argv, &nwords);
+ if (NULL == words)
+ return JIM_ERR;
+
+ struct log_capture_state *state = NULL;
+ if (capture)
+ state = command_log_capture_start(interp);
+
+ struct command_context *cmd_ctx = current_command_context(interp);
+ int retval = run_command(cmd_ctx, c, (const char **)words, nwords);
+
+ command_log_capture_finish(state);
+
+ script_command_args_free(words, nwords);
+ return command_retval_set(interp, retval);
+}
+
+static int script_command(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ /* the private data is stashed in the interp structure */
+
+ struct command *c = interp->cmdPrivData;
+ assert(c);
+ script_debug(interp, c->name, argc, argv);
+ return script_command_run(interp, argc, argv, c, true);
+}
+
+static struct command *command_root(struct command *c)
+{
+ while (NULL != c->parent)
+ c = c->parent;
+ return c;
+}
+
+/**
+ * Find a command by name from a list of commands.
+ * @returns Returns the named command if it exists in the list.
+ * Returns NULL otherwise.
+ */
+static struct command *command_find(struct command *head, const char *name)
+{
+ for (struct command *cc = head; cc; cc = cc->next)
+ {
+ if (strcmp(cc->name, name) == 0)
+ return cc;
+ }
+ return NULL;
+}
+struct command *command_find_in_context(struct command_context *cmd_ctx,
+ const char *name)
+{
+ return command_find(cmd_ctx->commands, name);
+}
+struct command *command_find_in_parent(struct command *parent,
+ const char *name)
+{
+ return command_find(parent->children, name);
+}
+
+/**
+ * Add the command into the linked list, sorted by name.
+ * @param head Address to head of command list pointer, which may be
+ * updated if @c c gets inserted at the beginning of the list.
+ * @param c The command to add to the list pointed to by @c head.
+ */
+static void command_add_child(struct command **head, struct command *c)
+{
+ assert(head);
+ if (NULL == *head)
+ {
+ *head = c;
+ return;
+ }
+
+ while ((*head)->next && (strcmp(c->name, (*head)->name) > 0))
+ head = &(*head)->next;
+
+ if (strcmp(c->name, (*head)->name) > 0) {
+ c->next = (*head)->next;
+ (*head)->next = c;
+ } else {
+ c->next = *head;
+ *head = c;
+ }
+}
+
+static struct command **command_list_for_parent(
+ struct command_context *cmd_ctx, struct command *parent)
+{
+ return parent ? &parent->children : &cmd_ctx->commands;
+}
+
+static void command_free(struct command *c)
+{
+ /// @todo if command has a handler, unregister its jim command!
+
+ while (NULL != c->children)
+ {
+ struct command *tmp = c->children;
+ c->children = tmp->next;
+ command_free(tmp);
+ }
+
+ if (c->name)
+ free(c->name);
+ if (c->help)
+ free((void*)c->help);
+ if (c->usage)
+ free((void*)c->usage);
+ free(c);