1 /*
   2  * Copyright (c) 1995, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * Shared source for 'java' command line tool.
  28  *
  29  * If JAVA_ARGS is defined, then acts as a launcher for applications. For
  30  * instance, the JDK command line tools such as javac and javadoc (see
  31  * makefiles for more details) are built with this program.  Any arguments
  32  * prefixed with '-J' will be passed directly to the 'java' command.
  33  */
  34 
  35 /*
  36  * One job of the launcher is to remove command line options which the
  37  * vm does not understand and will not process.  These options include
  38  * options which select which style of vm is run (e.g. -client and
  39  * -server) as well as options which select the data model to use.
  40  * Additionally, for tools which invoke an underlying vm "-J-foo"
  41  * options are turned into "-foo" options to the vm.  This option
  42  * filtering is handled in a number of places in the launcher, some of
  43  * it in machine-dependent code.  In this file, the function
  44  * CheckJvmType removes vm style options and TranslateApplicationArgs
  45  * removes "-J" prefixes.  The CreateExecutionEnvironment function processes
  46  * and removes -d<n> options. On unix, there is a possibility that the running
  47  * data model may not match to the desired data model, in this case an exec is
  48  * required to start the desired model. If the data models match, then
  49  * ParseArguments will remove the -d<n> flags. If the data models do not match
  50  * the CreateExecutionEnviroment will remove the -d<n> flags.
  51  */
  52 
  53 
  54 #include "java.h"
  55 
  56 /*
  57  * A NOTE TO DEVELOPERS: For performance reasons it is important that
  58  * the program image remain relatively small until after SelectVersion
  59  * CreateExecutionEnvironment have finished their possibly recursive
  60  * processing. Watch everything, but resist all temptations to use Java
  61  * interfaces.
  62  */
  63 
  64 /* we always print to stderr */
  65 #define USE_STDERR JNI_TRUE
  66 
  67 static jboolean printVersion = JNI_FALSE; /* print and exit */
  68 static jboolean showVersion = JNI_FALSE;  /* print but continue */
  69 static jboolean printUsage = JNI_FALSE;   /* print and exit*/
  70 static jboolean printXUsage = JNI_FALSE;  /* print and exit*/
  71 static char     *showSettings = NULL;      /* print but continue */
  72 
  73 static const char *_program_name;
  74 static const char *_launcher_name;
  75 static jboolean _is_java_args = JNI_FALSE;
  76 static const char *_fVersion;
  77 static const char *_dVersion;
  78 static jboolean _wc_enabled = JNI_FALSE;
  79 static jint _ergo_policy = DEFAULT_POLICY;
  80 
  81 /*
  82  * Entries for splash screen environment variables.
  83  * putenv is performed in SelectVersion. We need
  84  * them in memory until UnsetEnv, so they are made static
  85  * global instead of auto local.
  86  */
  87 static char* splash_file_entry = NULL;
  88 static char* splash_jar_entry = NULL;
  89 
  90 /*
  91  * List of VM options to be specified when the VM is created.
  92  */
  93 static JavaVMOption *options;
  94 static int numOptions, maxOptions;
  95 
  96 /*
  97  * Prototypes for functions internal to launcher.
  98  */
  99 static void SetClassPath(const char *s);
 100 static void SelectVersion(int argc, char **argv, char **main_class);
 101 static jboolean ParseArguments(int *pargc, char ***pargv,
 102                                int *pmode, char **pwhat,
 103                                int *pret, const char *jrepath);
 104 static jboolean InitializeJVM(JavaVM **pvm, JNIEnv **penv,
 105                               InvocationFunctions *ifn);
 106 static jstring NewPlatformString(JNIEnv *env, char *s);
 107 static jclass LoadMainClass(JNIEnv *env, int mode, char *name);
 108 static jclass GetApplicationClass(JNIEnv *env);
 109 
 110 static void TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv);
 111 static jboolean AddApplicationOptions(int cpathc, const char **cpathv);
 112 static void SetApplicationClassPath(const char**);
 113 
 114 static void PrintJavaVersion(JNIEnv *env, jboolean extraLF);
 115 static void PrintUsage(JNIEnv* env, jboolean doXUsage);
 116 static void ShowSettings(JNIEnv* env, char *optString);
 117 
 118 static void SetPaths(int argc, char **argv);
 119 
 120 static void DumpState();
 121 static jboolean RemovableOption(char *option);
 122 
 123 /* Maximum supported entries from jvm.cfg. */
 124 #define INIT_MAX_KNOWN_VMS      10
 125 
 126 /* Values for vmdesc.flag */
 127 enum vmdesc_flag {
 128     VM_UNKNOWN = -1,
 129     VM_KNOWN,
 130     VM_ALIASED_TO,
 131     VM_WARN,
 132     VM_ERROR,
 133     VM_IF_SERVER_CLASS,
 134     VM_IGNORE
 135 };
 136 
 137 struct vmdesc {
 138     char *name;
 139     int flag;
 140     char *alias;
 141     char *server_class;
 142 };
 143 static struct vmdesc *knownVMs = NULL;
 144 static int knownVMsCount = 0;
 145 static int knownVMsLimit = 0;
 146 
 147 static void GrowKnownVMs();
 148 static int  KnownVMIndex(const char* name);
 149 static void FreeKnownVMs();
 150 static jboolean IsWildCardEnabled();
 151 
 152 #define ARG_CHECK(n, f, a) if (n < 1) { \
 153     JLI_ReportErrorMessage(f, a); \
 154     printUsage = JNI_TRUE; \
 155     *pret = 1; \
 156     return JNI_TRUE; \
 157 }
 158 
 159 /*
 160  * Running Java code in primordial thread caused many problems. We will
 161  * create a new thread to invoke JVM. See 6316197 for more information.
 162  */
 163 static jlong threadStackSize    = 0;  /* stack size of the new thread */
 164 static jlong maxHeapSize        = 0;  /* max heap size */
 165 static jlong initialHeapSize    = 0;  /* inital heap size */
 166 
 167 /*
 168  * Entry point.
 169  */
 170 int
 171 JLI_Launch(int argc, char ** argv,              /* main argc, argc */
 172         int jargc, const char** jargv,          /* java args */
 173         int appclassc, const char** appclassv,  /* app classpath */
 174         const char* fullversion,                /* full version defined */
 175         const char* dotversion,                 /* dot version defined */
 176         const char* pname,                      /* program name */
 177         const char* lname,                      /* launcher name */
 178         jboolean javaargs,                      /* JAVA_ARGS */
 179         jboolean cpwildcard,                    /* classpath wildcard*/
 180         jboolean javaw,                         /* windows-only javaw */
 181         jint ergo                               /* ergonomics class policy */
 182 )
 183 {
 184     int mode = LM_UNKNOWN;
 185     char *what = NULL;
 186     char *cpath = 0;
 187     char *main_class = NULL;
 188     int ret;
 189     InvocationFunctions ifn;
 190     jlong start, end;
 191     char jvmpath[MAXPATHLEN];
 192     char jrepath[MAXPATHLEN];
 193     char jvmcfg[MAXPATHLEN];
 194 
 195     _fVersion = fullversion;
 196     _dVersion = dotversion;
 197     _launcher_name = lname;
 198     _program_name = pname;
 199     _is_java_args = javaargs;
 200     _wc_enabled = cpwildcard;
 201     _ergo_policy = ergo;
 202 
 203     InitLauncher(javaw);
 204     DumpState();
 205     if (JLI_IsTraceLauncher()) {
 206         int i;
 207         printf("Command line args:\n");
 208         for (i = 0; i < argc ; i++) {
 209             printf("argv[%d] = %s\n", i, argv[i]);
 210         }
 211         AddOption("-Dsun.java.launcher.diag=true", NULL);
 212     }
 213 
 214     /*
 215      * Make sure the specified version of the JRE is running.
 216      *
 217      * There are three things to note about the SelectVersion() routine:
 218      *  1) If the version running isn't correct, this routine doesn't
 219      *     return (either the correct version has been exec'd or an error
 220      *     was issued).
 221      *  2) Argc and Argv in this scope are *not* altered by this routine.
 222      *     It is the responsibility of subsequent code to ignore the
 223      *     arguments handled by this routine.
 224      *  3) As a side-effect, the variable "main_class" is guaranteed to
 225      *     be set (if it should ever be set).  This isn't exactly the
 226      *     poster child for structured programming, but it is a small
 227      *     price to pay for not processing a jar file operand twice.
 228      *     (Note: This side effect has been disabled.  See comment on
 229      *     bugid 5030265 below.)
 230      */
 231     SelectVersion(argc, argv, &main_class);
 232 
 233     CreateExecutionEnvironment(&argc, &argv,
 234                                jrepath, sizeof(jrepath),
 235                                jvmpath, sizeof(jvmpath),
 236                                jvmcfg,  sizeof(jvmcfg));
 237 
 238     ifn.CreateJavaVM = 0;
 239     ifn.GetDefaultJavaVMInitArgs = 0;
 240 
 241     if (JLI_IsTraceLauncher()) {
 242         start = CounterGet();
 243     }
 244 
 245     if (!LoadJavaVM(jvmpath, &ifn)) {
 246         return(6);
 247     }
 248 
 249     if (JLI_IsTraceLauncher()) {
 250         end   = CounterGet();
 251     }
 252 
 253     JLI_TraceLauncher("%ld micro seconds to LoadJavaVM\n",
 254              (long)(jint)Counter2Micros(end-start));
 255 
 256     ++argv;
 257     --argc;
 258 
 259     if (IsJavaArgs()) {
 260         /* Preprocess wrapper arguments */
 261         TranslateApplicationArgs(jargc, jargv, &argc, &argv);
 262         if (!AddApplicationOptions(appclassc, appclassv)) {
 263             return(1);
 264         }
 265     } else {
 266         /* Set default CLASSPATH */
 267         cpath = getenv("CLASSPATH");
 268         if (cpath == NULL) {
 269             cpath = ".";
 270         }
 271         SetClassPath(cpath);
 272     }
 273 
 274     /* Parse command line options; if the return value of
 275      * ParseArguments is false, the program should exit.
 276      */
 277     if (!ParseArguments(&argc, &argv, &mode, &what, &ret, jrepath))
 278     {
 279         return(ret);
 280     }
 281 
 282     /* Override class path if -jar flag was specified */
 283     if (mode == LM_JAR) {
 284         SetClassPath(what);     /* Override class path */
 285     }
 286 
 287     /* set the -Dsun.java.command pseudo property */
 288     SetJavaCommandLineProp(what, argc, argv);
 289 
 290     /* Set the -Dsun.java.launcher pseudo property */
 291     SetJavaLauncherProp();
 292 
 293     /* set the -Dsun.java.launcher.* platform properties */
 294     SetJavaLauncherPlatformProps();
 295 
 296     return JVMInit(&ifn, threadStackSize, argc, argv, mode, what, ret);
 297 }
 298 /*
 299  * Always detach the main thread so that it appears to have ended when
 300  * the application's main method exits.  This will invoke the
 301  * uncaught exception handler machinery if main threw an
 302  * exception.  An uncaught exception handler cannot change the
 303  * launcher's return code except by calling System.exit.
 304  *
 305  * Wait for all non-daemon threads to end, then destroy the VM.
 306  * This will actually create a trivial new Java waiter thread
 307  * named "DestroyJavaVM", but this will be seen as a different
 308  * thread from the one that executed main, even though they are
 309  * the same C thread.  This allows mainThread.join() and
 310  * mainThread.isAlive() to work as expected.
 311  */
 312 #define LEAVE() \
 313     if ((*vm)->DetachCurrentThread(vm) != 0) { \
 314         JLI_ReportErrorMessage(JVM_ERROR2); \
 315         ret = 1; \
 316     } \
 317     (*vm)->DestroyJavaVM(vm); \
 318     return ret \
 319 
 320 #define CHECK_EXCEPTION_NULL_LEAVE(e) \
 321     if ((*env)->ExceptionOccurred(env)) { \
 322         JLI_ReportExceptionDescription(env); \
 323         LEAVE(); \
 324     } \
 325     if ((e) == NULL) { \
 326         JLI_ReportErrorMessage(JNI_ERROR); \
 327         LEAVE(); \
 328     }
 329 
 330 #define CHECK_EXCEPTION_LEAVE(rv) \
 331     if ((*env)->ExceptionOccurred(env)) { \
 332         JLI_ReportExceptionDescription(env); \
 333         ret = (rv); \
 334         LEAVE(); \
 335     }
 336 
 337 int JNICALL
 338 JavaMain(void * _args)
 339 {
 340     JavaMainArgs *args = (JavaMainArgs *)_args;
 341     int argc = args->argc;
 342     char **argv = args->argv;
 343     int mode = args->mode;
 344     char *what = args->what;
 345     InvocationFunctions ifn = args->ifn;
 346 
 347     JavaVM *vm = 0;
 348     JNIEnv *env = 0;
 349     jclass mainClass = NULL;
 350     jclass appClass = NULL; // actual application class being launched
 351     jmethodID mainID;
 352     jobjectArray mainArgs;
 353     int ret = 0;
 354     jlong start, end;
 355 
 356     RegisterThread();
 357 
 358     /* Initialize the virtual machine */
 359     start = CounterGet();
 360     if (!InitializeJVM(&vm, &env, &ifn)) {
 361         JLI_ReportErrorMessage(JVM_ERROR1);
 362         exit(1);
 363     }
 364 
 365     if (showSettings != NULL) {
 366         ShowSettings(env, showSettings);
 367         CHECK_EXCEPTION_LEAVE(1);
 368     }
 369 
 370     if (printVersion || showVersion) {
 371         PrintJavaVersion(env, showVersion);
 372         CHECK_EXCEPTION_LEAVE(0);
 373         if (printVersion) {
 374             LEAVE();
 375         }
 376     }
 377 
 378     /* If the user specified neither a class name nor a JAR file */
 379     if (printXUsage || printUsage || what == 0 || mode == LM_UNKNOWN) {
 380         PrintUsage(env, printXUsage);
 381         CHECK_EXCEPTION_LEAVE(1);
 382         LEAVE();
 383     }
 384 
 385     FreeKnownVMs();  /* after last possible PrintUsage() */
 386 
 387     if (JLI_IsTraceLauncher()) {
 388         end = CounterGet();
 389         JLI_TraceLauncher("%ld micro seconds to InitializeJVM\n",
 390                (long)(jint)Counter2Micros(end-start));
 391     }
 392 
 393     /* At this stage, argc/argv have the application's arguments */
 394     if (JLI_IsTraceLauncher()){
 395         int i;
 396         printf("%s is '%s'\n", launchModeNames[mode], what);
 397         printf("App's argc is %d\n", argc);
 398         for (i=0; i < argc; i++) {
 399             printf("    argv[%2d] = '%s'\n", i, argv[i]);
 400         }
 401     }
 402 
 403     ret = 1;
 404 
 405     /*
 406      * Get the application's main class.
 407      *
 408      * See bugid 5030265.  The Main-Class name has already been parsed
 409      * from the manifest, but not parsed properly for UTF-8 support.
 410      * Hence the code here ignores the value previously extracted and
 411      * uses the pre-existing code to reextract the value.  This is
 412      * possibly an end of release cycle expedient.  However, it has
 413      * also been discovered that passing some character sets through
 414      * the environment has "strange" behavior on some variants of
 415      * Windows.  Hence, maybe the manifest parsing code local to the
 416      * launcher should never be enhanced.
 417      *
 418      * Hence, future work should either:
 419      *     1)   Correct the local parsing code and verify that the
 420      *          Main-Class attribute gets properly passed through
 421      *          all environments,
 422      *     2)   Remove the vestages of maintaining main_class through
 423      *          the environment (and remove these comments).
 424      *
 425      * This method also correctly handles launching existing JavaFX
 426      * applications that may or may not have a Main-Class manifest entry.
 427      */
 428     mainClass = LoadMainClass(env, mode, what);
 429     CHECK_EXCEPTION_NULL_LEAVE(mainClass);
 430     /*
 431      * In some cases when launching an application that needs a helper, e.g., a
 432      * JavaFX application with no main method, the mainClass will not be the
 433      * applications own main class but rather a helper class. To keep things
 434      * consistent in the UI we need to track and report the application main class.
 435      */
 436     appClass = GetApplicationClass(env);
 437     NULL_CHECK(appClass);
 438     /*
 439      * PostJVMInit uses the class name as the application name for GUI purposes,
 440      * for example, on OSX this sets the application name in the menu bar for
 441      * both SWT and JavaFX. So we'll pass the actual application class here
 442      * instead of mainClass as that may be a launcher or helper class instead
 443      * of the application class.
 444      */
 445     PostJVMInit(env, appClass, vm);
 446     /*
 447      * The LoadMainClass not only loads the main class, it will also ensure
 448      * that the main method's signature is correct, therefore further checking
 449      * is not required. The main method is invoked here so that extraneous java
 450      * stacks are not in the application stack trace.
 451      */
 452     mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
 453                                        "([Ljava/lang/String;)V");
 454     CHECK_EXCEPTION_NULL_LEAVE(mainID);
 455 
 456     /* Build platform specific argument array */
 457     mainArgs = CreateApplicationArgs(env, argv, argc);
 458     CHECK_EXCEPTION_NULL_LEAVE(mainArgs);
 459 
 460     /* Invoke main method. */
 461     (*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
 462 
 463     /*
 464      * The launcher's exit code (in the absence of calls to
 465      * System.exit) will be non-zero if main threw an exception.
 466      */
 467     ret = (*env)->ExceptionOccurred(env) == NULL ? 0 : 1;
 468     LEAVE();
 469 }
 470 
 471 /*
 472  * Checks the command line options to find which JVM type was
 473  * specified.  If no command line option was given for the JVM type,
 474  * the default type is used.  The environment variable
 475  * JDK_ALTERNATE_VM and the command line option -XXaltjvm= are also
 476  * checked as ways of specifying which JVM type to invoke.
 477  */
 478 char *
 479 CheckJvmType(int *pargc, char ***argv, jboolean speculative) {
 480     int i, argi;
 481     int argc;
 482     char **newArgv;
 483     int newArgvIdx = 0;
 484     int isVMType;
 485     int jvmidx = -1;
 486     char *jvmtype = getenv("JDK_ALTERNATE_VM");
 487 
 488     argc = *pargc;
 489 
 490     /* To make things simpler we always copy the argv array */
 491     newArgv = JLI_MemAlloc((argc + 1) * sizeof(char *));
 492 
 493     /* The program name is always present */
 494     newArgv[newArgvIdx++] = (*argv)[0];
 495 
 496     for (argi = 1; argi < argc; argi++) {
 497         char *arg = (*argv)[argi];
 498         isVMType = 0;
 499 
 500         if (IsJavaArgs()) {
 501             if (arg[0] != '-') {
 502                 newArgv[newArgvIdx++] = arg;
 503                 continue;
 504             }
 505         } else {
 506             if (JLI_StrCmp(arg, "-classpath") == 0 ||
 507                 JLI_StrCmp(arg, "-cp") == 0) {
 508                 newArgv[newArgvIdx++] = arg;
 509                 argi++;
 510                 if (argi < argc) {
 511                     newArgv[newArgvIdx++] = (*argv)[argi];
 512                 }
 513                 continue;
 514             }
 515             if (arg[0] != '-') break;
 516         }
 517 
 518         /* Did the user pass an explicit VM type? */
 519         i = KnownVMIndex(arg);
 520         if (i >= 0) {
 521             jvmtype = knownVMs[jvmidx = i].name + 1; /* skip the - */
 522             isVMType = 1;
 523             *pargc = *pargc - 1;
 524         }
 525 
 526         /* Did the user specify an "alternate" VM? */
 527         else if (JLI_StrCCmp(arg, "-XXaltjvm=") == 0 || JLI_StrCCmp(arg, "-J-XXaltjvm=") == 0) {
 528             isVMType = 1;
 529             jvmtype = arg+((arg[1]=='X')? 10 : 12);
 530             jvmidx = -1;
 531         }
 532 
 533         if (!isVMType) {
 534             newArgv[newArgvIdx++] = arg;
 535         }
 536     }
 537 
 538     /*
 539      * Finish copying the arguments if we aborted the above loop.
 540      * NOTE that if we aborted via "break" then we did NOT copy the
 541      * last argument above, and in addition argi will be less than
 542      * argc.
 543      */
 544     while (argi < argc) {
 545         newArgv[newArgvIdx++] = (*argv)[argi];
 546         argi++;
 547     }
 548 
 549     /* argv is null-terminated */
 550     newArgv[newArgvIdx] = 0;
 551 
 552     /* Copy back argv */
 553     *argv = newArgv;
 554     *pargc = newArgvIdx;
 555 
 556     /* use the default VM type if not specified (no alias processing) */
 557     if (jvmtype == NULL) {
 558       char* result = knownVMs[0].name+1;
 559       /* Use a different VM type if we are on a server class machine? */
 560       if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) &&
 561           (ServerClassMachine() == JNI_TRUE)) {
 562         result = knownVMs[0].server_class+1;
 563       }
 564       JLI_TraceLauncher("Default VM: %s\n", result);
 565       return result;
 566     }
 567 
 568     /* if using an alternate VM, no alias processing */
 569     if (jvmidx < 0)
 570       return jvmtype;
 571 
 572     /* Resolve aliases first */
 573     {
 574       int loopCount = 0;
 575       while (knownVMs[jvmidx].flag == VM_ALIASED_TO) {
 576         int nextIdx = KnownVMIndex(knownVMs[jvmidx].alias);
 577 
 578         if (loopCount > knownVMsCount) {
 579           if (!speculative) {
 580             JLI_ReportErrorMessage(CFG_ERROR1);
 581             exit(1);
 582           } else {
 583             return "ERROR";
 584             /* break; */
 585           }
 586         }
 587 
 588         if (nextIdx < 0) {
 589           if (!speculative) {
 590             JLI_ReportErrorMessage(CFG_ERROR2, knownVMs[jvmidx].alias);
 591             exit(1);
 592           } else {
 593             return "ERROR";
 594           }
 595         }
 596         jvmidx = nextIdx;
 597         jvmtype = knownVMs[jvmidx].name+1;
 598         loopCount++;
 599       }
 600     }
 601 
 602     switch (knownVMs[jvmidx].flag) {
 603     case VM_WARN:
 604         if (!speculative) {
 605             JLI_ReportErrorMessage(CFG_WARN1, jvmtype, knownVMs[0].name + 1);
 606         }
 607         /* fall through */
 608     case VM_IGNORE:
 609         jvmtype = knownVMs[jvmidx=0].name + 1;
 610         /* fall through */
 611     case VM_KNOWN:
 612         break;
 613     case VM_ERROR:
 614         if (!speculative) {
 615             JLI_ReportErrorMessage(CFG_ERROR3, jvmtype);
 616             exit(1);
 617         } else {
 618             return "ERROR";
 619         }
 620     }
 621 
 622     return jvmtype;
 623 }
 624 
 625 /* copied from HotSpot function "atomll()" */
 626 static int
 627 parse_size(const char *s, jlong *result) {
 628   jlong n = 0;
 629   int args_read = sscanf(s, jlong_format_specifier(), &n);
 630   if (args_read != 1) {
 631     return 0;
 632   }
 633   while (*s != '\0' && *s >= '0' && *s <= '9') {
 634     s++;
 635   }
 636   // 4705540: illegal if more characters are found after the first non-digit
 637   if (JLI_StrLen(s) > 1) {
 638     return 0;
 639   }
 640   switch (*s) {
 641     case 'T': case 't':
 642       *result = n * GB * KB;
 643       return 1;
 644     case 'G': case 'g':
 645       *result = n * GB;
 646       return 1;
 647     case 'M': case 'm':
 648       *result = n * MB;
 649       return 1;
 650     case 'K': case 'k':
 651       *result = n * KB;
 652       return 1;
 653     case '\0':
 654       *result = n;
 655       return 1;
 656     default:
 657       /* Create JVM with default stack and let VM handle malformed -Xss string*/
 658       return 0;
 659   }
 660 }
 661 
 662 /*
 663  * Adds a new VM option with the given given name and value.
 664  */
 665 void
 666 AddOption(char *str, void *info)
 667 {
 668     /*
 669      * Expand options array if needed to accommodate at least one more
 670      * VM option.
 671      */
 672     if (numOptions >= maxOptions) {
 673         if (options == 0) {
 674             maxOptions = 4;
 675             options = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
 676         } else {
 677             JavaVMOption *tmp;
 678             maxOptions *= 2;
 679             tmp = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
 680             memcpy(tmp, options, numOptions * sizeof(JavaVMOption));
 681             JLI_MemFree(options);
 682             options = tmp;
 683         }
 684     }
 685     options[numOptions].optionString = str;
 686     options[numOptions++].extraInfo = info;
 687 
 688     if (JLI_StrCCmp(str, "-Xss") == 0) {
 689         jlong tmp;
 690         if (parse_size(str + 4, &tmp)) {
 691             threadStackSize = tmp;
 692         }
 693     }
 694 
 695     if (JLI_StrCCmp(str, "-Xmx") == 0) {
 696         jlong tmp;
 697         if (parse_size(str + 4, &tmp)) {
 698             maxHeapSize = tmp;
 699         }
 700     }
 701 
 702     if (JLI_StrCCmp(str, "-Xms") == 0) {
 703         jlong tmp;
 704         if (parse_size(str + 4, &tmp)) {
 705            initialHeapSize = tmp;
 706         }
 707     }
 708 }
 709 
 710 static void
 711 SetClassPath(const char *s)
 712 {
 713     char *def;
 714     const char *orig = s;
 715     static const char format[] = "-Djava.class.path=%s";
 716     /*
 717      * usually we should not get a null pointer, but there are cases where
 718      * we might just get one, in which case we simply ignore it, and let the
 719      * caller deal with it
 720      */
 721     if (s == NULL)
 722         return;
 723     s = JLI_WildcardExpandClasspath(s);
 724     def = JLI_MemAlloc(sizeof(format)
 725                        - 2 /* strlen("%s") */
 726                        + JLI_StrLen(s));
 727     sprintf(def, format, s);
 728     AddOption(def, NULL);
 729     if (s != orig)
 730         JLI_MemFree((char *) s);
 731 }
 732 
 733 /*
 734  * The SelectVersion() routine ensures that an appropriate version of
 735  * the JRE is running.  The specification for the appropriate version
 736  * is obtained from either the manifest of a jar file (preferred) or
 737  * from command line options.
 738  * The routine also parses splash screen command line options and
 739  * passes on their values in private environment variables.
 740  */
 741 static void
 742 SelectVersion(int argc, char **argv, char **main_class)
 743 {
 744     char    *arg;
 745     char    **new_argv;
 746     char    **new_argp;
 747     char    *operand;
 748     char    *version = NULL;
 749     char    *jre = NULL;
 750     int     jarflag = 0;
 751     int     headlessflag = 0;
 752     int     restrict_search = -1;               /* -1 implies not known */
 753     manifest_info info;
 754     char    env_entry[MAXNAMELEN + 24] = ENV_ENTRY "=";
 755     char    *splash_file_name = NULL;
 756     char    *splash_jar_name = NULL;
 757     char    *env_in;
 758     int     res;
 759 
 760     /*
 761      * If the version has already been selected, set *main_class
 762      * with the value passed through the environment (if any) and
 763      * simply return.
 764      */
 765     if ((env_in = getenv(ENV_ENTRY)) != NULL) {
 766         if (*env_in != '\0')
 767             *main_class = JLI_StringDup(env_in);
 768         return;
 769     }
 770 
 771     /*
 772      * Scan through the arguments for options relevant to multiple JRE
 773      * support.  For reference, the command line syntax is defined as:
 774      *
 775      * SYNOPSIS
 776      *      java [options] class [argument...]
 777      *
 778      *      java [options] -jar file.jar [argument...]
 779      *
 780      * As the scan is performed, make a copy of the argument list with
 781      * the version specification options (new to 1.5) removed, so that
 782      * a version less than 1.5 can be exec'd.
 783      *
 784      * Note that due to the syntax of the native Windows interface
 785      * CreateProcess(), processing similar to the following exists in
 786      * the Windows platform specific routine ExecJRE (in java_md.c).
 787      * Changes here should be reproduced there.
 788      */
 789     new_argv = JLI_MemAlloc((argc + 1) * sizeof(char*));
 790     new_argv[0] = argv[0];
 791     new_argp = &new_argv[1];
 792     argc--;
 793     argv++;
 794     while ((arg = *argv) != 0 && *arg == '-') {
 795         if (JLI_StrCCmp(arg, "-version:") == 0) {
 796             version = arg + 9;
 797         } else if (JLI_StrCmp(arg, "-jre-restrict-search") == 0) {
 798             restrict_search = 1;
 799         } else if (JLI_StrCmp(arg, "-no-jre-restrict-search") == 0) {
 800             restrict_search = 0;
 801         } else {
 802             if (JLI_StrCmp(arg, "-jar") == 0)
 803                 jarflag = 1;
 804             /* deal with "unfortunate" classpath syntax */
 805             if ((JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) &&
 806               (argc >= 2)) {
 807                 *new_argp++ = arg;
 808                 argc--;
 809                 argv++;
 810                 arg = *argv;
 811             }
 812 
 813             /*
 814              * Checking for headless toolkit option in the some way as AWT does:
 815              * "true" means true and any other value means false
 816              */
 817             if (JLI_StrCmp(arg, "-Djava.awt.headless=true") == 0) {
 818                 headlessflag = 1;
 819             } else if (JLI_StrCCmp(arg, "-Djava.awt.headless=") == 0) {
 820                 headlessflag = 0;
 821             } else if (JLI_StrCCmp(arg, "-splash:") == 0) {
 822                 splash_file_name = arg+8;
 823             }
 824             *new_argp++ = arg;
 825         }
 826         argc--;
 827         argv++;
 828     }
 829     if (argc <= 0) {    /* No operand? Possibly legit with -[full]version */
 830         operand = NULL;
 831     } else {
 832         argc--;
 833         *new_argp++ = operand = *argv++;
 834     }
 835     while (argc-- > 0)  /* Copy over [argument...] */
 836         *new_argp++ = *argv++;
 837     *new_argp = NULL;
 838 
 839     /*
 840      * If there is a jar file, read the manifest. If the jarfile can't be
 841      * read, the manifest can't be read from the jar file, or the manifest
 842      * is corrupt, issue the appropriate error messages and exit.
 843      *
 844      * Even if there isn't a jar file, construct a manifest_info structure
 845      * containing the command line information.  It's a convenient way to carry
 846      * this data around.
 847      */
 848     if (jarflag && operand) {
 849         if ((res = JLI_ParseManifest(operand, &info)) != 0) {
 850             if (res == -1)
 851                 JLI_ReportErrorMessage(JAR_ERROR2, operand);
 852             else
 853                 JLI_ReportErrorMessage(JAR_ERROR3, operand);
 854             exit(1);
 855         }
 856 
 857         /*
 858          * Command line splash screen option should have precedence
 859          * over the manifest, so the manifest data is used only if
 860          * splash_file_name has not been initialized above during command
 861          * line parsing
 862          */
 863         if (!headlessflag && !splash_file_name && info.splashscreen_image_file_name) {
 864             splash_file_name = info.splashscreen_image_file_name;
 865             splash_jar_name = operand;
 866         }
 867     } else {
 868         info.manifest_version = NULL;
 869         info.main_class = NULL;
 870         info.jre_version = NULL;
 871         info.jre_restrict_search = 0;
 872     }
 873 
 874     /*
 875      * Passing on splash screen info in environment variables
 876      */
 877     if (splash_file_name && !headlessflag) {
 878         char* splash_file_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_FILE_ENV_ENTRY "=")+JLI_StrLen(splash_file_name)+1);
 879         JLI_StrCpy(splash_file_entry, SPLASH_FILE_ENV_ENTRY "=");
 880         JLI_StrCat(splash_file_entry, splash_file_name);
 881         putenv(splash_file_entry);
 882     }
 883     if (splash_jar_name && !headlessflag) {
 884         char* splash_jar_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_JAR_ENV_ENTRY "=")+JLI_StrLen(splash_jar_name)+1);
 885         JLI_StrCpy(splash_jar_entry, SPLASH_JAR_ENV_ENTRY "=");
 886         JLI_StrCat(splash_jar_entry, splash_jar_name);
 887         putenv(splash_jar_entry);
 888     }
 889 
 890     /*
 891      * The JRE-Version and JRE-Restrict-Search values (if any) from the
 892      * manifest are overwritten by any specified on the command line.
 893      */
 894     if (version != NULL)
 895         info.jre_version = version;
 896     if (restrict_search != -1)
 897         info.jre_restrict_search = restrict_search;
 898 
 899     /*
 900      * "Valid" returns (other than unrecoverable errors) follow.  Set
 901      * main_class as a side-effect of this routine.
 902      */
 903     if (info.main_class != NULL)
 904         *main_class = JLI_StringDup(info.main_class);
 905 
 906     /*
 907      * If no version selection information is found either on the command
 908      * line or in the manifest, simply return.
 909      */
 910     if (info.jre_version == NULL) {
 911         JLI_FreeManifest();
 912         JLI_MemFree(new_argv);
 913         return;
 914     }
 915 
 916     /*
 917      * Check for correct syntax of the version specification (JSR 56).
 918      */
 919     if (!JLI_ValidVersionString(info.jre_version)) {
 920         JLI_ReportErrorMessage(SPC_ERROR1, info.jre_version);
 921         exit(1);
 922     }
 923 
 924     /*
 925      * Find the appropriate JVM on the system. Just to be as forgiving as
 926      * possible, if the standard algorithms don't locate an appropriate
 927      * jre, check to see if the one running will satisfy the requirements.
 928      * This can happen on systems which haven't been set-up for multiple
 929      * JRE support.
 930      */
 931     jre = LocateJRE(&info);
 932     JLI_TraceLauncher("JRE-Version = %s, JRE-Restrict-Search = %s Selected = %s\n",
 933         (info.jre_version?info.jre_version:"null"),
 934         (info.jre_restrict_search?"true":"false"), (jre?jre:"null"));
 935 
 936     if (jre == NULL) {
 937         if (JLI_AcceptableRelease(GetFullVersion(), info.jre_version)) {
 938             JLI_FreeManifest();
 939             JLI_MemFree(new_argv);
 940             return;
 941         } else {
 942             JLI_ReportErrorMessage(CFG_ERROR4, info.jre_version);
 943             exit(1);
 944         }
 945     }
 946 
 947     /*
 948      * If I'm not the chosen one, exec the chosen one.  Returning from
 949      * ExecJRE indicates that I am indeed the chosen one.
 950      *
 951      * The private environment variable _JAVA_VERSION_SET is used to
 952      * prevent the chosen one from re-reading the manifest file and
 953      * using the values found within to override the (potential) command
 954      * line flags stripped from argv (because the target may not
 955      * understand them).  Passing the MainClass value is an optimization
 956      * to avoid locating, expanding and parsing the manifest extra
 957      * times.
 958      */
 959     if (info.main_class != NULL) {
 960         if (JLI_StrLen(info.main_class) <= MAXNAMELEN) {
 961             (void)JLI_StrCat(env_entry, info.main_class);
 962         } else {
 963             JLI_ReportErrorMessage(CLS_ERROR5, MAXNAMELEN);
 964             exit(1);
 965         }
 966     }
 967     (void)putenv(env_entry);
 968     ExecJRE(jre, new_argv);
 969     JLI_FreeManifest();
 970     JLI_MemFree(new_argv);
 971     return;
 972 }
 973 
 974 /*
 975  * Parses command line arguments.  Returns JNI_FALSE if launcher
 976  * should exit without starting vm, returns JNI_TRUE if vm needs
 977  * to be started to process given options.  *pret (the launcher
 978  * process return value) is set to 0 for a normal exit.
 979  */
 980 static jboolean
 981 ParseArguments(int *pargc, char ***pargv,
 982                int *pmode, char **pwhat,
 983                int *pret, const char *jrepath)
 984 {
 985     int argc = *pargc;
 986     char **argv = *pargv;
 987     int mode = LM_UNKNOWN;
 988     char *arg;
 989 
 990     *pret = 0;
 991 
 992     while ((arg = *argv) != 0 && *arg == '-') {
 993         argv++; --argc;
 994         if (JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) {
 995             ARG_CHECK (argc, ARG_ERROR1, arg);
 996             SetClassPath(*argv);
 997             mode = LM_CLASS;
 998             argv++; --argc;
 999         } else if (JLI_StrCmp(arg, "-jar") == 0) {
1000             ARG_CHECK (argc, ARG_ERROR2, arg);
1001             mode = LM_JAR;
1002         } else if (JLI_StrCmp(arg, "-help") == 0 ||
1003                    JLI_StrCmp(arg, "-h") == 0 ||
1004                    JLI_StrCmp(arg, "-?") == 0) {
1005             printUsage = JNI_TRUE;
1006             return JNI_TRUE;
1007         } else if (JLI_StrCmp(arg, "-version") == 0) {
1008             printVersion = JNI_TRUE;
1009             return JNI_TRUE;
1010         } else if (JLI_StrCmp(arg, "-showversion") == 0) {
1011             showVersion = JNI_TRUE;
1012         } else if (JLI_StrCmp(arg, "-X") == 0) {
1013             printXUsage = JNI_TRUE;
1014             return JNI_TRUE;
1015 /*
1016  * The following case checks for -XshowSettings OR -XshowSetting:SUBOPT.
1017  * In the latter case, any SUBOPT value not recognized will default to "all"
1018  */
1019         } else if (JLI_StrCmp(arg, "-XshowSettings") == 0 ||
1020                 JLI_StrCCmp(arg, "-XshowSettings:") == 0) {
1021             showSettings = arg;
1022         } else if (JLI_StrCmp(arg, "-Xdiag") == 0) {
1023             AddOption("-Dsun.java.launcher.diag=true", NULL);
1024 /*
1025  * The following case provide backward compatibility with old-style
1026  * command line options.
1027  */
1028         } else if (JLI_StrCmp(arg, "-fullversion") == 0) {
1029             JLI_ReportMessage("%s full version \"%s\"", _launcher_name, GetFullVersion());
1030             return JNI_FALSE;
1031         } else if (JLI_StrCmp(arg, "-verbosegc") == 0) {
1032             AddOption("-verbose:gc", NULL);
1033         } else if (JLI_StrCmp(arg, "-t") == 0) {
1034             AddOption("-Xt", NULL);
1035         } else if (JLI_StrCmp(arg, "-tm") == 0) {
1036             AddOption("-Xtm", NULL);
1037         } else if (JLI_StrCmp(arg, "-debug") == 0) {
1038             AddOption("-Xdebug", NULL);
1039         } else if (JLI_StrCmp(arg, "-noclassgc") == 0) {
1040             AddOption("-Xnoclassgc", NULL);
1041         } else if (JLI_StrCmp(arg, "-Xfuture") == 0) {
1042             AddOption("-Xverify:all", NULL);
1043         } else if (JLI_StrCmp(arg, "-verify") == 0) {
1044             AddOption("-Xverify:all", NULL);
1045         } else if (JLI_StrCmp(arg, "-verifyremote") == 0) {
1046             AddOption("-Xverify:remote", NULL);
1047         } else if (JLI_StrCmp(arg, "-noverify") == 0) {
1048             AddOption("-Xverify:none", NULL);
1049         } else if (JLI_StrCCmp(arg, "-prof") == 0) {
1050             char *p = arg + 5;
1051             char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 50);
1052             if (*p) {
1053                 sprintf(tmp, "-Xrunhprof:cpu=old,file=%s", p + 1);
1054             } else {
1055                 sprintf(tmp, "-Xrunhprof:cpu=old,file=java.prof");
1056             }
1057             AddOption(tmp, NULL);
1058         } else if (JLI_StrCCmp(arg, "-ss") == 0 ||
1059                    JLI_StrCCmp(arg, "-oss") == 0 ||
1060                    JLI_StrCCmp(arg, "-ms") == 0 ||
1061                    JLI_StrCCmp(arg, "-mx") == 0) {
1062             char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 6);
1063             sprintf(tmp, "-X%s", arg + 1); /* skip '-' */
1064             AddOption(tmp, NULL);
1065         } else if (JLI_StrCmp(arg, "-checksource") == 0 ||
1066                    JLI_StrCmp(arg, "-cs") == 0 ||
1067                    JLI_StrCmp(arg, "-noasyncgc") == 0) {
1068             /* No longer supported */
1069             JLI_ReportErrorMessage(ARG_WARN, arg);
1070         } else if (JLI_StrCCmp(arg, "-version:") == 0 ||
1071                    JLI_StrCmp(arg, "-no-jre-restrict-search") == 0 ||
1072                    JLI_StrCmp(arg, "-jre-restrict-search") == 0 ||
1073                    JLI_StrCCmp(arg, "-splash:") == 0) {
1074             ; /* Ignore machine independent options already handled */
1075         } else if (ProcessPlatformOption(arg)) {
1076             ; /* Processing of platform dependent options */
1077         } else if (RemovableOption(arg)) {
1078             ; /* Do not pass option to vm. */
1079         } else {
1080             AddOption(arg, NULL);
1081         }
1082     }
1083 
1084     if (--argc >= 0) {
1085         *pwhat = *argv++;
1086     }
1087 
1088     if (*pwhat == NULL) {
1089         *pret = 1;
1090     } else if (mode == LM_UNKNOWN) {
1091         /* default to LM_CLASS if -jar and -cp option are
1092          * not specified */
1093         mode = LM_CLASS;
1094     }
1095 
1096     if (argc >= 0) {
1097         *pargc = argc;
1098         *pargv = argv;
1099     }
1100 
1101     *pmode = mode;
1102 
1103     return JNI_TRUE;
1104 }
1105 
1106 /*
1107  * Initializes the Java Virtual Machine. Also frees options array when
1108  * finished.
1109  */
1110 static jboolean
1111 InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn)
1112 {
1113     JavaVMInitArgs args;
1114     jint r;
1115 
1116     memset(&args, 0, sizeof(args));
1117     args.version  = JNI_VERSION_1_2;
1118     args.nOptions = numOptions;
1119     args.options  = options;
1120     args.ignoreUnrecognized = JNI_FALSE;
1121 
1122     if (JLI_IsTraceLauncher()) {
1123         int i = 0;
1124         printf("JavaVM args:\n    ");
1125         printf("version 0x%08lx, ", (long)args.version);
1126         printf("ignoreUnrecognized is %s, ",
1127                args.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE");
1128         printf("nOptions is %ld\n", (long)args.nOptions);
1129         for (i = 0; i < numOptions; i++)
1130             printf("    option[%2d] = '%s'\n",
1131                    i, args.options[i].optionString);
1132     }
1133 
1134     r = ifn->CreateJavaVM(pvm, (void **)penv, &args);
1135     JLI_MemFree(options);
1136     return r == JNI_OK;
1137 }
1138 
1139 static jclass helperClass = NULL;
1140 
1141 jclass
1142 GetLauncherHelperClass(JNIEnv *env)
1143 {
1144     if (helperClass == NULL) {
1145         NULL_CHECK0(helperClass = FindBootStrapClass(env,
1146                 "sun/launcher/LauncherHelper"));
1147     }
1148     return helperClass;
1149 }
1150 
1151 static jmethodID makePlatformStringMID = NULL;
1152 /*
1153  * Returns a new Java string object for the specified platform string.
1154  */
1155 static jstring
1156 NewPlatformString(JNIEnv *env, char *s)
1157 {
1158     int len = (int)JLI_StrLen(s);
1159     jbyteArray ary;
1160     jclass cls = GetLauncherHelperClass(env);
1161     NULL_CHECK0(cls);
1162     if (s == NULL)
1163         return 0;
1164 
1165     ary = (*env)->NewByteArray(env, len);
1166     if (ary != 0) {
1167         jstring str = 0;
1168         (*env)->SetByteArrayRegion(env, ary, 0, len, (jbyte *)s);
1169         if (!(*env)->ExceptionOccurred(env)) {
1170             if (makePlatformStringMID == NULL) {
1171                 NULL_CHECK0(makePlatformStringMID = (*env)->GetStaticMethodID(env,
1172                         cls, "makePlatformString", "(Z[B)Ljava/lang/String;"));
1173             }
1174             str = (*env)->CallStaticObjectMethod(env, cls,
1175                     makePlatformStringMID, USE_STDERR, ary);
1176             (*env)->DeleteLocalRef(env, ary);
1177             return str;
1178         }
1179     }
1180     return 0;
1181 }
1182 
1183 /*
1184  * Returns a new array of Java string objects for the specified
1185  * array of platform strings.
1186  */
1187 jobjectArray
1188 NewPlatformStringArray(JNIEnv *env, char **strv, int strc)
1189 {
1190     jarray cls;
1191     jarray ary;
1192     int i;
1193 
1194     NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
1195     NULL_CHECK0(ary = (*env)->NewObjectArray(env, strc, cls, 0));
1196     for (i = 0; i < strc; i++) {
1197         jstring str = NewPlatformString(env, *strv++);
1198         NULL_CHECK0(str);
1199         (*env)->SetObjectArrayElement(env, ary, i, str);
1200         (*env)->DeleteLocalRef(env, str);
1201     }
1202     return ary;
1203 }
1204 
1205 /*
1206  * Loads a class and verifies that the main class is present and it is ok to
1207  * call it for more details refer to the java implementation.
1208  */
1209 static jclass
1210 LoadMainClass(JNIEnv *env, int mode, char *name)
1211 {
1212     jmethodID mid;
1213     jstring str;
1214     jobject result;
1215     jlong start, end;
1216     jclass cls = GetLauncherHelperClass(env);
1217     NULL_CHECK0(cls);
1218     if (JLI_IsTraceLauncher()) {
1219         start = CounterGet();
1220     }
1221     NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls,
1222                 "checkAndLoadMain",
1223                 "(ZILjava/lang/String;)Ljava/lang/Class;"));
1224 
1225     str = NewPlatformString(env, name);
1226     result = (*env)->CallStaticObjectMethod(env, cls, mid, USE_STDERR, mode, str);
1227 
1228     if (JLI_IsTraceLauncher()) {
1229         end   = CounterGet();
1230         printf("%ld micro seconds to load main class\n",
1231                (long)(jint)Counter2Micros(end-start));
1232         printf("----%s----\n", JLDEBUG_ENV_ENTRY);
1233     }
1234 
1235     return (jclass)result;
1236 }
1237 
1238 static jclass
1239 GetApplicationClass(JNIEnv *env)
1240 {
1241     jmethodID mid;
1242     jobject result;
1243     jclass cls = GetLauncherHelperClass(env);
1244     NULL_CHECK0(cls);
1245     NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls,
1246                 "getApplicationClass",
1247                 "()Ljava/lang/Class;"));
1248 
1249     return (*env)->CallStaticObjectMethod(env, cls, mid);
1250 }
1251 
1252 /*
1253  * For tools, convert command line args thus:
1254  *   javac -cp foo:foo/"*" -J-ms32m ...
1255  *   java -ms32m -cp JLI_WildcardExpandClasspath(foo:foo/"*") ...
1256  *
1257  * Takes 4 parameters, and returns the populated arguments
1258  */
1259 static void
1260 TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv)
1261 {
1262     int argc = *pargc;
1263     char **argv = *pargv;
1264     int nargc = argc + jargc;
1265     char **nargv = JLI_MemAlloc((nargc + 1) * sizeof(char *));
1266     int i;
1267 
1268     *pargc = nargc;
1269     *pargv = nargv;
1270 
1271     /* Copy the VM arguments (i.e. prefixed with -J) */
1272     for (i = 0; i < jargc; i++) {
1273         const char *arg = jargv[i];
1274         if (arg[0] == '-' && arg[1] == 'J') {
1275             *nargv++ = ((arg + 2) == NULL) ? NULL : JLI_StringDup(arg + 2);
1276         }
1277     }
1278 
1279     for (i = 0; i < argc; i++) {
1280         char *arg = argv[i];
1281         if (arg[0] == '-' && arg[1] == 'J') {
1282             if (arg[2] == '\0') {
1283                 JLI_ReportErrorMessage(ARG_ERROR3);
1284                 exit(1);
1285             }
1286             *nargv++ = arg + 2;
1287         }
1288     }
1289 
1290     /* Copy the rest of the arguments */
1291     for (i = 0; i < jargc ; i++) {
1292         const char *arg = jargv[i];
1293         if (arg[0] != '-' || arg[1] != 'J') {
1294             *nargv++ = (arg == NULL) ? NULL : JLI_StringDup(arg);
1295         }
1296     }
1297     for (i = 0; i < argc; i++) {
1298         char *arg = argv[i];
1299         if (arg[0] == '-') {
1300             if (arg[1] == 'J')
1301                 continue;
1302             if (IsWildCardEnabled() && arg[1] == 'c'
1303                 && (JLI_StrCmp(arg, "-cp") == 0 ||
1304                     JLI_StrCmp(arg, "-classpath") == 0)
1305                 && i < argc - 1) {
1306                 *nargv++ = arg;
1307                 *nargv++ = (char *) JLI_WildcardExpandClasspath(argv[i+1]);
1308                 i++;
1309                 continue;
1310             }
1311         }
1312         *nargv++ = arg;
1313     }
1314     *nargv = 0;
1315 }
1316 
1317 /*
1318  * For our tools, we try to add 3 VM options:
1319  *      -Denv.class.path=<envcp>
1320  *      -Dapplication.home=<apphome>
1321  *      -Djava.class.path=<appcp>
1322  * <envcp>   is the user's setting of CLASSPATH -- for instance the user
1323  *           tells javac where to find binary classes through this environment
1324  *           variable.  Notice that users will be able to compile against our
1325  *           tools classes (sun.tools.javac.Main) only if they explicitly add
1326  *           tools.jar to CLASSPATH.
1327  * <apphome> is the directory where the application is installed.
1328  * <appcp>   is the classpath to where our apps' classfiles are.
1329  */
1330 static jboolean
1331 AddApplicationOptions(int cpathc, const char **cpathv)
1332 {
1333     char *envcp, *appcp, *apphome;
1334     char home[MAXPATHLEN]; /* application home */
1335     char separator[] = { PATH_SEPARATOR, '\0' };
1336     int size, i;
1337 
1338     {
1339         const char *s = getenv("CLASSPATH");
1340         if (s) {
1341             s = (char *) JLI_WildcardExpandClasspath(s);
1342             /* 40 for -Denv.class.path= */
1343             envcp = (char *)JLI_MemAlloc(JLI_StrLen(s) + 40);
1344             sprintf(envcp, "-Denv.class.path=%s", s);
1345             AddOption(envcp, NULL);
1346         }
1347     }
1348 
1349     if (!GetApplicationHome(home, sizeof(home))) {
1350         JLI_ReportErrorMessage(CFG_ERROR5);
1351         return JNI_FALSE;
1352     }
1353 
1354     /* 40 for '-Dapplication.home=' */
1355     apphome = (char *)JLI_MemAlloc(JLI_StrLen(home) + 40);
1356     sprintf(apphome, "-Dapplication.home=%s", home);
1357     AddOption(apphome, NULL);
1358 
1359     /* How big is the application's classpath? */
1360     size = 40;                                 /* 40: "-Djava.class.path=" */
1361     for (i = 0; i < cpathc; i++) {
1362         size += (int)JLI_StrLen(home) + (int)JLI_StrLen(cpathv[i]) + 1; /* 1: separator */
1363     }
1364     appcp = (char *)JLI_MemAlloc(size + 1);
1365     JLI_StrCpy(appcp, "-Djava.class.path=");
1366     for (i = 0; i < cpathc; i++) {
1367         JLI_StrCat(appcp, home);                        /* c:\program files\myapp */
1368         JLI_StrCat(appcp, cpathv[i]);           /* \lib\myapp.jar         */
1369         JLI_StrCat(appcp, separator);           /* ;                      */
1370     }
1371     appcp[JLI_StrLen(appcp)-1] = '\0';  /* remove trailing path separator */
1372     AddOption(appcp, NULL);
1373     return JNI_TRUE;
1374 }
1375 
1376 /*
1377  * inject the -Dsun.java.command pseudo property into the args structure
1378  * this pseudo property is used in the HotSpot VM to expose the
1379  * Java class name and arguments to the main method to the VM. The
1380  * HotSpot VM uses this pseudo property to store the Java class name
1381  * (or jar file name) and the arguments to the class's main method
1382  * to the instrumentation memory region. The sun.java.command pseudo
1383  * property is not exported by HotSpot to the Java layer.
1384  */
1385 void
1386 SetJavaCommandLineProp(char *what, int argc, char **argv)
1387 {
1388 
1389     int i = 0;
1390     size_t len = 0;
1391     char* javaCommand = NULL;
1392     char* dashDstr = "-Dsun.java.command=";
1393 
1394     if (what == NULL) {
1395         /* unexpected, one of these should be set. just return without
1396          * setting the property
1397          */
1398         return;
1399     }
1400 
1401     /* determine the amount of memory to allocate assuming
1402      * the individual components will be space separated
1403      */
1404     len = JLI_StrLen(what);
1405     for (i = 0; i < argc; i++) {
1406         len += JLI_StrLen(argv[i]) + 1;
1407     }
1408 
1409     /* allocate the memory */
1410     javaCommand = (char*) JLI_MemAlloc(len + JLI_StrLen(dashDstr) + 1);
1411 
1412     /* build the -D string */
1413     *javaCommand = '\0';
1414     JLI_StrCat(javaCommand, dashDstr);
1415     JLI_StrCat(javaCommand, what);
1416 
1417     for (i = 0; i < argc; i++) {
1418         /* the components of the string are space separated. In
1419          * the case of embedded white space, the relationship of
1420          * the white space separated components to their true
1421          * positional arguments will be ambiguous. This issue may
1422          * be addressed in a future release.
1423          */
1424         JLI_StrCat(javaCommand, " ");
1425         JLI_StrCat(javaCommand, argv[i]);
1426     }
1427 
1428     AddOption(javaCommand, NULL);
1429 }
1430 
1431 /*
1432  * JVM would like to know if it's created by a standard Sun launcher, or by
1433  * user native application, the following property indicates the former.
1434  */
1435 void
1436 SetJavaLauncherProp() {
1437   AddOption("-Dsun.java.launcher=SUN_STANDARD", NULL);
1438 }
1439 
1440 /*
1441  * Prints the version information from the java.version and other properties.
1442  */
1443 static void
1444 PrintJavaVersion(JNIEnv *env, jboolean extraLF)
1445 {
1446     jclass ver;
1447     jmethodID print;
1448 
1449     NULL_CHECK(ver = FindBootStrapClass(env, "sun/misc/Version"));
1450     NULL_CHECK(print = (*env)->GetStaticMethodID(env,
1451                                                  ver,
1452                                                  (extraLF == JNI_TRUE) ? "println" : "print",
1453                                                  "()V"
1454                                                  )
1455               );
1456 
1457     (*env)->CallStaticVoidMethod(env, ver, print);
1458 }
1459 
1460 /*
1461  * Prints all the Java settings, see the java implementation for more details.
1462  */
1463 static void
1464 ShowSettings(JNIEnv *env, char *optString)
1465 {
1466     jmethodID showSettingsID;
1467     jstring joptString;
1468     jclass cls = GetLauncherHelperClass(env);
1469     NULL_CHECK(cls);
1470     NULL_CHECK(showSettingsID = (*env)->GetStaticMethodID(env, cls,
1471             "showSettings", "(ZLjava/lang/String;JJJZ)V"));
1472     joptString = (*env)->NewStringUTF(env, optString);
1473     (*env)->CallStaticVoidMethod(env, cls, showSettingsID,
1474                                  USE_STDERR,
1475                                  joptString,
1476                                  (jlong)initialHeapSize,
1477                                  (jlong)maxHeapSize,
1478                                  (jlong)threadStackSize,
1479                                  ServerClassMachine());
1480 }
1481 
1482 /*
1483  * Prints default usage or the Xusage message, see sun.launcher.LauncherHelper.java
1484  */
1485 static void
1486 PrintUsage(JNIEnv* env, jboolean doXUsage)
1487 {
1488   jmethodID initHelp, vmSelect, vmSynonym, vmErgo, printHelp, printXUsageMessage;
1489   jstring jprogname, vm1, vm2;
1490   int i;
1491   jclass cls = GetLauncherHelperClass(env);
1492   NULL_CHECK(cls);
1493   if (doXUsage) {
1494     NULL_CHECK(printXUsageMessage = (*env)->GetStaticMethodID(env, cls,
1495                                         "printXUsageMessage", "(Z)V"));
1496     (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, USE_STDERR);
1497   } else {
1498     NULL_CHECK(initHelp = (*env)->GetStaticMethodID(env, cls,
1499                                         "initHelpMessage", "(Ljava/lang/String;)V"));
1500 
1501     NULL_CHECK(vmSelect = (*env)->GetStaticMethodID(env, cls, "appendVmSelectMessage",
1502                                         "(Ljava/lang/String;Ljava/lang/String;)V"));
1503 
1504     NULL_CHECK(vmSynonym = (*env)->GetStaticMethodID(env, cls,
1505                                         "appendVmSynonymMessage",
1506                                         "(Ljava/lang/String;Ljava/lang/String;)V"));
1507     NULL_CHECK(vmErgo = (*env)->GetStaticMethodID(env, cls,
1508                                         "appendVmErgoMessage", "(ZLjava/lang/String;)V"));
1509 
1510     NULL_CHECK(printHelp = (*env)->GetStaticMethodID(env, cls,
1511                                         "printHelpMessage", "(Z)V"));
1512 
1513     jprogname = (*env)->NewStringUTF(env, _program_name);
1514 
1515     /* Initialize the usage message with the usual preamble */
1516     (*env)->CallStaticVoidMethod(env, cls, initHelp, jprogname);
1517 
1518 
1519     /* Assemble the other variant part of the usage */
1520     if ((knownVMs[0].flag == VM_KNOWN) ||
1521         (knownVMs[0].flag == VM_IF_SERVER_CLASS)) {
1522       vm1 = (*env)->NewStringUTF(env, knownVMs[0].name);
1523       vm2 =  (*env)->NewStringUTF(env, knownVMs[0].name+1);
1524       (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1525     }
1526     for (i=1; i<knownVMsCount; i++) {
1527       if (knownVMs[i].flag == VM_KNOWN) {
1528         vm1 =  (*env)->NewStringUTF(env, knownVMs[i].name);
1529         vm2 =  (*env)->NewStringUTF(env, knownVMs[i].name+1);
1530         (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1531       }
1532     }
1533     for (i=1; i<knownVMsCount; i++) {
1534       if (knownVMs[i].flag == VM_ALIASED_TO) {
1535         vm1 =  (*env)->NewStringUTF(env, knownVMs[i].name);
1536         vm2 =  (*env)->NewStringUTF(env, knownVMs[i].alias+1);
1537         (*env)->CallStaticVoidMethod(env, cls, vmSynonym, vm1, vm2);
1538       }
1539     }
1540 
1541     /* The first known VM is the default */
1542     {
1543       jboolean isServerClassMachine = ServerClassMachine();
1544 
1545       const char* defaultVM  =  knownVMs[0].name+1;
1546       if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) && isServerClassMachine) {
1547         defaultVM = knownVMs[0].server_class+1;
1548       }
1549 
1550       vm1 =  (*env)->NewStringUTF(env, defaultVM);
1551       (*env)->CallStaticVoidMethod(env, cls, vmErgo, isServerClassMachine,  vm1);
1552     }
1553 
1554     /* Complete the usage message and print to stderr*/
1555     (*env)->CallStaticVoidMethod(env, cls, printHelp, USE_STDERR);
1556   }
1557   return;
1558 }
1559 
1560 /*
1561  * Read the jvm.cfg file and fill the knownJVMs[] array.
1562  *
1563  * The functionality of the jvm.cfg file is subject to change without
1564  * notice and the mechanism will be removed in the future.
1565  *
1566  * The lexical structure of the jvm.cfg file is as follows:
1567  *
1568  *     jvmcfg         :=  { vmLine }
1569  *     vmLine         :=  knownLine
1570  *                    |   aliasLine
1571  *                    |   warnLine
1572  *                    |   ignoreLine
1573  *                    |   errorLine
1574  *                    |   predicateLine
1575  *                    |   commentLine
1576  *     knownLine      :=  flag  "KNOWN"                  EOL
1577  *     warnLine       :=  flag  "WARN"                   EOL
1578  *     ignoreLine     :=  flag  "IGNORE"                 EOL
1579  *     errorLine      :=  flag  "ERROR"                  EOL
1580  *     aliasLine      :=  flag  "ALIASED_TO"       flag  EOL
1581  *     predicateLine  :=  flag  "IF_SERVER_CLASS"  flag  EOL
1582  *     commentLine    :=  "#" text                       EOL
1583  *     flag           :=  "-" identifier
1584  *
1585  * The semantics are that when someone specifies a flag on the command line:
1586  * - if the flag appears on a knownLine, then the identifier is used as
1587  *   the name of the directory holding the JVM library (the name of the JVM).
1588  * - if the flag appears as the first flag on an aliasLine, the identifier
1589  *   of the second flag is used as the name of the JVM.
1590  * - if the flag appears on a warnLine, the identifier is used as the
1591  *   name of the JVM, but a warning is generated.
1592  * - if the flag appears on an ignoreLine, the identifier is recognized as the
1593  *   name of a JVM, but the identifier is ignored and the default vm used
1594  * - if the flag appears on an errorLine, an error is generated.
1595  * - if the flag appears as the first flag on a predicateLine, and
1596  *   the machine on which you are running passes the predicate indicated,
1597  *   then the identifier of the second flag is used as the name of the JVM,
1598  *   otherwise the identifier of the first flag is used as the name of the JVM.
1599  * If no flag is given on the command line, the first vmLine of the jvm.cfg
1600  * file determines the name of the JVM.
1601  * PredicateLines are only interpreted on first vmLine of a jvm.cfg file,
1602  * since they only make sense if someone hasn't specified the name of the
1603  * JVM on the command line.
1604  *
1605  * The intent of the jvm.cfg file is to allow several JVM libraries to
1606  * be installed in different subdirectories of a single JRE installation,
1607  * for space-savings and convenience in testing.
1608  * The intent is explicitly not to provide a full aliasing or predicate
1609  * mechanism.
1610  */
1611 jint
1612 ReadKnownVMs(const char *jvmCfgName, jboolean speculative)
1613 {
1614     FILE *jvmCfg;
1615     char line[MAXPATHLEN+20];
1616     int cnt = 0;
1617     int lineno = 0;
1618     jlong start, end;
1619     int vmType;
1620     char *tmpPtr;
1621     char *altVMName = NULL;
1622     char *serverClassVMName = NULL;
1623     static char *whiteSpace = " \t";
1624     if (JLI_IsTraceLauncher()) {
1625         start = CounterGet();
1626     }
1627 
1628     jvmCfg = fopen(jvmCfgName, "r");
1629     if (jvmCfg == NULL) {
1630       if (!speculative) {
1631         JLI_ReportErrorMessage(CFG_ERROR6, jvmCfgName);
1632         exit(1);
1633       } else {
1634         return -1;
1635       }
1636     }
1637     while (fgets(line, sizeof(line), jvmCfg) != NULL) {
1638         vmType = VM_UNKNOWN;
1639         lineno++;
1640         if (line[0] == '#')
1641             continue;
1642         if (line[0] != '-') {
1643             JLI_ReportErrorMessage(CFG_WARN2, lineno, jvmCfgName);
1644         }
1645         if (cnt >= knownVMsLimit) {
1646             GrowKnownVMs(cnt);
1647         }
1648         line[JLI_StrLen(line)-1] = '\0'; /* remove trailing newline */
1649         tmpPtr = line + JLI_StrCSpn(line, whiteSpace);
1650         if (*tmpPtr == 0) {
1651             JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
1652         } else {
1653             /* Null-terminate this string for JLI_StringDup below */
1654             *tmpPtr++ = 0;
1655             tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1656             if (*tmpPtr == 0) {
1657                 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
1658             } else {
1659                 if (!JLI_StrCCmp(tmpPtr, "KNOWN")) {
1660                     vmType = VM_KNOWN;
1661                 } else if (!JLI_StrCCmp(tmpPtr, "ALIASED_TO")) {
1662                     tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1663                     if (*tmpPtr != 0) {
1664                         tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1665                     }
1666                     if (*tmpPtr == 0) {
1667                         JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
1668                     } else {
1669                         /* Null terminate altVMName */
1670                         altVMName = tmpPtr;
1671                         tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1672                         *tmpPtr = 0;
1673                         vmType = VM_ALIASED_TO;
1674                     }
1675                 } else if (!JLI_StrCCmp(tmpPtr, "WARN")) {
1676                     vmType = VM_WARN;
1677                 } else if (!JLI_StrCCmp(tmpPtr, "IGNORE")) {
1678                     vmType = VM_IGNORE;
1679                 } else if (!JLI_StrCCmp(tmpPtr, "ERROR")) {
1680                     vmType = VM_ERROR;
1681                 } else if (!JLI_StrCCmp(tmpPtr, "IF_SERVER_CLASS")) {
1682                     tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1683                     if (*tmpPtr != 0) {
1684                         tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1685                     }
1686                     if (*tmpPtr == 0) {
1687                         JLI_ReportErrorMessage(CFG_WARN4, lineno, jvmCfgName);
1688                     } else {
1689                         /* Null terminate server class VM name */
1690                         serverClassVMName = tmpPtr;
1691                         tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1692                         *tmpPtr = 0;
1693                         vmType = VM_IF_SERVER_CLASS;
1694                     }
1695                 } else {
1696                     JLI_ReportErrorMessage(CFG_WARN5, lineno, &jvmCfgName[0]);
1697                     vmType = VM_KNOWN;
1698                 }
1699             }
1700         }
1701 
1702         JLI_TraceLauncher("jvm.cfg[%d] = ->%s<-\n", cnt, line);
1703         if (vmType != VM_UNKNOWN) {
1704             knownVMs[cnt].name = JLI_StringDup(line);
1705             knownVMs[cnt].flag = vmType;
1706             switch (vmType) {
1707             default:
1708                 break;
1709             case VM_ALIASED_TO:
1710                 knownVMs[cnt].alias = JLI_StringDup(altVMName);
1711                 JLI_TraceLauncher("    name: %s  vmType: %s  alias: %s\n",
1712                    knownVMs[cnt].name, "VM_ALIASED_TO", knownVMs[cnt].alias);
1713                 break;
1714             case VM_IF_SERVER_CLASS:
1715                 knownVMs[cnt].server_class = JLI_StringDup(serverClassVMName);
1716                 JLI_TraceLauncher("    name: %s  vmType: %s  server_class: %s\n",
1717                     knownVMs[cnt].name, "VM_IF_SERVER_CLASS", knownVMs[cnt].server_class);
1718                 break;
1719             }
1720             cnt++;
1721         }
1722     }
1723     fclose(jvmCfg);
1724     knownVMsCount = cnt;
1725 
1726     if (JLI_IsTraceLauncher()) {
1727         end   = CounterGet();
1728         printf("%ld micro seconds to parse jvm.cfg\n",
1729                (long)(jint)Counter2Micros(end-start));
1730     }
1731 
1732     return cnt;
1733 }
1734 
1735 
1736 static void
1737 GrowKnownVMs(int minimum)
1738 {
1739     struct vmdesc* newKnownVMs;
1740     int newMax;
1741 
1742     newMax = (knownVMsLimit == 0 ? INIT_MAX_KNOWN_VMS : (2 * knownVMsLimit));
1743     if (newMax <= minimum) {
1744         newMax = minimum;
1745     }
1746     newKnownVMs = (struct vmdesc*) JLI_MemAlloc(newMax * sizeof(struct vmdesc));
1747     if (knownVMs != NULL) {
1748         memcpy(newKnownVMs, knownVMs, knownVMsLimit * sizeof(struct vmdesc));
1749     }
1750     JLI_MemFree(knownVMs);
1751     knownVMs = newKnownVMs;
1752     knownVMsLimit = newMax;
1753 }
1754 
1755 
1756 /* Returns index of VM or -1 if not found */
1757 static int
1758 KnownVMIndex(const char* name)
1759 {
1760     int i;
1761     if (JLI_StrCCmp(name, "-J") == 0) name += 2;
1762     for (i = 0; i < knownVMsCount; i++) {
1763         if (!JLI_StrCmp(name, knownVMs[i].name)) {
1764             return i;
1765         }
1766     }
1767     return -1;
1768 }
1769 
1770 static void
1771 FreeKnownVMs()
1772 {
1773     int i;
1774     for (i = 0; i < knownVMsCount; i++) {
1775         JLI_MemFree(knownVMs[i].name);
1776         knownVMs[i].name = NULL;
1777     }
1778     JLI_MemFree(knownVMs);
1779 }
1780 
1781 /*
1782  * Displays the splash screen according to the jar file name
1783  * and image file names stored in environment variables
1784  */
1785 void
1786 ShowSplashScreen()
1787 {
1788     const char *jar_name = getenv(SPLASH_JAR_ENV_ENTRY);
1789     const char *file_name = getenv(SPLASH_FILE_ENV_ENTRY);
1790     int data_size;
1791     void *image_data;
1792     if (jar_name) {
1793         image_data = JLI_JarUnpackFile(jar_name, file_name, &data_size);
1794         if (image_data) {
1795             DoSplashInit();
1796             DoSplashLoadMemory(image_data, data_size);
1797             JLI_MemFree(image_data);
1798         }
1799     } else if (file_name) {
1800         DoSplashInit();
1801         DoSplashLoadFile(file_name);
1802     } else {
1803         return;
1804     }
1805     DoSplashSetFileJarName(file_name, jar_name);
1806 
1807     /*
1808      * Done with all command line processing and potential re-execs so
1809      * clean up the environment.
1810      */
1811     (void)UnsetEnv(ENV_ENTRY);
1812     (void)UnsetEnv(SPLASH_FILE_ENV_ENTRY);
1813     (void)UnsetEnv(SPLASH_JAR_ENV_ENTRY);
1814 
1815     JLI_MemFree(splash_jar_entry);
1816     JLI_MemFree(splash_file_entry);
1817 
1818 }
1819 
1820 const char*
1821 GetDotVersion()
1822 {
1823     return _dVersion;
1824 }
1825 
1826 const char*
1827 GetFullVersion()
1828 {
1829     return _fVersion;
1830 }
1831 
1832 const char*
1833 GetProgramName()
1834 {
1835     return _program_name;
1836 }
1837 
1838 const char*
1839 GetLauncherName()
1840 {
1841     return _launcher_name;
1842 }
1843 
1844 jint
1845 GetErgoPolicy()
1846 {
1847     return _ergo_policy;
1848 }
1849 
1850 jboolean
1851 IsJavaArgs()
1852 {
1853     return _is_java_args;
1854 }
1855 
1856 static jboolean
1857 IsWildCardEnabled()
1858 {
1859     return _wc_enabled;
1860 }
1861 
1862 int
1863 ContinueInNewThread(InvocationFunctions* ifn, jlong threadStackSize,
1864                     int argc, char **argv,
1865                     int mode, char *what, int ret)
1866 {
1867 
1868     /*
1869      * If user doesn't specify stack size, check if VM has a preference.
1870      * Note that HotSpot no longer supports JNI_VERSION_1_1 but it will
1871      * return its default stack size through the init args structure.
1872      */
1873     if (threadStackSize == 0) {
1874       struct JDK1_1InitArgs args1_1;
1875       memset((void*)&args1_1, 0, sizeof(args1_1));
1876       args1_1.version = JNI_VERSION_1_1;
1877       ifn->GetDefaultJavaVMInitArgs(&args1_1);  /* ignore return value */
1878       if (args1_1.javaStackSize > 0) {
1879          threadStackSize = args1_1.javaStackSize;
1880       }
1881     }
1882 
1883     { /* Create a new thread to create JVM and invoke main method */
1884       JavaMainArgs args;
1885       int rslt;
1886 
1887       args.argc = argc;
1888       args.argv = argv;
1889       args.mode = mode;
1890       args.what = what;
1891       args.ifn = *ifn;
1892 
1893       rslt = ContinueInNewThread0(JavaMain, threadStackSize, (void*)&args);
1894       /* If the caller has deemed there is an error we
1895        * simply return that, otherwise we return the value of
1896        * the callee
1897        */
1898       return (ret != 0) ? ret : rslt;
1899     }
1900 }
1901 
1902 static void
1903 DumpState()
1904 {
1905     if (!JLI_IsTraceLauncher()) return ;
1906     printf("Launcher state:\n");
1907     printf("\tdebug:%s\n", (JLI_IsTraceLauncher() == JNI_TRUE) ? "on" : "off");
1908     printf("\tjavargs:%s\n", (_is_java_args == JNI_TRUE) ? "on" : "off");
1909     printf("\tprogram name:%s\n", GetProgramName());
1910     printf("\tlauncher name:%s\n", GetLauncherName());
1911     printf("\tjavaw:%s\n", (IsJavaw() == JNI_TRUE) ? "on" : "off");
1912     printf("\tfullversion:%s\n", GetFullVersion());
1913     printf("\tdotversion:%s\n", GetDotVersion());
1914     printf("\tergo_policy:");
1915     switch(GetErgoPolicy()) {
1916         case NEVER_SERVER_CLASS:
1917             printf("NEVER_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1918             break;
1919         case ALWAYS_SERVER_CLASS:
1920             printf("ALWAYS_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1921             break;
1922         default:
1923             printf("DEFAULT_ERGONOMICS_POLICY\n");
1924     }
1925 }
1926 
1927 /*
1928  * Return JNI_TRUE for an option string that has no effect but should
1929  * _not_ be passed on to the vm; return JNI_FALSE otherwise.  On
1930  * Solaris SPARC, this screening needs to be done if:
1931  *    -d32 or -d64 is passed to a binary with an unmatched data model
1932  *    (the exec in CreateExecutionEnvironment removes -d<n> options and points the
1933  *    exec to the proper binary).  In the case of when the data model and the
1934  *    requested version is matched, an exec would not occur, and these options
1935  *    were erroneously passed to the vm.
1936  */
1937 jboolean
1938 RemovableOption(char * option)
1939 {
1940   /*
1941    * Unconditionally remove both -d32 and -d64 options since only
1942    * the last such options has an effect; e.g.
1943    * java -d32 -d64 -d32 -version
1944    * is equivalent to
1945    * java -d32 -version
1946    */
1947 
1948   if( (JLI_StrCCmp(option, "-d32")  == 0 ) ||
1949       (JLI_StrCCmp(option, "-d64")  == 0 ) )
1950     return JNI_TRUE;
1951   else
1952     return JNI_FALSE;
1953 }
1954 
1955 /*
1956  * A utility procedure to always print to stderr
1957  */
1958 void
1959 JLI_ReportMessage(const char* fmt, ...)
1960 {
1961     va_list vl;
1962     va_start(vl, fmt);
1963     vfprintf(stderr, fmt, vl);
1964     fprintf(stderr, "\n");
1965     va_end(vl);
1966 }