Harmonize this monitor_load with generic_load.
[binutils-gdb.git] / gdb / monitor.c
index d56179a67a75fad36ee581dea22ff0a6ec6623a2..f4ec12ca2d951f5b108bc59277df23ae527c16d3 100644 (file)
@@ -54,6 +54,7 @@
 #include "srec.h"
 #include "regcache.h"
 #include "gdbthread.h"
+#include "readline/readline.h"
 
 static char *dev_name;
 static struct target_ops *targ_ops;
@@ -2175,35 +2176,56 @@ monitor_wait_srec_ack (void)
 /* monitor_load -- download a file.  */
 
 static void
-monitor_load (char *file, int from_tty)
+monitor_load (char *args, int from_tty)
 {
   monitor_debug ("MON load\n");
 
   if (current_monitor->load_routine)
-    current_monitor->load_routine (monitor_desc, file, hashmark);
+    current_monitor->load_routine (monitor_desc, args, hashmark);
   else
     {                          /* The default is ascii S-records.  */
-      int n;
-      unsigned long load_offset;
-      char buf[128];
+      CORE_ADDR load_offset = 0;
+      char **argv;
+      struct cleanup *old_cleanups;
+      char *filename;
+
+      if (args == NULL)
+       error_no_arg (_("file to load"));
+
+      argv = gdb_buildargv (args);
+      old_cleanups = make_cleanup_freeargv (argv);
+
+      filename = tilde_expand (argv[0]);
+      make_cleanup (xfree, filename);
 
       /* Enable user to specify address for downloading as 2nd arg to load.  */
-      n = sscanf (file, "%s 0x%lx", buf, &load_offset);
-      if (n > 1)
-       file = buf;
-      else
-       load_offset = 0;
+      if (argv[1] != NULL)
+       {
+         const char *endptr;
+
+         load_offset = strtoulst (argv[1], &endptr, 0);
+
+         /* If the last word was not a valid number then
+            treat it as a file name with spaces in.  */
+         if (argv[1] == endptr)
+           error (_("Invalid download offset:%s."), argv[1]);
+
+         if (argv[2] != NULL)
+           error (_("Too many parameters."));
+       }
 
       monitor_printf (current_monitor->load);
       if (current_monitor->loadresp)
        monitor_expect (current_monitor->loadresp, NULL, 0);
 
-      load_srec (monitor_desc, file, (bfd_vma) load_offset,
+      load_srec (monitor_desc, filename, load_offset,
                 32, SREC_ALL, hashmark,
                 current_monitor->flags & MO_SREC_ACK ?
                 monitor_wait_srec_ack : NULL);
 
       monitor_expect_prompt (NULL, 0);
+
+      do_cleanups (old_cleanups);
     }
 
   /* Finally, make the PC point at the start address.  */