1 /*
   2  * Copyright (c) 1995, 2018, 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 /*
  28  * This file contains the main entry point into the launcher code
  29  * this is the only file which will be repeatedly compiled by other
  30  * tools. The rest of the files will be linked in.
  31  */
  32 
  33 #include "defines.h"
  34 #include "jli_util.h"
  35 #include "jni.h"
  36 
  37 #ifdef _MSC_VER
  38 #if _MSC_VER > 1400 && _MSC_VER < 1600
  39 
  40 /*
  41  * When building for Microsoft Windows, main has a dependency on msvcr??.dll.
  42  *
  43  * When using Visual Studio 2005 or 2008, that must be recorded in
  44  * the [java,javaw].exe.manifest file.
  45  *
  46  * As of VS2010 (ver=1600), the runtimes again no longer need manifests.
  47  *
  48  * Reference:
  49  *     C:/Program Files/Microsoft SDKs/Windows/v6.1/include/crtdefs.h
  50  */
  51 #include <crtassem.h>
  52 #ifdef _M_IX86
  53 
  54 #pragma comment(linker,"/manifestdependency:\"type='win32' "            \
  55         "name='" __LIBRARIES_ASSEMBLY_NAME_PREFIX ".CRT' "              \
  56         "version='" _CRT_ASSEMBLY_VERSION "' "                          \
  57         "processorArchitecture='x86' "                                  \
  58         "publicKeyToken='" _VC_ASSEMBLY_PUBLICKEYTOKEN "'\"")
  59 
  60 #endif /* _M_IX86 */
  61 
  62 //This may not be necessary yet for the Windows 64-bit build, but it
  63 //will be when that build environment is updated.  Need to test to see
  64 //if it is harmless:
  65 #ifdef _M_AMD64
  66 
  67 #pragma comment(linker,"/manifestdependency:\"type='win32' "            \
  68         "name='" __LIBRARIES_ASSEMBLY_NAME_PREFIX ".CRT' "              \
  69         "version='" _CRT_ASSEMBLY_VERSION "' "                          \
  70         "processorArchitecture='amd64' "                                \
  71         "publicKeyToken='" _VC_ASSEMBLY_PUBLICKEYTOKEN "'\"")
  72 
  73 #endif  /* _M_AMD64 */
  74 #endif  /* _MSC_VER > 1400 && _MSC_VER < 1600 */
  75 #endif  /* _MSC_VER */
  76 
  77 /*
  78  * Entry point.
  79  */
  80 #ifdef JAVAW
  81 
  82 char **__initenv;
  83 
  84 int WINAPI
  85 WinMain(HINSTANCE inst, HINSTANCE previnst, LPSTR cmdline, int cmdshow)
  86 {
  87     int margc;
  88     char** margv;
  89     int jargc;
  90     char** jargv;
  91     const jboolean const_javaw = JNI_TRUE;
  92 
  93     __initenv = _environ;
  94 
  95 #else /* JAVAW */
  96 JNIEXPORT int JNICALL
  97 main(int argc, char **argv)
  98 {
  99     int margc;
 100     char** margv;
 101     int jargc;
 102     char** jargv;
 103     const jboolean const_javaw = JNI_FALSE;
 104 #endif /* JAVAW */
 105     {
 106         int i, main_jargc, extra_jargc;
 107         JLI_List list;
 108 
 109         main_jargc = (sizeof(const_jargs) / sizeof(char *)) > 1
 110             ? sizeof(const_jargs) / sizeof(char *)
 111             : 0; // ignore the null terminator index
 112 
 113         extra_jargc = (sizeof(const_extra_jargs) / sizeof(char *)) > 1
 114             ? sizeof(const_extra_jargs) / sizeof(char *)
 115             : 0; // ignore the null terminator index
 116 
 117         if (main_jargc > 0 && extra_jargc > 0) { // combine extra java args
 118             jargc = main_jargc + extra_jargc;
 119             list = JLI_List_new(jargc + 1);
 120 
 121             for (i = 0 ; i < extra_jargc; i++) {
 122                 JLI_List_add(list, JLI_StringDup(const_extra_jargs[i]));
 123             }
 124 
 125             for (i = 0 ; i < main_jargc ; i++) {
 126                 JLI_List_add(list, JLI_StringDup(const_jargs[i]));
 127             }
 128 
 129             // terminate the list
 130             JLI_List_add(list, NULL);
 131             jargv = list->elements;
 132          } else if (extra_jargc > 0) { // should never happen
 133             fprintf(stderr, "EXTRA_JAVA_ARGS defined without JAVA_ARGS");
 134             abort();
 135          } else { // no extra args, business as usual
 136             jargc = main_jargc;
 137             jargv = (char **) const_jargs;
 138          }
 139     }
 140 
 141     JLI_InitArgProcessing(jargc > 0, const_disable_argfile);
 142 
 143 #ifdef _WIN32
 144     {
 145         int i = 0;
 146         if (getenv(JLDEBUG_ENV_ENTRY) != NULL) {
 147             printf("Windows original main args:\n");
 148             for (i = 0 ; i < __argc ; i++) {
 149                 printf("wwwd_args[%d] = %s\n", i, __argv[i]);
 150             }
 151         }
 152     }
 153     JLI_CmdToArgs(GetCommandLine());
 154     margc = JLI_GetStdArgc();
 155     // add one more to mark the end
 156     margv = (char **)JLI_MemAlloc((margc + 1) * (sizeof(char *)));
 157     {
 158         int i = 0;
 159         StdArg *stdargs = JLI_GetStdArgs();
 160         for (i = 0 ; i < margc ; i++) {
 161             margv[i] = stdargs[i].arg;
 162         }
 163         margv[i] = NULL;
 164     }
 165 #else /* *NIXES */
 166     {
 167         // accommodate the NULL at the end
 168         JLI_List args = JLI_List_new(argc + 1);
 169         int i = 0;
 170 
 171         // Add first arg, which is the app name
 172         JLI_List_add(args, JLI_StringDup(argv[0]));
 173         // Append JDK_JAVA_OPTIONS
 174         if (JLI_AddArgsFromEnvVar(args, JDK_JAVA_OPTIONS)) {
 175             // JLI_SetTraceLauncher is not called yet
 176             // Show _JAVA_OPTIONS content along with JDK_JAVA_OPTIONS to aid diagnosis
 177             if (getenv(JLDEBUG_ENV_ENTRY)) {
 178                 char *tmp = getenv("_JAVA_OPTIONS");
 179                 if (NULL != tmp) {
 180                     JLI_ReportMessage(ARG_INFO_ENVVAR, "_JAVA_OPTIONS", tmp);
 181                 }
 182             }
 183         }
 184         // Iterate the rest of command line
 185         for (i = 1; i < argc; i++) {
 186             JLI_List argsInFile = JLI_PreprocessArg(argv[i]);
 187             if (NULL == argsInFile) {
 188                 JLI_List_add(args, JLI_StringDup(argv[i]));
 189             } else {
 190                 int cnt, idx;
 191                 cnt = argsInFile->size;
 192                 for (idx = 0; idx < cnt; idx++) {
 193                     JLI_List_add(args, argsInFile->elements[idx]);
 194                 }
 195                 // Shallow free, we reuse the string to avoid copy
 196                 JLI_MemFree(argsInFile->elements);
 197                 JLI_MemFree(argsInFile);
 198             }
 199         }
 200         margc = args->size;
 201         // add the NULL pointer at argv[argc]
 202         JLI_List_add(args, NULL);
 203         margv = args->elements;
 204     }
 205 #endif /* WIN32 */
 206     return JLI_Launch(margc, margv,
 207                    jargc, (const char**) jargv,
 208                    0, NULL,
 209                    VERSION_STRING,
 210                    DOT_VERSION,
 211                    (const_progname != NULL) ? const_progname : *margv,
 212                    (const_launcher != NULL) ? const_launcher : *margv,
 213                    jargc > 0,
 214                    const_cpwildcard, const_javaw, 0);
 215 }