author | wenzelm |
Sat, 18 Mar 2017 21:40:47 +0100 | |
changeset 65316 | c0fb8405416c |
parent 64904 | 14c760e0e1cf |
child 65317 | b9f5cd845616 |
permissions | -rw-r--r-- |
62584 | 1 |
/* Title: Pure/System/bash.scala |
60991 | 2 |
Author: Makarius |
3 |
||
4 |
GNU bash processes, with propagation of interrupts. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
10 |
import java.io.{File => JFile, BufferedReader, InputStreamReader, |
|
11 |
BufferedWriter, OutputStreamWriter} |
|
12 |
||
13 |
||
14 |
object Bash |
|
15 |
{ |
|
64304 | 16 |
/* concrete syntax */ |
17 |
||
18 |
private def bash_chr(c: Byte): String = |
|
19 |
{ |
|
20 |
val ch = c.toChar |
|
21 |
ch match { |
|
22 |
case '\t' => "$'\\t'" |
|
23 |
case '\n' => "$'\\n'" |
|
24 |
case '\f' => "$'\\f'" |
|
25 |
case '\r' => "$'\\r'" |
|
26 |
case _ => |
|
64904 | 27 |
if (Symbol.is_ascii_letter(ch) || Symbol.is_ascii_digit(ch) || "+-./:_".contains(ch)) |
64304 | 28 |
Symbol.ascii(ch) |
29 |
else if (c < 0) "$'\\x" + Integer.toHexString(256 + c) + "'" |
|
30 |
else if (c < 16) "$'\\x0" + Integer.toHexString(c) + "'" |
|
31 |
else if (c < 32 || c >= 127) "$'\\x" + Integer.toHexString(c) + "'" |
|
32 |
else "\\" + ch |
|
33 |
} |
|
34 |
} |
|
35 |
||
36 |
def string(s: String): String = |
|
37 |
if (s == "") "\"\"" |
|
38 |
else UTF8.bytes(s).iterator.map(bash_chr(_)).mkString |
|
39 |
||
40 |
def strings(ss: List[String]): String = |
|
41 |
ss.iterator.map(Bash.string(_)).mkString(" ") |
|
42 |
||
43 |
||
44 |
/* process and result */ |
|
45 |
||
62543 | 46 |
private class Limited_Progress(proc: Process, progress_limit: Option[Long]) |
47 |
{ |
|
48 |
private var count = 0L |
|
49 |
def apply(progress: String => Unit)(line: String): Unit = synchronized { |
|
50 |
progress(line) |
|
51 |
count = count + line.length + 1 |
|
52 |
progress_limit match { |
|
53 |
case Some(limit) if count > limit => proc.terminate |
|
54 |
case _ => |
|
55 |
} |
|
56 |
} |
|
57 |
} |
|
58 |
||
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
59 |
def process(script: String, |
62573 | 60 |
cwd: JFile = null, |
62610
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62604
diff
changeset
|
61 |
env: Map[String, String] = Isabelle_System.settings(), |
62602 | 62 |
redirect: Boolean = false, |
63 |
cleanup: () => Unit = () => ()): Process = |
|
64 |
new Process(script, cwd, env, redirect, cleanup) |
|
60991 | 65 |
|
63996 | 66 |
class Process private[Bash]( |
62602 | 67 |
script: String, |
68 |
cwd: JFile, |
|
69 |
env: Map[String, String], |
|
70 |
redirect: Boolean, |
|
71 |
cleanup: () => Unit) |
|
60991 | 72 |
{ |
62604 | 73 |
private val timing_file = Isabelle_System.tmp_file("bash_timing") |
62569 | 74 |
private val timing = Synchronized[Option[Timing]](None) |
75 |
||
76 |
private val script_file = Isabelle_System.tmp_file("bash_script") |
|
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
77 |
File.write(script_file, script) |
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
78 |
|
62296 | 79 |
private val proc = |
62610
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62604
diff
changeset
|
80 |
Isabelle_System.process( |
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62604
diff
changeset
|
81 |
List(File.platform_path(Path.variable("ISABELLE_BASH_PROCESS")), "-", |
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62604
diff
changeset
|
82 |
File.standard_path(timing_file), "bash", File.standard_path(script_file)), |
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62604
diff
changeset
|
83 |
cwd = cwd, env = env, redirect = redirect) |
60991 | 84 |
|
85 |
||
86 |
// channels |
|
87 |
||
88 |
val stdin: BufferedWriter = |
|
89 |
new BufferedWriter(new OutputStreamWriter(proc.getOutputStream, UTF8.charset)) |
|
90 |
||
91 |
val stdout: BufferedReader = |
|
92 |
new BufferedReader(new InputStreamReader(proc.getInputStream, UTF8.charset)) |
|
93 |
||
94 |
val stderr: BufferedReader = |
|
95 |
new BufferedReader(new InputStreamReader(proc.getErrorStream, UTF8.charset)) |
|
96 |
||
97 |
||
98 |
// signals |
|
99 |
||
100 |
private val pid = stdout.readLine |
|
101 |
||
62558
c46418f12ee1
clarified process interrupt: exactly one signal (like thread interrupt);
wenzelm
parents:
62545
diff
changeset
|
102 |
def interrupt() |
c46418f12ee1
clarified process interrupt: exactly one signal (like thread interrupt);
wenzelm
parents:
62545
diff
changeset
|
103 |
{ Exn.Interrupt.postpone { Isabelle_System.kill("INT", pid) } } |
c46418f12ee1
clarified process interrupt: exactly one signal (like thread interrupt);
wenzelm
parents:
62545
diff
changeset
|
104 |
|
60991 | 105 |
private def kill(signal: String): Boolean = |
61025 | 106 |
Exn.Interrupt.postpone { |
107 |
Isabelle_System.kill(signal, pid) |
|
108 |
Isabelle_System.kill("0", pid)._2 == 0 } getOrElse true |
|
60991 | 109 |
|
110 |
private def multi_kill(signal: String): Boolean = |
|
111 |
{ |
|
112 |
var running = true |
|
113 |
var count = 10 |
|
114 |
while (running && count > 0) { |
|
115 |
if (kill(signal)) { |
|
116 |
Exn.Interrupt.postpone { |
|
117 |
Thread.sleep(100) |
|
118 |
count -= 1 |
|
119 |
} |
|
120 |
} |
|
121 |
else running = false |
|
122 |
} |
|
123 |
running |
|
124 |
} |
|
125 |
||
62574 | 126 |
def terminate() |
127 |
{ |
|
128 |
multi_kill("INT") && multi_kill("TERM") && kill("KILL") |
|
129 |
proc.destroy |
|
62602 | 130 |
do_cleanup() |
62574 | 131 |
} |
60991 | 132 |
|
133 |
||
134 |
// JVM shutdown hook |
|
135 |
||
136 |
private val shutdown_hook = new Thread { override def run = terminate() } |
|
137 |
||
138 |
try { Runtime.getRuntime.addShutdownHook(shutdown_hook) } |
|
139 |
catch { case _: IllegalStateException => } |
|
140 |
||
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
141 |
|
62574 | 142 |
// cleanup |
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
143 |
|
62602 | 144 |
private def do_cleanup() |
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
145 |
{ |
60991 | 146 |
try { Runtime.getRuntime.removeShutdownHook(shutdown_hook) } |
147 |
catch { case _: IllegalStateException => } |
|
148 |
||
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
149 |
script_file.delete |
60991 | 150 |
|
62569 | 151 |
timing.change { |
152 |
case None => |
|
62574 | 153 |
if (timing_file.isFile) { |
154 |
val t = |
|
155 |
Word.explode(File.read(timing_file)) match { |
|
63805 | 156 |
case List(Value.Long(elapsed), Value.Long(cpu)) => |
62574 | 157 |
Timing(Time.ms(elapsed), Time.ms(cpu), Time.zero) |
158 |
case _ => Timing.zero |
|
159 |
} |
|
160 |
timing_file.delete |
|
161 |
Some(t) |
|
162 |
} |
|
163 |
else Some(Timing.zero) |
|
62569 | 164 |
case some => some |
165 |
} |
|
62602 | 166 |
|
167 |
cleanup() |
|
62574 | 168 |
} |
62569 | 169 |
|
62574 | 170 |
|
171 |
// join |
|
172 |
||
173 |
def join: Int = |
|
174 |
{ |
|
175 |
val rc = proc.waitFor |
|
62602 | 176 |
do_cleanup() |
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
177 |
rc |
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62543
diff
changeset
|
178 |
} |
62543 | 179 |
|
180 |
||
62574 | 181 |
// result |
62543 | 182 |
|
183 |
def result( |
|
184 |
progress_stdout: String => Unit = (_: String) => (), |
|
185 |
progress_stderr: String => Unit = (_: String) => (), |
|
186 |
progress_limit: Option[Long] = None, |
|
187 |
strict: Boolean = true): Process_Result = |
|
188 |
{ |
|
189 |
stdin.close |
|
190 |
||
191 |
val limited = new Limited_Progress(this, progress_limit) |
|
192 |
val out_lines = |
|
193 |
Future.thread("bash_stdout") { File.read_lines(stdout, limited(progress_stdout)) } |
|
194 |
val err_lines = |
|
195 |
Future.thread("bash_stderr") { File.read_lines(stderr, limited(progress_stderr)) } |
|
196 |
||
197 |
val rc = |
|
198 |
try { join } |
|
62574 | 199 |
catch { case Exn.Interrupt() => terminate(); Exn.Interrupt.return_code } |
62543 | 200 |
if (strict && rc == Exn.Interrupt.return_code) throw Exn.Interrupt() |
201 |
||
62574 | 202 |
Process_Result(rc, out_lines.join, err_lines.join, false, timing.value getOrElse Timing.zero) |
62543 | 203 |
} |
60991 | 204 |
} |
205 |
} |