--- mon/src/mon.cpp 2002/08/03 14:54:28 1.19 +++ mon/src/mon.cpp 2007/01/21 17:32:05 1.26 @@ -1,7 +1,7 @@ /* * mon.cpp - cxmon main program * - * cxmon (C) 1997-2002 Christian Bauer, Marc Hellwig + * cxmon (C) 1997-2004 Christian Bauer, Marc Hellwig * * 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 @@ -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; @@ -258,21 +258,26 @@ void mon_write_word(uintptr adr, uint32 static void read_line(char *prompt) { #ifdef HAVE_LIBREADLINE - static char *line_read = NULL; - - if (line_read) { - free(line_read); - line_read = NULL; + if (input) + free(input); + input = readline(prompt); + + if (input) { + if (*input) + add_history(input); + } else { + // EOF, quit cxmon + input = (char *)malloc(2); + input[0] = 'x'; + input[1] = 0; + fprintf(monout, "x\n"); } - line_read = readline(prompt); - - if (line_read && *line_read) - add_history(line_read); - - strncpy(in_ptr = input, line_read, INPUT_LENGTH); - input[INPUT_LENGTH-1] = 0; + in_ptr = input; #else + static const unsigned INPUT_LENGTH = 256; + if (!input) + input = (char *)malloc(INPUT_LENGTH); fprintf(monout, prompt); fflush(monout); fgets(in_ptr = input, INPUT_LENGTH, monin); @@ -287,7 +292,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++; } @@ -307,12 +312,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: @@ -446,41 +452,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; } @@ -774,7 +803,7 @@ static bool factor(uintptr *number) * set [var[=value]] */ -static void set_var(void) +static void set_var() { if (mon_token == T_END) { @@ -784,7 +813,7 @@ static void set_var(void) else { var_map::const_iterator v = vars.begin(), end = vars.end(); for (v=vars.begin(); v!=end; ++v) - fprintf(monout, "%s = %08x\n", v->first.c_str(), v->second); + fprintf(monout, "%s = %08lx\n", v->first.c_str(), v->second); } } else if (mon_token == T_NAME) { @@ -820,7 +849,7 @@ static void set_var(void) * cv */ -static void clear_vars(void) +static void clear_vars() { vars.clear(); } @@ -831,7 +860,7 @@ static void clear_vars(void) * h */ -static void help_or_hunt(void) +static void help_or_hunt() { if (mon_token != T_END) { hunt(); @@ -848,7 +877,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); + sprintf(prompt, "[%0*lx]-> ", int(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; @@ -1178,6 +1240,9 @@ void mon(int argc, char **argv) cmd_done: ; } + if (cmd) + free(cmd); + exit_abort(); // Free buffer