author | wenzelm |
Mon, 31 Dec 2012 13:49:01 +0100 | |
changeset 50651 | 1fe68f1c3069 |
parent 50403 | 87868964733c |
child 50652 | ead5714cc480 |
permissions | -rw-r--r-- |
30175 | 1 |
/* Title: Pure/System/isabelle_system.scala |
27919 | 2 |
Author: Makarius |
3 |
||
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43670
diff
changeset
|
4 |
Fundamental Isabelle system environment: quasi-static module with |
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43670
diff
changeset
|
5 |
optional init operation. |
27919 | 6 |
*/ |
7 |
||
8 |
package isabelle |
|
9 |
||
43520
cec9b95fa35d
explicit import java.lang.System to prevent odd scope problems;
wenzelm
parents:
43514
diff
changeset
|
10 |
import java.lang.System |
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
11 |
import java.util.regex.Pattern |
48409 | 12 |
import java.io.{InputStream, OutputStream, File => JFile, BufferedReader, InputStreamReader, |
49334 | 13 |
BufferedWriter, OutputStreamWriter, IOException, FileInputStream, BufferedInputStream} |
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 |
{ |
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
24 |
/** implicit state **/ |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
25 |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43670
diff
changeset
|
26 |
private case class State(standard_system: Standard_System, settings: Map[String, String]) |
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
27 |
|
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
28 |
@volatile private var _state: Option[State] = None |
43514
45cf8d5e109a
lazy Isabelle_System.default supports implicit boot;
wenzelm
parents:
43484
diff
changeset
|
29 |
|
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
30 |
private def check_state(): State = |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
31 |
{ |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
32 |
if (_state.isEmpty) init() // unsynchronized check |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
33 |
_state.get |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
34 |
} |
37013
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
35 |
|
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
36 |
def standard_system: Standard_System = check_state().standard_system |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
37 |
def settings: Map[String, String] = check_state().settings |
31796 | 38 |
|
37013
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 |
isabelle_home precedence: |
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
41 |
(1) this_isabelle_home as explicit argument |
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
42 |
(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
|
43 |
(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
|
44 |
*/ |
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
45 |
def init(this_isabelle_home: String = null) = synchronized { |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
46 |
if (_state.isEmpty) { |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
47 |
import scala.collection.JavaConversions._ |
37013
641923374eba
Isabelle_System: allow explicit isabelle_home argument;
wenzelm
parents:
36991
diff
changeset
|
48 |
|
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
49 |
val standard_system = new Standard_System |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
50 |
val settings = |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
51 |
{ |
48193 | 52 |
val env0 = sys.env + ("ISABELLE_JDK_HOME" -> standard_system.this_jdk_home()) |
27919 | 53 |
|
47725
447b635bcea5
cold-start HOME is user.home, in accordance with Cygwin-Terminal.bat;
wenzelm
parents:
47674
diff
changeset
|
54 |
val user_home = System.getProperty("user.home") |
447b635bcea5
cold-start HOME is user.home, in accordance with Cygwin-Terminal.bat;
wenzelm
parents:
47674
diff
changeset
|
55 |
val env = |
447b635bcea5
cold-start HOME is user.home, in accordance with Cygwin-Terminal.bat;
wenzelm
parents:
47674
diff
changeset
|
56 |
if (user_home == null || env0.isDefinedAt("HOME")) env0 |
447b635bcea5
cold-start HOME is user.home, in accordance with Cygwin-Terminal.bat;
wenzelm
parents:
47674
diff
changeset
|
57 |
else env0 + ("HOME" -> user_home) |
447b635bcea5
cold-start HOME is user.home, in accordance with Cygwin-Terminal.bat;
wenzelm
parents:
47674
diff
changeset
|
58 |
|
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
59 |
val isabelle_home = |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
60 |
if (this_isabelle_home != null) this_isabelle_home |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
61 |
else |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
62 |
env.get("ISABELLE_HOME") match { |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
63 |
case None | Some("") => |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
64 |
val path = System.getProperty("isabelle.home") |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
65 |
if (path == null || path == "") error("Unknown Isabelle home directory") |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
66 |
else path |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
67 |
case Some(path) => path |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
68 |
} |
29177 | 69 |
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
48409
diff
changeset
|
70 |
File.with_tmp_file("settings") { dump => |
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
71 |
val shell_prefix = |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
72 |
if (Platform.is_windows) List(standard_system.platform_root + "\\bin\\bash", "-l") |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
73 |
else Nil |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
74 |
val cmdline = |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
75 |
shell_prefix ::: List(isabelle_home + "/bin/isabelle", "getenv", "-d", dump.toString) |
50651 | 76 |
val (output, rc) = process_output(raw_execute(null, env, true, cmdline: _*)) |
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
77 |
if (rc != 0) error(output) |
31498 | 78 |
|
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
79 |
val entries = |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
48409
diff
changeset
|
80 |
(for (entry <- File.read(dump) split "\0" if entry != "") yield { |
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
81 |
val i = entry.indexOf('=') |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
82 |
if (i <= 0) (entry -> "") |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
83 |
else (entry.substring(0, i) -> entry.substring(i + 1)) |
47674
cdf95042e09c
more robust handling of PATH vs PATH_JVM -- required for cold start of Cygwin from Windows (e.g. Isabelle.exe);
wenzelm
parents:
47661
diff
changeset
|
84 |
}).toMap |
cdf95042e09c
more robust handling of PATH vs PATH_JVM -- required for cold start of Cygwin from Windows (e.g. Isabelle.exe);
wenzelm
parents:
47661
diff
changeset
|
85 |
entries + ("PATH" -> entries("PATH_JVM")) - "PATH_JVM" |
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
86 |
} |
34201
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
87 |
} |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43670
diff
changeset
|
88 |
_state = Some(State(standard_system, settings)) |
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
89 |
} |
27953 | 90 |
} |
27919 | 91 |
|
31796 | 92 |
|
93 |
/* getenv */ |
|
94 |
||
47661
012a887997f3
USER_HOME settings variable points to cross-platform user home directory;
wenzelm
parents:
47113
diff
changeset
|
95 |
def getenv(name: String): String = settings.getOrElse(name, "") |
31498 | 96 |
|
97 |
def getenv_strict(name: String): String = |
|
98 |
{ |
|
31234
6ce6801129de
getenv_strict needs to be based on getenv (accidentally broken in 0e88d33e8d19);
wenzelm
parents:
30175
diff
changeset
|
99 |
val value = getenv(name) |
27993
6dd90ef9f927
simplified exceptions: use plain error function / RuntimeException;
wenzelm
parents:
27974
diff
changeset
|
100 |
if (value != "") value else error("Undefined environment variable: " + name) |
27919 | 101 |
} |
102 |
||
31796 | 103 |
|
43606 | 104 |
/** file-system operations **/ |
31796 | 105 |
|
43606 | 106 |
/* path specifications */ |
36136
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
107 |
|
43664 | 108 |
def standard_path(path: Path): String = path.expand.implode |
31796 | 109 |
|
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
110 |
def platform_path(path: Path): String = standard_system.jvm_path(standard_path(path)) |
48409 | 111 |
def platform_file(path: Path): JFile = new JFile(platform_path(path)) |
29152 | 112 |
|
45100 | 113 |
def platform_file_url(raw_path: Path): String = |
114 |
{ |
|
115 |
val path = raw_path.expand |
|
116 |
require(path.is_absolute) |
|
45101
6317e969fb30
proper platform_file_url for Windows UNC paths (server shares);
wenzelm
parents:
45100
diff
changeset
|
117 |
val s = platform_path(path).replaceAll(" ", "%20") |
6317e969fb30
proper platform_file_url for Windows UNC paths (server shares);
wenzelm
parents:
45100
diff
changeset
|
118 |
if (!Platform.is_windows) "file://" + s |
6317e969fb30
proper platform_file_url for Windows UNC paths (server shares);
wenzelm
parents:
45100
diff
changeset
|
119 |
else if (s.startsWith("\\\\")) "file:" + s.replace('\\', '/') |
6317e969fb30
proper platform_file_url for Windows UNC paths (server shares);
wenzelm
parents:
45100
diff
changeset
|
120 |
else "file:///" + s.replace('\\', '/') |
45100 | 121 |
} |
122 |
||
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
123 |
def posix_path(jvm_path: String): String = standard_system.posix_path(jvm_path) |
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
124 |
|
27953 | 125 |
|
48923
a2df77fcf1eb
prefer jEdit file name representation (potentially via VFS);
wenzelm
parents:
48550
diff
changeset
|
126 |
/* source files of Isabelle/ML bootstrap */ |
31436 | 127 |
|
48923
a2df77fcf1eb
prefer jEdit file name representation (potentially via VFS);
wenzelm
parents:
48550
diff
changeset
|
128 |
def source_file(path: Path): Option[Path] = |
31498 | 129 |
{ |
48923
a2df77fcf1eb
prefer jEdit file name representation (potentially via VFS);
wenzelm
parents:
48550
diff
changeset
|
130 |
def check(p: Path): Option[Path] = if (p.is_file) Some(p) else None |
a2df77fcf1eb
prefer jEdit file name representation (potentially via VFS);
wenzelm
parents:
48550
diff
changeset
|
131 |
|
a2df77fcf1eb
prefer jEdit file name representation (potentially via VFS);
wenzelm
parents:
48550
diff
changeset
|
132 |
if (path.is_absolute || path.is_current) check(path) |
31436 | 133 |
else { |
48923
a2df77fcf1eb
prefer jEdit file name representation (potentially via VFS);
wenzelm
parents:
48550
diff
changeset
|
134 |
check(Path.explode("~~/src/Pure") + path) orElse |
a2df77fcf1eb
prefer jEdit file name representation (potentially via VFS);
wenzelm
parents:
48550
diff
changeset
|
135 |
(if (getenv("ML_SOURCES") == "") None else check(Path.explode("$ML_SOURCES") + path)) |
31436 | 136 |
} |
137 |
} |
|
138 |
||
139 |
||
32450 | 140 |
|
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
141 |
/** external processes **/ |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
142 |
|
50651 | 143 |
/* raw execute for bootstrapping */ |
144 |
||
145 |
private def raw_execute(cwd: JFile, env: Map[String, String], redirect: Boolean, args: String*) |
|
146 |
: Process = |
|
147 |
{ |
|
148 |
val cmdline = new java.util.LinkedList[String] |
|
149 |
for (s <- args) cmdline.add(s) |
|
150 |
||
151 |
val proc = new ProcessBuilder(cmdline) |
|
152 |
if (cwd != null) proc.directory(cwd) |
|
153 |
if (env != null) { |
|
154 |
proc.environment.clear |
|
155 |
for ((x, y) <- env) proc.environment.put(x, y) |
|
156 |
} |
|
157 |
proc.redirectErrorStream(redirect) |
|
158 |
proc.start |
|
159 |
} |
|
160 |
||
161 |
private def process_output(proc: Process): (String, Int) = |
|
162 |
{ |
|
163 |
proc.getOutputStream.close |
|
164 |
val output = File.read(proc.getInputStream) |
|
165 |
val rc = |
|
166 |
try { proc.waitFor } |
|
167 |
finally { |
|
168 |
proc.getInputStream.close |
|
169 |
proc.getErrorStream.close |
|
170 |
proc.destroy |
|
171 |
Thread.interrupted |
|
172 |
} |
|
173 |
(output, rc) |
|
174 |
} |
|
175 |
||
176 |
||
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
177 |
/* plain execute */ |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
178 |
|
48409 | 179 |
def execute_env(cwd: JFile, env: Map[String, String], redirect: Boolean, args: String*): Process = |
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
180 |
{ |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
181 |
val cmdline = |
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
182 |
if (Platform.is_windows) List(standard_system.platform_root + "\\bin\\env.exe") ++ args |
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
183 |
else args |
48353
bcce872202b3
support external processes with explicit environment;
wenzelm
parents:
48278
diff
changeset
|
184 |
val env1 = if (env == null) settings else settings ++ env |
50651 | 185 |
raw_execute(cwd, env1, redirect, cmdline: _*) |
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
186 |
} |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
187 |
|
48353
bcce872202b3
support external processes with explicit environment;
wenzelm
parents:
48278
diff
changeset
|
188 |
def execute(redirect: Boolean, args: String*): Process = |
bcce872202b3
support external processes with explicit environment;
wenzelm
parents:
48278
diff
changeset
|
189 |
execute_env(null, null, redirect, args: _*) |
bcce872202b3
support external processes with explicit environment;
wenzelm
parents:
48278
diff
changeset
|
190 |
|
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
191 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
192 |
/* managed process */ |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
193 |
|
48409 | 194 |
class Managed_Process(cwd: JFile, env: Map[String, String], redirect: Boolean, args: String*) |
39583
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 |
private val params = |
43606 | 197 |
List(standard_path(Path.explode("~~/lib/scripts/process")), "group", "-", "no_script") |
48353
bcce872202b3
support external processes with explicit environment;
wenzelm
parents:
48278
diff
changeset
|
198 |
private val proc = execute_env(cwd, env, redirect, (params ++ args):_*) |
39583
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 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
201 |
// channels |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
202 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
203 |
val stdin: BufferedWriter = |
50203 | 204 |
new BufferedWriter(new OutputStreamWriter(proc.getOutputStream, UTF8.charset)) |
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
205 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
206 |
val stdout: BufferedReader = |
50203 | 207 |
new BufferedReader(new InputStreamReader(proc.getInputStream, UTF8.charset)) |
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
208 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
209 |
val stderr: BufferedReader = |
50203 | 210 |
new BufferedReader(new InputStreamReader(proc.getErrorStream, UTF8.charset)) |
39583
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 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
213 |
// signals |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
214 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
215 |
private val pid = stdout.readLine |
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 |
private def kill(signal: String): Boolean = |
39584
f2a10986e85a
more robust Managed_Process.kill: check after sending signal;
wenzelm
parents:
39583
diff
changeset
|
218 |
{ |
f2a10986e85a
more robust Managed_Process.kill: check after sending signal;
wenzelm
parents:
39583
diff
changeset
|
219 |
execute(true, "kill", "-" + signal, "-" + pid).waitFor |
f2a10986e85a
more robust Managed_Process.kill: check after sending signal;
wenzelm
parents:
39583
diff
changeset
|
220 |
execute(true, "kill", "-0", "-" + pid).waitFor == 0 |
f2a10986e85a
more robust Managed_Process.kill: check after sending signal;
wenzelm
parents:
39583
diff
changeset
|
221 |
} |
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
222 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
223 |
private def multi_kill(signal: String): Boolean = |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
224 |
{ |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
225 |
var running = true |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
226 |
var count = 10 |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
227 |
while (running && count > 0) { |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
228 |
if (kill(signal)) { |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
229 |
Thread.sleep(100) |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
230 |
count -= 1 |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
231 |
} |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
232 |
else running = false |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
233 |
} |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
234 |
running |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
235 |
} |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
236 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
237 |
def interrupt() { multi_kill("INT") } |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
238 |
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
|
239 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
240 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
241 |
// JVM shutdown hook |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
242 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
243 |
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
|
244 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
245 |
try { Runtime.getRuntime.addShutdownHook(shutdown_hook) } |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
246 |
catch { case _: IllegalStateException => } |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
247 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
248 |
private def cleanup() = |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
249 |
try { Runtime.getRuntime.removeShutdownHook(shutdown_hook) } |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
250 |
catch { case _: IllegalStateException => } |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
251 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
252 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
253 |
/* result */ |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
254 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
255 |
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
|
256 |
} |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
257 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
258 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
259 |
/* bash */ |
31796 | 260 |
|
48409 | 261 |
def bash_env(cwd: JFile, env: Map[String, String], script: String): (String, String, Int) = |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
262 |
{ |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
48409
diff
changeset
|
263 |
File.with_tmp_file("isabelle_script") { script_file => |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
48409
diff
changeset
|
264 |
File.write(script_file, script) |
48353
bcce872202b3
support external processes with explicit environment;
wenzelm
parents:
48278
diff
changeset
|
265 |
val proc = new Managed_Process(cwd, env, 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
|
266 |
|
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
267 |
proc.stdin.close |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
48409
diff
changeset
|
268 |
val (_, stdout) = Simple_Thread.future("bash_stdout") { File.read(proc.stdout) } |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
48409
diff
changeset
|
269 |
val (_, stderr) = Simple_Thread.future("bash_stderr") { File.read(proc.stderr) } |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
270 |
|
39581
430ff865089b
refined Isabelle_System.bash_output: pass pid via stdout, separate stdout/stderr;
wenzelm
parents:
39576
diff
changeset
|
271 |
val rc = |
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
272 |
try { proc.join } |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
273 |
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
|
274 |
(stdout.join, stderr.join, rc) |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
275 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
276 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
277 |
|
48353
bcce872202b3
support external processes with explicit environment;
wenzelm
parents:
48278
diff
changeset
|
278 |
def bash(script: String): (String, String, Int) = bash_env(null, null, script) |
bcce872202b3
support external processes with explicit environment;
wenzelm
parents:
48278
diff
changeset
|
279 |
|
39583
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
280 |
|
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
281 |
/* system tools */ |
c1e9c6dfeff8
more robust lib/scripts/process, with explicit script/no_script mode;
wenzelm
parents:
39581
diff
changeset
|
282 |
|
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
283 |
def isabelle_tool(name: String, args: String*): (String, Int) = |
31498 | 284 |
{ |
43669 | 285 |
Path.split(getenv_strict("ISABELLE_TOOLS")).find { dir => |
48373 | 286 |
val file = (dir + Path.basic(name)).file |
42124 | 287 |
try { |
288 |
file.isFile && file.canRead && file.canExecute && |
|
289 |
!name.endsWith("~") && !name.endsWith(".orig") |
|
290 |
} |
|
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
291 |
catch { case _: SecurityException => false } |
34200 | 292 |
} match { |
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
293 |
case Some(dir) => |
43669 | 294 |
val file = standard_path(dir + Path.basic(name)) |
50651 | 295 |
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
|
296 |
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
|
297 |
} |
28063 | 298 |
} |
299 |
||
300 |
||
32450 | 301 |
|
31796 | 302 |
/** Isabelle resources **/ |
303 |
||
32328 | 304 |
/* components */ |
305 |
||
43669 | 306 |
def components(): List[Path] = |
307 |
Path.split(getenv_strict("ISABELLE_COMPONENTS")) |
|
32328 | 308 |
|
309 |
||
50403
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
310 |
/* logic images */ |
29152 | 311 |
|
48503 | 312 |
def find_logics_dirs(): List[Path] = |
31498 | 313 |
{ |
48503 | 314 |
val ml_ident = Path.explode("$ML_IDENTIFIER").expand |
315 |
Path.split(getenv_strict("ISABELLE_PATH")).map(_ + ml_ident) |
|
29152 | 316 |
} |
29570 | 317 |
|
48503 | 318 |
def find_logics(): List[String] = |
319 |
(for { |
|
320 |
dir <- find_logics_dirs() |
|
321 |
files = dir.file.listFiles() if files != null |
|
322 |
file <- files.toList if file.isFile } yield file.getName).sorted |
|
323 |
||
50403
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
324 |
def default_logic(args: String*): String = |
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
325 |
{ |
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
326 |
args.find(_ != "") match { |
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
327 |
case Some(logic) => logic |
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
328 |
case None => Isabelle_System.getenv_strict("ISABELLE_LOGIC") |
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
329 |
} |
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
330 |
} |
87868964733c
more uniform default logic, using settings, options, args etc.;
wenzelm
parents:
50298
diff
changeset
|
331 |
|
29570 | 332 |
|
34024 | 333 |
/* fonts */ |
334 |
||
43484 | 335 |
def get_font(family: String = "IsabelleText", size: Int = 1, bold: Boolean = false): Font = |
336 |
new Font(family, if (bold) Font.BOLD else Font.PLAIN, size) |
|
37367 | 337 |
|
34044
09afb1d49fe7
slightly more robust and less ambitious version of install_fonts;
wenzelm
parents:
34027
diff
changeset
|
338 |
def install_fonts() |
09afb1d49fe7
slightly more robust and less ambitious version of install_fonts;
wenzelm
parents:
34027
diff
changeset
|
339 |
{ |
37367 | 340 |
val ge = GraphicsEnvironment.getLocalGraphicsEnvironment() |
49449 | 341 |
for (font <- Path.split(getenv_strict("ISABELLE_FONTS"))) |
48373 | 342 |
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, font.file)) |
49449 | 343 |
} |
49334 | 344 |
|
49449 | 345 |
def install_fonts_jfx() |
346 |
{ |
|
347 |
for (font <- Path.split(getenv_strict("ISABELLE_FONTS"))) { |
|
49334 | 348 |
val stream = new BufferedInputStream(new FileInputStream(font.file)) |
349 |
try { javafx.scene.text.Font.loadFont(stream, 1.0) } |
|
350 |
finally { stream.close } |
|
351 |
} |
|
34024 | 352 |
} |
27919 | 353 |
} |