gdb/cli/cli-script.c - gdb
Global variables defined
Data types defined
Functions defined
Macros defined
Source code
- #include "defs.h"
- #include "value.h"
- #include "language.h"
- #include <ctype.h>
- #include "ui-out.h"
- #include "top.h"
- #include "breakpoint.h"
- #include "cli/cli-cmds.h"
- #include "cli/cli-decode.h"
- #include "cli/cli-script.h"
- #include "extension.h"
- #include "interps.h"
- #include "compile/compile.h"
- static enum command_control_type
- recurse_read_control_structure (char * (*read_next_line_func) (void),
- struct command_line *current_cmd,
- void (*validator)(char *, void *),
- void *closure);
- static char *insert_args (char *line);
- static struct cleanup * setup_user_args (char *p);
- static char *read_next_line (void);
- static int control_level;
- static int command_nest_depth = 1;
- static int suppress_next_print_command_trace = 0;
- #define MAXUSERARGS 10
- struct user_args
- {
- struct user_args *next;
-
- char *command;
- struct
- {
- char *arg;
- int len;
- }
- a[MAXUSERARGS];
- int count;
- }
- *user_args;
- static int
- multi_line_command_p (enum command_control_type type)
- {
- switch (type)
- {
- case if_control:
- case while_control:
- case while_stepping_control:
- case commands_control:
- case compile_control:
- case python_control:
- case guile_control:
- return 1;
- default:
- return 0;
- }
- }
- static struct command_line *
- build_command_line (enum command_control_type type, char *args)
- {
- struct command_line *cmd;
- if (args == NULL && (type == if_control || type == while_control))
- error (_("if/while commands require arguments."));
- gdb_assert (args != NULL);
- cmd = (struct command_line *) xmalloc (sizeof (struct command_line));
- cmd->next = NULL;
- cmd->control_type = type;
- cmd->body_count = 1;
- cmd->body_list
- = (struct command_line **) xmalloc (sizeof (struct command_line *)
- * cmd->body_count);
- memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
- cmd->line = xstrdup (args);
- return cmd;
- }
- struct command_line *
- get_command_line (enum command_control_type type, char *arg)
- {
- struct command_line *cmd;
- struct cleanup *old_chain = NULL;
-
- cmd = build_command_line (type, arg);
- old_chain = make_cleanup_free_command_lines (&cmd);
-
- if (recurse_read_control_structure (read_next_line, cmd, 0, 0)
- == invalid_control)
- {
- warning (_("Error reading in canned sequence of commands."));
- do_cleanups (old_chain);
- return NULL;
- }
- discard_cleanups (old_chain);
- return cmd;
- }
- void
- print_command_lines (struct ui_out *uiout, struct command_line *cmd,
- unsigned int depth)
- {
- struct command_line *list;
- list = cmd;
- while (list)
- {
- if (depth)
- ui_out_spaces (uiout, 2 * depth);
-
- if (list->control_type == simple_control)
- {
- ui_out_field_string (uiout, NULL, list->line);
- ui_out_text (uiout, "\n");
- list = list->next;
- continue;
- }
-
- if (list->control_type == continue_control)
- {
- ui_out_field_string (uiout, NULL, "loop_continue");
- ui_out_text (uiout, "\n");
- list = list->next;
- continue;
- }
-
- if (list->control_type == break_control)
- {
- ui_out_field_string (uiout, NULL, "loop_break");
- ui_out_text (uiout, "\n");
- list = list->next;
- continue;
- }
-
- if (list->control_type == while_control
- || list->control_type == while_stepping_control)
- {
-
- if (list->control_type == while_control)
- ui_out_field_fmt (uiout, NULL, "while %s", list->line);
- else
- ui_out_field_string (uiout, NULL, list->line);
- ui_out_text (uiout, "\n");
- print_command_lines (uiout, *list->body_list, depth + 1);
- if (depth)
- ui_out_spaces (uiout, 2 * depth);
- ui_out_field_string (uiout, NULL, "end");
- ui_out_text (uiout, "\n");
- list = list->next;
- continue;
- }
-
- if (list->control_type == if_control)
- {
- ui_out_field_fmt (uiout, NULL, "if %s", list->line);
- ui_out_text (uiout, "\n");
-
- print_command_lines (uiout, list->body_list[0], depth + 1);
-
- if (list->body_count == 2)
- {
- if (depth)
- ui_out_spaces (uiout, 2 * depth);
- ui_out_field_string (uiout, NULL, "else");
- ui_out_text (uiout, "\n");
- print_command_lines (uiout, list->body_list[1], depth + 1);
- }
- if (depth)
- ui_out_spaces (uiout, 2 * depth);
- ui_out_field_string (uiout, NULL, "end");
- ui_out_text (uiout, "\n");
- list = list->next;
- continue;
- }
-
- if (list->control_type == commands_control)
- {
- if (*(list->line))
- ui_out_field_fmt (uiout, NULL, "commands %s", list->line);
- else
- ui_out_field_string (uiout, NULL, "commands");
- ui_out_text (uiout, "\n");
- print_command_lines (uiout, *list->body_list, depth + 1);
- if (depth)
- ui_out_spaces (uiout, 2 * depth);
- ui_out_field_string (uiout, NULL, "end");
- ui_out_text (uiout, "\n");
- list = list->next;
- continue;
- }
- if (list->control_type == python_control)
- {
- ui_out_field_string (uiout, NULL, "python");
- ui_out_text (uiout, "\n");
-
- print_command_lines (uiout, *list->body_list, 0);
- if (depth)
- ui_out_spaces (uiout, 2 * depth);
- ui_out_field_string (uiout, NULL, "end");
- ui_out_text (uiout, "\n");
- list = list->next;
- continue;
- }
- if (list->control_type == compile_control)
- {
- ui_out_field_string (uiout, NULL, "compile expression");
- ui_out_text (uiout, "\n");
- print_command_lines (uiout, *list->body_list, 0);
- if (depth)
- ui_out_spaces (uiout, 2 * depth);
- ui_out_field_string (uiout, NULL, "end");
- ui_out_text (uiout, "\n");
- list = list->next;
- continue;
- }
- if (list->control_type == guile_control)
- {
- ui_out_field_string (uiout, NULL, "guile");
- ui_out_text (uiout, "\n");
- print_command_lines (uiout, *list->body_list, depth + 1);
- if (depth)
- ui_out_spaces (uiout, 2 * depth);
- ui_out_field_string (uiout, NULL, "end");
- ui_out_text (uiout, "\n");
- list = list->next;
- continue;
- }
-
- list = list->next;
- }
- }
- static void
- clear_hook_in_cleanup (void *data)
- {
- struct cmd_list_element *c = data;
- c->hook_in = 0;
- }
- void
- execute_cmd_pre_hook (struct cmd_list_element *c)
- {
- if ((c->hook_pre) && (!c->hook_in))
- {
- struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
- c->hook_in = 1;
- execute_user_command (c->hook_pre, (char *) 0);
- do_cleanups (cleanups);
- }
- }
- void
- execute_cmd_post_hook (struct cmd_list_element *c)
- {
- if ((c->hook_post) && (!c->hook_in))
- {
- struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
- c->hook_in = 1;
- execute_user_command (c->hook_post, (char *) 0);
- do_cleanups (cleanups);
- }
- }
- static void
- do_restore_user_call_depth (void * call_depth)
- {
- int *depth = call_depth;
- (*depth)--;
- if ((*depth) == 0)
- in_user_command = 0;
- }
- void
- execute_user_command (struct cmd_list_element *c, char *args)
- {
- struct command_line *cmdlines;
- struct cleanup *old_chain;
- enum command_control_type ret;
- static int user_call_depth = 0;
- extern unsigned int max_user_call_depth;
- cmdlines = c->user_commands;
- if (cmdlines == 0)
-
- return;
- old_chain = setup_user_args (args);
- if (++user_call_depth > max_user_call_depth)
- error (_("Max user call depth exceeded -- command aborted."));
- make_cleanup (do_restore_user_call_depth, &user_call_depth);
-
- make_cleanup (do_restore_instream_cleanup, instream);
- instream = (FILE *) 0;
-
- in_user_command = 1;
- make_cleanup_restore_integer (&interpreter_async);
- interpreter_async = 0;
- command_nest_depth++;
- while (cmdlines)
- {
- ret = execute_control_command (cmdlines);
- if (ret != simple_control && ret != break_control)
- {
- warning (_("Error executing canned sequence of commands."));
- break;
- }
- cmdlines = cmdlines->next;
- }
- command_nest_depth--;
- do_cleanups (old_chain);
- }
- void
- reset_command_nest_depth (void)
- {
- command_nest_depth = 1;
-
- suppress_next_print_command_trace = 0;
- }
- void
- print_command_trace (const char *cmd)
- {
- int i;
- if (suppress_next_print_command_trace)
- {
- suppress_next_print_command_trace = 0;
- return;
- }
- if (!source_verbose && !trace_commands)
- return;
- for (i=0; i < command_nest_depth; i++)
- printf_filtered ("+");
- printf_filtered ("%s\n", cmd);
- }
- enum command_control_type
- execute_control_command (struct command_line *cmd)
- {
- struct expression *expr;
- struct command_line *current;
- struct cleanup *old_chain = make_cleanup (null_cleanup, 0);
- struct value *val;
- struct value *val_mark;
- int loop;
- enum command_control_type ret;
- char *new_line;
-
- ret = invalid_control;
- switch (cmd->control_type)
- {
- case simple_control:
-
- new_line = insert_args (cmd->line);
- if (!new_line)
- break;
- make_cleanup (free_current_contents, &new_line);
- execute_command (new_line, 0);
- ret = cmd->control_type;
- break;
- case continue_control:
- print_command_trace ("loop_continue");
-
- ret = cmd->control_type;
- break;
- case break_control:
- print_command_trace ("loop_break");
-
- ret = cmd->control_type;
- break;
- case while_control:
- {
- int len = strlen (cmd->line) + 7;
- char *buffer = alloca (len);
- xsnprintf (buffer, len, "while %s", cmd->line);
- print_command_trace (buffer);
-
- new_line = insert_args (cmd->line);
- if (!new_line)
- break;
- make_cleanup (free_current_contents, &new_line);
- expr = parse_expression (new_line);
- make_cleanup (free_current_contents, &expr);
- ret = simple_control;
- loop = 1;
-
- while (loop == 1)
- {
- int cond_result;
- QUIT;
-
- val_mark = value_mark ();
- val = evaluate_expression (expr);
- cond_result = value_true (val);
- value_free_to_mark (val_mark);
-
- if (!cond_result)
- break;
-
- current = *cmd->body_list;
- while (current)
- {
- command_nest_depth++;
- ret = execute_control_command (current);
- command_nest_depth--;
-
- if (ret == invalid_control || ret == break_control)
- {
- loop = 0;
- break;
- }
-
- if (ret == continue_control)
- break;
-
- current = current->next;
- }
- }
-
- if (ret == break_control)
- ret = simple_control;
- break;
- }
- case if_control:
- {
- int len = strlen (cmd->line) + 4;
- char *buffer = alloca (len);
- xsnprintf (buffer, len, "if %s", cmd->line);
- print_command_trace (buffer);
- new_line = insert_args (cmd->line);
- if (!new_line)
- break;
- make_cleanup (free_current_contents, &new_line);
-
- expr = parse_expression (new_line);
- make_cleanup (free_current_contents, &expr);
- current = NULL;
- ret = simple_control;
-
- val_mark = value_mark ();
- val = evaluate_expression (expr);
-
- 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);
-
- while (current)
- {
- command_nest_depth++;
- ret = execute_control_command (current);
- command_nest_depth--;
-
- if (ret != simple_control)
- break;
-
- current = current->next;
- }
- break;
- }
- case commands_control:
- {
-
- new_line = insert_args (cmd->line);
- if (!new_line)
- break;
- make_cleanup (free_current_contents, &new_line);
- ret = commands_from_control_command (new_line, cmd);
- break;
- }
- case compile_control:
- eval_compile_command (cmd, NULL, cmd->control_u.compile.scope);
- ret = simple_control;
- break;
- case python_control:
- case guile_control:
- {
- eval_ext_lang_from_control_command (cmd);
- ret = simple_control;
- break;
- }
- default:
- warning (_("Invalid control type in canned commands structure."));
- break;
- }
- do_cleanups (old_chain);
- return ret;
- }
- enum command_control_type
- execute_control_command_untraced (struct command_line *cmd)
- {
- suppress_next_print_command_trace = 1;
- return execute_control_command (cmd);
- }
- static void
- while_command (char *arg, int from_tty)
- {
- struct command_line *command = NULL;
- struct cleanup *old_chain;
- control_level = 1;
- command = get_command_line (while_control, arg);
- if (command == NULL)
- return;
- old_chain = make_cleanup_restore_integer (&interpreter_async);
- interpreter_async = 0;
- execute_control_command_untraced (command);
- free_command_lines (&command);
- do_cleanups (old_chain);
- }
- static void
- if_command (char *arg, int from_tty)
- {
- struct command_line *command = NULL;
- struct cleanup *old_chain;
- control_level = 1;
- command = get_command_line (if_control, arg);
- if (command == NULL)
- return;
- old_chain = make_cleanup_restore_integer (&interpreter_async);
- interpreter_async = 0;
- execute_control_command_untraced (command);
- free_command_lines (&command);
- do_cleanups (old_chain);
- }
- static void
- arg_cleanup (void *ignore)
- {
- struct user_args *oargs = user_args;
- if (!user_args)
- internal_error (__FILE__, __LINE__,
- _("arg_cleanup called with no user args.\n"));
- user_args = user_args->next;
- xfree (oargs->command);
- xfree (oargs);
- }
- static struct cleanup *
- setup_user_args (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 (arg_cleanup, 0);
- if (p == NULL)
- return old_chain;
- user_args->command = p = xstrdup (p);
- 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."),
- MAXUSERARGS);
-
- while (*p == ' ' || *p == '\t')
- p++;
-
- start_arg = p;
- user_args->a[arg_count].arg = p;
-
- 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;
- }
- static char *
- locate_arg (char *p)
- {
- while ((p = strchr (p, '$')))
- {
- if (strncmp (p, "$arg", 4) == 0
- && (isdigit (p[4]) || p[4] == 'c'))
- return p;
- p++;
- }
- return NULL;
- }
- static char *
- insert_args (char *line)
- {
- char *p, *save_line, *new_line;
- unsigned len, i;
-
- if (user_args == NULL)
- return xstrdup (line);
-
- save_line = line;
- len = 0;
- while ((p = locate_arg (line)))
- {
- len += p - line;
- i = p[4] - '0';
- if (p[4] == 'c')
- {
-
- len += user_args->count == 10 ? 2 : 1;
- }
- else if (i >= user_args->count)
- {
- error (_("Missing argument %d in user function."), i);
- return NULL;
- }
- else
- {
- len += user_args->a[i].len;
- }
- line = p + 5;
- }
-
- len += strlen (line);
-
- new_line = (char *) xmalloc (len + 1);
- if (new_line == NULL)
- return NULL;
-
- line = save_line;
-
- save_line = new_line;
- while ((p = locate_arg (line)))
- {
- int i, len;
- memcpy (new_line, line, p - line);
- new_line += p - line;
- if (p[4] == 'c')
- {
- gdb_assert (user_args->count >= 0 && user_args->count <= 10);
- if (user_args->count == 10)
- {
- *(new_line++) = '1';
- *(new_line++) = '0';
- }
- else
- *(new_line++) = user_args->count + '0';
- }
- else
- {
- 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;
- }
-
- strcpy (new_line, line);
-
- return save_line;
- }
- static void
- realloc_body_list (struct command_line *command, int new_length)
- {
- int n;
- struct command_line **body_list;
- n = command->body_count;
-
- if (new_length <= n)
- return;
- body_list = (struct command_line **)
- xmalloc (sizeof (struct command_line *) * new_length);
- memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
- memset (body_list + n, 0, sizeof (struct command_line *) * (new_length - n));
- xfree (command->body_list);
- command->body_list = body_list;
- command->body_count = new_length;
- }
- static char *
- read_next_line (void)
- {
- char *prompt_ptr, control_prompt[256];
- int i = 0;
- if (control_level >= 254)
- error (_("Control nesting too deep!"));
-
- if (instream == stdin || (instream == 0 && deprecated_readline_hook != NULL))
- {
- for (i = 0; i < control_level; i++)
- control_prompt[i] = ' ';
- control_prompt[i] = '>';
- control_prompt[i + 1] = '\0';
- prompt_ptr = (char *) &control_prompt[0];
- }
- else
- prompt_ptr = NULL;
- return command_line_input (prompt_ptr, instream == stdin, "commands");
- }
- static enum misc_command_type
- process_next_line (char *p, struct command_line **command, int parse_commands,
- void (*validator)(char *, void *), void *closure)
- {
- char *p_end;
- char *p_start;
- int not_handled = 0;
-
- if (p == NULL)
- return end_command;
-
- p_end = p + strlen (p);
- while (p_end > p && (p_end[-1] == ' ' || p_end[-1] == '\t'))
- p_end--;
- p_start = p;
-
- while (p_start < p_end && (*p_start == ' ' || *p_start == '\t'))
- p_start++;
-
- if (p_end - p_start == 3 && !strncmp (p_start, "end", 3))
- return end_command;
- if (parse_commands)
- {
-
- p = p_start;
-
- if (p_end == p || p[0] == '#')
- return nop_command;
-
- if (p_end - p == 4 && !strncmp (p, "else", 4))
- return else_command;
-
- if ((p_end - p >= 14 && !strncmp (p, "while-stepping", 14))
- || (p_end - p >= 8 && !strncmp (p, "stepping", 8))
- || (p_end - p >= 2 && !strncmp (p, "ws", 2)))
- {
-
- *command = build_command_line (while_stepping_control, p);
- }
- else if (p_end - p > 5 && !strncmp (p, "while", 5))
- {
- char *first_arg;
- first_arg = p + 5;
- while (first_arg < p_end && isspace (*first_arg))
- first_arg++;
- *command = build_command_line (while_control, first_arg);
- }
- else if (p_end - p > 2 && !strncmp (p, "if", 2))
- {
- char *first_arg;
- first_arg = p + 2;
- while (first_arg < p_end && isspace (*first_arg))
- first_arg++;
- *command = build_command_line (if_control, first_arg);
- }
- else if (p_end - p >= 8 && !strncmp (p, "commands", 8))
- {
- char *first_arg;
- first_arg = p + 8;
- while (first_arg < p_end && isspace (*first_arg))
- first_arg++;
- *command = build_command_line (commands_control, first_arg);
- }
- else if (p_end - p == 6 && !strncmp (p, "python", 6))
- {
-
- *command = build_command_line (python_control, "");
- }
- else if (p_end - p == 6 && !strncmp (p, "compile", 7))
- {
-
- *command = build_command_line (compile_control, "");
- (*command)->control_u.compile.scope = COMPILE_I_INVALID_SCOPE;
- }
- else if (p_end - p == 5 && !strncmp (p, "guile", 5))
- {
-
- *command = build_command_line (guile_control, "");
- }
- else if (p_end - p == 10 && !strncmp (p, "loop_break", 10))
- {
- *command = (struct command_line *)
- xmalloc (sizeof (struct command_line));
- (*command)->next = NULL;
- (*command)->line = NULL;
- (*command)->control_type = break_control;
- (*command)->body_count = 0;
- (*command)->body_list = NULL;
- }
- else if (p_end - p == 13 && !strncmp (p, "loop_continue", 13))
- {
- *command = (struct command_line *)
- xmalloc (sizeof (struct command_line));
- (*command)->next = NULL;
- (*command)->line = NULL;
- (*command)->control_type = continue_control;
- (*command)->body_count = 0;
- (*command)->body_list = NULL;
- }
- else
- not_handled = 1;
- }
- if (!parse_commands || not_handled)
- {
-
- *command = (struct command_line *)
- xmalloc (sizeof (struct command_line));
- (*command)->next = NULL;
- (*command)->line = savestring (p, p_end - p);
- (*command)->control_type = simple_control;
- (*command)->body_count = 0;
- (*command)->body_list = NULL;
- }
- if (validator)
- {
- volatile struct gdb_exception ex;
- TRY_CATCH (ex, RETURN_MASK_ALL)
- {
- validator ((*command)->line, closure);
- }
- if (ex.reason < 0)
- {
- xfree (*command);
- throw_exception (ex);
- }
- }
-
- return ok_command;
- }
- static enum command_control_type
- recurse_read_control_structure (char * (*read_next_line_func) (void),
- struct command_line *current_cmd,
- void (*validator)(char *, void *),
- void *closure)
- {
- int current_body, i;
- enum misc_command_type val;
- enum command_control_type ret;
- struct command_line **body_ptr, *child_tail, *next;
- child_tail = NULL;
- current_body = 1;
-
- if (current_cmd->control_type == simple_control)
- error (_("Recursed on a simple control type."));
- if (current_body > current_cmd->body_count)
- error (_("Allocated body is smaller than this command type needs."));
-
- while (1)
- {
- dont_repeat ();
- next = NULL;
- val = process_next_line (read_next_line_func (), &next,
- current_cmd->control_type != python_control
- && current_cmd->control_type != guile_control
- && current_cmd->control_type != compile_control,
- validator, closure);
-
- if (val == nop_command)
- continue;
- if (val == end_command)
- {
- if (multi_line_command_p (current_cmd->control_type))
- {
-
- ret = simple_control;
- break;
- }
- else
- {
- ret = invalid_control;
- break;
- }
- }
-
- if (val == else_command)
- {
- if (current_cmd->control_type == if_control
- && current_body == 1)
- {
- realloc_body_list (current_cmd, 2);
- current_body = 2;
- child_tail = NULL;
- continue;
- }
- else
- {
- ret = invalid_control;
- break;
- }
- }
- if (child_tail)
- {
- child_tail->next = next;
- }
- else
- {
- body_ptr = current_cmd->body_list;
- for (i = 1; i < current_body; i++)
- body_ptr++;
- *body_ptr = next;
- }
- child_tail = next;
-
- if (multi_line_command_p (next->control_type))
- {
- control_level++;
- ret = recurse_read_control_structure (read_next_line_func, next,
- validator, closure);
- control_level--;
- if (ret != simple_control)
- break;
- }
- }
- dont_repeat ();
- return ret;
- }
- static void
- restore_interp (void *arg)
- {
- interp_set_temp (interp_name ((struct interp *)arg));
- }
- #define END_MESSAGE "End with a line saying just \"end\"."
- struct command_line *
- read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
- void (*validator)(char *, void *), void *closure)
- {
- struct command_line *head;
- if (from_tty && input_from_terminal_p ())
- {
- if (deprecated_readline_begin_hook)
- {
-
- (*deprecated_readline_begin_hook) ("%s %s\n", prompt_arg,
- END_MESSAGE);
- }
- else
- {
- printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
- gdb_flush (gdb_stdout);
- }
- }
-
- if (current_interp_named_p (INTERP_CONSOLE))
- head = read_command_lines_1 (read_next_line, parse_commands,
- validator, closure);
- else
- {
- struct interp *old_interp = interp_set_temp (INTERP_CONSOLE);
- struct cleanup *old_chain = make_cleanup (restore_interp, old_interp);
- head = read_command_lines_1 (read_next_line, parse_commands,
- validator, closure);
- do_cleanups (old_chain);
- }
- if (deprecated_readline_end_hook && from_tty && input_from_terminal_p ())
- {
- (*deprecated_readline_end_hook) ();
- }
- return (head);
- }
- struct command_line *
- read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
- void (*validator)(char *, void *), void *closure)
- {
- struct command_line *head, *tail, *next;
- struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
- enum command_control_type ret;
- enum misc_command_type val;
- control_level = 0;
- head = tail = NULL;
- while (1)
- {
- dont_repeat ();
- val = process_next_line (read_next_line_func (), &next, parse_commands,
- validator, closure);
-
- if (val == nop_command)
- continue;
- if (val == end_command)
- {
- ret = simple_control;
- break;
- }
- if (val != ok_command)
- {
- ret = invalid_control;
- break;
- }
- if (multi_line_command_p (next->control_type))
- {
- control_level++;
- ret = recurse_read_control_structure (read_next_line_func, next,
- validator, closure);
- control_level--;
- if (ret == invalid_control)
- break;
- }
- if (tail)
- {
- tail->next = next;
- }
- else
- {
- head = next;
- make_cleanup_free_command_lines (&head);
- }
- tail = next;
- }
- dont_repeat ();
- if (ret != invalid_control)
- discard_cleanups (old_chain);
- else
- do_cleanups (old_chain);
- return head;
- }
- void
- free_command_lines (struct command_line **lptr)
- {
- struct command_line *l = *lptr;
- struct command_line *next;
- struct command_line **blist;
- int i;
- while (l)
- {
- if (l->body_count > 0)
- {
- blist = l->body_list;
- for (i = 0; i < l->body_count; i++, blist++)
- free_command_lines (blist);
- }
- next = l->next;
- xfree (l->line);
- xfree (l);
- l = next;
- }
- *lptr = NULL;
- }
- static void
- do_free_command_lines_cleanup (void *arg)
- {
- free_command_lines (arg);
- }
- struct cleanup *
- make_cleanup_free_command_lines (struct command_line **arg)
- {
- return make_cleanup (do_free_command_lines_cleanup, arg);
- }
- struct command_line *
- copy_command_lines (struct command_line *cmds)
- {
- struct command_line *result = NULL;
- if (cmds)
- {
- result = (struct command_line *) xmalloc (sizeof (struct command_line));
- result->next = copy_command_lines (cmds->next);
- result->line = xstrdup (cmds->line);
- result->control_type = cmds->control_type;
- result->body_count = cmds->body_count;
- if (cmds->body_count > 0)
- {
- int i;
- result->body_list = (struct command_line **)
- xmalloc (sizeof (struct command_line *) * cmds->body_count);
- for (i = 0; i < cmds->body_count; i++)
- result->body_list[i] = copy_command_lines (cmds->body_list[i]);
- }
- else
- result->body_list = NULL;
- }
- return result;
- }
- static struct cmd_list_element **
- validate_comname (char **comname)
- {
- struct cmd_list_element **list = &cmdlist;
- char *p, *last_word;
- if (*comname == 0)
- error_no_arg (_("name of command to define"));
-
- p = *comname + strlen (*comname);
- while (p > *comname && isspace (p[-1]))
- p--;
- while (p > *comname && !isspace (p[-1]))
- p--;
- last_word = p;
-
- if (last_word != *comname)
- {
- struct cmd_list_element *c;
- char saved_char;
- const char *tem = *comname;
-
- saved_char = last_word[-1];
- last_word[-1] = '\0';
- c = lookup_cmd (&tem, cmdlist, "", 0, 1);
- if (c->prefixlist == NULL)
- error (_("\"%s\" is not a prefix command."), *comname);
- list = c->prefixlist;
- last_word[-1] = saved_char;
- *comname = last_word;
- }
- p = *comname;
- while (*p)
- {
- if (!isalnum (*p) && *p != '-' && *p != '_')
- error (_("Junk in argument list: \"%s\""), p);
- p++;
- }
- return list;
- }
- static void
- user_defined_command (char *ignore, int from_tty)
- {
- }
- static void
- define_command (char *comname, int from_tty)
- {
- #define MAX_TMPBUF 128
- enum cmd_hook_type
- {
- CMD_NO_HOOK = 0,
- CMD_PRE_HOOK,
- CMD_POST_HOOK
- };
- struct command_line *cmds;
- struct cmd_list_element *c, *newc, *hookc = 0, **list;
- char *tem, *comfull;
- const char *tem_c;
- char tmpbuf[MAX_TMPBUF];
- int hook_type = CMD_NO_HOOK;
- int hook_name_size = 0;
- #define HOOK_STRING "hook-"
- #define HOOK_LEN 5
- #define HOOK_POST_STRING "hookpost-"
- #define HOOK_POST_LEN 9
- comfull = comname;
- list = validate_comname (&comname);
-
- tem_c = comname;
- c = lookup_cmd (&tem_c, *list, "", -1, 1);
- if (c && strcmp (comname, c->name) != 0)
- c = 0;
- if (c)
- {
- int q;
- if (c->class == class_user || c->class == class_alias)
- q = query (_("Redefine command \"%s\"? "), c->name);
- else
- q = query (_("Really redefine built-in command \"%s\"? "), c->name);
- if (!q)
- error (_("Command \"%s\" not redefined."), c->name);
- }
-
- if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
- {
- hook_type = CMD_PRE_HOOK;
- hook_name_size = HOOK_LEN;
- }
- else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
- {
- hook_type = CMD_POST_HOOK;
- hook_name_size = HOOK_POST_LEN;
- }
- if (hook_type != CMD_NO_HOOK)
- {
-
- tem_c = comname + hook_name_size;
- hookc = lookup_cmd (&tem_c, *list, "", -1, 0);
- if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
- hookc = 0;
- if (!hookc)
- {
- warning (_("Your new `%s' command does not "
- "hook any existing command."),
- comfull);
- if (!query (_("Proceed? ")))
- error (_("Not confirmed."));
- }
- }
- comname = xstrdup (comname);
-
- for (tem = comname; *tem; tem++)
- if (isupper (*tem))
- *tem = tolower (*tem);
- xsnprintf (tmpbuf, sizeof (tmpbuf),
- "Type commands for definition of \"%s\".", comfull);
- cmds = read_command_lines (tmpbuf, from_tty, 1, 0, 0);
- if (c && c->class == class_user)
- free_command_lines (&c->user_commands);
- newc = add_cmd (comname, class_user, user_defined_command,
- (c && c->class == class_user)
- ? c->doc : xstrdup ("User-defined."), list);
- newc->user_commands = cmds;
-
- if (hookc)
- {
- switch (hook_type)
- {
- case CMD_PRE_HOOK:
- hookc->hook_pre = newc;
- newc->hookee_pre = hookc;
- break;
- case CMD_POST_HOOK:
- hookc->hook_post = newc;
- newc->hookee_post = hookc;
- break;
- default:
-
- internal_error (__FILE__, __LINE__, _("bad switch"));
- }
- }
- }
- static void
- document_command (char *comname, int from_tty)
- {
- struct command_line *doclines;
- struct cmd_list_element *c, **list;
- const char *tem;
- char *comfull;
- char tmpbuf[128];
- comfull = comname;
- list = validate_comname (&comname);
- tem = comname;
- c = lookup_cmd (&tem, *list, "", 0, 1);
- if (c->class != class_user)
- error (_("Command \"%s\" is built-in."), comfull);
- xsnprintf (tmpbuf, sizeof (tmpbuf), "Type documentation for \"%s\".",
- comfull);
- doclines = read_command_lines (tmpbuf, from_tty, 0, 0, 0);
- if (c->doc)
- xfree ((char *) c->doc);
- {
- struct command_line *cl1;
- int len = 0;
- char *doc;
- for (cl1 = doclines; cl1; cl1 = cl1->next)
- len += strlen (cl1->line) + 1;
- doc = (char *) xmalloc (len + 1);
- *doc = 0;
- for (cl1 = doclines; cl1; cl1 = cl1->next)
- {
- strcat (doc, cl1->line);
- if (cl1->next)
- strcat (doc, "\n");
- }
- c->doc = doc;
- }
- free_command_lines (&doclines);
- }
- struct source_cleanup_lines_args
- {
- int old_line;
- const char *old_file;
- };
- static void
- source_cleanup_lines (void *args)
- {
- struct source_cleanup_lines_args *p =
- (struct source_cleanup_lines_args *) args;
- source_line_number = p->old_line;
- source_file_name = p->old_file;
- }
- void
- script_from_file (FILE *stream, const char *file)
- {
- struct cleanup *old_cleanups;
- struct source_cleanup_lines_args old_lines;
- if (stream == NULL)
- internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
- old_lines.old_line = source_line_number;
- old_lines.old_file = source_file_name;
- old_cleanups = make_cleanup (source_cleanup_lines, &old_lines);
- source_line_number = 0;
- source_file_name = file;
- make_cleanup_restore_integer (&interpreter_async);
- interpreter_async = 0;
- {
- volatile struct gdb_exception e;
- TRY_CATCH (e, RETURN_MASK_ERROR)
- {
- read_command_file (stream);
- }
- switch (e.reason)
- {
- case 0:
- break;
- case RETURN_ERROR:
-
- throw_error (e.error,
- _("%s:%d: Error in sourced command file:\n%s"),
- source_file_name, source_line_number, e.message);
- default:
- internal_error (__FILE__, __LINE__, _("bad reason"));
- }
- }
- do_cleanups (old_cleanups);
- }
- void
- show_user_1 (struct cmd_list_element *c, const char *prefix, const char *name,
- struct ui_file *stream)
- {
- struct command_line *cmdlines;
- if (c->prefixlist != NULL)
- {
- const char *prefixname = c->prefixname;
- for (c = *c->prefixlist; c != NULL; c = c->next)
- if (c->class == class_user || c->prefixlist != NULL)
- show_user_1 (c, prefixname, c->name, gdb_stdout);
- return;
- }
- cmdlines = c->user_commands;
- fprintf_filtered (stream, "User command \"%s%s\":\n", prefix, name);
- if (!cmdlines)
- return;
- print_command_lines (current_uiout, cmdlines, 1);
- fputs_filtered ("\n", stream);
- }
- initialize_file_ftype _initialize_cli_script;
- void
- _initialize_cli_script (void)
- {
- add_com ("document", class_support, document_command, _("\
- Document a user-defined command.\n\
- Give command name as argument. Give documentation on following lines.\n\
- End with a line of just \"end\"."));
- add_com ("define", class_support, define_command, _("\
- Define a new command name. Command name is argument.\n\
- Definition appears on following lines, one command per line.\n\
- End with a line of just \"end\".\n\
- Use the \"document\" command to give documentation for the new command.\n\
- Commands defined in this way may have up to ten arguments."));
- add_com ("while", class_support, while_command, _("\
- Execute nested commands WHILE the conditional expression is non zero.\n\
- The conditional expression must follow the word `while' and must in turn be\n\
- followed by a new line. The nested commands must be entered one per line,\n\
- and should be terminated by the word `end'."));
- add_com ("if", class_support, if_command, _("\
- Execute nested commands once IF the conditional expression is non zero.\n\
- The conditional expression must follow the word `if' and must in turn be\n\
- followed by a new line. The nested commands must be entered one per line,\n\
- and should be terminated by the word 'else' or `end'. If an else clause\n\
- is used, the same rules apply to its nested commands as to the first ones."));
- }