re PR libgcj/23549 (gij swallows args after -ea)
[gcc.git] / libjava / gij.cc
1 /* Copyright (C) 1999-2005 Free Software Foundation
2
3 This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
8
9 #include <config.h>
10
11 #include <jvm.h>
12 #include <gcj/cni.h>
13
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdlib.h>
17
18 static void
19 help ()
20 {
21 printf ("Usage: gij [OPTION] ... CLASS [ARGS] ...\n");
22 printf (" to interpret Java bytecodes, or\n");
23 printf (" gij -jar [OPTION] ... JARFILE [ARGS] ...\n");
24 printf (" to execute a jar file\n\n");
25 printf (" --cp LIST set class path\n");
26 printf (" --classpath LIST set class path\n");
27 printf (" -DVAR=VAL define property VAR with value VAL\n");
28 printf (" -?, --help print this help, then exit\n");
29 printf (" -X print help on supported -X options, then exit\n");
30 printf (" --ms=NUMBER set initial heap size\n");
31 printf (" --mx=NUMBER set maximum heap size\n");
32 printf (" --verbose[:class] print information about class loading\n");
33 printf (" --showversion print version number, then keep going\n");
34 printf (" --version print version number, then exit\n");
35 printf ("\nOptions can be specified with `-' or `--'.\n");
36 printf ("\nSee http://gcc.gnu.org/java/ for information on reporting bugs\n");
37 exit (0);
38 }
39
40 static void
41 version ()
42 {
43 printf ("java version \"" JV_VERSION "\"\n");
44 printf ("gij (GNU libgcj) version %s\n\n", __VERSION__);
45 printf ("Copyright (C) 2005 Free Software Foundation, Inc.\n");
46 printf ("This is free software; see the source for copying conditions. There is NO\n");
47 printf ("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
48 }
49
50 static void
51 nonstandard_opts_help ()
52 {
53 printf (" -Xms<size> set initial heap size\n");
54 printf (" -Xmx<size> set maximum heap size\n");
55 exit (0);
56 }
57
58 static void
59 add_option (JvVMInitArgs& vm_args, char const* option, void const* extra)
60 {
61 vm_args.options =
62 (JvVMOption*) JvRealloc (vm_args.options,
63 (vm_args.nOptions + 1) * sizeof (JvVMOption));
64
65 vm_args.options[vm_args.nOptions].optionString = const_cast<char*> (option);
66 vm_args.options[vm_args.nOptions].extraInfo = const_cast<void*> (extra);
67 ++vm_args.nOptions;
68 }
69
70 int
71 main (int argc, char const** argv)
72 {
73 JvVMInitArgs vm_args;
74 bool jar_mode = false;
75
76 vm_args.options = NULL;
77 vm_args.nOptions = 0;
78 vm_args.ignoreUnrecognized = true;
79
80 // Command-line options always override the CLASSPATH environment
81 // variable.
82 char *classpath = getenv("CLASSPATH");
83
84 if (classpath)
85 {
86 char* darg = (char*) JvMalloc (strlen (classpath)
87 + sizeof ("-Djava.class.path="));
88 sprintf (darg, "-Djava.class.path=%s", classpath);
89 add_option (vm_args, darg, NULL);
90 }
91
92 // Handle arguments to the java command. Store in vm_args arguments
93 // handled by the invocation API.
94 int i;
95 for (i = 1; i < argc; ++i)
96 {
97 char* arg = const_cast<char*> (argv[i]);
98
99 // A non-option stops processing.
100 if (arg[0] != '-')
101 break;
102
103 // A "--" stops processing.
104 if (! strcmp (arg, "--"))
105 {
106 ++i;
107 break;
108 }
109
110 // Allow both single or double hyphen for all options.
111 if (arg[1] == '-')
112 ++arg;
113
114 // Ignore JIT options
115 if (! strcmp (arg, "-client"))
116 continue;
117 else if (! strcmp (arg, "-server"))
118 continue;
119 else if (! strcmp (arg, "-hotspot"))
120 continue;
121 else if (! strcmp (arg, "-jrockit"))
122 continue;
123 // Ignore JVM Tool Interface options
124 else if (! strncmp (arg, "-agentlib:", sizeof ("-agentlib:") - 1))
125 continue;
126 else if (! strncmp (arg, "-agentpath:", sizeof ("-agentpath:") - 1))
127 continue;
128 else if (! strcmp (arg, "-classpath") || ! strcmp (arg, "-cp"))
129 {
130 if (i >= argc - 1)
131 {
132 no_arg:
133 fprintf (stderr, "gij: option requires an argument -- `%s'\n",
134 argv[i]);
135 fprintf (stderr, "Try `gij --help' for more information.\n");
136 exit (1);
137 }
138
139 // Sun seems to translate the -classpath option into
140 // -Djava.class.path because if both -classpath and
141 // -Djava.class.path are specified on the java command line,
142 // the last one always wins.
143 char* darg = (char*) JvMalloc (strlen (argv[++i])
144 + sizeof ("-Djava.class.path="));
145 sprintf (darg, "-Djava.class.path=%s", argv[i]);
146 add_option (vm_args, darg, NULL);
147 }
148 else if (! strcmp (arg, "-debug"))
149 {
150 char* xarg = strdup ("-Xdebug");
151 add_option (vm_args, xarg, NULL);
152 }
153 else if (! strncmp (arg, "-D", sizeof ("-D") - 1))
154 add_option (vm_args, arg, NULL);
155 // Ignore 32/64-bit JIT options
156 else if (! strcmp (arg, "-d32") || ! strcmp (arg, "-d64"))
157 continue;
158 else if (! strncmp (arg, "-enableassertions", sizeof ("-enableassertions") - 1)
159 || ! strncmp (arg, "-ea", sizeof ("-ea") - 1))
160 {
161 // FIXME: hook up assertion support
162 continue;
163 }
164 else if (! strncmp (arg, "-disableassertions", sizeof ("-disableassertions") - 1)
165 || ! strncmp (arg, "-da", sizeof ("-da") - 1))
166 {
167 // FIXME: hook up assertion support
168 continue;
169 }
170 else if (! strcmp (arg, "-enablesystemassertions")
171 || ! strcmp (arg, "-esa"))
172 {
173 // FIXME: hook up system assertion support
174 continue;
175 }
176 else if (! strcmp (arg, "-disablesystemassertions")
177 || ! strcmp (arg, "-dsa"))
178 {
179 // FIXME
180 continue;
181 }
182 else if (! strcmp (arg, "-jar"))
183 {
184 jar_mode = true;
185 continue;
186 }
187 // Ignore java.lang.instrument option
188 else if (! strncmp (arg, "-javaagent:", sizeof ("-javaagent:") - 1))
189 continue;
190 else if (! strcmp (arg, "-noclassgc"))
191 {
192 char* xarg = strdup ("-Xnoclassgc");
193 add_option (vm_args, xarg, NULL);
194 }
195 // -ms=n
196 else if (! strncmp (arg, "-ms=", sizeof ("-ms=") - 1))
197 {
198 arg[1] = 'X';
199 arg[2] = 'm';
200 arg[3] = 's';
201 add_option (vm_args, arg, NULL);
202 }
203 // -ms n
204 else if (! strcmp (arg, "-ms"))
205 {
206 if (i >= argc - 1)
207 goto no_arg;
208
209 char* xarg = (char*) JvMalloc (strlen (argv[++i])
210 + sizeof ("-Xms"));
211 sprintf (xarg, "-Xms%s", argv[i]);
212 add_option (vm_args, xarg, NULL);
213 }
214 // -msn
215 else if (! strncmp (arg, "-ms", sizeof ("-ms") - 1))
216 {
217 char* xarg = (char*) JvMalloc (strlen (arg) + sizeof ("X"));
218 sprintf (xarg, "-Xms%s", arg + sizeof ("-Xms") - 1);
219 add_option (vm_args, xarg, NULL);
220 }
221 // -mx=n
222 else if (! strncmp (arg, "-mx=", sizeof ("-mx=") - 1))
223 {
224 arg[1] = 'X';
225 arg[2] = 'm';
226 arg[3] = 'x';
227 add_option (vm_args, arg, NULL);
228 }
229 // -mx n
230 else if (! strcmp (arg, "-mx"))
231 {
232 if (i >= argc - 1)
233 goto no_arg;
234
235 char* xarg = (char*) JvMalloc (strlen (argv[++i])
236 + sizeof ("-Xmx"));
237 sprintf (xarg, "-Xmx%s", argv[i]);
238 add_option (vm_args, xarg, NULL);
239 }
240 // -mxn
241 else if (! strncmp (arg, "-mx", sizeof ("-mx") - 1))
242 {
243 char* xarg = (char*) JvMalloc (strlen (arg) + sizeof ("X"));
244 sprintf (xarg, "-Xmx%s", arg + sizeof ("-Xmx") - 1);
245 add_option (vm_args, xarg, NULL);
246 }
247 // -ss=n
248 else if (! strncmp (arg, "-ss=", sizeof ("-ss=") - 1))
249 {
250 arg[1] = 'X';
251 arg[2] = 's';
252 arg[3] = 's';
253 add_option (vm_args, arg, NULL);
254 }
255 // -ss n
256 else if (! strcmp (arg, "-ss"))
257 {
258 if (i >= argc - 1)
259 goto no_arg;
260
261 char* xarg = (char*) JvMalloc (strlen (argv[++i])
262 + sizeof ("-Xss"));
263 sprintf (xarg, "-Xss%s", argv[i]);
264 add_option (vm_args, xarg, NULL);
265 }
266 // -ssn
267 else if (! strncmp (arg, "-ss", sizeof ("-ss") - 1))
268 {
269 char* xarg = (char*) JvMalloc (strlen (arg) + sizeof ("X"));
270 sprintf (xarg, "-Xss%s", arg + sizeof ("-Xss") - 1);
271 add_option (vm_args, xarg, NULL);
272 }
273 // This handles all the option variants that begin with
274 // -verbose.
275 else if (! strncmp (arg, "-verbose", 8))
276 add_option (vm_args, arg, NULL);
277 else if (! strcmp (arg, "-version"))
278 {
279 version ();
280 exit (0);
281 }
282 else if (! strcmp (arg, "-fullversion"))
283 {
284 printf ("java full version \"gcj-" JV_VERSION "\"\n");
285 exit (0);
286 }
287 else if (! strcmp (arg, "-showversion"))
288 version ();
289 else if (! strcmp (arg, "-help") || ! strcmp (arg, "-?"))
290 help ();
291 else if (! strcmp (arg, "-X"))
292 nonstandard_opts_help ();
293 else if (! strncmp (arg, "-X", 2))
294 add_option (vm_args, arg, NULL);
295 // Obsolete options recognized for backwards-compatibility.
296 else if (! strcmp (arg, "-verify")
297 || ! strcmp (arg, "-verifyremote"))
298 continue;
299 else if (! strcmp (arg, "-noverify"))
300 {
301 gcj::verifyClasses = false;
302 }
303 else
304 {
305 fprintf (stderr, "gij: unrecognized option -- `%s'\n", argv[i]);
306 fprintf (stderr, "Try `gij --help' for more information.\n");
307 exit (1);
308 }
309 }
310
311 if (argc - i < 1)
312 {
313 fprintf (stderr, "Usage: gij [OPTION] ... CLASS [ARGS] ...\n");
314 fprintf (stderr, " to invoke CLASS.main, or\n");
315 fprintf (stderr, " gij -jar [OPTION] ... JARFILE [ARGS] ...\n");
316 fprintf (stderr, " to execute a jar file\n");
317 fprintf (stderr, "Try `gij --help' for more information.\n");
318 exit (1);
319 }
320
321 // -jar mode overrides all other modes of specifying class path:
322 // CLASSPATH, -Djava.class.path, -classpath and -cp.
323 if (jar_mode)
324 {
325 char* darg = (char*) JvMalloc (strlen (argv[i])
326 + sizeof ("-Djava.class.path="));
327 sprintf (darg, "-Djava.class.path=%s", argv[i]);
328 add_option (vm_args, darg, NULL);
329 }
330
331 _Jv_RunMain (&vm_args, NULL, argv[i], argc - i,
332 (char const**) (argv + i), jar_mode);
333 }