* Make-common.in (nrun.o): Add rule for.
[external/binutils.git] / sim / common / nrun.c
1 /* New version of run front end support for simulators.
2    Copyright (C) 1997 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #include "sim-main.h"
19
20 #ifdef HAVE_ENVIRON
21 extern char **environ;
22 #endif
23
24 static void usage PARAMS ((void));
25
26 extern host_callback default_callback;
27
28 static char *myname;
29
30 int
31 main (argc, argv)
32      int argc;
33      char **argv;
34 {
35   char *name;
36   char **prog_argv = NULL;
37   enum sim_stop reason;
38   int sigrc;
39   SIM_DESC sd;
40
41   myname = argv[0] + strlen (argv[0]);
42   while (myname > argv[0] && myname[-1] != '/')
43     --myname;
44
45   sim_set_callbacks (NULL, &default_callback);
46   default_callback.init (&default_callback);
47
48   /* Create an instance of the simulator.  */
49   sd = sim_open (SIM_OPEN_STANDALONE, argv);
50   if (sd == 0)
51     exit (1);
52
53   /* Was there a program to run?  */
54   prog_argv = STATE_PROG_ARGV (sd);
55   if (prog_argv == NULL || *prog_argv == NULL)
56     usage ();
57
58   name = *prog_argv;
59
60   if (STATE_VERBOSE_P (sd))
61     printf ("%s %s\n", myname, name);
62
63   /* Load the program into the simulator.  */
64   if (sim_load (sd, name, NULL, 0) == SIM_RC_FAIL)
65     exit (1);
66
67   /* Prepare the program for execution.  */
68 #ifdef HAVE_ENVIRON
69   sim_create_inferior (sd, prog_argv, environ);
70 #else
71   sim_create_inferior (sd, prog_argv, NULL);
72 #endif
73
74   /* Run the program.  */
75   sim_resume (sd, 0, 0);
76
77   /* Print any stats the simulator collected.  */
78   sim_info (sd, 0);
79
80   /* Find out why the program exited.  */
81   sim_stop_reason (sd, &reason, &sigrc);
82
83   /* Shutdown the simulator.  */
84   sim_close (sd, 0);
85
86   /* If reason is sim_exited, then sigrc holds the exit code which we want
87      to return.  If reason is sim_stopped or sim_signalled, then sigrc holds
88      the signal that the simulator received; we want to return that to
89      indicate failure.  */
90
91 #ifdef SIM_H8300 /* FIXME: Ugh.  grep for SLEEP in compile.c  */
92   if (sigrc == SIGILL)
93     abort ();
94   sigrc = 0;
95 #else
96   /* Why did we stop? */
97   switch (reason)
98     {
99     case sim_signalled:
100     case sim_stopped:
101       if (sigrc != 0)
102         fprintf (stderr, "program stopped with signal %d.\n", sigrc);
103       break;
104
105     case sim_exited:
106       break;
107     }
108 #endif
109
110   return sigrc;
111 }
112
113 static void
114 usage ()
115 {
116   fprintf (stderr, "Usage: %s [options] program [program args]\n", myname);
117   fprintf (stderr, "Run `%s --help' for full list of options.\n", myname);
118   exit (1);
119 }