--- mon/src/mon.cpp 2003/09/27 20:33:06 1.22 +++ mon/src/mon.cpp 2004/02/12 16:42:35 1.23 @@ -66,7 +66,7 @@ static uint8 *mem; FILE *monin, *monout, *monerr; // Input line -static char input[INPUT_LENGTH]; +static char *input; static char *in_ptr; char *mon_args_ptr; @@ -78,21 +78,21 @@ static uint32 colon_value; // Scanner variables -enum Token mon_token; // Last token read -uintptr mon_number; // Contains the number if mon_token==T_NUMBER -char mon_string[INPUT_LENGTH]; // Contains the string if mon_token==T_STRING -char mon_name[INPUT_LENGTH]; // Contains the variable name if mon_token==T_NAME +enum Token mon_token; // Last token read +uintptr mon_number; // Contains the number if mon_token==T_NUMBER +char *mon_string; // Contains the string if mon_token==T_STRING +char *mon_name; // Contains the variable name if mon_token==T_NAME // List of installed commands struct CmdSpec { - const char *name; // Name of command - void (*func)(void); // Function that executes this command + const char *name; // Name of command + void (*func)(); // Function that executes this command }; -static CmdSpec *cmds; // Array of CmdSpecs -static int num_cmds; // Number of installed commands -static char *cmd_help; // Help text for commands +static CmdSpec *cmds; // Array of CmdSpecs +static int num_cmds; // Number of installed commands +static char *cmd_help; // Help text for commands // List of variables @@ -101,17 +101,17 @@ static var_map vars; // Prototypes -static void init_abort(void); -static void exit_abort(void); +static void init_abort(); +static void exit_abort(); static void read_line(char *prompt); // Scanner -static char get_char(void); +static char get_char(); static void put_back(char c); static enum Token get_hex_number(uintptr &i); static enum Token get_dec_number(uintptr &i); static enum Token get_char_number(uintptr &i); -static enum Token get_string(char *str); -static enum Token get_hex_or_name(uintptr &i, char *name); +static enum Token get_string(char *&str); +static enum Token get_hex_or_name(uintptr &i, char *&name); static bool eor_expr(uintptr *number); // Parser static bool and_expr(uintptr *number); @@ -125,7 +125,7 @@ static bool factor(uintptr *number); * Add command to mon */ -void mon_add_command(const char *name, void (*func)(void), const char *help_text) +void mon_add_command(const char *name, void (*func)(), const char *help_text) { num_cmds++; if (cmds) @@ -170,7 +170,7 @@ static void handle_abort(int sig) was_aborted = true; } -static void init_abort(void) +static void init_abort() { was_aborted = false; sigemptyset(&my_sa.sa_mask); @@ -184,13 +184,13 @@ static void init_abort(void) sigaction(SIGINT, &my_sa, NULL); } -static void exit_abort(void) +static void exit_abort() { my_sa.sa_handler = SIG_DFL; sigaction(SIGINT, &my_sa, NULL); } -bool mon_aborted(void) +bool mon_aborted() { bool ret = was_aborted; was_aborted = false; @@ -260,30 +260,26 @@ static void read_line(char *prompt) #ifdef HAVE_LIBREADLINE static char *line_read = NULL; - if (line_read) { - free(line_read); - line_read = NULL; - } - - line_read = readline(prompt); - - if (line_read) { - - if (*line_read) - add_history(line_read); - - strncpy(in_ptr = input, line_read, INPUT_LENGTH); - input[INPUT_LENGTH-1] = 0; - + if (input) + free(input); + input = readline(prompt); + + if (input) { + if (*input) + add_history(input); } else { - // EOF, quit cxmon - fprintf(monout, "x\n"); + input = (char *)malloc(2); input[0] = 'x'; input[1] = 0; - in_ptr = input; + fprintf(monout, "x\n"); } + + in_ptr = input; #else +#define INPUT_LENGTH 256 + if (!input) + input = (char *)malloc(INPUT_LENGTH); fprintf(monout, prompt); fflush(monout); fgets(in_ptr = input, INPUT_LENGTH, monin); @@ -298,7 +294,7 @@ static void read_line(char *prompt) * Read a character from the input line */ -static char get_char(void) +static char get_char() { return *in_ptr++; } @@ -318,12 +314,13 @@ static void put_back(char c) * Scanner: Get a token from the input line */ -enum Token mon_get_token(void) +enum Token mon_get_token() { - char c; + char c = get_char(); // Skip spaces - while ((c = get_char()) == ' ') ; + while (isspace(c)) + c = get_char(); switch (c) { case 0: @@ -457,41 +454,64 @@ static enum Token get_char_number(uintpt return T_NULL; } -static enum Token get_string(char *str) +static enum Token get_string(char *&str) { - char c; + // Remember start of string + char *old_in_ptr = in_ptr; + // Determine string length + char c; + unsigned n = 0; while ((c = get_char()) != 0) { - if (c == '"') { - *str = 0; - return T_STRING; - } - *str++ = c; + n++; + if (c == '"') + break; + } + if (c == 0) { + mon_error("Unterminated string"); + return T_NULL; } - mon_error("Unterminated string"); - return T_NULL; + // Allocate new buffer (n: size needed including terminating 0) + str = (char *)realloc(str, n); + + // Copy string to buffer + char *p = str; + in_ptr = old_in_ptr; + while (--n) + *p++ = get_char(); + *p++ = 0; + get_char(); // skip closing '"' + return T_STRING; } -static enum Token get_hex_or_name(uintptr &i, char *name) +static enum Token get_hex_or_name(uintptr &i, char *&name) { + // Remember start of token char *old_in_ptr = in_ptr; - char c; // Try hex number first if (get_hex_number(i) == T_NUMBER) return T_NUMBER; - // Not a hex number, must be a variable name + // Not a hex number, must be a variable name; determine its length in_ptr = old_in_ptr; - c = get_char(); + char c = get_char(); + unsigned n = 1; do { - *name++ = c; + n++; c = get_char(); } while (isalnum(c)); - *name = 0; - put_back(c); + // Allocate new buffer (n: size needed including terminating 0) + name = (char *)realloc(name, n); + + // Copy name to buffer + in_ptr = old_in_ptr; + char *p = name; + while (--n) + *p++ = get_char(); + *p = 0; return T_NAME; } @@ -785,7 +805,7 @@ static bool factor(uintptr *number) * set [var[=value]] */ -static void set_var(void) +static void set_var() { if (mon_token == T_END) { @@ -831,7 +851,7 @@ static void set_var(void) * cv */ -static void clear_vars(void) +static void clear_vars() { vars.clear(); } @@ -842,7 +862,7 @@ static void clear_vars(void) * h */ -static void help_or_hunt(void) +static void help_or_hunt() { if (mon_token != T_END) { hunt(); @@ -859,7 +879,7 @@ static void help_or_hunt(void) * ?? */ -static void mon_cmd_list(void) +static void mon_cmd_list() { for (int i=0; i ", 2 * sizeof(mon_dot_address), mon_dot_address); read_line(prompt); + if (!input) { + done = true; + continue; + } } else { if (argc == 0) { done = true; break; } else { - strncpy(in_ptr = input, argv[0], INPUT_LENGTH); + unsigned n = strlen(argv[0]) + 1; + input = (char *)realloc(input, n); + strcpy(in_ptr = input, argv[0]); argc--; argv++; } } // Skip leading spaces - while ((c = get_char()) == ' ') ; + char c = get_char(); + while (isspace(c)) + c = get_char(); + put_back(c); + if (!c) + continue; // blank line // Read command word - char *p = cmd; - do { - *p++ = c; + char *p = in_ptr; + while (isgraph(c)) c = get_char(); - } while (isgraph(c)); - *p = 0; put_back(c); + unsigned n = in_ptr - p; + cmd = (char *)realloc(cmd, n + 1); + memcpy(cmd, p, n); + cmd[n] = 0; // Execute command - if (cmd[0] == 0) // Blank line - continue; if (strcmp(cmd, "x") == 0) { // Exit done = true; continue; @@ -1190,6 +1242,9 @@ void mon(int argc, char **argv) cmd_done: ; } + if (cmd) + free(cmd); + exit_abort(); // Free buffer