author | wenzelm |
Thu, 06 Aug 2020 22:58:18 +0200 | |
changeset 72105 | a1fb4d28e609 |
parent 71712 | c6b7f4da67b3 |
child 72598 | d9f2be66ebad |
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 |
||
71706 | 13 |
import scala.annotation.tailrec |
14 |
||
60991 | 15 |
|
16 |
object Bash |
|
17 |
{ |
|
64304 | 18 |
/* concrete syntax */ |
19 |
||
20 |
private def bash_chr(c: Byte): String = |
|
21 |
{ |
|
22 |
val ch = c.toChar |
|
23 |
ch match { |
|
24 |
case '\t' => "$'\\t'" |
|
25 |
case '\n' => "$'\\n'" |
|
26 |
case '\f' => "$'\\f'" |
|
27 |
case '\r' => "$'\\r'" |
|
28 |
case _ => |
|
67200 | 29 |
if (Symbol.is_ascii_letter(ch) || Symbol.is_ascii_digit(ch) || "+,-./:_".contains(ch)) |
64304 | 30 |
Symbol.ascii(ch) |
31 |
else if (c < 0) "$'\\x" + Integer.toHexString(256 + c) + "'" |
|
32 |
else if (c < 16) "$'\\x0" + Integer.toHexString(c) + "'" |
|
33 |
else if (c < 32 || c >= 127) "$'\\x" + Integer.toHexString(c) + "'" |
|
34 |
else "\\" + ch |
|
35 |
} |
|
36 |
} |
|
37 |
||
38 |
def string(s: String): String = |
|
39 |
if (s == "") "\"\"" |
|
71601 | 40 |
else UTF8.bytes(s).iterator.map(bash_chr).mkString |
64304 | 41 |
|
42 |
def strings(ss: List[String]): String = |
|
71601 | 43 |
ss.iterator.map(Bash.string).mkString(" ") |
64304 | 44 |
|
45 |
||
46 |
/* process and result */ |
|
47 |
||
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
|
48 |
def process(script: String, |
62573 | 49 |
cwd: JFile = null, |
62610
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62604
diff
changeset
|
50 |
env: Map[String, String] = Isabelle_System.settings(), |
62602 | 51 |
redirect: Boolean = false, |
52 |
cleanup: () => Unit = () => ()): Process = |
|
53 |
new Process(script, cwd, env, redirect, cleanup) |
|
60991 | 54 |
|
63996 | 55 |
class Process private[Bash]( |
62602 | 56 |
script: String, |
57 |
cwd: JFile, |
|
58 |
env: Map[String, String], |
|
59 |
redirect: Boolean, |
|
60 |
cleanup: () => Unit) |
|
60991 | 61 |
{ |
62604 | 62 |
private val timing_file = Isabelle_System.tmp_file("bash_timing") |
62569 | 63 |
private val timing = Synchronized[Option[Timing]](None) |
65317 | 64 |
def get_timing: Timing = timing.value getOrElse Timing.zero |
62569 | 65 |
|
66 |
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
|
67 |
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
|
68 |
|
62296 | 69 |
private val proc = |
62610
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62604
diff
changeset
|
70 |
Isabelle_System.process( |
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62604
diff
changeset
|
71 |
List(File.platform_path(Path.variable("ISABELLE_BASH_PROCESS")), "-", |
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62604
diff
changeset
|
72 |
File.standard_path(timing_file), "bash", File.standard_path(script_file)), |
4c89504c76fb
more uniform signature for various process invocations;
wenzelm
parents:
62604
diff
changeset
|
73 |
cwd = cwd, env = env, redirect = redirect) |
60991 | 74 |
|
75 |
||
76 |
// channels |
|
77 |
||
78 |
val stdin: BufferedWriter = |
|
79 |
new BufferedWriter(new OutputStreamWriter(proc.getOutputStream, UTF8.charset)) |
|
80 |
||
81 |
val stdout: BufferedReader = |
|
82 |
new BufferedReader(new InputStreamReader(proc.getInputStream, UTF8.charset)) |
|
83 |
||
84 |
val stderr: BufferedReader = |
|
85 |
new BufferedReader(new InputStreamReader(proc.getErrorStream, UTF8.charset)) |
|
86 |
||
87 |
||
88 |
// signals |
|
89 |
||
90 |
private val pid = stdout.readLine |
|
91 |
||
71707 | 92 |
@tailrec private def kill(signal: String, count: Int = 1): Boolean = |
60991 | 93 |
{ |
71706 | 94 |
count <= 0 || |
95 |
{ |
|
71707 | 96 |
Isabelle_System.kill(signal, pid) |
97 |
val running = Isabelle_System.kill("0", pid)._2 == 0 |
|
98 |
if (running) { |
|
71705 | 99 |
Time.seconds(0.1).sleep |
71707 | 100 |
kill(signal, count - 1) |
60991 | 101 |
} |
71706 | 102 |
else false |
60991 | 103 |
} |
104 |
} |
|
105 |
||
71712
c6b7f4da67b3
more robust kill: not always running on Isabelle_Thread (e.g. POSIX_Interrupt handler);
wenzelm
parents:
71708
diff
changeset
|
106 |
def terminate(): Unit = Isabelle_Thread.try_uninterruptible |
62574 | 107 |
{ |
71708 | 108 |
kill("INT", count = 7) && kill("TERM", count = 3) && kill("KILL") |
62574 | 109 |
proc.destroy |
62602 | 110 |
do_cleanup() |
62574 | 111 |
} |
60991 | 112 |
|
71712
c6b7f4da67b3
more robust kill: not always running on Isabelle_Thread (e.g. POSIX_Interrupt handler);
wenzelm
parents:
71708
diff
changeset
|
113 |
def interrupt(): Unit = Isabelle_Thread.try_uninterruptible |
71705 | 114 |
{ |
115 |
Isabelle_System.kill("INT", pid) |
|
116 |
} |
|
117 |
||
60991 | 118 |
|
119 |
// JVM shutdown hook |
|
120 |
||
71712
c6b7f4da67b3
more robust kill: not always running on Isabelle_Thread (e.g. POSIX_Interrupt handler);
wenzelm
parents:
71708
diff
changeset
|
121 |
private val shutdown_hook = Isabelle_Thread.create(() => terminate()) |
60991 | 122 |
|
123 |
try { Runtime.getRuntime.addShutdownHook(shutdown_hook) } |
|
124 |
catch { case _: IllegalStateException => } |
|
125 |
||
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
|
126 |
|
62574 | 127 |
// 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
|
128 |
|
62602 | 129 |
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
|
130 |
{ |
60991 | 131 |
try { Runtime.getRuntime.removeShutdownHook(shutdown_hook) } |
132 |
catch { case _: IllegalStateException => } |
|
133 |
||
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
|
134 |
script_file.delete |
60991 | 135 |
|
62569 | 136 |
timing.change { |
137 |
case None => |
|
62574 | 138 |
if (timing_file.isFile) { |
139 |
val t = |
|
140 |
Word.explode(File.read(timing_file)) match { |
|
63805 | 141 |
case List(Value.Long(elapsed), Value.Long(cpu)) => |
62574 | 142 |
Timing(Time.ms(elapsed), Time.ms(cpu), Time.zero) |
143 |
case _ => Timing.zero |
|
144 |
} |
|
145 |
timing_file.delete |
|
146 |
Some(t) |
|
147 |
} |
|
148 |
else Some(Timing.zero) |
|
62569 | 149 |
case some => some |
150 |
} |
|
62602 | 151 |
|
152 |
cleanup() |
|
62574 | 153 |
} |
62569 | 154 |
|
62574 | 155 |
|
156 |
// join |
|
157 |
||
158 |
def join: Int = |
|
159 |
{ |
|
160 |
val rc = proc.waitFor |
|
62602 | 161 |
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
|
162 |
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
|
163 |
} |
62543 | 164 |
|
165 |
||
62574 | 166 |
// result |
62543 | 167 |
|
168 |
def result( |
|
169 |
progress_stdout: String => Unit = (_: String) => (), |
|
170 |
progress_stderr: String => Unit = (_: String) => (), |
|
171 |
strict: Boolean = true): Process_Result = |
|
172 |
{ |
|
173 |
stdin.close |
|
174 |
||
175 |
val out_lines = |
|
72105 | 176 |
Future.thread("bash_stdout") { File.read_lines(stdout, progress_stdout) } |
62543 | 177 |
val err_lines = |
72105 | 178 |
Future.thread("bash_stderr") { File.read_lines(stderr, progress_stderr) } |
62543 | 179 |
|
180 |
val rc = |
|
181 |
try { join } |
|
62574 | 182 |
catch { case Exn.Interrupt() => terminate(); Exn.Interrupt.return_code } |
62543 | 183 |
if (strict && rc == Exn.Interrupt.return_code) throw Exn.Interrupt() |
184 |
||
65317 | 185 |
Process_Result(rc, out_lines.join, err_lines.join, false, get_timing) |
62543 | 186 |
} |
60991 | 187 |
} |
188 |
} |