ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/mon/src/mon.cpp
(Generate patch)

Comparing mon/src/mon.cpp (file contents):
Revision 1.21 by cebix, 2003-09-27T20:30:44Z vs.
Revision 1.26 by cebix, 2007-01-21T17:32:05Z

# Line 1 | Line 1
1   /*
2   *  mon.cpp - cxmon main program
3   *
4 < *  cxmon (C) 1997-2002 Christian Bauer, Marc Hellwig
4 > *  cxmon (C) 1997-2004 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
# Line 66 | Line 66 | static uint8 *mem;
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  
# Line 78 | Line 78 | static uint32 colon_value;
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
# Line 101 | Line 101 | static var_map vars;
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);
# Line 125 | Line 125 | static bool factor(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)
# Line 170 | Line 170 | static void handle_abort(int sig)
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);
# Line 184 | Line 184 | static void init_abort(void)
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;
# Line 258 | Line 258 | void mon_write_word(uintptr adr, uint32
258   static void read_line(char *prompt)
259   {
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 <
261 >        if (input)
262 >                free(input);
263 >        input = readline(prompt);
264 >
265 >        if (input) {
266 >                if (*input)
267 >                        add_history(input);
268          } else {
279
269                  // EOF, quit cxmon
270 <                fprintf(monout, "x\n");
270 >                input = (char *)malloc(2);
271                  input[0] = 'x';
272                  input[1] = 0;
273 <                in_ptr = input;
273 >                fprintf(monout, "x\n");
274          }
275 +
276 +        in_ptr = input;
277   #else
278 +        static const unsigned INPUT_LENGTH = 256;
279 +        if (!input)
280 +                input = (char *)malloc(INPUT_LENGTH);
281          fprintf(monout, prompt);
282          fflush(monout);
283          fgets(in_ptr = input, INPUT_LENGTH, monin);
# Line 298 | Line 292 | static void read_line(char *prompt)
292   *  Read a character from the input line
293   */
294  
295 < static char get_char(void)
295 > static char get_char()
296   {
297          return *in_ptr++;
298   }
# Line 318 | Line 312 | static void put_back(char c)
312   *  Scanner: Get a token from the input line
313   */
314  
315 < enum Token mon_get_token(void)
315 > enum Token mon_get_token()
316   {
317 <        char c;
317 >        char c = get_char();
318  
319          // Skip spaces
320 <        while ((c = get_char()) == ' ') ;
320 >        while (isspace(c))
321 >                c = get_char();
322  
323          switch (c) {
324                  case 0:
# Line 457 | Line 452 | static enum Token get_char_number(uintpt
452          return T_NULL;
453   }
454  
455 < static enum Token get_string(char *str)
455 > static enum Token get_string(char *&str)
456   {
457 <        char c;
457 >        // Remember start of string
458 >        char *old_in_ptr = in_ptr;
459  
460 +        // Determine string length
461 +        char c;
462 +        unsigned n = 0;
463          while ((c = get_char()) != 0) {
464 <                if (c == '"') {
465 <                        *str = 0;
466 <                        return T_STRING;
467 <                }
468 <                *str++ = c;
464 >                n++;
465 >                if (c == '"')
466 >                        break;
467 >        }
468 >        if (c == 0) {
469 >                mon_error("Unterminated string");
470 >                return T_NULL;
471          }
472  
473 <        mon_error("Unterminated string");
474 <        return T_NULL;
473 >        // Allocate new buffer (n: size needed including terminating 0)
474 >        str = (char *)realloc(str, n);
475 >
476 >        // Copy string to buffer
477 >        char *p = str;
478 >        in_ptr = old_in_ptr;
479 >        while (--n)
480 >                *p++ = get_char();
481 >        *p++ = 0;
482 >        get_char();  // skip closing '"'
483 >        return T_STRING;
484   }
485  
486 < static enum Token get_hex_or_name(uintptr &i, char *name)
486 > static enum Token get_hex_or_name(uintptr &i, char *&name)
487   {
488 +        // Remember start of token
489          char *old_in_ptr = in_ptr;
479        char c;
490  
491          // Try hex number first
492          if (get_hex_number(i) == T_NUMBER)
493                  return T_NUMBER;
494  
495 <        // Not a hex number, must be a variable name
495 >        // Not a hex number, must be a variable name; determine its length
496          in_ptr = old_in_ptr;
497 <        c = get_char();
497 >        char c = get_char();
498 >        unsigned n = 1;
499          do {
500 <                *name++ = c;
500 >                n++;
501                  c = get_char();
502          } while (isalnum(c));
503  
504 <        *name = 0;
505 <        put_back(c);
504 >        // Allocate new buffer (n: size needed including terminating 0)
505 >        name = (char *)realloc(name, n);
506 >
507 >        // Copy name to buffer
508 >        in_ptr = old_in_ptr;
509 >        char *p = name;
510 >        while (--n)
511 >                *p++ = get_char();
512 >        *p = 0;
513          return T_NAME;
514   }
515  
# Line 785 | Line 803 | static bool factor(uintptr *number)
803   *  set [var[=value]]
804   */
805  
806 < static void set_var(void)
806 > static void set_var()
807   {
808          if (mon_token == T_END) {
809  
# Line 795 | Line 813 | static void set_var(void)
813                  else {
814                          var_map::const_iterator v = vars.begin(), end = vars.end();
815                          for (v=vars.begin(); v!=end; ++v)
816 <                                fprintf(monout, "%s = %08x\n", v->first.c_str(), v->second);
816 >                                fprintf(monout, "%s = %08lx\n", v->first.c_str(), v->second);
817                  }
818  
819          } else if (mon_token == T_NAME) {
# Line 831 | Line 849 | static void set_var(void)
849   *  cv
850   */
851  
852 < static void clear_vars(void)
852 > static void clear_vars()
853   {
854          vars.clear();
855   }
# Line 842 | Line 860 | static void clear_vars(void)
860   *  h
861   */
862  
863 < static void help_or_hunt(void)
863 > static void help_or_hunt()
864   {
865          if (mon_token != T_END) {
866                  hunt();
# Line 859 | Line 877 | static void help_or_hunt(void)
877   *  ??
878   */
879  
880 < static void mon_cmd_list(void)
880 > static void mon_cmd_list()
881   {
882          for (int i=0; i<num_cmds; i++)
883                  fprintf(monout, "%s ", cmds[i].name);
# Line 872 | Line 890 | static void mon_cmd_list(void)
890   *  @ [size]
891   */
892  
893 < static void reallocate(void)
893 > static void reallocate()
894   {
895          uintptr size;
896  
# Line 962 | Line 980 | static void apply(int size)
980          mon_dot_address = adr;
981   }
982  
983 < static void apply_byte(void)
983 > static void apply_byte()
984   {
985          apply(1);
986   }
987  
988 < static void apply_half(void)
988 > static void apply_half()
989   {
990          apply(2);
991   }
992  
993 < static void apply_word(void)
993 > static void apply_word()
994   {
995          apply(4);
996   }
# Line 982 | Line 1000 | static void apply_word(void)
1000   *  Execute command via system() (for ls, rm, etc.)
1001   */
1002  
1003 < static void mon_exec(void)
1003 > static void mon_exec()
1004   {
1005          system(input);
1006   }
# Line 992 | Line 1010 | static void mon_exec(void)
1010   *  Change current directory
1011   */
1012  
1013 < static void mon_change_dir(void)
1013 > void mon_change_dir()
1014   {
997        char c;
1015          in_ptr = input;
1016 <        while ((c = get_char()) == ' ') ;
1017 <        while ((c = get_char()) != ' ') ;
1018 <        while ((c = get_char()) == ' ') ;
1016 >        char c = get_char();
1017 >        while (isspace(c))
1018 >                c = get_char();
1019 >        while (isgraph(c))
1020 >                c = get_char();
1021 >        while (isspace(c))
1022 >                c = get_char();
1023          put_back(c);
1024          if (chdir(in_ptr) != 0)
1025                  mon_error("Cannot change directory");
# Line 1009 | Line 1030 | static void mon_change_dir(void)
1030   *  Initialize mon
1031   */
1032  
1033 < void mon_init(void)
1033 > void mon_init()
1034   {
1035          cmds = NULL;
1036          num_cmds = 0;
# Line 1052 | Line 1073 | void mon_init(void)
1073  
1074          mon_read_byte = NULL;
1075          mon_write_byte = NULL;
1076 +
1077 +        input = NULL;
1078 +        mon_string = NULL;
1079 +        mon_name = NULL;
1080   }
1081  
1082  
# Line 1059 | Line 1084 | void mon_init(void)
1084   *  Deinitialize mon
1085   */
1086  
1087 < void mon_exit(void)
1087 > void mon_exit()
1088   {
1089 <        free(cmds);
1090 <        cmds = NULL;
1089 >        if (cmds) {
1090 >                free(cmds);
1091 >                cmds = NULL;
1092 >        }
1093          num_cmds = 0;
1094          cmd_help = NULL;
1095 +
1096 +        if (input) {
1097 +                free(input);
1098 +                input = NULL;
1099 +        }
1100 +        if (mon_string) {
1101 +                free(mon_string);
1102 +                mon_string = NULL;
1103 +        }
1104 +        if (mon_name) {
1105 +                free(mon_name);
1106 +                mon_name = NULL;
1107 +        }
1108   }
1109  
1110  
# Line 1075 | Line 1115 | void mon_exit(void)
1115   void mon(int argc, char **argv)
1116   {
1117          bool done = false, interactive = true;
1078        char c, cmd[INPUT_LENGTH];
1118  
1119          // Setup input/output streams
1120          monin = stdin;
# Line 1144 | Line 1183 | void mon(int argc, char **argv)
1183          init_abort();
1184  
1185          // Read and parse command line
1186 +        char *cmd = NULL;
1187          while (!done) {
1188                  if (interactive) {
1189                          char prompt[16];
1190 <                        sprintf(prompt, "[%0*lx]-> ", 2 * sizeof(mon_dot_address), mon_dot_address);
1190 >                        sprintf(prompt, "[%0*lx]-> ", int(2 * sizeof(mon_dot_address)), mon_dot_address);
1191                          read_line(prompt);
1192 +                        if (!input) {
1193 +                                done = true;
1194 +                                continue;
1195 +                        }
1196                  } else {
1197                          if (argc == 0) {
1198                                  done = true;
1199                                  break;
1200                          } else {
1201 <                                strncpy(in_ptr = input, argv[0], INPUT_LENGTH);
1201 >                                unsigned n = strlen(argv[0]) + 1;
1202 >                                input = (char *)realloc(input, n);
1203 >                                strcpy(in_ptr = input, argv[0]);
1204                                  argc--;
1205                                  argv++;
1206                          }
1207                  }
1208  
1209                  // Skip leading spaces
1210 <                while ((c = get_char()) == ' ') ;
1210 >                char c = get_char();
1211 >                while (isspace(c))
1212 >                        c = get_char();
1213 >                put_back(c);
1214 >                if (!c)
1215 >                        continue;  // blank line
1216  
1217                  // Read command word
1218 <                char *p = cmd;
1219 <                do {
1169 <                        *p++ = c;
1218 >                char *p = in_ptr;
1219 >                while (isgraph(c))
1220                          c = get_char();
1171                } while (isgraph(c));
1172                *p = 0;
1221                  put_back(c);
1222 +                unsigned n = in_ptr - p;
1223 +                cmd = (char *)realloc(cmd, n + 1);
1224 +                memcpy(cmd, p, n);
1225 +                cmd[n] = 0;
1226  
1227                  // Execute command
1176                if (cmd[0] == 0)                                // Blank line
1177                        continue;
1228                  if (strcmp(cmd, "x") == 0) {    // Exit
1229                          done = true;
1230                          continue;
# Line 1190 | Line 1240 | void mon(int argc, char **argv)
1240   cmd_done: ;
1241          }
1242  
1243 +        if (cmd)
1244 +                free(cmd);
1245 +
1246          exit_abort();
1247  
1248          // Free buffer

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines