- if (depth)
- {
- for (i = 0; i < depth; i++)
- fputs_filtered (" ", gdb_stdout);
- }
-
- /* A simple command, print it and return. */
- if (cmd->control_type == simple_control)
- {
- fputs_filtered (cmd->line, gdb_stdout);
- fputs_filtered ("\n", gdb_stdout);
- return;
- }
-
- /* loop_continue to jump to the start of a while loop, print it
- and return. */
- if (cmd->control_type == continue_control)
- {
- fputs_filtered ("loop_continue\n", gdb_stdout);
- return;
- }
-
- /* loop_break to break out of a while loop, print it and return. */
- if (cmd->control_type == break_control)
- {
- fputs_filtered ("loop_break\n", gdb_stdout);
- return;
- }
-
- /* A while command. Recursively print its subcommands before returning. */
- if (cmd->control_type == while_control)
- {
- struct command_line *list;
- fputs_filtered ("while ", gdb_stdout);
- fputs_filtered (cmd->line, gdb_stdout);
- fputs_filtered ("\n", gdb_stdout);
- list = *cmd->body_list;
- while (list)
- {
- print_command_line (list, depth + 1);
- list = list->next;
- }
- }
-
- /* An if command. Recursively print both arms before returning. */
- if (cmd->control_type == if_control)
- {
- fputs_filtered ("if ", gdb_stdout);
- fputs_filtered (cmd->line, gdb_stdout);
- fputs_filtered ("\n", gdb_stdout);
- /* The true arm. */
- print_command_line (cmd->body_list[0], depth + 1);
-
- /* Show the false arm if it exists. */
- if (cmd->body_count == 2)
- {
- if (depth)
- {
- for (i = 0; i < depth; i++)
- fputs_filtered (" ", gdb_stdout);
- }
- fputs_filtered ("else\n", gdb_stdout);
- print_command_line (cmd->body_list[1], depth + 1);
- }
- if (depth)
- {
- for (i = 0; i < depth; i++)
- fputs_filtered (" ", gdb_stdout);
- }
- fputs_filtered ("end\n", gdb_stdout);
- }
-}
-
-/* Execute the command in CMD. */
-
-enum command_control_type
-execute_control_command (cmd)
- struct command_line *cmd;
-{
- struct expression *expr;
- struct command_line *current;
- struct cleanup *old_chain = 0;
- value_ptr val;
- value_ptr val_mark;
- int loop;
- enum command_control_type ret;
- char *new_line;
-
- switch (cmd->control_type)
- {
- case simple_control:
- /* A simple command, execute it and return. */
- new_line = insert_args (cmd->line);
- if (!new_line)
- return invalid_control;
- old_chain = make_cleanup ((make_cleanup_func) free_current_contents,
- &new_line);
- execute_command (new_line, 0);
- ret = cmd->control_type;
- break;
-
- case continue_control:
- case break_control:
- /* Return for "continue", and "break" so we can either
- continue the loop at the top, or break out. */
- ret = cmd->control_type;
- break;
-
- case while_control:
- {
- /* Parse the loop control expression for the while statement. */
- new_line = insert_args (cmd->line);
- if (!new_line)
- return invalid_control;
- old_chain = make_cleanup ((make_cleanup_func) free_current_contents,
- &new_line);
- expr = parse_expression (new_line);
- make_cleanup ((make_cleanup_func) free_current_contents, &expr);
-
- ret = simple_control;
- loop = 1;
-
- /* Keep iterating so long as the expression is true. */
- while (loop == 1)
- {
- int cond_result;
-
- QUIT;
-
- /* Evaluate the expression. */
- val_mark = value_mark ();
- val = evaluate_expression (expr);
- cond_result = value_true (val);
- value_free_to_mark (val_mark);
-
- /* If the value is false, then break out of the loop. */
- if (!cond_result)
- break;
-
- /* Execute the body of the while statement. */
- current = *cmd->body_list;
- while (current)
- {
- ret = execute_control_command (current);
-
- /* If we got an error, or a "break" command, then stop
- looping. */
- if (ret == invalid_control || ret == break_control)
- {
- loop = 0;
- break;
- }
-
- /* If we got a "continue" command, then restart the loop
- at this point. */
- if (ret == continue_control)
- break;
-
- /* Get the next statement. */
- current = current->next;
- }
- }
-
- /* Reset RET so that we don't recurse the break all the way down. */
- if (ret == break_control)
- ret = simple_control;
-
- break;
- }
-
- case if_control:
- {
- new_line = insert_args (cmd->line);
- if (!new_line)
- return invalid_control;
- old_chain = make_cleanup ((make_cleanup_func) free_current_contents,
- &new_line);
- /* Parse the conditional for the if statement. */
- expr = parse_expression (new_line);
- make_cleanup ((make_cleanup_func) free_current_contents, &expr);
-
- current = NULL;
- ret = simple_control;
-
- /* Evaluate the conditional. */
- val_mark = value_mark ();
- val = evaluate_expression (expr);
-
- /* Choose which arm to take commands from based on the value of the
- conditional expression. */
- if (value_true (val))
- current = *cmd->body_list;
- else if (cmd->body_count == 2)
- current = *(cmd->body_list + 1);
- value_free_to_mark (val_mark);
-
- /* Execute commands in the given arm. */
- while (current)
- {
- ret = execute_control_command (current);
-
- /* If we got an error, get out. */
- if (ret != simple_control)
- break;
-
- /* Get the next statement in the body. */
- current = current->next;
- }
-
- break;
- }
-
- default:
- warning ("Invalid control type in command structure.");
- return invalid_control;
- }
-
- if (old_chain)
- do_cleanups (old_chain);
-
- return ret;
-}
-
-/* "while" command support. Executes a body of statements while the
- loop condition is nonzero. */
-
-static void
-while_command (arg, from_tty)
- char *arg;
- int from_tty;
-{
- struct command_line *command = NULL;
-
- control_level = 1;
- command = get_command_line (while_control, arg);
-
- if (command == NULL)
- return;
-
- execute_control_command (command);
- free_command_lines (&command);
-}
-
-/* "if" command support. Execute either the true or false arm depending
- on the value of the if conditional. */
-
-static void
-if_command (arg, from_tty)
- char *arg;
- int from_tty;
-{
- struct command_line *command = NULL;
-
- control_level = 1;
- command = get_command_line (if_control, arg);
-
- if (command == NULL)
- return;
-
- execute_control_command (command);
- free_command_lines (&command);
-}
-
-/* Cleanup */
-static void
-arg_cleanup ()
-{
- struct user_args *oargs = user_args;
- if (!user_args)
- fatal ("Internal error, arg_cleanup called with no user args.\n");
-
- user_args = user_args->next;
- free (oargs);
-}
-
-/* Bind the incomming arguments for a user defined command to
- $arg0, $arg1 ... $argMAXUSERARGS. */
-
-static struct cleanup *
-setup_user_args (p)
- char *p;
-{
- struct user_args *args;
- struct cleanup *old_chain;
- unsigned int arg_count = 0;
-
- args = (struct user_args *)xmalloc (sizeof (struct user_args));
- memset (args, 0, sizeof (struct user_args));
-
- args->next = user_args;
- user_args = args;
-
- old_chain = make_cleanup ((make_cleanup_func) arg_cleanup, 0);
-
- if (p == NULL)
- return old_chain;
-
- while (*p)
- {
- char *start_arg;
- int squote = 0;
- int dquote = 0;
- int bsquote = 0;
-
- if (arg_count >= MAXUSERARGS)
- {
- error ("user defined function may only have %d arguments.\n",
- MAXUSERARGS);
- return old_chain;
- }
-
- /* Strip whitespace. */
- while (*p == ' ' || *p == '\t')
- p++;
-
- /* P now points to an argument. */
- start_arg = p;
- user_args->a[arg_count].arg = p;
-
- /* Get to the end of this argument. */
- while (*p)
- {
- if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
- break;
- else
- {
- if (bsquote)
- bsquote = 0;
- else if (*p == '\\')
- bsquote = 1;
- else if (squote)
- {
- if (*p == '\'')
- squote = 0;
- }
- else if (dquote)
- {
- if (*p == '"')
- dquote = 0;
- }
- else
- {
- if (*p == '\'')
- squote = 1;
- else if (*p == '"')
- dquote = 1;
- }
- p++;
- }
- }
-
- user_args->a[arg_count].len = p - start_arg;
- arg_count++;
- user_args->count++;
- }
- return old_chain;
-}
-
-/* Given character string P, return a point to the first argument ($arg),
- or NULL if P contains no arguments. */
-
-static char *
-locate_arg (p)
- char *p;
-{
- while ((p = strchr (p, '$')))
- {
- if (strncmp (p, "$arg", 4) == 0 && isdigit (p[4]))
- return p;
- p++;
- }
- return NULL;
-}
-
-/* Insert the user defined arguments stored in user_arg into the $arg
- arguments found in line, with the updated copy being placed into nline. */
-
-static char *
-insert_args (line)
- char *line;
-{
- char *p, *save_line, *new_line;
- unsigned len, i;
-
- /* First we need to know how much memory to allocate for the new line. */
- save_line = line;
- len = 0;
- while ((p = locate_arg (line)))
- {
- len += p - line;
- i = p[4] - '0';
-
- if (i >= user_args->count)
- {
- error ("Missing argument %d in user function.\n", i);
- return NULL;
- }
- len += user_args->a[i].len;
- line = p + 5;
- }
-
- /* Don't forget the tail. */
- len += strlen (line);
-
- /* Allocate space for the new line and fill it in. */
- new_line = (char *)xmalloc (len + 1);
- if (new_line == NULL)
- return NULL;
-
- /* Restore pointer to beginning of old line. */
- line = save_line;
-
- /* Save pointer to beginning of new line. */
- save_line = new_line;
-
- while ((p = locate_arg (line)))
- {
- int i, len;
-
- memcpy (new_line, line, p - line);
- new_line += p - line;
- i = p[4] - '0';
-
- len = user_args->a[i].len;
- if (len)
- {
- memcpy (new_line, user_args->a[i].arg, len);
- new_line += len;
- }
- line = p + 5;
- }
- /* Don't forget the tail. */
- strcpy (new_line, line);
-
- /* Return a pointer to the beginning of the new line. */
- return save_line;
-}
-
-void
-execute_user_command (c, args)
- struct cmd_list_element *c;
- char *args;
-{
- register struct command_line *cmdlines;
- struct cleanup *old_chain;
- enum command_control_type ret;
-
- old_chain = setup_user_args (args);
-
- cmdlines = c->user_commands;
- if (cmdlines == 0)
- /* Null command */
- return;
-
- /* Set the instream to 0, indicating execution of a
- user-defined function. */
- old_chain = make_cleanup ((make_cleanup_func) source_cleanup, instream);
- instream = (FILE *) 0;
- while (cmdlines)
- {
- ret = execute_control_command (cmdlines);
- if (ret != simple_control && ret != break_control)
- {
- warning ("Error in control structure.\n");
- break;
- }
- cmdlines = cmdlines->next;
- }
- do_cleanups (old_chain);
-}
-
-/* Execute the line P as a command.
- Pass FROM_TTY as second argument to the defining function. */
-
-void
-execute_command (p, from_tty)
- char *p;
- int from_tty;
-{
- register struct cmd_list_element *c;
- register enum language flang;
- static int warned = 0;
- /* FIXME: These should really be in an appropriate header file */
- extern void serial_log_command PARAMS ((const char *));
-
- free_all_values ();
-
- /* Force cleanup of any alloca areas if using C alloca instead of
- a builtin alloca. */
- alloca (0);
-
- /* This can happen when command_line_input hits end of file. */
- if (p == NULL)
- return;
-
- serial_log_command (p);
-
- while (*p == ' ' || *p == '\t') p++;
- if (*p)
- {
- char *arg;
-
- c = lookup_cmd (&p, cmdlist, "", 0, 1);
- /* Pass null arg rather than an empty one. */
- arg = *p ? p : 0;