66 |
|
FILE *monin, *monout, *monerr; |
67 |
|
|
68 |
|
// Input line |
69 |
< |
static char input[INPUT_LENGTH]; |
69 |
> |
static char *input; |
70 |
|
static char *in_ptr; |
71 |
|
char *mon_args_ptr; |
72 |
|
|
78 |
|
|
79 |
|
|
80 |
|
// Scanner variables |
81 |
< |
enum Token mon_token; // Last token read |
82 |
< |
uintptr mon_number; // Contains the number if mon_token==T_NUMBER |
83 |
< |
char mon_string[INPUT_LENGTH]; // Contains the string if mon_token==T_STRING |
84 |
< |
char mon_name[INPUT_LENGTH]; // Contains the variable name if mon_token==T_NAME |
81 |
> |
enum Token mon_token; // Last token read |
82 |
> |
uintptr mon_number; // Contains the number if mon_token==T_NUMBER |
83 |
> |
char *mon_string; // Contains the string if mon_token==T_STRING |
84 |
> |
char *mon_name; // Contains the variable name if mon_token==T_NAME |
85 |
|
|
86 |
|
|
87 |
|
// List of installed commands |
88 |
|
struct CmdSpec { |
89 |
< |
const char *name; // Name of command |
90 |
< |
void (*func)(void); // Function that executes this command |
89 |
> |
const char *name; // Name of command |
90 |
> |
void (*func)(); // Function that executes this command |
91 |
|
}; |
92 |
|
|
93 |
< |
static CmdSpec *cmds; // Array of CmdSpecs |
94 |
< |
static int num_cmds; // Number of installed commands |
95 |
< |
static char *cmd_help; // Help text for commands |
93 |
> |
static CmdSpec *cmds; // Array of CmdSpecs |
94 |
> |
static int num_cmds; // Number of installed commands |
95 |
> |
static char *cmd_help; // Help text for commands |
96 |
|
|
97 |
|
|
98 |
|
// List of variables |
101 |
|
|
102 |
|
|
103 |
|
// Prototypes |
104 |
< |
static void init_abort(void); |
105 |
< |
static void exit_abort(void); |
104 |
> |
static void init_abort(); |
105 |
> |
static void exit_abort(); |
106 |
|
|
107 |
|
static void read_line(char *prompt); // Scanner |
108 |
< |
static char get_char(void); |
108 |
> |
static char get_char(); |
109 |
|
static void put_back(char c); |
110 |
|
static enum Token get_hex_number(uintptr &i); |
111 |
|
static enum Token get_dec_number(uintptr &i); |
112 |
|
static enum Token get_char_number(uintptr &i); |
113 |
< |
static enum Token get_string(char *str); |
114 |
< |
static enum Token get_hex_or_name(uintptr &i, char *name); |
113 |
> |
static enum Token get_string(char *&str); |
114 |
> |
static enum Token get_hex_or_name(uintptr &i, char *&name); |
115 |
|
|
116 |
|
static bool eor_expr(uintptr *number); // Parser |
117 |
|
static bool and_expr(uintptr *number); |
125 |
|
* Add command to mon |
126 |
|
*/ |
127 |
|
|
128 |
< |
void mon_add_command(const char *name, void (*func)(void), const char *help_text) |
128 |
> |
void mon_add_command(const char *name, void (*func)(), const char *help_text) |
129 |
|
{ |
130 |
|
num_cmds++; |
131 |
|
if (cmds) |
170 |
|
was_aborted = true; |
171 |
|
} |
172 |
|
|
173 |
< |
static void init_abort(void) |
173 |
> |
static void init_abort() |
174 |
|
{ |
175 |
|
was_aborted = false; |
176 |
|
sigemptyset(&my_sa.sa_mask); |
184 |
|
sigaction(SIGINT, &my_sa, NULL); |
185 |
|
} |
186 |
|
|
187 |
< |
static void exit_abort(void) |
187 |
> |
static void exit_abort() |
188 |
|
{ |
189 |
|
my_sa.sa_handler = SIG_DFL; |
190 |
|
sigaction(SIGINT, &my_sa, NULL); |
191 |
|
} |
192 |
|
|
193 |
< |
bool mon_aborted(void) |
193 |
> |
bool mon_aborted() |
194 |
|
{ |
195 |
|
bool ret = was_aborted; |
196 |
|
was_aborted = false; |
260 |
|
#ifdef HAVE_LIBREADLINE |
261 |
|
static char *line_read = NULL; |
262 |
|
|
263 |
< |
if (line_read) { |
264 |
< |
free(line_read); |
265 |
< |
line_read = NULL; |
266 |
< |
} |
267 |
< |
|
268 |
< |
line_read = readline(prompt); |
269 |
< |
|
270 |
< |
if (line_read) { |
271 |
< |
|
272 |
< |
if (*line_read) |
273 |
< |
add_history(line_read); |
274 |
< |
|
275 |
< |
strncpy(in_ptr = input, line_read, INPUT_LENGTH); |
276 |
< |
input[INPUT_LENGTH-1] = 0; |
277 |
< |
|
263 |
> |
if (input) |
264 |
> |
free(input); |
265 |
> |
input = readline(prompt); |
266 |
> |
|
267 |
> |
if (input) { |
268 |
> |
if (*input) |
269 |
> |
add_history(input); |
270 |
|
} else { |
279 |
– |
|
271 |
|
// EOF, quit cxmon |
272 |
< |
fprintf(monout, "x\n"); |
272 |
> |
input = (char *)malloc(2); |
273 |
|
input[0] = 'x'; |
274 |
|
input[1] = 0; |
275 |
< |
in_ptr = input; |
275 |
> |
fprintf(monout, "x\n"); |
276 |
|
} |
277 |
+ |
|
278 |
+ |
in_ptr = input; |
279 |
|
#else |
280 |
+ |
static const unsigned INPUT_LENGTH = 256; |
281 |
+ |
if (!input) |
282 |
+ |
input = (char *)malloc(INPUT_LENGTH); |
283 |
|
fprintf(monout, prompt); |
284 |
|
fflush(monout); |
285 |
|
fgets(in_ptr = input, INPUT_LENGTH, monin); |
294 |
|
* Read a character from the input line |
295 |
|
*/ |
296 |
|
|
297 |
< |
static char get_char(void) |
297 |
> |
static char get_char() |
298 |
|
{ |
299 |
|
return *in_ptr++; |
300 |
|
} |
314 |
|
* Scanner: Get a token from the input line |
315 |
|
*/ |
316 |
|
|
317 |
< |
enum Token mon_get_token(void) |
317 |
> |
enum Token mon_get_token() |
318 |
|
{ |
319 |
< |
char c; |
319 |
> |
char c = get_char(); |
320 |
|
|
321 |
|
// Skip spaces |
322 |
< |
while ((c = get_char()) == ' ') ; |
322 |
> |
while (isspace(c)) |
323 |
> |
c = get_char(); |
324 |
|
|
325 |
|
switch (c) { |
326 |
|
case 0: |
454 |
|
return T_NULL; |
455 |
|
} |
456 |
|
|
457 |
< |
static enum Token get_string(char *str) |
457 |
> |
static enum Token get_string(char *&str) |
458 |
|
{ |
459 |
< |
char c; |
459 |
> |
// Remember start of string |
460 |
> |
char *old_in_ptr = in_ptr; |
461 |
|
|
462 |
+ |
// Determine string length |
463 |
+ |
char c; |
464 |
+ |
unsigned n = 0; |
465 |
|
while ((c = get_char()) != 0) { |
466 |
< |
if (c == '"') { |
467 |
< |
*str = 0; |
468 |
< |
return T_STRING; |
469 |
< |
} |
470 |
< |
*str++ = c; |
466 |
> |
n++; |
467 |
> |
if (c == '"') |
468 |
> |
break; |
469 |
> |
} |
470 |
> |
if (c == 0) { |
471 |
> |
mon_error("Unterminated string"); |
472 |
> |
return T_NULL; |
473 |
|
} |
474 |
|
|
475 |
< |
mon_error("Unterminated string"); |
476 |
< |
return T_NULL; |
475 |
> |
// Allocate new buffer (n: size needed including terminating 0) |
476 |
> |
str = (char *)realloc(str, n); |
477 |
> |
|
478 |
> |
// Copy string to buffer |
479 |
> |
char *p = str; |
480 |
> |
in_ptr = old_in_ptr; |
481 |
> |
while (--n) |
482 |
> |
*p++ = get_char(); |
483 |
> |
*p++ = 0; |
484 |
> |
get_char(); // skip closing '"' |
485 |
> |
return T_STRING; |
486 |
|
} |
487 |
|
|
488 |
< |
static enum Token get_hex_or_name(uintptr &i, char *name) |
488 |
> |
static enum Token get_hex_or_name(uintptr &i, char *&name) |
489 |
|
{ |
490 |
+ |
// Remember start of token |
491 |
|
char *old_in_ptr = in_ptr; |
479 |
– |
char c; |
492 |
|
|
493 |
|
// Try hex number first |
494 |
|
if (get_hex_number(i) == T_NUMBER) |
495 |
|
return T_NUMBER; |
496 |
|
|
497 |
< |
// Not a hex number, must be a variable name |
497 |
> |
// Not a hex number, must be a variable name; determine its length |
498 |
|
in_ptr = old_in_ptr; |
499 |
< |
c = get_char(); |
499 |
> |
char c = get_char(); |
500 |
> |
unsigned n = 1; |
501 |
|
do { |
502 |
< |
*name++ = c; |
502 |
> |
n++; |
503 |
|
c = get_char(); |
504 |
|
} while (isalnum(c)); |
505 |
|
|
506 |
< |
*name = 0; |
507 |
< |
put_back(c); |
506 |
> |
// Allocate new buffer (n: size needed including terminating 0) |
507 |
> |
name = (char *)realloc(name, n); |
508 |
> |
|
509 |
> |
// Copy name to buffer |
510 |
> |
in_ptr = old_in_ptr; |
511 |
> |
char *p = name; |
512 |
> |
while (--n) |
513 |
> |
*p++ = get_char(); |
514 |
> |
*p = 0; |
515 |
|
return T_NAME; |
516 |
|
} |
517 |
|
|
805 |
|
* set [var[=value]] |
806 |
|
*/ |
807 |
|
|
808 |
< |
static void set_var(void) |
808 |
> |
static void set_var() |
809 |
|
{ |
810 |
|
if (mon_token == T_END) { |
811 |
|
|
851 |
|
* cv |
852 |
|
*/ |
853 |
|
|
854 |
< |
static void clear_vars(void) |
854 |
> |
static void clear_vars() |
855 |
|
{ |
856 |
|
vars.clear(); |
857 |
|
} |
862 |
|
* h |
863 |
|
*/ |
864 |
|
|
865 |
< |
static void help_or_hunt(void) |
865 |
> |
static void help_or_hunt() |
866 |
|
{ |
867 |
|
if (mon_token != T_END) { |
868 |
|
hunt(); |
879 |
|
* ?? |
880 |
|
*/ |
881 |
|
|
882 |
< |
static void mon_cmd_list(void) |
882 |
> |
static void mon_cmd_list() |
883 |
|
{ |
884 |
|
for (int i=0; i<num_cmds; i++) |
885 |
|
fprintf(monout, "%s ", cmds[i].name); |
892 |
|
* @ [size] |
893 |
|
*/ |
894 |
|
|
895 |
< |
static void reallocate(void) |
895 |
> |
static void reallocate() |
896 |
|
{ |
897 |
|
uintptr size; |
898 |
|
|
982 |
|
mon_dot_address = adr; |
983 |
|
} |
984 |
|
|
985 |
< |
static void apply_byte(void) |
985 |
> |
static void apply_byte() |
986 |
|
{ |
987 |
|
apply(1); |
988 |
|
} |
989 |
|
|
990 |
< |
static void apply_half(void) |
990 |
> |
static void apply_half() |
991 |
|
{ |
992 |
|
apply(2); |
993 |
|
} |
994 |
|
|
995 |
< |
static void apply_word(void) |
995 |
> |
static void apply_word() |
996 |
|
{ |
997 |
|
apply(4); |
998 |
|
} |
1002 |
|
* Execute command via system() (for ls, rm, etc.) |
1003 |
|
*/ |
1004 |
|
|
1005 |
< |
static void mon_exec(void) |
1005 |
> |
static void mon_exec() |
1006 |
|
{ |
1007 |
|
system(input); |
1008 |
|
} |
1012 |
|
* Change current directory |
1013 |
|
*/ |
1014 |
|
|
1015 |
< |
static void mon_change_dir(void) |
1015 |
> |
void mon_change_dir() |
1016 |
|
{ |
997 |
– |
char c; |
1017 |
|
in_ptr = input; |
1018 |
< |
while ((c = get_char()) == ' ') ; |
1019 |
< |
while ((c = get_char()) != ' ') ; |
1020 |
< |
while ((c = get_char()) == ' ') ; |
1018 |
> |
char c = get_char(); |
1019 |
> |
while (isspace(c)) |
1020 |
> |
c = get_char(); |
1021 |
> |
while (isgraph(c)) |
1022 |
> |
c = get_char(); |
1023 |
> |
while (isspace(c)) |
1024 |
> |
c = get_char(); |
1025 |
|
put_back(c); |
1026 |
|
if (chdir(in_ptr) != 0) |
1027 |
|
mon_error("Cannot change directory"); |
1032 |
|
* Initialize mon |
1033 |
|
*/ |
1034 |
|
|
1035 |
< |
void mon_init(void) |
1035 |
> |
void mon_init() |
1036 |
|
{ |
1037 |
|
cmds = NULL; |
1038 |
|
num_cmds = 0; |
1075 |
|
|
1076 |
|
mon_read_byte = NULL; |
1077 |
|
mon_write_byte = NULL; |
1078 |
+ |
|
1079 |
+ |
input = NULL; |
1080 |
+ |
mon_string = NULL; |
1081 |
+ |
mon_name = NULL; |
1082 |
|
} |
1083 |
|
|
1084 |
|
|
1086 |
|
* Deinitialize mon |
1087 |
|
*/ |
1088 |
|
|
1089 |
< |
void mon_exit(void) |
1089 |
> |
void mon_exit() |
1090 |
|
{ |
1091 |
< |
free(cmds); |
1092 |
< |
cmds = NULL; |
1091 |
> |
if (cmds) { |
1092 |
> |
free(cmds); |
1093 |
> |
cmds = NULL; |
1094 |
> |
} |
1095 |
|
num_cmds = 0; |
1096 |
|
cmd_help = NULL; |
1097 |
+ |
|
1098 |
+ |
if (input) { |
1099 |
+ |
free(input); |
1100 |
+ |
input = NULL; |
1101 |
+ |
} |
1102 |
+ |
if (mon_string) { |
1103 |
+ |
free(mon_string); |
1104 |
+ |
mon_string = NULL; |
1105 |
+ |
} |
1106 |
+ |
if (mon_name) { |
1107 |
+ |
free(mon_name); |
1108 |
+ |
mon_name = NULL; |
1109 |
+ |
} |
1110 |
|
} |
1111 |
|
|
1112 |
|
|
1117 |
|
void mon(int argc, char **argv) |
1118 |
|
{ |
1119 |
|
bool done = false, interactive = true; |
1078 |
– |
char c, cmd[INPUT_LENGTH]; |
1120 |
|
|
1121 |
|
// Setup input/output streams |
1122 |
|
monin = stdin; |
1185 |
|
init_abort(); |
1186 |
|
|
1187 |
|
// Read and parse command line |
1188 |
+ |
char *cmd = NULL; |
1189 |
|
while (!done) { |
1190 |
|
if (interactive) { |
1191 |
|
char prompt[16]; |
1192 |
|
sprintf(prompt, "[%0*lx]-> ", 2 * sizeof(mon_dot_address), mon_dot_address); |
1193 |
|
read_line(prompt); |
1194 |
+ |
if (!input) { |
1195 |
+ |
done = true; |
1196 |
+ |
continue; |
1197 |
+ |
} |
1198 |
|
} else { |
1199 |
|
if (argc == 0) { |
1200 |
|
done = true; |
1201 |
|
break; |
1202 |
|
} else { |
1203 |
< |
strncpy(in_ptr = input, argv[0], INPUT_LENGTH); |
1203 |
> |
unsigned n = strlen(argv[0]) + 1; |
1204 |
> |
input = (char *)realloc(input, n); |
1205 |
> |
strcpy(in_ptr = input, argv[0]); |
1206 |
|
argc--; |
1207 |
|
argv++; |
1208 |
|
} |
1209 |
|
} |
1210 |
|
|
1211 |
|
// Skip leading spaces |
1212 |
< |
while ((c = get_char()) == ' ') ; |
1212 |
> |
char c = get_char(); |
1213 |
> |
while (isspace(c)) |
1214 |
> |
c = get_char(); |
1215 |
> |
put_back(c); |
1216 |
> |
if (!c) |
1217 |
> |
continue; // blank line |
1218 |
|
|
1219 |
|
// Read command word |
1220 |
< |
char *p = cmd; |
1221 |
< |
do { |
1169 |
< |
*p++ = c; |
1220 |
> |
char *p = in_ptr; |
1221 |
> |
while (isgraph(c)) |
1222 |
|
c = get_char(); |
1171 |
– |
} while (isgraph(c)); |
1172 |
– |
*p = 0; |
1223 |
|
put_back(c); |
1224 |
+ |
unsigned n = in_ptr - p; |
1225 |
+ |
cmd = (char *)realloc(cmd, n + 1); |
1226 |
+ |
memcpy(cmd, p, n); |
1227 |
+ |
cmd[n] = 0; |
1228 |
|
|
1229 |
|
// Execute command |
1176 |
– |
if (cmd[0] == 0) // Blank line |
1177 |
– |
continue; |
1230 |
|
if (strcmp(cmd, "x") == 0) { // Exit |
1231 |
|
done = true; |
1232 |
|
continue; |
1242 |
|
cmd_done: ; |
1243 |
|
} |
1244 |
|
|
1245 |
+ |
if (cmd) |
1246 |
+ |
free(cmd); |
1247 |
+ |
|
1248 |
|
exit_abort(); |
1249 |
|
|
1250 |
|
// Free buffer |