gij.cc 10 KB

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