@@ -26,77 +26,81 @@ if [ -z "${PROG_HOME-}" ] ; then
26
26
cd " $saveddir "
27
27
fi
28
28
29
- addJava () {
30
- java_args+=(" '$1 '" )
31
- }
29
+ source " $PROG_HOME /bin/common"
32
30
33
- addScalacOptions () {
34
- java_options+=(" '$1 '" )
31
+ # This script operates in one of 3 usage modes:
32
+ # script
33
+ # repl
34
+ # run
35
+ # execute_mode replaces mutually exclusive booleans:
36
+ # execute_repl=false
37
+ # execute_run=false
38
+ # execute_script=false
39
+ setExecuteMode () {
40
+ case " ${execute_mode-} " in
41
+ " " ) execute_mode=" $1 " ; shift ;;
42
+ * ) echo " execute_mode==[${execute_mode-} ], attempted overwrite by [$1 ]" 1>&2
43
+ exit 1
44
+ ;;
45
+ esac
35
46
}
36
47
37
- source " $PROG_HOME /bin/common"
38
48
39
- declare -a residual_args
40
- declare -a script_args
41
- execute_repl=false
42
- execute_run=false
43
- execute_script=false
44
- with_compiler=false
45
- class_path_count=0
46
- CLASS_PATH=" "
47
- save_compiled=false
49
+ with_compiler=false # to add compiler jars to repl classpath
50
+ let class_path_count=0 || true # count classpath args, warning if more than 1
51
+ save_compiled=false # to save compiled script jar in script directory
52
+ CLASS_PATH=" " || true # scala classpath
48
53
49
54
# Little hack to check if all arguments are options
50
55
all_params=" $* "
51
56
truncated_params=" ${*# -} "
52
57
# options_indicator != 0 if at least one parameter is not an option
53
58
options_indicator=$(( ${# all_params} - ${# truncated_params} - $# ))
54
59
55
- [ -n " $SCALA_OPTS " ] && set -- $SCALA_OPTS " $@ "
60
+ [ -n " ${ SCALA_OPTS-} " ] && set -- $SCALA_OPTS " $@ "
56
61
57
62
while [[ $# -gt 0 ]]; do
58
63
case " $1 " in
59
64
-repl)
60
- execute_repl=true
65
+ setExecuteMode ' repl '
61
66
shift
62
67
;;
63
68
-run)
64
- execute_run=true
69
+ setExecuteMode ' run '
65
70
shift
66
71
;;
67
72
-cp | -classpath)
68
73
CLASS_PATH=" $2 ${PSEP} "
69
- class_path_count+=1
74
+ let class_path_count+=1
70
75
shift
71
76
shift
72
77
;;
73
- -cp* |-classpath* )
78
+ -cp* |-classpath* ) # partial fix for #10761
74
79
# hashbang can combine args, e.g. "-classpath 'lib/*'"
75
80
CLASS_PATH=" ${1#* * }${PSEP} "
76
- class_path_count+=1
81
+ let class_path_count+=1
77
82
shift
78
83
;;
79
84
-with-compiler)
80
85
with_compiler=true
81
86
shift
82
87
;;
83
88
@* |-color:* )
84
- addScalacOptions " ${1} "
89
+ addScala " ${1} "
85
90
shift
86
91
;;
87
92
-save|-savecompiled)
88
93
save_compiled=true
89
- scala_script_options+=( " $1 " )
94
+ addScala " $1 "
90
95
shift
91
96
;;
92
97
-compile-only)
93
- scala_script_options+=( " $1 " )
98
+ addScala " $1 "
94
99
shift
95
100
;;
96
- -d)
101
+ -d|-debug )
97
102
DEBUG=" $DEBUG_STR "
98
- shift
99
- ;;
103
+ shift ;;
100
104
-version)
101
105
# defer to scalac, then exit
102
106
shift
@@ -106,17 +110,32 @@ while [[ $# -gt 0 ]]; do
106
110
;;
107
111
-J* )
108
112
addJava " ${1: 2} "
109
- addScalacOptions " ${1} "
113
+ addScala " ${1} "
114
+ shift ;;
115
+ -v|-verbose)
116
+ verbose=true
117
+ addScala " -verbose"
110
118
shift ;;
119
+ -run|-repl)
120
+ PROG_NAME=" $ReplMain "
121
+ shift ;;
122
+
111
123
* )
112
- if [ $execute_script == false ]; then
124
+ if [ " ${execute_mode-} " == ' script' ]; then
125
+ addScript " $1 "
126
+ else
127
+ # script if extension .scala or .sc, or if has scala hashbang line
128
+
129
+ # no -f test, issue meaningful error message (file not found)
113
130
if [[ " $1 " == * .scala || " $1 " == * .sc ]]; then
114
- # is a script if extension .scala or .sc
115
- # (even if file not found, to avoid adding likely typo to residual_args)
116
- execute_script=true
117
- target_script=" $1 "
131
+ setExecuteMode ' script' # execute_script=true
132
+
133
+ # -f test needed before we examine the hashbang line
118
134
elif [[ (-f " $1 " && ` head -n 1 -- " $1 " | grep ' #!.*scala' ` ) ]]; then
119
- execute_script=true
135
+ setExecuteMode ' script' # execute_script=true
136
+ fi
137
+
138
+ if [ " ${execute_mode-} " == ' script' ]; then
120
139
target_script=" $1 "
121
140
if [ ! -f $target_script ]; then
122
141
# likely a typo or missing script file, quit early
@@ -125,70 +144,93 @@ while [[ $# -gt 0 ]]; do
125
144
onExit
126
145
fi
127
146
else
128
- # unrecognized args appearing prior to a script name
129
- residual_args+=( " $1 " )
147
+ # all unrecognized args appearing prior to a script name
148
+ addResidual " $1 "
130
149
fi
131
- else
132
- script_args+=(" $1 " )
133
150
fi
134
151
shift
135
152
;;
136
153
137
154
esac
138
155
done
139
156
157
+ # [ -n "${dump_args}" ] && dumpArgs ; exit 2
158
+
159
+ if [ -z " ${execute_mode-} " ]; then
160
+ # no script was specified, set run or repl mode
161
+ if [[ $options_indicator -ne 0 || ${# residual_args[@]} -ne 0 ]]; then
162
+ setExecuteMode ' run'
163
+ else
164
+ setExecuteMode ' repl'
165
+ fi
166
+ fi
167
+
140
168
[ -n " ${script_trace-} " ] && set -x
141
- if [ $execute_script == true ]; then
169
+
170
+ case " ${execute_mode-} " in
171
+ script)
142
172
if [ " $CLASS_PATH " ]; then
143
- cp_arg =" -classpath \" $CLASS_PATH \" "
173
+ script_cp_arg =" -classpath ' $CLASS_PATH ' "
144
174
fi
145
- java_options+=(${scala_script_options} )
146
175
setScriptName=" -Dscript.path=$target_script "
147
176
target_jar=" ${target_script% .* } .jar"
148
177
if [[ $save_compiled == true && " $target_jar " -nt " $target_script " ]]; then
149
178
eval " \" $JAVACMD \" " $setScriptName -jar " $target_jar " " ${script_args[@]} "
150
179
scala_exit_status=$?
151
180
else
152
181
[[ $save_compiled == true ]] && rm -f $target_jar
153
- set -- ${cp_arg-} ${java_options[@]} ${residual_args[@]} -script " $target_script " ${script_args[@]}
154
- PROG_MAIN=$ScriptingMain
155
- prepScalacCommandLine " $@ "
156
- # exec here would prevent onExit from being called, leaving terminal in unusable state
182
+ PROG_NAME=$ScriptingMain
183
+ classpathArgs # initialize jvm_cp_args with toolchain classpath
184
+ scripting_string=" -script $target_script ${script_args[@]} "
185
+ # use eval instead of exec, to insure that onExit is subsequently called
186
+
187
+ # $script_cp_arg must be the first argument to $ScriptingMain
188
+ # $scripting_string must be last
157
189
eval " \" $JAVACMD \" " \
158
- ${JAVA_OPTS:- $default_java_opts } \
159
- " ${DEBUG-} " \
160
- " ${java_args[@]} " \
161
- " -classpath \" $jvm_cp_args \" " \
162
- -Dscala.usejavacp=true \
163
- " $PROG_NAME " \
164
- " ${scala_args[@]} " \
165
- " ${residual_args[@]} " \
166
- " ${scripting_string-} "
190
+ ${JAVA_OPTS:- $default_java_opts } \
191
+ " ${DEBUG-} " \
192
+ " ${java_args[@]} " \
193
+ " -classpath \" $jvm_cp_args \" " \
194
+ -Dscala.usejavacp=true \
195
+ " $setScriptName " \
196
+ " $ScriptingMain " \
197
+ ${script_cp_arg-} \
198
+ " ${scala_args[@]} " \
199
+ " ${residual_args[@]} " \
200
+ " ${scripting_string-} " # must be the last arguments
167
201
scala_exit_status=$?
168
202
fi
169
- elif [ $execute_repl == true ] || ([ $execute_run == false ] && [ $options_indicator == 0 ]); then
203
+ ;;
204
+
205
+ repl)
170
206
if [ " $CLASS_PATH " ]; then
171
- cp_arg =" -classpath \" $CLASS_PATH \" "
207
+ repl_cparg =" -classpath \" $CLASS_PATH \" "
172
208
fi
173
- eval " \" $PROG_HOME /bin/scalac\" ${cp_arg -} ${java_options [@]} -repl ${residual_args[@]} "
209
+ eval " \" $PROG_HOME /bin/scalac\" ${repl_cparg -} ${scalac_options [@]} -repl ${residual_args[@]} "
174
210
scala_exit_status=$?
175
- elif [ ${# residual_args[@]} -ne 0 ]; then
176
- cp_arg=" $DOTTY_LIB$PSEP$SCALA_LIB "
211
+ ;;
212
+
213
+ run)
214
+ repl_cparg=" $DOTTY_LIB$PSEP$SCALA_LIB "
177
215
if [ -z " $CLASS_PATH " ]; then
178
- cp_arg +=" $PSEP ."
216
+ repl_cparg +=" $PSEP ."
179
217
else
180
- cp_arg +=" $PSEP$CLASS_PATH "
218
+ repl_cparg +=" $PSEP$CLASS_PATH "
181
219
fi
182
220
if [ " $class_path_count " -gt 1 ]; then
183
221
echo " warning: multiple classpaths are found, scala only use the last one."
184
222
fi
185
223
if [ $with_compiler == true ]; then
186
- cp_arg +=" $PSEP$DOTTY_COMP$PSEP$TASTY_CORE$PSEP$DOTTY_INTF$PSEP$SCALA_ASM$PSEP$DOTTY_STAGING$PSEP$DOTTY_TASTY_INSPECTOR "
224
+ repl_cparg +=" $PSEP$DOTTY_COMP$PSEP$TASTY_CORE$PSEP$DOTTY_INTF$PSEP$SCALA_ASM$PSEP$DOTTY_STAGING$PSEP$DOTTY_TASTY_INSPECTOR "
187
225
fi
188
226
# exec here would prevent onExit from being called, leaving terminal in unusable state
189
- eval " \" $JAVACMD \" " " $DEBUG " " -classpath \" $cp_arg \" " " ${java_args[@]} " " ${residual_args[@]} "
227
+ eval " \" $JAVACMD \" " " $DEBUG " " -classpath \" $repl_cparg \" " " ${java_args[@]} " " ${residual_args[@]} "
190
228
scala_exit_status=$?
191
- else
229
+ ;;
230
+
231
+ * )
192
232
echo " warning: command option is not correct."
193
- fi
233
+ ;;
234
+ esac
235
+
194
236
onExit
0 commit comments