author | wenzelm |
Tue, 05 Apr 2016 21:23:32 +0200 | |
changeset 62879 | 4764473c9b8d |
child 62891 | 7a11ea5c9626 |
permissions | -rw-r--r-- |
62879
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
1 |
(* Title: Pure/System/windows/bash.ML |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
3 |
|
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
4 |
GNU bash processes, with propagation of interrupts -- Windows version. |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
5 |
*) |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
6 |
|
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
7 |
signature BASH = |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
8 |
sig |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
9 |
val process: string -> {out: string, err: string, rc: int, terminate: unit -> unit} |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
10 |
end; |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
11 |
|
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
12 |
structure Bash: BASH = |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
13 |
struct |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
14 |
|
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
15 |
val process = uninterruptible (fn restore_attributes => fn script => |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
16 |
let |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
17 |
datatype result = Wait | Signal | Result of int; |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
18 |
val result = Synchronized.var "bash_result" Wait; |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
19 |
|
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
20 |
val id = serial_string (); |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
21 |
val script_path = File.tmp_path (Path.basic ("bash_script" ^ id)); |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
22 |
val out_path = File.tmp_path (Path.basic ("bash_out" ^ id)); |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
23 |
val err_path = File.tmp_path (Path.basic ("bash_err" ^ id)); |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
24 |
val pid_path = File.tmp_path (Path.basic ("bash_pid" ^ id)); |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
25 |
|
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
26 |
fun cleanup_files () = |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
27 |
(try File.rm script_path; |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
28 |
try File.rm out_path; |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
29 |
try File.rm err_path; |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
30 |
try File.rm pid_path); |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
31 |
val _ = cleanup_files (); |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
32 |
|
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
33 |
val system_thread = |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
34 |
Standard_Thread.fork {name = "bash", stack_limit = NONE, interrupts = false} (fn () => |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
35 |
Multithreading.with_attributes Multithreading.private_interrupts (fn _ => |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
36 |
let |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
37 |
val _ = File.write script_path script; |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
38 |
val bash_script = |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
39 |
"bash " ^ File.bash_path script_path ^ |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
40 |
" > " ^ File.bash_path out_path ^ |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
41 |
" 2> " ^ File.bash_path err_path; |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
42 |
val bash_process = getenv_strict "ISABELLE_BASH_PROCESS"; |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
43 |
val rc = |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
44 |
Windows.simpleExecute ("", |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
45 |
quote (ML_System.platform_path bash_process) ^ " " ^ |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
46 |
quote (File.platform_path pid_path) ^ " \"\" bash -c " ^ quote bash_script) |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
47 |
|> Windows.fromStatus |> SysWord.toInt; |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
48 |
val res = if rc = 130 orelse rc = 512 then Signal else Result rc; |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
49 |
in Synchronized.change result (K res) end |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
50 |
handle exn => |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
51 |
(Synchronized.change result (fn Wait => Signal | res => res); Exn.reraise exn))); |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
52 |
|
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
53 |
fun read_pid 0 = NONE |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
54 |
| read_pid count = |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
55 |
(case (Int.fromString (File.read pid_path) handle IO.Io _ => NONE) of |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
56 |
NONE => (OS.Process.sleep (seconds 0.1); read_pid (count - 1)) |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
57 |
| some => some); |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
58 |
|
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
59 |
fun terminate NONE = () |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
60 |
| terminate (SOME pid) = |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
61 |
let |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
62 |
fun kill s = |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
63 |
let |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
64 |
val cmd = getenv_strict "CYGWIN_ROOT" ^ "\\bin\\bash.exe"; |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
65 |
val arg = "kill -" ^ s ^ " -" ^ string_of_int pid; |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
66 |
in |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
67 |
OS.Process.isSuccess (Windows.simpleExecute ("", quote cmd ^ " -c " ^ quote arg)) |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
68 |
handle OS.SysErr _ => false |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
69 |
end; |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
70 |
|
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
71 |
fun multi_kill count s = |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
72 |
count = 0 orelse |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
73 |
(kill s; kill "0") andalso |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
74 |
(OS.Process.sleep (seconds 0.1); multi_kill (count - 1) s); |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
75 |
val _ = |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
76 |
multi_kill 10 "INT" andalso |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
77 |
multi_kill 10 "TERM" andalso |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
78 |
multi_kill 10 "KILL"; |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
79 |
in () end; |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
80 |
|
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
81 |
fun cleanup () = |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
82 |
(Standard_Thread.interrupt_unsynchronized system_thread; |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
83 |
cleanup_files ()); |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
84 |
in |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
85 |
let |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
86 |
val _ = |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
87 |
restore_attributes (fn () => |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
88 |
Synchronized.guarded_access result (fn Wait => NONE | x => SOME ((), x))) (); |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
89 |
|
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
90 |
val out = the_default "" (try File.read out_path); |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
91 |
val err = the_default "" (try File.read err_path); |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
92 |
val rc = (case Synchronized.value result of Signal => Exn.interrupt () | Result rc => rc); |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
93 |
val pid = read_pid 1; |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
94 |
val _ = cleanup (); |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
95 |
in {out = out, err = err, rc = rc, terminate = fn () => terminate pid} end |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
96 |
handle exn => (terminate (read_pid 10); cleanup (); Exn.reraise exn) |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
97 |
end); |
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
98 |
|
4764473c9b8d
back to static conditional compilation -- simplified bootstrap;
wenzelm
parents:
diff
changeset
|
99 |
end; |