?? cli-script.c
字號(hào):
/* GDB CLI command scripting. Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004 Free Software Foundation, Inc. This file is part of GDB. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */#include "defs.h"#include "value.h"#include "language.h" /* For value_true */#include <ctype.h>#include "ui-out.h"#include "gdb_string.h"#include "top.h"#include "cli/cli-cmds.h"#include "cli/cli-decode.h"#include "cli/cli-script.h"/* Prototypes for local functions */static enum command_control_type recurse_read_control_structure (struct command_line *current_cmd);static char *insert_args (char *line);static struct cleanup * setup_user_args (char *p);static void validate_comname (char *);/* Level of control structure. */static int control_level;/* Source command state variable. */static int source_error_allocated;/* Structure for arguments to user defined functions. */#define MAXUSERARGS 10struct user_args { struct user_args *next; struct { char *arg; int len; } a[MAXUSERARGS]; int count; } *user_args;/* Allocate, initialize a new command line structure for one of the control commands (if/while). */static struct command_line *build_command_line (enum command_control_type type, char *args){ struct command_line *cmd; if (args == NULL) error ("if/while commands require arguments.\n"); 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 = savestring (args, strlen (args)); return cmd;}/* Build and return a new command structure for the control commands such as "if" and "while". */static struct command_line *get_command_line (enum command_control_type type, char *arg){ struct command_line *cmd; struct cleanup *old_chain = NULL; /* Allocate and build a new command line structure. */ cmd = build_command_line (type, arg); old_chain = make_cleanup_free_command_lines (&cmd); /* Read in the body of this command. */ if (recurse_read_control_structure (cmd) == invalid_control) { warning ("error reading in control structure\n"); do_cleanups (old_chain); return NULL; } discard_cleanups (old_chain); return cmd;}/* Recursively print a command (including full control structures). */voidprint_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); /* A simple command, print it and continue. */ if (list->control_type == simple_control) { ui_out_field_string (uiout, NULL, list->line); ui_out_text (uiout, "\n"); list = list->next; continue; } /* loop_continue to jump to the start of a while loop, print it and continue. */ if (list->control_type == continue_control) { ui_out_field_string (uiout, NULL, "loop_continue"); ui_out_text (uiout, "\n"); list = list->next; continue; } /* loop_break to break out of a while loop, print it and continue. */ if (list->control_type == break_control) { ui_out_field_string (uiout, NULL, "loop_break"); ui_out_text (uiout, "\n"); list = list->next; continue; } /* A while command. Recursively print its subcommands and continue. */ if (list->control_type == while_control) { ui_out_field_fmt (uiout, NULL, "while %s", 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; } /* An if command. Recursively print both arms before continueing. */ if (list->control_type == if_control) { ui_out_field_fmt (uiout, NULL, "if %s", list->line); ui_out_text (uiout, "\n"); /* The true arm. */ print_command_lines (uiout, list->body_list[0], depth + 1); /* Show the false arm if it exists. */ 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; } /* ignore illegal command type and try next */ list = list->next; } /* while (list) */}/* Handle pre-post hooks. */static voidclear_hook_in_cleanup (void *data){ struct cmd_list_element *c = data; c->hook_in = 0; /* Allow hook to work again once it is complete */}voidexecute_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; /* Prevent recursive hooking */ execute_user_command (c->hook_pre, (char *) 0); do_cleanups (cleanups); }}voidexecute_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; /* Prevent recursive hooking */ execute_user_command (c->hook_post, (char *) 0); do_cleanups (cleanups); }}/* Execute the command in CMD. */static voiddo_restore_user_call_depth (void * call_depth){ int * depth = call_depth; /* We will be returning_to_top_level() at this point, so we want to reset our depth. */ (*depth) = 0;}voidexecute_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 int max_user_call_depth; old_chain = setup_user_args (args); cmdlines = c->user_commands; if (cmdlines == 0) /* Null command */ return; if (++user_call_depth > max_user_call_depth) error ("Max user call depth exceeded -- command aborted\n"); old_chain = make_cleanup (do_restore_user_call_depth, &user_call_depth); /* Set the instream to 0, indicating execution of a user-defined function. */ old_chain = make_cleanup (do_restore_instream_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); user_call_depth--;}enum command_control_typeexecute_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; /* Start by assuming failure, if a problem is detected, the code below will simply "break" out of the switch. */ ret = invalid_control; switch (cmd->control_type) { case simple_control: /* A simple command, execute it and return. */ 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: 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) break; make_cleanup (free_current_contents, &new_line); expr = parse_expression (new_line); make_cleanup (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) break; make_cleanup (free_current_contents, &new_line); /* Parse the conditional for the if statement. */ expr = parse_expression (new_line); make_cleanup (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."); break; } do_cleanups (old_chain); return ret;}/* "while" command support. Executes a body of statements while the
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -