author | blanchet |
Fri, 01 Jul 2011 15:53:38 +0200 | |
changeset 43627 | ecd4bb7a8bc0 |
parent 43606 | e1a09c2a6248 |
child 43660 | bfc0bb115fa1 |
permissions | -rw-r--r-- |
30175 | 1 |
/* Title: Pure/System/isabelle_system.scala |
27919 | 2 |
Author: Makarius |
3 |
||
34201
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
4 |
Isabelle system support (settings environment etc.). |
27919 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
43520
cec9b95fa35d
explicit import java.lang.System to prevent odd scope problems;
wenzelm
parents:
43514
diff
changeset
|
9 |
import java.lang.System |
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
10 |
import java.util.regex.Pattern |
31820 | 11 |
import java.util.Locale |
39576
48baf61cb888
refined ML/Scala bash wrapper, based on more general lib/scripts/process;
wenzelm
parents:
39574
diff
changeset
|
12 |
import java.io.{InputStream, FileInputStream, OutputStream, FileOutputStream, File, |
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
13 |
BufferedReader, InputStreamReader, BufferedWriter, OutputStreamWriter, IOException} |
34024 | 14 |
import java.awt.{GraphicsEnvironment, Font} |
37367 | 15 |
import java.awt.font.TextAttribute |
28063 | 16 |
|
17 |
import scala.io.Source |
|
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
18 |
import scala.util.matching.Regex |
34163 | 19 |
import scala.collection.mutable |
27936 | 20 |
|
27919 | 21 |
|
43514
45cf8d5e109a
lazy Isabelle_System.default supports implicit boot;
wenzelm
parents:
43484
diff
changeset
|
22 |
object Isabelle_System |
45cf8d5e109a
lazy Isabelle_System.default supports implicit boot;
wenzelm
parents:
43484
diff
changeset
|
23 |
{ |
45cf8d5e109a
lazy Isabelle_System.default supports implicit boot;
wenzelm
parents:
43484
diff
changeset
|
24 |
lazy val default: Isabelle_System = new Isabelle_System |
45cf8d5e109a
lazy Isabelle_System.default supports implicit boot;
wenzelm
parents:
43484
diff
changeset
|
25 |
} |
45cf8d5e109a
lazy Isabelle_System.default supports implicit boot;
wenzelm
parents:
43484
diff
changeset
|
26 |
|
37013
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
27 |
class Isabelle_System(this_isabelle_home: String) extends Standard_System |
31498 | 28 |
{ |
37013
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
29 |
def this() = this(null) |
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
30 |
|
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
31 |
|
31796 | 32 |
/** Isabelle environment **/ |
33 |
||
37013
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
34 |
/* |
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
35 |
isabelle_home precedence: |
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
36 |
(1) this_isabelle_home as explicit argument |
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
37 |
(2) ISABELLE_HOME process environment variable (e.g. inherited from running isabelle tool) |
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
38 |
(3) isabelle.home system property (e.g. via JVM application boot process) |
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
39 |
*/ |
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
40 |
|
31498 | 41 |
private val environment: Map[String, String] = |
42 |
{ |
|
36011
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents:
35023
diff
changeset
|
43 |
import scala.collection.JavaConversions._ |
31498 | 44 |
|
43520
cec9b95fa35d
explicit import java.lang.System to prevent odd scope problems;
wenzelm
parents:
43514
diff
changeset
|
45 |
val env0 = Map(System.getenv.toList: _*) + |
36193
067a01827fca
improved ISABELLE_JAVA, based on THIS_JAVA of the actually running JVM;
wenzelm
parents:
36136
diff
changeset
|
46 |
("THIS_JAVA" -> this_java()) |
27919 | 47 |
|
31927
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
48 |
val isabelle_home = |
37013
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
49 |
if (this_isabelle_home != null) this_isabelle_home |
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
50 |
else |
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
51 |
env0.get("ISABELLE_HOME") match { |
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
52 |
case None | Some("") => |
43520
cec9b95fa35d
explicit import java.lang.System to prevent odd scope problems;
wenzelm
parents:
43514
diff
changeset
|
53 |
val path = System.getProperty("isabelle.home") |
37013
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
54 |
if (path == null || path == "") error("Unknown Isabelle home directory") |
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
55 |
else path |
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
56 |
case Some(path) => path |
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
57 |
} |
29177 | 58 |
|
34201
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
59 |
Standard_System.with_tmp_file("settings") { dump => |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
60 |
val shell_prefix = |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
61 |
if (Platform.is_windows) List(platform_root + "\\bin\\bash", "-l") else Nil |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
62 |
val cmdline = |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
63 |
shell_prefix ::: List(isabelle_home + "/bin/isabelle", "getenv", "-d", dump.toString) |
34258 | 64 |
val (output, rc) = Standard_System.raw_exec(null, env0, true, cmdline: _*) |
34201
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
65 |
if (rc != 0) error(output) |
31498 | 66 |
|
34201
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
67 |
val entries = |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
68 |
for (entry <- Source.fromFile(dump).mkString split "\0" if entry != "") yield { |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
69 |
val i = entry.indexOf('=') |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
70 |
if (i <= 0) (entry -> "") |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
71 |
else (entry.substring(0, i) -> entry.substring(i + 1)) |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
72 |
} |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
73 |
Map(entries: _*) + |
43520
cec9b95fa35d
explicit import java.lang.System to prevent odd scope problems;
wenzelm
parents:
43514
diff
changeset
|
74 |
("HOME" -> System.getenv("HOME")) + |
cec9b95fa35d
explicit import java.lang.System to prevent odd scope problems;
wenzelm
parents:
43514
diff
changeset
|
75 |
("PATH" -> System.getenv("PATH")) |
34201
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
76 |
} |
27953 | 77 |
} |
27919 | 78 |
|
31796 | 79 |
|
80 |
/* getenv */ |
|
81 |
||
31498 | 82 |
def getenv(name: String): String = |
83 |
environment.getOrElse(if (name == "HOME") "HOME_JVM" else name, "") |
|
84 |
||
85 |
def getenv_strict(name: String): String = |
|
86 |
{ |
|
31234
6ce6801129de
getenv_strict needs to be based on getenv (accidentally broken in 0e88d33e8d19);
wenzelm
parents:
30175
diff
changeset
|
87 |
val value = getenv(name) |
27993
6dd90ef9f927
simplified exceptions: use plain error function / RuntimeException;
wenzelm
parents:
27974
diff
changeset
|
88 |
if (value != "") value else error("Undefined environment variable: " + name) |
27919 | 89 |
} |
90 |
||
37058
c47653f3ec14
rendering information and style sheets via settings;
wenzelm
parents:
37013
diff
changeset
|
91 |
override def toString = getenv_strict("ISABELLE_HOME") |
31703 | 92 |
|
27936 | 93 |
|
31796 | 94 |
|
43606 | 95 |
/** file-system operations **/ |
31796 | 96 |
|
43606 | 97 |
/* path specifications */ |
36136
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
98 |
|
43606 | 99 |
def standard_path(path: Path): String = path.expand(getenv_strict).implode |
31796 | 100 |
|
43606 | 101 |
def platform_path(path: Path): String = jvm_path(standard_path(path)) |
102 |
def platform_file(path: Path) = new File(platform_path(path)) |
|
29152 | 103 |
|
27953 | 104 |
|
36991 | 105 |
/* try_read */ |
106 |
||
43606 | 107 |
def try_read(paths: Seq[Path]): String = |
36991 | 108 |
{ |
37058
c47653f3ec14
rendering information and style sheets via settings;
wenzelm
parents:
37013
diff
changeset
|
109 |
val buf = new StringBuilder |
c47653f3ec14
rendering information and style sheets via settings;
wenzelm
parents:
37013
diff
changeset
|
110 |
for { |
c47653f3ec14
rendering information and style sheets via settings;
wenzelm
parents:
37013
diff
changeset
|
111 |
path <- paths |
c47653f3ec14
rendering information and style sheets via settings;
wenzelm
parents:
37013
diff
changeset
|
112 |
file = platform_file(path) if file.isFile |
c47653f3ec14
rendering information and style sheets via settings;
wenzelm
parents:
37013
diff
changeset
|
113 |
c <- (Source.fromFile(file) ++ Iterator.single('\n')) |
c47653f3ec14
rendering information and style sheets via settings;
wenzelm
parents:
37013
diff
changeset
|
114 |
} buf.append(c) |
c47653f3ec14
rendering information and style sheets via settings;
wenzelm
parents:
37013
diff
changeset
|
115 |
buf.toString |
36991 | 116 |
} |
117 |
||
118 |
||
31436 | 119 |
/* source files */ |
120 |
||
121 |
private def try_file(file: File) = if (file.isFile) Some(file) else None |
|
122 |
||
43606 | 123 |
def source_file(path: Path): Option[File] = |
31498 | 124 |
{ |
43606 | 125 |
if (path.is_absolute || path.is_current) |
31436 | 126 |
try_file(platform_file(path)) |
127 |
else { |
|
43606 | 128 |
val pure_file = platform_file(Path.explode("~~/src/Pure") + path) |
31436 | 129 |
if (pure_file.isFile) Some(pure_file) |
130 |
else if (getenv("ML_SOURCES") != "") |
|
43606 | 131 |
try_file(platform_file(Path.explode("$ML_SOURCES") + path)) |
31436 | 132 |
else None |
133 |
} |
|
134 |
} |
|
135 |
||
136 |
||
32450 | 137 |
|
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
138 |
/** external processes **/ |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
139 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
140 |
/* plain execute */ |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
141 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
142 |
def execute(redirect: Boolean, args: String*): Process = |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
143 |
{ |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
144 |
val cmdline = |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
145 |
if (Platform.is_windows) List(platform_root + "\\bin\\env.exe") ++ args |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
146 |
else args |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
147 |
Standard_System.raw_execute(null, environment, redirect, cmdline: _*) |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
148 |
} |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
149 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
150 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
151 |
/* managed process */ |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
152 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
153 |
class Managed_Process(redirect: Boolean, args: String*) |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
154 |
{ |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
155 |
private val params = |
43606 | 156 |
List(standard_path(Path.explode("~~/lib/scripts/process")), "group", "-", "no_script") |
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
157 |
private val proc = execute(redirect, (params ++ args):_*) |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
158 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
159 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
160 |
// channels |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
161 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
162 |
val stdin: BufferedWriter = |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
163 |
new BufferedWriter(new OutputStreamWriter(proc.getOutputStream, Standard_System.charset)) |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
164 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
165 |
val stdout: BufferedReader = |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
166 |
new BufferedReader(new InputStreamReader(proc.getInputStream, Standard_System.charset)) |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
167 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
168 |
val stderr: BufferedReader = |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
169 |
new BufferedReader(new InputStreamReader(proc.getErrorStream, Standard_System.charset)) |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
170 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
171 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
172 |
// signals |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
173 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
174 |
private val pid = stdout.readLine |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
175 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
176 |
private def kill(signal: String): Boolean = |
39584
f2a10986e85a
more robust Managed_Process.kill: check after sending signal;
wenzelm
parents:
39583
diff
changeset
|
177 |
{ |
f2a10986e85a
more robust Managed_Process.kill: check after sending signal;
wenzelm
parents:
39583
diff
changeset
|
178 |
execute(true, "kill", "-" + signal, "-" + pid).waitFor |
f2a10986e85a
more robust Managed_Process.kill: check after sending signal;
wenzelm
parents:
39583
diff
changeset
|
179 |
execute(true, "kill", "-0", "-" + pid).waitFor == 0 |
f2a10986e85a
more robust Managed_Process.kill: check after sending signal;
wenzelm
parents:
39583
diff
changeset
|
180 |
} |
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
181 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
182 |
private def multi_kill(signal: String): Boolean = |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
183 |
{ |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
184 |
var running = true |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
185 |
var count = 10 |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
186 |
while (running && count > 0) { |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
187 |
if (kill(signal)) { |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
188 |
Thread.sleep(100) |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
189 |
count -= 1 |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
190 |
} |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
191 |
else running = false |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
192 |
} |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
193 |
running |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
194 |
} |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
195 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
196 |
def interrupt() { multi_kill("INT") } |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
197 |
def terminate() { multi_kill("INT") && multi_kill("TERM") && kill("KILL"); proc.destroy } |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
198 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
199 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
200 |
// JVM shutdown hook |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
201 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
202 |
private val shutdown_hook = new Thread { override def run = terminate() } |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
203 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
204 |
try { Runtime.getRuntime.addShutdownHook(shutdown_hook) } |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
205 |
catch { case _: IllegalStateException => } |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
206 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
207 |
private def cleanup() = |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
208 |
try { Runtime.getRuntime.removeShutdownHook(shutdown_hook) } |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
209 |
catch { case _: IllegalStateException => } |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
210 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
211 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
212 |
/* result */ |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
213 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
214 |
def join: Int = { val rc = proc.waitFor; cleanup(); rc } |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
215 |
} |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
216 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
217 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
218 |
/* bash */ |
31796 | 219 |
|
39581
430ff865089b
refined Isabelle_System.bash_output: pass pid via stdout, separate stdout/stderr;
wenzelm
parents:
39576
diff
changeset
|
220 |
def bash(script: String): (String, String, Int) = |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
221 |
{ |
34201
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
222 |
Standard_System.with_tmp_file("isabelle_script") { script_file => |
39581
430ff865089b
refined Isabelle_System.bash_output: pass pid via stdout, separate stdout/stderr;
wenzelm
parents:
39576
diff
changeset
|
223 |
Standard_System.write_file(script_file, script) |
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
224 |
val proc = new Managed_Process(false, "bash", posix_path(script_file.getPath)) |
39581
430ff865089b
refined Isabelle_System.bash_output: pass pid via stdout, separate stdout/stderr;
wenzelm
parents:
39576
diff
changeset
|
225 |
|
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
226 |
proc.stdin.close |
39586
ea8f3ea13a95
eliminated Simple_Thread shorthands that can overlap with full version;
wenzelm
parents:
39584
diff
changeset
|
227 |
val stdout = Simple_Thread.future("bash_stdout") { Standard_System.slurp(proc.stdout) } |
ea8f3ea13a95
eliminated Simple_Thread shorthands that can overlap with full version;
wenzelm
parents:
39584
diff
changeset
|
228 |
val stderr = Simple_Thread.future("bash_stderr") { Standard_System.slurp(proc.stderr) } |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
229 |
|
39581
430ff865089b
refined Isabelle_System.bash_output: pass pid via stdout, separate stdout/stderr;
wenzelm
parents:
39576
diff
changeset
|
230 |
val rc = |
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
231 |
try { proc.join } |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
232 |
catch { case e: InterruptedException => Thread.interrupted; proc.terminate; 130 } |
39581
430ff865089b
refined Isabelle_System.bash_output: pass pid via stdout, separate stdout/stderr;
wenzelm
parents:
39576
diff
changeset
|
233 |
(stdout.join, stderr.join, rc) |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
234 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
235 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
236 |
|
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
237 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
238 |
/* system tools */ |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
239 |
|
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
240 |
def isabelle_tool(name: String, args: String*): (String, Int) = |
31498 | 241 |
{ |
34200 | 242 |
getenv_strict("ISABELLE_TOOLS").split(":").find { dir => |
43606 | 243 |
val file = platform_file(Path.explode(dir) + Path.basic(name)) |
42124 | 244 |
try { |
245 |
file.isFile && file.canRead && file.canExecute && |
|
246 |
!name.endsWith("~") && !name.endsWith(".orig") |
|
247 |
} |
|
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
248 |
catch { case _: SecurityException => false } |
34200 | 249 |
} match { |
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
250 |
case Some(dir) => |
43606 | 251 |
val file = standard_path(Path.explode(dir) + Path.basic(name)) |
252 |
Standard_System.process_output(execute(true, (List(file) ++ args): _*)) |
|
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
253 |
case None => ("Unknown Isabelle tool: " + name, 2) |
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
254 |
} |
28063 | 255 |
} |
256 |
||
257 |
||
258 |
/* named pipes */ |
|
259 |
||
39520
bad14b7d0520
Isabelle_System.mk_fifo: more robust enumeration of unique names, based on persisting JVM pid (parent of shell process);
wenzelm
parents:
38371
diff
changeset
|
260 |
private var fifo_count: Long = 0 |
bad14b7d0520
Isabelle_System.mk_fifo: more robust enumeration of unique names, based on persisting JVM pid (parent of shell process);
wenzelm
parents:
38371
diff
changeset
|
261 |
private def next_fifo(): String = synchronized { |
bad14b7d0520
Isabelle_System.mk_fifo: more robust enumeration of unique names, based on persisting JVM pid (parent of shell process);
wenzelm
parents:
38371
diff
changeset
|
262 |
require(fifo_count < java.lang.Long.MAX_VALUE) |
bad14b7d0520
Isabelle_System.mk_fifo: more robust enumeration of unique names, based on persisting JVM pid (parent of shell process);
wenzelm
parents:
38371
diff
changeset
|
263 |
fifo_count += 1 |
bad14b7d0520
Isabelle_System.mk_fifo: more robust enumeration of unique names, based on persisting JVM pid (parent of shell process);
wenzelm
parents:
38371
diff
changeset
|
264 |
fifo_count.toString |
bad14b7d0520
Isabelle_System.mk_fifo: more robust enumeration of unique names, based on persisting JVM pid (parent of shell process);
wenzelm
parents:
38371
diff
changeset
|
265 |
} |
bad14b7d0520
Isabelle_System.mk_fifo: more robust enumeration of unique names, based on persisting JVM pid (parent of shell process);
wenzelm
parents:
38371
diff
changeset
|
266 |
|
31498 | 267 |
def mk_fifo(): String = |
268 |
{ |
|
39529
4e466a5f67f3
simplified Isabelle_System.mk_fifo: inlined script, append PPID and PID uniformly;
wenzelm
parents:
39523
diff
changeset
|
269 |
val i = next_fifo() |
4e466a5f67f3
simplified Isabelle_System.mk_fifo: inlined script, append PPID and PID uniformly;
wenzelm
parents:
39523
diff
changeset
|
270 |
val script = |
4e466a5f67f3
simplified Isabelle_System.mk_fifo: inlined script, append PPID and PID uniformly;
wenzelm
parents:
39523
diff
changeset
|
271 |
"FIFO=\"/tmp/isabelle-fifo-${PPID}-$$-" + i + "\"\n" + |
39699 | 272 |
"echo -n \"$FIFO\"\n" + |
273 |
"mkfifo -m 600 \"$FIFO\"\n" |
|
39581
430ff865089b
refined Isabelle_System.bash_output: pass pid via stdout, separate stdout/stderr;
wenzelm
parents:
39576
diff
changeset
|
274 |
val (out, err, rc) = bash(script) |
39699 | 275 |
if (rc == 0) out else error(err.trim) |
28063 | 276 |
} |
277 |
||
39574
91b23d141dbf
more robust Isabelle_System.rm_fifo: avoid external bash invocation, which might not work in JVM shutdown phase (due to Runtime.addShutdownHook);
wenzelm
parents:
39529
diff
changeset
|
278 |
def rm_fifo(fifo: String): Boolean = |
91b23d141dbf
more robust Isabelle_System.rm_fifo: avoid external bash invocation, which might not work in JVM shutdown phase (due to Runtime.addShutdownHook);
wenzelm
parents:
39529
diff
changeset
|
279 |
(new File(jvm_path(if (Platform.is_windows) fifo + ".lnk" else fifo))).delete |
28063 | 280 |
|
38371
5b615a4a3a68
do not buffer fifo streams here -- done in Isabelle_Process;
wenzelm
parents:
38255
diff
changeset
|
281 |
def fifo_input_stream(fifo: String): InputStream = |
31498 | 282 |
{ |
38371
5b615a4a3a68
do not buffer fifo streams here -- done in Isabelle_Process;
wenzelm
parents:
38255
diff
changeset
|
283 |
if (Platform.is_windows) { // Cygwin fifo as Windows/Java input stream |
43606 | 284 |
val proc = execute(false, standard_path(Path.explode("~~/lib/scripts/raw_dump")), fifo, "-") |
38371
5b615a4a3a68
do not buffer fifo streams here -- done in Isabelle_Process;
wenzelm
parents:
38255
diff
changeset
|
285 |
proc.getOutputStream.close |
5b615a4a3a68
do not buffer fifo streams here -- done in Isabelle_Process;
wenzelm
parents:
38255
diff
changeset
|
286 |
proc.getErrorStream.close |
5b615a4a3a68
do not buffer fifo streams here -- done in Isabelle_Process;
wenzelm
parents:
38255
diff
changeset
|
287 |
proc.getInputStream |
5b615a4a3a68
do not buffer fifo streams here -- done in Isabelle_Process;
wenzelm
parents:
38255
diff
changeset
|
288 |
} |
5b615a4a3a68
do not buffer fifo streams here -- done in Isabelle_Process;
wenzelm
parents:
38255
diff
changeset
|
289 |
else new FileInputStream(fifo) |
29177 | 290 |
} |
28063 | 291 |
|
38371
5b615a4a3a68
do not buffer fifo streams here -- done in Isabelle_Process;
wenzelm
parents:
38255
diff
changeset
|
292 |
def fifo_output_stream(fifo: String): OutputStream = |
38253
3d4e521014f7
Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents:
37367
diff
changeset
|
293 |
{ |
38371
5b615a4a3a68
do not buffer fifo streams here -- done in Isabelle_Process;
wenzelm
parents:
38255
diff
changeset
|
294 |
if (Platform.is_windows) { // Cygwin fifo as Windows/Java output stream |
43606 | 295 |
val proc = execute(false, standard_path(Path.explode("~~/lib/scripts/raw_dump")), "-", fifo) |
38371
5b615a4a3a68
do not buffer fifo streams here -- done in Isabelle_Process;
wenzelm
parents:
38255
diff
changeset
|
296 |
proc.getInputStream.close |
5b615a4a3a68
do not buffer fifo streams here -- done in Isabelle_Process;
wenzelm
parents:
38255
diff
changeset
|
297 |
proc.getErrorStream.close |
5b615a4a3a68
do not buffer fifo streams here -- done in Isabelle_Process;
wenzelm
parents:
38255
diff
changeset
|
298 |
val out = proc.getOutputStream |
5b615a4a3a68
do not buffer fifo streams here -- done in Isabelle_Process;
wenzelm
parents:
38255
diff
changeset
|
299 |
new OutputStream { |
5b615a4a3a68
do not buffer fifo streams here -- done in Isabelle_Process;
wenzelm
parents:
38255
diff
changeset
|
300 |
override def close() { out.close(); proc.waitFor() } |
5b615a4a3a68
do not buffer fifo streams here -- done in Isabelle_Process;
wenzelm
parents:
38255
diff
changeset
|
301 |
override def flush() { out.flush() } |
5b615a4a3a68
do not buffer fifo streams here -- done in Isabelle_Process;
wenzelm
parents:
38255
diff
changeset
|
302 |
override def write(b: Array[Byte]) { out.write(b) } |
5b615a4a3a68
do not buffer fifo streams here -- done in Isabelle_Process;
wenzelm
parents:
38255
diff
changeset
|
303 |
override def write(b: Array[Byte], off: Int, len: Int) { out.write(b, off, len) } |
5b615a4a3a68
do not buffer fifo streams here -- done in Isabelle_Process;
wenzelm
parents:
38255
diff
changeset
|
304 |
override def write(b: Int) { out.write(b) } |
38253
3d4e521014f7
Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents:
37367
diff
changeset
|
305 |
} |
38371
5b615a4a3a68
do not buffer fifo streams here -- done in Isabelle_Process;
wenzelm
parents:
38255
diff
changeset
|
306 |
} |
5b615a4a3a68
do not buffer fifo streams here -- done in Isabelle_Process;
wenzelm
parents:
38255
diff
changeset
|
307 |
else new FileOutputStream(fifo) |
38253
3d4e521014f7
Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents:
37367
diff
changeset
|
308 |
} |
3d4e521014f7
Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents:
37367
diff
changeset
|
309 |
|
29152 | 310 |
|
32450 | 311 |
|
31796 | 312 |
/** Isabelle resources **/ |
313 |
||
32328 | 314 |
/* components */ |
315 |
||
316 |
def components(): List[String] = |
|
37058
c47653f3ec14
rendering information and style sheets via settings;
wenzelm
parents:
37013
diff
changeset
|
317 |
getenv_strict("ISABELLE_COMPONENTS").split(":").toList |
32328 | 318 |
|
319 |
||
29152 | 320 |
/* find logics */ |
321 |
||
31498 | 322 |
def find_logics(): List[String] = |
323 |
{ |
|
29152 | 324 |
val ml_ident = getenv_strict("ML_IDENTIFIER") |
34163 | 325 |
val logics = new mutable.ListBuffer[String] |
29152 | 326 |
for (dir <- getenv_strict("ISABELLE_PATH").split(":")) { |
43606 | 327 |
val files = platform_file(Path.explode(dir) + Path.explode(ml_ident)).listFiles() |
29152 | 328 |
if (files != null) { |
329 |
for (file <- files if file.isFile) logics += file.getName |
|
330 |
} |
|
331 |
} |
|
36012 | 332 |
logics.toList.sortWith(_ < _) |
29152 | 333 |
} |
29570 | 334 |
|
335 |
||
336 |
/* symbols */ |
|
337 |
||
338 |
val symbols = new Symbol.Interpretation( |
|
43606 | 339 |
try_read(getenv_strict("ISABELLE_SYMBOLS").split(":").toList.map(Path.explode)) |
340 |
.split("\n").toList) |
|
34024 | 341 |
|
342 |
||
343 |
/* fonts */ |
|
344 |
||
43484 | 345 |
def get_font(family: String = "IsabelleText", size: Int = 1, bold: Boolean = false): Font = |
346 |
new Font(family, if (bold) Font.BOLD else Font.PLAIN, size) |
|
37367 | 347 |
|
34044
09afb1d49fe7
slightly more robust and less ambitious version of install_fonts;
wenzelm
parents:
34027
diff
changeset
|
348 |
def install_fonts() |
09afb1d49fe7
slightly more robust and less ambitious version of install_fonts;
wenzelm
parents:
34027
diff
changeset
|
349 |
{ |
37367 | 350 |
val ge = GraphicsEnvironment.getLocalGraphicsEnvironment() |
43484 | 351 |
for (font <- getenv_strict("ISABELLE_FONTS").split(":")) |
43606 | 352 |
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, platform_file(Path.explode(font)))) |
34024 | 353 |
} |
27919 | 354 |
} |