gdb/readline: Fix date in last ChangeLog entry
[deliverable/binutils-gdb.git] / sim / ppc / main.c
index 99201dbd43bfebe357786a06f18cec6eea190b51..667c02f2d43ab36b225f62a33e064d3a0141f922 100644 (file)
@@ -1,10 +1,10 @@
 /*  This file is part of the program psim.
 
-    Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
+    Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
 
     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
-    the Free Software Foundation; either version 2 of the License, or
+    the Free Software Foundation; either version 3 of the License, or
     (at your option) any later version.
 
     This program is distributed in the hope that it will be useful,
@@ -13,8 +13,7 @@
     GNU General Public License for more details.
  
     You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+    along with this program; if not, see <http://www.gnu.org/licenses/>.
  
     */
 
 #include <stdio.h>
 #include <fcntl.h>
 
+#include <signal.h>
+
 #include "psim.h"
 #include "options.h"
 #include "device.h" /* FIXME: psim should provide the interface */
+#include "events.h" /* FIXME: psim should provide the interface */
+
+#include "bfd.h"
+#include "gdb/callback.h"
+#include "gdb/remote-sim.h"
 
 #ifdef HAVE_STDLIB_H
 #include <stdlib.h>
@@ -43,6 +49,8 @@
 #endif
 #endif
 
+#include <errno.h>
+
 #if !defined(O_NDELAY) || !defined(F_GETFL) || !defined(F_SETFL)
 #undef WITH_STDIO
 #define WITH_STDIO DO_USE_STDIO
@@ -54,6 +62,12 @@ extern char **environ;
 static psim *simulation = NULL;
 
 
+void
+sim_io_poll_quit (void)
+{
+  /* nothing to do */
+}
+
 void
 sim_io_printf_filtered(const char *msg, ...)
 {
@@ -64,11 +78,12 @@ sim_io_printf_filtered(const char *msg, ...)
 }
 
 void
-error (char *msg, ...)
+error (const char *msg, ...)
 {
   va_list ap;
   va_start(ap, msg);
   vprintf(msg, ap);
+  printf("\n");
   va_end(ap);
 
   /* any final clean up */
@@ -98,6 +113,7 @@ sim_io_write_stdout(const char *buf,
   default:
     error("sim_io_write_stdout: invalid switch\n");
   }
+  return 0;
 }
 
 int
@@ -120,6 +136,7 @@ sim_io_write_stderr(const char *buf,
   default:
     error("sim_io_write_stdout: invalid switch\n");
   }
+  return 0;
 }
 
 int
@@ -128,17 +145,29 @@ sim_io_read_stdin(char *buf,
 {
   switch (CURRENT_STDIO) {
   case DO_USE_STDIO:
-    if (fgets(buf, sizeof_buf, stdin) == NULL)
-      return sim_io_eof;
-    else
-      return strlen(buf);
+    if (sizeof_buf > 1) {
+      if (fgets(buf, sizeof_buf, stdin) != NULL)
+       return strlen(buf);
+    }
+    else if (sizeof_buf == 1) {
+      char b[2];
+      if (fgets(b, sizeof(b), stdin) != NULL) {
+       memcpy(buf, b, strlen(b));
+       return strlen(b);
+      }
+    }
+    else if (sizeof_buf == 0)
+      return 0;
+    return sim_io_eof;
     break;
   case DONT_USE_STDIO:
+#if defined(O_NDELAY) && defined(F_GETFL) && defined(F_SETFL)
     {
       /* check for input */
       int flags;
       int status;
       int nr_read;
+      int result;
       /* get the old status */
       flags = fcntl(0, F_GETFL, 0);
       if (flags == -1) {
@@ -152,25 +181,33 @@ sim_io_read_stdin(char *buf,
        return sim_io_eof;
       }
       /* try for input */
-      nr_read = read(0, &buf, sizeof_buf);
+      nr_read = read(0, buf, sizeof_buf);
+      if (nr_read > 0
+         || (nr_read == 0 && sizeof_buf == 0))
+       result = nr_read;
+      else if (nr_read == 0)
+       result = sim_io_eof;
+      else { /* nr_read < 0 */
+       if (errno == EAGAIN)
+         result = sim_io_not_ready;
+       else 
+         result = sim_io_eof;
+      }
       /* return to regular vewing */
       status = fcntl(0, F_SETFL, flags);
       if (status == -1) {
        perror("sim_io_read_stdin");
        return sim_io_eof;
       }
-      if (status > 0)
-       return 1;
-      else if (status < 0)
-       return sim_io_eof;
-      else
-       return sim_io_not_ready;
+      return result;
     }
     break;
+#endif
   default:
     error("sim_io_read_stdin: invalid switch\n");
     break;
   }
+  return 0;
 }
 
 void
@@ -188,23 +225,42 @@ sim_io_flush_stdoutput(void)
   }
 }
 
+void
+sim_io_error (SIM_DESC sd, const char *msg, ...)
+{
+  va_list ap;
+  va_start(ap, msg);
+  vprintf(msg, ap);
+  printf("\n");
+  va_end(ap);
+
+  /* any final clean up */
+  if (ppc_trace[trace_print_info] && simulation != NULL)
+    psim_print_info (simulation, ppc_trace[trace_print_info]);
+
+  exit (1);
+}
+
 
 void *
 zalloc(long size)
 {
   void *memory = malloc(size);
   if (memory == NULL)
-    error("zmalloc failed\n");
+    error("zalloc failed\n");
   memset(memory, 0, size);
   return memory;
 }
 
-void
-zfree(void *chunk)
+/* When a CNTRL-C occures, queue an event to shut down the simulation */
+
+static RETSIGTYPE
+cntrl_c(int sig)
 {
-  free(chunk);
+  psim_stop (simulation);
 }
 
+
 int
 main(int argc, char **argv)
 {
@@ -214,9 +270,15 @@ main(int argc, char **argv)
   device *root = psim_tree();
 
   /* parse the arguments */
-  argv = psim_options(root, argv + 1);
-  if (argv[0] == NULL)
-    psim_usage(0);
+  argv = psim_options (root, argv + 1, SIM_OPEN_STANDALONE);
+  if (argv[0] == NULL) {
+    if (ppc_trace[trace_opts]) {
+      print_options ();
+      return 0;
+    } else {
+      psim_usage (0, 0, SIM_OPEN_STANDALONE);
+    }
+  }
   name_of_file = argv[0];
 
   if (ppc_trace[trace_opts])
@@ -235,7 +297,12 @@ main(int argc, char **argv)
   psim_init(simulation);
   psim_stack(simulation, argv, environ);
 
-  psim_run(simulation);
+  {
+    RETSIGTYPE (*prev) ();
+    prev = signal(SIGINT, cntrl_c);
+    psim_run(simulation);
+    signal(SIGINT, prev);
+  }
 
   /* any final clean up */
   if (ppc_trace[trace_print_info])
@@ -245,7 +312,7 @@ main(int argc, char **argv)
   status = psim_get_status(simulation);
   switch (status.reason) {
   case was_continuing:
-    error("psim: continuing while stoped!\n");
+    error("psim: continuing while stopped!\n");
     return 0;
   case was_trap:
     error("psim: no trap insn\n");
This page took 0.025531 seconds and 4 git commands to generate.