--- mon/src/mon.cpp 2000/06/27 14:52:44 1.9 +++ mon/src/mon.cpp 2007/06/07 09:51:56 1.27 @@ -1,7 +1,7 @@ /* - * mon.cpp - mon main program + * mon.cpp - cxmon main program * - * mon (C) 1997-2000 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 @@ -24,6 +24,8 @@ #include #include #include +#include +#include #if defined(HAVE_READLINE_H) extern "C" { @@ -47,9 +49,10 @@ extern "C" { #include "mon.h" #include "mon_cmd.h" +#include "mon_lowmem.h" #ifndef VERSION -#define VERSION "2" +#define VERSION "3" #endif @@ -63,74 +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 -struct Variable { - Variable *next; // Pointer to next variable (must be first element of struct) - char *name; // Variable name - uint32 value; // Variable value -}; - -static Variable *first_var; // Pointer to first variable +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 Variable *lookup_var(const char *s); -static Variable *insert_var(const char *s); -static void remove_var(const char *s); +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) @@ -175,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); @@ -189,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; @@ -207,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); @@ -263,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); @@ -292,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++; } @@ -312,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: @@ -390,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(); @@ -399,6 +400,7 @@ static enum Token get_hex_number(uint32 return T_NULL; do { + c = tolower(c); if (c < 'a') i = (i << 4) + (c - '0'); else @@ -414,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(); @@ -435,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; @@ -450,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; } @@ -494,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; @@ -522,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; @@ -550,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; @@ -578,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; @@ -613,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; @@ -648,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; @@ -698,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: @@ -707,13 +732,14 @@ static bool factor(uint32 *number) return true; case T_NAME:{ - Variable *var; - if ((var = lookup_var(mon_name)) != NULL) { - *number = var->value; + var_map::const_iterator v = vars.find(mon_name); + if (v == vars.end()) + return false; + else { + *number = v->second; mon_get_token(); return true; - } else - return false; + } } case T_DOT: @@ -773,85 +799,30 @@ static bool factor(uint32 *number) /* - * Lookup the value of a variable - */ - -static Variable *lookup_var(const char *s) -{ - // Lookup variable - for (Variable *var=first_var; var; var=var->next) - if (!strcmp(s, var->name)) - return var; - - // Not found, error - mon_error("Undefined variable"); - return NULL; -} - - -/* - * Insert new variable (or redefine old) - */ - -static Variable *insert_var(const char *s) -{ - // Lookup variable - for (Variable *var=first_var; var; var=var->next) - if (!strcmp(s, var->name)) - return var; - - // Insert new variable - Variable *var = new Variable; - var->name = strdup(s); - var->next = first_var; - first_var = var; - return var; -} - - -/* - * Remove variable - */ - -static void remove_var(const char *s) -{ - Variable *var, *prev = (Variable *)&first_var; - - // Lookup variable and remove it - for (var=prev->next; var; prev=var, var=var->next) - if (!strcmp(s, var->name)) { - prev->next = var->next; - free(var->name); - free(var); - return; - } -} - - -/* * Set/clear/show variables * set [var[=value]] */ -static void set_var(void) +static void set_var() { if (mon_token == T_END) { // Show all variables - if (first_var == NULL) + if (vars.empty()) fprintf(monout, "No variables defined\n"); - else - for (Variable *v=first_var; v; v=v->next) - fprintf(monout, "%s = %08x\n", v->name, v->value); + else { + var_map::const_iterator v = vars.begin(), end = vars.end(); + for (v=vars.begin(); v!=end; ++v) + fprintf(monout, "%s = %08lx\n", v->first.c_str(), v->second); + } } else if (mon_token == T_NAME) { - char var_name[256]; - strcpy(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; @@ -859,12 +830,12 @@ static void set_var(void) mon_error("Too many arguments"); return; } - insert_var(var_name)->value = value; + vars[var_name] = value; } else if (mon_token == T_END) { // Clear variable - remove_var(var_name); + vars.erase(var_name); } else mon_error("'=' expected"); @@ -878,15 +849,9 @@ static void set_var(void) * cv */ -static void clear_vars(void) +static void clear_vars() { - Variable *var, *next; - for (var=first_var; var; var=next) { - free(var->name); - next = var->next; - free(var); - } - first_var = NULL; + vars.clear(); } @@ -895,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(); @@ -912,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 0) { + if (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0) { + printf("Usage: %s [-m] [-r] [command...]\n", prg_name); + exit(0); + } else if (strcmp(argv[0], "-m") == 0) + mon_macos_mode = true; + else if (strcmp(argv[0], "-r") == 0) mon_use_real_mem = true; - - // Make argc/argv point to the actual arguments - argc--; - argv++; - interactive = (argc == 0); + else + break; + argc--; argv++; } + interactive = (argc == 0); // Set up memory access functions if not supplied by the user if (mon_read_byte == NULL) { @@ -1169,44 +1166,67 @@ void mon(int argc, char **argv) // Print banner if (interactive) - fprintf(monerr, "\n *** mon V" VERSION " by Christian Bauer and Marc Hellwig ***\n" + fprintf(monerr, "\n *** cxmon V" VERSION " by Christian Bauer and Marc Hellwig ***\n" " *** Press 'h' for help ***\n\n"); } + // Clear variables + vars.clear(); + + // In MacOS mode, pull in the lowmem globals as variables + if (mon_macos_mode) { + const lowmem_info *l = lowmem; + while (l->name) { + vars[l->name] = l->addr; + l++; + } + } + init_abort(); // Read and parse command line + char *cmd = NULL; while (!done) { if (interactive) { char prompt[16]; - sprintf(prompt, "[%08x]-> ", 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; @@ -1222,6 +1242,9 @@ void mon(int argc, char **argv) cmd_done: ; } + if (cmd) + free(cmd); + exit_abort(); // Free buffer