--- mon/src/mon.cpp 2002/03/03 16:19:10 1.16 +++ 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 @@ -52,7 +52,7 @@ extern "C" { #include "mon_lowmem.h" #ifndef VERSION -#define VERSION "2" +#define VERSION "3" #endif @@ -66,66 +66,66 @@ 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; // Current address, value of '.' in expressions -uint32 mon_dot_address; +uintptr mon_dot_address; // Current value of ':' in expression static uint32 colon_value; // Scanner variables -enum Token mon_token; // Last token read -uint32 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 -typedef map var_map; +typedef std::map var_map; 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(uint32 &i); -static enum Token get_dec_number(uint32 &i); -static enum Token get_char_number(uint32 &i); -static enum Token get_string(char *str); -static enum Token get_hex_or_name(uint32 &i, char *name); - -static bool eor_expr(uint32 *number); // Parser -static bool and_expr(uint32 *number); -static bool shift_expr(uint32 *number); -static bool add_expr(uint32 *number); -static bool mul_expr(uint32 *number); -static bool factor(uint32 *number); +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 bool eor_expr(uintptr *number); // Parser +static bool and_expr(uintptr *number); +static bool shift_expr(uintptr *number); +static bool add_expr(uintptr *number); +static bool mul_expr(uintptr *number); +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; @@ -202,47 +202,47 @@ bool mon_aborted(void) * Access to buffer */ -uint32 (*mon_read_byte)(uint32 adr); +uint32 (*mon_read_byte)(uintptr adr); -uint32 mon_read_byte_buffer(uint32 adr) +uint32 mon_read_byte_buffer(uintptr adr) { return mem[adr % mon_mem_size]; } -uint32 mon_read_byte_real(uint32 adr) +uint32 mon_read_byte_real(uintptr adr) { return *(uint8 *)adr; } -void (*mon_write_byte)(uint32 adr, uint32 b); +void (*mon_write_byte)(uintptr adr, uint32 b); -void mon_write_byte_buffer(uint32 adr, uint32 b) +void mon_write_byte_buffer(uintptr adr, uint32 b) { mem[adr % mon_mem_size] = b; } -void mon_write_byte_real(uint32 adr, uint32 b) +void mon_write_byte_real(uintptr adr, uint32 b) { *(uint8 *)adr = b; } -uint32 mon_read_half(uint32 adr) +uint32 mon_read_half(uintptr adr) { return (mon_read_byte(adr) << 8) | mon_read_byte(adr+1); } -void mon_write_half(uint32 adr, uint32 w) +void mon_write_half(uintptr adr, uint32 w) { mon_write_byte(adr, w >> 8); mon_write_byte(adr+1, w); } -uint32 mon_read_word(uint32 adr) +uint32 mon_read_word(uintptr adr) { return (mon_read_byte(adr) << 24) | (mon_read_byte(adr+1) << 16) | (mon_read_byte(adr+2) << 8) | mon_read_byte(adr+3); } -void mon_write_word(uint32 adr, uint32 l) +void mon_write_word(uintptr adr, uint32 l) { mon_write_byte(adr, l >> 24); mon_write_byte(adr+1, l >> 16); @@ -258,21 +258,26 @@ void mon_write_word(uint32 adr, uint32 l 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: @@ -385,7 +391,7 @@ enum Token mon_get_token(void) } } -static enum Token get_hex_number(uint32 &i) +static enum Token get_hex_number(uintptr &i) { char c = get_char(); @@ -410,7 +416,7 @@ static enum Token get_hex_number(uint32 } } -static enum Token get_dec_number(uint32 &i) +static enum Token get_dec_number(uintptr &i) { char c = get_char(); @@ -431,7 +437,7 @@ static enum Token get_dec_number(uint32 } } -static enum Token get_char_number(uint32 &i) +static enum Token get_char_number(uintptr &i) { char c; @@ -446,41 +452,64 @@ static enum Token get_char_number(uint32 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(uint32 &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; } @@ -490,9 +519,9 @@ static enum Token get_hex_or_name(uint32 * true: OK, false: Error */ -bool mon_expression(uint32 *number) +bool mon_expression(uintptr *number) { - uint32 accu, expr; + uintptr accu, expr; if (!eor_expr(&accu)) return false; @@ -518,9 +547,9 @@ bool mon_expression(uint32 *number) * true: OK, false: Error */ -static bool eor_expr(uint32 *number) +static bool eor_expr(uintptr *number) { - uint32 accu, expr; + uintptr accu, expr; if (!and_expr(&accu)) return false; @@ -546,9 +575,9 @@ static bool eor_expr(uint32 *number) * true: OK, false: Error */ -static bool and_expr(uint32 *number) +static bool and_expr(uintptr *number) { - uint32 accu, expr; + uintptr accu, expr; if (!shift_expr(&accu)) return false; @@ -574,9 +603,9 @@ static bool and_expr(uint32 *number) * true: OK, false: Error */ -static bool shift_expr(uint32 *number) +static bool shift_expr(uintptr *number) { - uint32 accu, expr; + uintptr accu, expr; if (!add_expr(&accu)) return false; @@ -609,9 +638,9 @@ static bool shift_expr(uint32 *number) * true: OK, false: Error */ -static bool add_expr(uint32 *number) +static bool add_expr(uintptr *number) { - uint32 accu, expr; + uintptr accu, expr; if (!mul_expr(&accu)) return false; @@ -644,9 +673,9 @@ static bool add_expr(uint32 *number) * true: OK, false: Error */ -static bool mul_expr(uint32 *number) +static bool mul_expr(uintptr *number) { - uint32 accu, fact; + uintptr accu, fact; if (!factor(&accu)) return false; @@ -694,7 +723,7 @@ static bool mul_expr(uint32 *number) * true: OK, false: Error */ -static bool factor(uint32 *number) +static bool factor(uintptr *number) { switch (mon_token) { case T_NUMBER: @@ -774,7 +803,7 @@ static bool factor(uint32 *number) * set [var[=value]] */ -static void set_var(void) +static void set_var() { if (mon_token == T_END) { @@ -784,16 +813,16 @@ 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) { - string var_name = mon_name; + std::string var_name = mon_name; mon_get_token(); if (mon_token == T_ASSIGN) { // Set variable - uint32 value; + uintptr value; mon_get_token(); if (!mon_expression(&value)) return; @@ -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 ", 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