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