1010# Modifications Copyright OpenSearch Contributors. See
1111# GitHub history for details.
1212#
13- # Copyright 2015 the original author or authors.
13+ # Copyright © 2015-2021 the original authors.
1414#
1515# Licensed under the Apache License, Version 2.0 (the "License");
1616# you may not use this file except in compliance with the License.
2626#
2727
2828# #############################################################################
29- # #
30- # # Gradle start up script for UN*X
31- # #
29+ #
30+ # Gradle start up script for POSIX generated by Gradle.
31+ #
32+ # Important for running:
33+ #
34+ # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
35+ # noncompliant, but you have some other compliant shell such as ksh or
36+ # bash, then to run this script, type that shell name before the whole
37+ # command line, like:
38+ #
39+ # ksh Gradle
40+ #
41+ # Busybox and similar reduced shells will NOT work, because this script
42+ # requires all of these POSIX shell features:
43+ # * functions;
44+ # * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
45+ # «${var#prefix}», «${var%suffix}», and «$( cmd )»;
46+ # * compound commands having a testable exit status, especially «case»;
47+ # * various built-in commands including «command», «set», and «ulimit».
48+ #
49+ # Important for patching:
50+ #
51+ # (2) This script targets any POSIX shell, so it avoids extensions provided
52+ # by Bash, Ksh, etc; in particular arrays are avoided.
53+ #
54+ # The "traditional" practice of packing multiple parameters into a
55+ # space-separated string is a well documented source of bugs and security
56+ # problems, so this is (mostly) avoided, by progressively accumulating
57+ # options in "$@", and eventually passing that to Java.
58+ #
59+ # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
60+ # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
61+ # see the in-line comments for details.
62+ #
63+ # There are tweaks for specific operating systems such as AIX, CygWin,
64+ # Darwin, MinGW, and NonStop.
65+ #
66+ # (3) This script is generated from the Groovy template
67+ # https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
68+ # within the Gradle project.
69+ #
70+ # You can find Gradle at https://github.com/gradle/gradle/.
71+ #
3272# #############################################################################
3373
3474# Attempt to set APP_HOME
75+
3576# Resolve links: $0 may be a link
36- PRG=" $0 "
37- # Need this for relative symlinks.
38- while [ -h " $PRG " ] ; do
39- ls=` ls -ld " $PRG " `
40- link=` expr " $ls " : ' .*-> \(.*\)$' `
41- if expr " $link " : ' /.*' > /dev/null; then
42- PRG=" $link "
43- else
44- PRG=` dirname " $PRG " ` " /$link "
45- fi
77+ app_path=$0
78+
79+ # Need this for daisy-chained symlinks.
80+ while
81+ APP_HOME=${app_path% " ${app_path##*/ } " } # leaves a trailing /; empty if no leading path
82+ [ -h " $app_path " ]
83+ do
84+ ls=$( ls -ld " $app_path " )
85+ link=${ls#* ' -> ' }
86+ case $link in # (
87+ /* ) app_path=$link ;; # (
88+ * ) app_path=$APP_HOME$link ;;
89+ esac
4690done
47- SAVED=" ` pwd` "
48- cd " ` dirname \" $PRG \" ` /" > /dev/null
49- APP_HOME=" ` pwd -P` "
50- cd " $SAVED " > /dev/null
5191
52- APP_NAME=" Gradle"
53- APP_BASE_NAME=` basename " $0 " `
92+ # This is normally unused
93+ # shellcheck disable=SC2034
94+ APP_BASE_NAME=${0##*/ }
95+ APP_HOME=$( cd " ${APP_HOME:- ./ } " && pwd -P ) || exit
5496
5597# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
5698DEFAULT_JVM_OPTS=' "-Xmx64m" "-Xms64m"'
5799
58100# Use the maximum available, or set MAX_FD != -1 to use that value.
59- MAX_FD=" maximum"
101+ MAX_FD=maximum
60102
61103warn () {
62104 echo " $* "
63- }
105+ } >&2
64106
65107die () {
66108 echo
67109 echo " $* "
68110 echo
69111 exit 1
70- }
112+ } >&2
71113
72114# OS specific support (must be 'true' or 'false').
73115cygwin=false
74116msys=false
75117darwin=false
76118nonstop=false
77- case " ` uname` " in
78- CYGWIN* )
79- cygwin=true
80- ;;
81- Darwin* )
82- darwin=true
83- ;;
84- MINGW* )
85- msys=true
86- ;;
87- NONSTOP* )
88- nonstop=true
89- ;;
119+ case " $( uname ) " in # (
120+ CYGWIN* ) cygwin=true ;; # (
121+ Darwin* ) darwin=true ;; # (
122+ MSYS* | MINGW* ) msys=true ;; # (
123+ NONSTOP* ) nonstop=true ;;
90124esac
91125
92126CLASSPATH=$APP_HOME /gradle/wrapper/gradle-wrapper.jar
@@ -96,9 +130,9 @@ CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
96130if [ -n " $JAVA_HOME " ] ; then
97131 if [ -x " $JAVA_HOME /jre/sh/java" ] ; then
98132 # IBM's JDK on AIX uses strange locations for the executables
99- JAVACMD=" $JAVA_HOME /jre/sh/java"
133+ JAVACMD=$JAVA_HOME /jre/sh/java
100134 else
101- JAVACMD=" $JAVA_HOME /bin/java"
135+ JAVACMD=$JAVA_HOME /bin/java
102136 fi
103137 if [ ! -x " $JAVACMD " ] ; then
104138 die " ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
@@ -107,88 +141,113 @@ Please set the JAVA_HOME variable in your environment to match the
107141location of your Java installation."
108142 fi
109143else
110- JAVACMD=" java"
144+ JAVACMD=java
111145 which java > /dev/null 2>&1 || die " ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
112146
113147Please set the JAVA_HOME variable in your environment to match the
114148location of your Java installation."
115149fi
116150
117151# Increase the maximum file descriptors if we can.
118- if [ " $cygwin " = " false" -a " $darwin " = " false" -a " $nonstop " = " false" ] ; then
119- MAX_FD_LIMIT=` ulimit -H -n`
120- if [ $? -eq 0 ] ; then
121- if [ " $MAX_FD " = " maximum" -o " $MAX_FD " = " max" ] ; then
122- MAX_FD=" $MAX_FD_LIMIT "
123- fi
124- ulimit -n $MAX_FD
125- if [ $? -ne 0 ] ; then
126- warn " Could not set maximum file descriptor limit: $MAX_FD "
127- fi
128- else
129- warn " Could not query maximum file descriptor limit: $MAX_FD_LIMIT "
130- fi
152+ if ! " $cygwin " && ! " $darwin " && ! " $nonstop " ; then
153+ case $MAX_FD in # (
154+ max* )
155+ # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
156+ # shellcheck disable=SC3045
157+ MAX_FD=$( ulimit -H -n ) ||
158+ warn " Could not query maximum file descriptor limit"
159+ esac
160+ case $MAX_FD in # (
161+ ' ' | soft) : ;; # (
162+ * )
163+ # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
164+ # shellcheck disable=SC3045
165+ ulimit -n " $MAX_FD " ||
166+ warn " Could not set maximum file descriptor limit to $MAX_FD "
167+ esac
131168fi
132169
133- # For Darwin, add options to specify how the application appears in the dock
134- if $darwin ; then
135- GRADLE_OPTS=" $GRADLE_OPTS \" -Xdock:name=$APP_NAME \" \" -Xdock:icon=$APP_HOME /media/gradle.icns\" "
136- fi
170+ # Collect all arguments for the java command, stacking in reverse order:
171+ # * args from the command line
172+ # * the main class name
173+ # * -classpath
174+ # * -D...appname settings
175+ # * --module-path (only if needed)
176+ # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
137177
138178# For Cygwin or MSYS, switch paths to Windows format before running java
139- if [ " $cygwin " = " true" -o " $msys " = " true" ] ; then
140- APP_HOME=` cygpath --path --mixed " $APP_HOME " `
141- CLASSPATH=` cygpath --path --mixed " $CLASSPATH " `
142-
143- JAVACMD=` cygpath --unix " $JAVACMD " `
144-
145- # We build the pattern for arguments to be converted via cygpath
146- ROOTDIRSRAW=` find -L / -maxdepth 1 -mindepth 1 -type d 2> /dev/null`
147- SEP=" "
148- for dir in $ROOTDIRSRAW ; do
149- ROOTDIRS=" $ROOTDIRS$SEP$dir "
150- SEP=" |"
151- done
152- OURCYGPATTERN=" (^($ROOTDIRS ))"
153- # Add a user-defined pattern to the cygpath arguments
154- if [ " $GRADLE_CYGPATTERN " != " " ] ; then
155- OURCYGPATTERN=" $OURCYGPATTERN |($GRADLE_CYGPATTERN )"
156- fi
179+ if " $cygwin " || " $msys " ; then
180+ APP_HOME=$( cygpath --path --mixed " $APP_HOME " )
181+ CLASSPATH=$( cygpath --path --mixed " $CLASSPATH " )
182+
183+ JAVACMD=$( cygpath --unix " $JAVACMD " )
184+
157185 # Now convert the arguments - kludge to limit ourselves to /bin/sh
158- i=0
159- for arg in " $@ " ; do
160- CHECK=` echo " $arg " | egrep -c " $OURCYGPATTERN " -`
161- CHECK2=` echo " $arg " | egrep -c " ^-" ` # ## Determine if an option
162-
163- if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then # ## Added a condition
164- eval ` echo args$i ` =` cygpath --path --ignore --mixed " $arg " `
165- else
166- eval ` echo args$i ` =" \" $arg \" "
186+ for arg do
187+ if
188+ case $arg in # (
189+ -* ) false ;; # don't mess with options #(
190+ /?* ) t=${arg#/ } t=/${t%%/* } # looks like a POSIX filepath
191+ [ -e " $t " ] ;; # (
192+ * ) false ;;
193+ esac
194+ then
195+ arg=$( cygpath --path --ignore --mixed " $arg " )
167196 fi
168- i=` expr $i + 1`
197+ # Roll the args list around exactly as many times as the number of
198+ # args, so each arg winds up back in the position where it started, but
199+ # possibly modified.
200+ #
201+ # NB: a `for` loop captures its iteration list before it begins, so
202+ # changing the positional parameters here affects neither the number of
203+ # iterations, nor the values presented in `arg`.
204+ shift # remove old arg
205+ set -- " $@ " " $arg " # push replacement arg
169206 done
170- case $i in
171- 0) set -- ;;
172- 1) set -- " $args0 " ;;
173- 2) set -- " $args0 " " $args1 " ;;
174- 3) set -- " $args0 " " $args1 " " $args2 " ;;
175- 4) set -- " $args0 " " $args1 " " $args2 " " $args3 " ;;
176- 5) set -- " $args0 " " $args1 " " $args2 " " $args3 " " $args4 " ;;
177- 6) set -- " $args0 " " $args1 " " $args2 " " $args3 " " $args4 " " $args5 " ;;
178- 7) set -- " $args0 " " $args1 " " $args2 " " $args3 " " $args4 " " $args5 " " $args6 " ;;
179- 8) set -- " $args0 " " $args1 " " $args2 " " $args3 " " $args4 " " $args5 " " $args6 " " $args7 " ;;
180- 9) set -- " $args0 " " $args1 " " $args2 " " $args3 " " $args4 " " $args5 " " $args6 " " $args7 " " $args8 " ;;
181- esac
182207fi
183208
184- # Escape application args
185- save () {
186- for i do printf %s\\ n " $i " | sed " s/'/'\\\\ ''/g;1s/^/'/;\$ s/\$ /' \\\\ /" ; done
187- echo " "
188- }
189- APP_ARGS=` save " $@ " `
209+ # Collect all arguments for the java command;
210+ # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
211+ # shell script including quotes and variable substitutions, so put them in
212+ # double quotes to make sure that they get re-expanded; and
213+ # * put everything else in single quotes, so that it's not re-expanded.
214+
215+ set -- \
216+ " -Dorg.gradle.appname=$APP_BASE_NAME " \
217+ -classpath " $CLASSPATH " \
218+ org.gradle.wrapper.GradleWrapperMain \
219+ " $@ "
220+
221+ # Stop when "xargs" is not available.
222+ if ! command -v xargs > /dev/null 2>&1
223+ then
224+ die " xargs is not available"
225+ fi
226+
227+ # Use "xargs" to parse quoted args.
228+ #
229+ # With -n1 it outputs one arg per line, with the quotes and backslashes removed.
230+ #
231+ # In Bash we could simply go:
232+ #
233+ # readarray ARGS < <( xargs -n1 <<<"$var" ) &&
234+ # set -- "${ARGS[@]}" "$@"
235+ #
236+ # but POSIX shell has neither arrays nor command substitution, so instead we
237+ # post-process each arg (as a line of input to sed) to backslash-escape any
238+ # character that might be a shell metacharacter, then use eval to reverse
239+ # that process (while maintaining the separation between arguments), and wrap
240+ # the whole thing up as a single "set" statement.
241+ #
242+ # This will of course break if any of these variables contains a newline or
243+ # an unmatched quote.
244+ #
190245
191- # Collect all arguments for the java command, following the shell quoting and substitution rules
192- eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS " \" -Dorg.gradle.appname=$APP_BASE_NAME \" " -classpath " \" $CLASSPATH \" " org.gradle.wrapper.GradleWrapperMain " $APP_ARGS "
246+ eval " set -- $(
247+ printf ' %s\n' " $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS " |
248+ xargs -n1 |
249+ sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
250+ tr ' \n' ' '
251+ ) " ' "$@"'
193252
194253exec " $JAVACMD " " $@ "
0 commit comments