1 |
|
/* |
2 |
|
* mon.cpp - cxmon main program |
3 |
|
* |
4 |
< |
* cxmon (C) 1997-2000 Christian Bauer, Marc Hellwig |
4 |
> |
* cxmon (C) 1997-2002 Christian Bauer, Marc Hellwig |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
24 |
|
#include <stdlib.h> |
25 |
|
#include <signal.h> |
26 |
|
#include <ctype.h> |
27 |
+ |
#include <string> |
28 |
+ |
#include <map> |
29 |
|
|
30 |
|
#if defined(HAVE_READLINE_H) |
31 |
|
extern "C" { |
49 |
|
|
50 |
|
#include "mon.h" |
51 |
|
#include "mon_cmd.h" |
52 |
+ |
#include "mon_lowmem.h" |
53 |
|
|
54 |
|
#ifndef VERSION |
55 |
|
#define VERSION "2" |
96 |
|
|
97 |
|
|
98 |
|
// List of variables |
99 |
< |
struct Variable { |
100 |
< |
Variable *next; // Pointer to next variable (must be first element of struct) |
98 |
< |
char *name; // Variable name |
99 |
< |
uint32 value; // Variable value |
100 |
< |
}; |
101 |
< |
|
102 |
< |
static Variable *first_var; // Pointer to first variable |
99 |
> |
typedef map<string, uint32> var_map; |
100 |
> |
static var_map vars; |
101 |
|
|
102 |
|
|
103 |
|
// Prototypes |
119 |
|
static bool add_expr(uint32 *number); |
120 |
|
static bool mul_expr(uint32 *number); |
121 |
|
static bool factor(uint32 *number); |
124 |
– |
static Variable *lookup_var(const char *s); |
125 |
– |
static Variable *insert_var(const char *s); |
126 |
– |
static void remove_var(const char *s); |
122 |
|
|
123 |
|
|
124 |
|
/* |
702 |
|
return true; |
703 |
|
|
704 |
|
case T_NAME:{ |
705 |
< |
Variable *var; |
706 |
< |
if ((var = lookup_var(mon_name)) != NULL) { |
707 |
< |
*number = var->value; |
705 |
> |
var_map::const_iterator v = vars.find(mon_name); |
706 |
> |
if (v == vars.end()) |
707 |
> |
return false; |
708 |
> |
else { |
709 |
> |
*number = v->second; |
710 |
|
mon_get_token(); |
711 |
|
return true; |
712 |
< |
} else |
716 |
< |
return false; |
712 |
> |
} |
713 |
|
} |
714 |
|
|
715 |
|
case T_DOT: |
769 |
|
|
770 |
|
|
771 |
|
/* |
776 |
– |
* Lookup the value of a variable |
777 |
– |
*/ |
778 |
– |
|
779 |
– |
static Variable *lookup_var(const char *s) |
780 |
– |
{ |
781 |
– |
// Lookup variable |
782 |
– |
for (Variable *var=first_var; var; var=var->next) |
783 |
– |
if (!strcmp(s, var->name)) |
784 |
– |
return var; |
785 |
– |
|
786 |
– |
// Not found, error |
787 |
– |
mon_error("Undefined variable"); |
788 |
– |
return NULL; |
789 |
– |
} |
790 |
– |
|
791 |
– |
|
792 |
– |
/* |
793 |
– |
* Insert new variable (or redefine old) |
794 |
– |
*/ |
795 |
– |
|
796 |
– |
static Variable *insert_var(const char *s) |
797 |
– |
{ |
798 |
– |
// Lookup variable |
799 |
– |
for (Variable *var=first_var; var; var=var->next) |
800 |
– |
if (!strcmp(s, var->name)) |
801 |
– |
return var; |
802 |
– |
|
803 |
– |
// Insert new variable |
804 |
– |
Variable *var = new Variable; |
805 |
– |
var->name = strdup(s); |
806 |
– |
var->next = first_var; |
807 |
– |
first_var = var; |
808 |
– |
return var; |
809 |
– |
} |
810 |
– |
|
811 |
– |
|
812 |
– |
/* |
813 |
– |
* Remove variable |
814 |
– |
*/ |
815 |
– |
|
816 |
– |
static void remove_var(const char *s) |
817 |
– |
{ |
818 |
– |
Variable *var, *prev = (Variable *)&first_var; |
819 |
– |
|
820 |
– |
// Lookup variable and remove it |
821 |
– |
for (var=prev->next; var; prev=var, var=var->next) |
822 |
– |
if (!strcmp(s, var->name)) { |
823 |
– |
prev->next = var->next; |
824 |
– |
free(var->name); |
825 |
– |
free(var); |
826 |
– |
return; |
827 |
– |
} |
828 |
– |
} |
829 |
– |
|
830 |
– |
|
831 |
– |
/* |
772 |
|
* Set/clear/show variables |
773 |
|
* set [var[=value]] |
774 |
|
*/ |
778 |
|
if (mon_token == T_END) { |
779 |
|
|
780 |
|
// Show all variables |
781 |
< |
if (first_var == NULL) |
781 |
> |
if (vars.empty()) |
782 |
|
fprintf(monout, "No variables defined\n"); |
783 |
< |
else |
784 |
< |
for (Variable *v=first_var; v; v=v->next) |
785 |
< |
fprintf(monout, "%s = %08x\n", v->name, v->value); |
783 |
> |
else { |
784 |
> |
var_map::const_iterator v = vars.begin(), end = vars.end(); |
785 |
> |
for (v=vars.begin(); v!=end; ++v) |
786 |
> |
fprintf(monout, "%s = %08x\n", v->first.c_str(), v->second); |
787 |
> |
} |
788 |
|
|
789 |
|
} else if (mon_token == T_NAME) { |
790 |
< |
char var_name[256]; |
849 |
< |
strcpy(var_name, mon_name); |
790 |
> |
string var_name = mon_name; |
791 |
|
mon_get_token(); |
792 |
|
if (mon_token == T_ASSIGN) { |
793 |
|
|
800 |
|
mon_error("Too many arguments"); |
801 |
|
return; |
802 |
|
} |
803 |
< |
insert_var(var_name)->value = value; |
803 |
> |
vars[var_name] = value; |
804 |
|
|
805 |
|
} else if (mon_token == T_END) { |
806 |
|
|
807 |
|
// Clear variable |
808 |
< |
remove_var(var_name); |
808 |
> |
vars.erase(var_name); |
809 |
|
|
810 |
|
} else |
811 |
|
mon_error("'=' expected"); |
821 |
|
|
822 |
|
static void clear_vars(void) |
823 |
|
{ |
824 |
< |
Variable *var, *next; |
884 |
< |
for (var=first_var; var; var=next) { |
885 |
< |
free(var->name); |
886 |
< |
next = var->next; |
887 |
< |
free(var); |
888 |
< |
} |
889 |
< |
first_var = NULL; |
824 |
> |
vars.clear(); |
825 |
|
} |
826 |
|
|
827 |
|
|
1070 |
|
monerr = stdout; |
1071 |
|
|
1072 |
|
// Make argc/argv point to the actual arguments |
1073 |
+ |
const char *prg_name = argv[0]; |
1074 |
|
if (argc) |
1075 |
|
argc--; argv++; |
1076 |
|
|
1078 |
|
mon_macos_mode = false; |
1079 |
|
mon_use_real_mem = false; |
1080 |
|
while (argc > 0) { |
1081 |
< |
if (strcmp(argv[0], "-m") == 0) |
1081 |
> |
if (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0) { |
1082 |
> |
printf("Usage: %s [-m] [-r] [command...]\n", prg_name); |
1083 |
> |
exit(0); |
1084 |
> |
} else if (strcmp(argv[0], "-m") == 0) |
1085 |
|
mon_macos_mode = true; |
1086 |
|
else if (strcmp(argv[0], "-r") == 0) |
1087 |
|
mon_use_real_mem = true; |
1116 |
|
" *** Press 'h' for help ***\n\n"); |
1117 |
|
} |
1118 |
|
|
1119 |
+ |
// Clear variables |
1120 |
+ |
vars.clear(); |
1121 |
+ |
|
1122 |
+ |
// In MacOS mode, pull in the lowmem globals as variables |
1123 |
+ |
if (mon_macos_mode) { |
1124 |
+ |
const lowmem_info *l = lowmem; |
1125 |
+ |
while (l->name) { |
1126 |
+ |
vars[l->name] = l->addr; |
1127 |
+ |
l++; |
1128 |
+ |
} |
1129 |
+ |
} |
1130 |
+ |
|
1131 |
|
init_abort(); |
1132 |
|
|
1133 |
|
// Read and parse command line |