Chris@1706
|
1 #!/bin/bash
|
Chris@1706
|
2
|
Chris@1706
|
3 # Disable shellcheck warnings for useless-use-of-cat. UUOC is good
|
Chris@1706
|
4 # practice, not bad: clearer, safer, less error-prone.
|
Chris@1706
|
5 # shellcheck disable=SC2002
|
Chris@1706
|
6
|
Chris@1706
|
7 sml="$VEXT_SML"
|
Chris@1706
|
8
|
Chris@1706
|
9 set -eu
|
Chris@1706
|
10
|
Chris@1706
|
11 mydir=$(dirname "$0")
|
Chris@1706
|
12 program="$mydir/vext.sml"
|
Chris@1706
|
13
|
Chris@1706
|
14 # We need one of Poly/ML, SML/NJ, or MLton. Since we're running a
|
Chris@1706
|
15 # single-file SML program as if it were a script, our order of
|
Chris@1706
|
16 # preference is based on startup speed.
|
Chris@1706
|
17
|
Chris@1706
|
18 if [ -z "$sml" ]; then
|
Chris@1718
|
19 if sml -h 2>&1 | grep -q 'Standard ML of New Jersey'; then
|
Chris@1718
|
20 sml="smlnj"
|
Chris@1706
|
21 # I think there may be a race condition in the poly interpreter's
|
Chris@1706
|
22 # tests for open or closed I/O streams - without the "echo" here,
|
Chris@1706
|
23 # or with stderr redirection, this pipeline will sometimes hang
|
Chris@1718
|
24 elif echo | poly -v 2>/dev/null | grep -q 'Poly/ML'; then
|
Chris@1706
|
25 sml="poly"
|
Chris@1706
|
26 elif mlton 2>&1 | grep -q 'MLton'; then
|
Chris@1706
|
27 sml="mlton"
|
Chris@1706
|
28 else cat 1>&2 <<EOF
|
Chris@1706
|
29
|
Chris@1706
|
30 ERROR: No supported SML compiler or interpreter found
|
Chris@1706
|
31 EOF
|
Chris@1706
|
32 cat <<EOF
|
Chris@1706
|
33
|
Chris@1706
|
34 The Vext external source code manager needs a Standard ML (SML)
|
Chris@1706
|
35 compiler or interpreter to run.
|
Chris@1706
|
36
|
Chris@1706
|
37 Please ensure you have one of the following SML implementations
|
Chris@1718
|
38 installed and present in your PATH, and try again.
|
Chris@1706
|
39
|
Chris@1718
|
40 1. Standard ML of New Jersey
|
Chris@1718
|
41 - often found in a distribution package called: smlnj
|
Chris@1718
|
42 - executable name: sml
|
Chris@1718
|
43
|
Chris@1718
|
44 2. Poly/ML
|
Chris@1706
|
45 - often found in a distribution package called: polyml
|
Chris@1706
|
46 - executable name: poly
|
Chris@1706
|
47
|
Chris@1706
|
48 3. MLton
|
Chris@1706
|
49 - often found in a distribution package called: mlton
|
Chris@1706
|
50 - executable name: mlton
|
Chris@1706
|
51
|
Chris@1706
|
52 EOF
|
Chris@1706
|
53 exit 2
|
Chris@1706
|
54 fi
|
Chris@1706
|
55 fi
|
Chris@1706
|
56
|
Chris@1706
|
57 tmp_sml=$(mktemp /tmp/vext-XXXXXXXX.sml)
|
Chris@1706
|
58 tmp_out=$(mktemp /tmp/vext-XXXXXXXX.bin)
|
Chris@1706
|
59
|
Chris@1706
|
60 trap 'rm -f $tmp_sml $tmp_out' 0
|
Chris@1706
|
61
|
Chris@1706
|
62 arglist=""
|
Chris@1706
|
63 for arg in "$@"; do
|
Chris@1706
|
64 if [ -n "$arglist" ]; then arglist="$arglist,"; fi
|
Chris@1706
|
65 if echo "$arg" | grep -q '[^a-z]' ; then
|
Chris@1706
|
66 arglist="$arglist\"usage\""
|
Chris@1706
|
67 else
|
Chris@1706
|
68 arglist="$arglist\"$arg\""
|
Chris@1706
|
69 fi
|
Chris@1706
|
70 done
|
Chris@1706
|
71
|
Chris@1706
|
72 case "$sml" in
|
Chris@1706
|
73 poly) echo 'use "'"$program"'"; vext ['"$arglist"'];' |
|
Chris@1706
|
74 poly -q --error-exit ;;
|
Chris@1706
|
75 mlton)
|
Chris@1706
|
76 cat "$program" > "$tmp_sml"
|
Chris@1706
|
77 echo 'val _ = main ()' >> "$tmp_sml"
|
Chris@1706
|
78 mlton -output "$tmp_out" "$tmp_sml"
|
Chris@1706
|
79 "$tmp_out" "$@" ;;
|
Chris@1706
|
80 smlnj)
|
Chris@1706
|
81 cat "$program" | (
|
Chris@1706
|
82 cat <<EOF
|
Chris@1706
|
83 val smlrun__cp =
|
Chris@1706
|
84 let val x = !Control.Print.out in
|
Chris@1706
|
85 Control.Print.out := { say = fn _ => (), flush = fn () => () };
|
Chris@1706
|
86 x
|
Chris@1706
|
87 end;
|
Chris@1706
|
88 val smlrun__prev = ref "";
|
Chris@1706
|
89 Control.Print.out := {
|
Chris@1706
|
90 say = fn s =>
|
Chris@1706
|
91 (if String.isSubstring " Error" s
|
Chris@1706
|
92 then (Control.Print.out := smlrun__cp;
|
Chris@1706
|
93 (#say smlrun__cp) (!smlrun__prev);
|
Chris@1706
|
94 (#say smlrun__cp) s)
|
Chris@1706
|
95 else (smlrun__prev := s; ())),
|
Chris@1706
|
96 flush = fn s => ()
|
Chris@1706
|
97 };
|
Chris@1706
|
98 EOF
|
Chris@1706
|
99 cat -
|
Chris@1706
|
100 cat <<EOF
|
Chris@1706
|
101 val _ = vext [$arglist];
|
Chris@1706
|
102 val _ = OS.Process.exit (OS.Process.success);
|
Chris@1706
|
103 EOF
|
Chris@1706
|
104 ) > "$tmp_sml"
|
Chris@1706
|
105 CM_VERBOSE=false sml "$tmp_sml" ;;
|
Chris@1706
|
106 *)
|
Chris@1706
|
107 echo "Unknown SML implementation name: $sml";
|
Chris@1706
|
108 exit 2 ;;
|
Chris@1706
|
109 esac
|
Chris@1706
|
110
|