author | wenzelm |
Thu, 20 May 2010 11:36:30 +0200 | |
changeset 36991 | ccb8da7f76e6 |
parent 36788 | 1fd4f28e6ce1 |
child 37013 | 641923374eba |
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 |
||
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
9 |
import java.util.regex.Pattern |
31820 | 10 |
import java.util.Locale |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
11 |
import java.io.{BufferedWriter, OutputStreamWriter, FileOutputStream, |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
12 |
BufferedInputStream, FileInputStream, BufferedReader, InputStreamReader, |
34162 | 13 |
File, IOException} |
34024 | 14 |
import java.awt.{GraphicsEnvironment, Font} |
28063 | 15 |
|
16 |
import scala.io.Source |
|
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
17 |
import scala.util.matching.Regex |
34163 | 18 |
import scala.collection.mutable |
27936 | 19 |
|
27919 | 20 |
|
34201
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
21 |
class Isabelle_System extends Standard_System |
31498 | 22 |
{ |
31796 | 23 |
/** Isabelle environment **/ |
24 |
||
31498 | 25 |
private val environment: Map[String, String] = |
26 |
{ |
|
36011
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents:
35023
diff
changeset
|
27 |
import scala.collection.JavaConversions._ |
31498 | 28 |
|
36193
067a01827fca
improved ISABELLE_JAVA, based on THIS_JAVA of the actually running JVM;
wenzelm
parents:
36136
diff
changeset
|
29 |
val env0 = Map(java.lang.System.getenv.toList: _*) + |
067a01827fca
improved ISABELLE_JAVA, based on THIS_JAVA of the actually running JVM;
wenzelm
parents:
36136
diff
changeset
|
30 |
("THIS_JAVA" -> this_java()) |
27919 | 31 |
|
31927
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
32 |
val isabelle_home = |
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
33 |
env0.get("ISABELLE_HOME") match { |
31498 | 34 |
case None | Some("") => |
31927
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
35 |
val path = java.lang.System.getProperty("isabelle.home") |
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
36 |
if (path == null || path == "") error("Unknown Isabelle home directory") |
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
37 |
else path |
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
38 |
case Some(path) => path |
31498 | 39 |
} |
29177 | 40 |
|
34201
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
41 |
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
|
42 |
val shell_prefix = |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
43 |
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
|
44 |
val cmdline = |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
45 |
shell_prefix ::: List(isabelle_home + "/bin/isabelle", "getenv", "-d", dump.toString) |
34258 | 46 |
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
|
47 |
if (rc != 0) error(output) |
31498 | 48 |
|
34201
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
49 |
val entries = |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
50 |
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
|
51 |
val i = entry.indexOf('=') |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
52 |
if (i <= 0) (entry -> "") |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
53 |
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
|
54 |
} |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
55 |
Map(entries: _*) + |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
56 |
("HOME" -> java.lang.System.getenv("HOME")) + |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
57 |
("PATH" -> java.lang.System.getenv("PATH")) |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
58 |
} |
27953 | 59 |
} |
27919 | 60 |
|
31796 | 61 |
|
34204 | 62 |
/* external processes */ |
63 |
||
64 |
def execute(redirect: Boolean, args: String*): Process = |
|
65 |
{ |
|
66 |
val cmdline = |
|
67 |
if (Platform.is_windows) List(platform_root + "\\bin\\env.exe") ++ args |
|
68 |
else args |
|
34219 | 69 |
Standard_System.raw_execute(null, environment, redirect, cmdline: _*) |
34204 | 70 |
} |
71 |
||
72 |
||
31796 | 73 |
/* getenv */ |
74 |
||
31498 | 75 |
def getenv(name: String): String = |
76 |
environment.getOrElse(if (name == "HOME") "HOME_JVM" else name, "") |
|
77 |
||
78 |
def getenv_strict(name: String): String = |
|
79 |
{ |
|
31234
6ce6801129de
getenv_strict needs to be based on getenv (accidentally broken in 0e88d33e8d19);
wenzelm
parents:
30175
diff
changeset
|
80 |
val value = getenv(name) |
27993
6dd90ef9f927
simplified exceptions: use plain error function / RuntimeException;
wenzelm
parents:
27974
diff
changeset
|
81 |
if (value != "") value else error("Undefined environment variable: " + name) |
27919 | 82 |
} |
83 |
||
31703 | 84 |
override def toString = getenv("ISABELLE_HOME") |
85 |
||
27936 | 86 |
|
31796 | 87 |
|
88 |
/** file path specifications **/ |
|
89 |
||
31820 | 90 |
/* expand_path */ |
31796 | 91 |
|
36136
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
92 |
private val Root = new Regex("(//+[^/]*|/)(.*)") |
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
93 |
private val Only_Root = new Regex("//+[^/]*|/") |
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
94 |
|
31820 | 95 |
def expand_path(isabelle_path: String): String = |
31796 | 96 |
{ |
97 |
val result_path = new StringBuilder |
|
36136
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
98 |
def init(path: String): String = |
31796 | 99 |
{ |
36136
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
100 |
path match { |
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
101 |
case Root(root, rest) => |
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
102 |
result_path.clear |
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
103 |
result_path ++= root |
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
104 |
rest |
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
105 |
case _ => path |
31796 | 106 |
} |
107 |
} |
|
108 |
def append(path: String) |
|
109 |
{ |
|
36136
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
110 |
val rest = init(path) |
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
111 |
for (p <- rest.split("/") if p != "" && p != ".") { |
31803 | 112 |
if (p == "..") { |
113 |
val result = result_path.toString |
|
36136
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
114 |
if (!Only_Root.pattern.matcher(result).matches) { |
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
115 |
val i = result.lastIndexOf("/") |
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
116 |
if (result == "") |
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
117 |
result_path ++= ".." |
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
118 |
else if (result.substring(i + 1) == "..") |
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
119 |
result_path ++= "/.." |
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
120 |
else if (i < 0) |
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
121 |
result_path.length = 0 |
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
122 |
else |
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
123 |
result_path.length = i |
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
124 |
} |
31803 | 125 |
} |
126 |
else { |
|
127 |
val len = result_path.length |
|
128 |
if (len > 0 && result_path(len - 1) != '/') |
|
129 |
result_path += '/' |
|
130 |
result_path ++= p |
|
131 |
} |
|
31796 | 132 |
} |
133 |
} |
|
36136
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
134 |
val rest = init(isabelle_path) |
89b1a136edef
more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents:
36012
diff
changeset
|
135 |
for (p <- rest.split("/")) { |
31796 | 136 |
if (p.startsWith("$")) append(getenv_strict(p.substring(1))) |
137 |
else if (p == "~") append(getenv_strict("HOME")) |
|
138 |
else if (p == "~~") append(getenv_strict("ISABELLE_HOME")) |
|
139 |
else append(p) |
|
140 |
} |
|
141 |
result_path.toString |
|
142 |
} |
|
143 |
||
144 |
||
31820 | 145 |
/* platform_path */ |
27936 | 146 |
|
31820 | 147 |
def platform_path(isabelle_path: String): String = |
34201
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
148 |
jvm_path(expand_path(isabelle_path)) |
27953 | 149 |
|
31498 | 150 |
def platform_file(path: String) = new File(platform_path(path)) |
29152 | 151 |
|
27953 | 152 |
|
36991 | 153 |
/* try_read */ |
154 |
||
155 |
def try_read(path: String): String = |
|
156 |
{ |
|
157 |
val file = platform_file(path) |
|
158 |
if (file.isFile) Source.fromFile(file).mkString else "" |
|
159 |
} |
|
160 |
||
161 |
||
31436 | 162 |
/* source files */ |
163 |
||
164 |
private def try_file(file: File) = if (file.isFile) Some(file) else None |
|
165 |
||
31498 | 166 |
def source_file(path: String): Option[File] = |
167 |
{ |
|
168 |
if (path.startsWith("/") || path == "") |
|
31436 | 169 |
try_file(platform_file(path)) |
170 |
else { |
|
34201
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
171 |
val pure_file = platform_file("$ISABELLE_HOME/src/Pure/" + path) |
31436 | 172 |
if (pure_file.isFile) Some(pure_file) |
173 |
else if (getenv("ML_SOURCES") != "") |
|
174 |
try_file(platform_file("$ML_SOURCES/" + path)) |
|
175 |
else None |
|
176 |
} |
|
177 |
} |
|
178 |
||
179 |
||
32450 | 180 |
|
31796 | 181 |
/** system tools **/ |
182 |
||
35010
d6e492cea6e4
renamed system/system_out to bash/bash_output -- to emphasized that this is really GNU bash, not some undefined POSIX sh;
wenzelm
parents:
35006
diff
changeset
|
183 |
def bash_output(script: String): (String, Int) = |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
184 |
{ |
34201
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
185 |
Standard_System.with_tmp_file("isabelle_script") { script_file => |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
186 |
Standard_System.with_tmp_file("isabelle_pid") { pid_file => |
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
187 |
Standard_System.with_tmp_file("isabelle_output") { output_file => |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
188 |
|
34201
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
189 |
Standard_System.write_file(script_file, script) |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
190 |
|
35023
16f9877abf0b
modernized perl scripts: prefer standalone executables;
wenzelm
parents:
35010
diff
changeset
|
191 |
val proc = execute(true, expand_path("$ISABELLE_HOME/lib/scripts/bash"), "group", |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
192 |
script_file.getPath, pid_file.getPath, output_file.getPath) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
193 |
|
34199 | 194 |
def kill(strict: Boolean) = |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
195 |
{ |
34199 | 196 |
val pid = |
34201
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
197 |
try { Some(Standard_System.read_file(pid_file)) } |
34199 | 198 |
catch { case _: IOException => None } |
199 |
if (pid.isDefined) { |
|
200 |
var running = true |
|
201 |
var count = 10 |
|
202 |
while (running && count > 0) { |
|
203 |
if (execute(true, "kill", "-INT", "-" + pid.get).waitFor != 0) |
|
204 |
running = false |
|
205 |
else { |
|
206 |
Thread.sleep(100) |
|
207 |
if (!strict) count -= 1 |
|
208 |
} |
|
209 |
} |
|
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
210 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
211 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
212 |
|
34199 | 213 |
val shutdown_hook = new Thread { override def run = kill(true) } |
34201
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
214 |
Runtime.getRuntime.addShutdownHook(shutdown_hook) // FIXME tmp files during shutdown?!? |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
215 |
|
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
216 |
def cleanup() = |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
217 |
try { Runtime.getRuntime.removeShutdownHook(shutdown_hook) } |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
218 |
catch { case _: IllegalStateException => } |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
219 |
|
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
220 |
val rc = |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
221 |
try { |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
222 |
try { proc.waitFor } // FIXME read stderr (!??) |
34199 | 223 |
catch { case e: InterruptedException => Thread.interrupted; kill(false); throw e } |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
224 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
225 |
finally { |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
226 |
proc.getOutputStream.close |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
227 |
proc.getInputStream.close |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
228 |
proc.getErrorStream.close |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
229 |
proc.destroy |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
230 |
cleanup() |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
231 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
232 |
|
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
233 |
val output = |
34201
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
234 |
try { Standard_System.read_file(output_file) } |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
235 |
catch { case _: IOException => "" } |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
236 |
|
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
237 |
(output, rc) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
238 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
239 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
240 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
241 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
242 |
|
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
243 |
def isabelle_tool(name: String, args: String*): (String, Int) = |
31498 | 244 |
{ |
34200 | 245 |
getenv_strict("ISABELLE_TOOLS").split(":").find { dir => |
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
246 |
val file = platform_file(dir + "/" + name) |
34025 | 247 |
try { file.isFile && file.canRead && file.canExecute } |
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) => |
34201
c95dcd12f48a
separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
34200
diff
changeset
|
251 |
Standard_System.process_output( |
34195 | 252 |
execute(true, (List(expand_path(dir + "/" + name)) ++ 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 |
||
31498 | 260 |
def mk_fifo(): String = |
261 |
{ |
|
28496
4cff10648928
renamed isatool to isabelle_tool in programming interfaces;
wenzelm
parents:
28063
diff
changeset
|
262 |
val (result, rc) = isabelle_tool("mkfifo") |
28063 | 263 |
if (rc == 0) result.trim |
264 |
else error(result) |
|
265 |
} |
|
266 |
||
31498 | 267 |
def rm_fifo(fifo: String) |
268 |
{ |
|
28496
4cff10648928
renamed isatool to isabelle_tool in programming interfaces;
wenzelm
parents:
28063
diff
changeset
|
269 |
val (result, rc) = isabelle_tool("rmfifo", fifo) |
28063 | 270 |
if (rc != 0) error(result) |
271 |
} |
|
272 |
||
34100 | 273 |
def fifo_stream(fifo: String): BufferedInputStream = |
31498 | 274 |
{ |
29177 | 275 |
// blocks until writer is ready |
276 |
val stream = |
|
34194
001321ca185c
slightly more paranoid cleanup of process (cf. http://kylecartmell.com/?p=9 "Five Common java.lang.Process Pitfalls");
wenzelm
parents:
34186
diff
changeset
|
277 |
if (Platform.is_windows) { |
001321ca185c
slightly more paranoid cleanup of process (cf. http://kylecartmell.com/?p=9 "Five Common java.lang.Process Pitfalls");
wenzelm
parents:
34186
diff
changeset
|
278 |
val proc = execute(false, "cat", fifo) |
001321ca185c
slightly more paranoid cleanup of process (cf. http://kylecartmell.com/?p=9 "Five Common java.lang.Process Pitfalls");
wenzelm
parents:
34186
diff
changeset
|
279 |
proc.getOutputStream.close |
001321ca185c
slightly more paranoid cleanup of process (cf. http://kylecartmell.com/?p=9 "Five Common java.lang.Process Pitfalls");
wenzelm
parents:
34186
diff
changeset
|
280 |
proc.getErrorStream.close |
001321ca185c
slightly more paranoid cleanup of process (cf. http://kylecartmell.com/?p=9 "Five Common java.lang.Process Pitfalls");
wenzelm
parents:
34186
diff
changeset
|
281 |
proc.getInputStream |
001321ca185c
slightly more paranoid cleanup of process (cf. http://kylecartmell.com/?p=9 "Five Common java.lang.Process Pitfalls");
wenzelm
parents:
34186
diff
changeset
|
282 |
} |
29177 | 283 |
else new FileInputStream(fifo) |
34100 | 284 |
new BufferedInputStream(stream) |
29177 | 285 |
} |
28063 | 286 |
|
29152 | 287 |
|
32450 | 288 |
|
31796 | 289 |
/** Isabelle resources **/ |
290 |
||
32328 | 291 |
/* components */ |
292 |
||
293 |
def components(): List[String] = |
|
294 |
getenv("ISABELLE_COMPONENTS").split(":").toList |
|
295 |
||
296 |
||
29152 | 297 |
/* find logics */ |
298 |
||
31498 | 299 |
def find_logics(): List[String] = |
300 |
{ |
|
29152 | 301 |
val ml_ident = getenv_strict("ML_IDENTIFIER") |
34163 | 302 |
val logics = new mutable.ListBuffer[String] |
29152 | 303 |
for (dir <- getenv_strict("ISABELLE_PATH").split(":")) { |
304 |
val files = platform_file(dir + "/" + ml_ident).listFiles() |
|
305 |
if (files != null) { |
|
306 |
for (file <- files if file.isFile) logics += file.getName |
|
307 |
} |
|
308 |
} |
|
36012 | 309 |
logics.toList.sortWith(_ < _) |
29152 | 310 |
} |
29570 | 311 |
|
312 |
||
313 |
/* symbols */ |
|
314 |
||
34137 | 315 |
private def read_symbols(path: String): List[String] = |
36991 | 316 |
Library.chunks(try_read(path)).map(_.toString).toList |
317 |
||
29570 | 318 |
val symbols = new Symbol.Interpretation( |
34137 | 319 |
read_symbols("$ISABELLE_HOME/etc/symbols") ::: |
29570 | 320 |
read_symbols("$ISABELLE_HOME_USER/etc/symbols")) |
34024 | 321 |
|
322 |
||
323 |
/* fonts */ |
|
324 |
||
325 |
val font_family = "IsabelleText" |
|
326 |
||
36788 | 327 |
def get_font(size: Int = 1, bold: Boolean = false): Font = |
36785 | 328 |
new Font(font_family, if (bold) Font.BOLD else Font.PLAIN, size) |
34024 | 329 |
|
34044
09afb1d49fe7
slightly more robust and less ambitious version of install_fonts;
wenzelm
parents:
34027
diff
changeset
|
330 |
def install_fonts() |
09afb1d49fe7
slightly more robust and less ambitious version of install_fonts;
wenzelm
parents:
34027
diff
changeset
|
331 |
{ |
35006 | 332 |
def create_font(bold: Boolean): Font = |
333 |
{ |
|
334 |
val name = |
|
335 |
if (bold) "$ISABELLE_HOME/lib/fonts/IsabelleTextBold.ttf" |
|
336 |
else "$ISABELLE_HOME/lib/fonts/IsabelleText.ttf" |
|
337 |
Font.createFont(Font.TRUETYPE_FONT, platform_file(name)) |
|
338 |
} |
|
36788 | 339 |
def check_font() = get_font().getFamily == font_family |
35006 | 340 |
|
34044
09afb1d49fe7
slightly more robust and less ambitious version of install_fonts;
wenzelm
parents:
34027
diff
changeset
|
341 |
if (!check_font()) { |
35006 | 342 |
val font = create_font(false) |
343 |
val bold_font = create_font(true) |
|
344 |
||
34044
09afb1d49fe7
slightly more robust and less ambitious version of install_fonts;
wenzelm
parents:
34027
diff
changeset
|
345 |
val ge = GraphicsEnvironment.getLocalGraphicsEnvironment() |
35006 | 346 |
ge.registerFont(font) |
347 |
// workaround strange problem with Apple's Java 1.6 font manager |
|
348 |
if (bold_font.getFamily == font_family) ge.registerFont(bold_font) |
|
349 |
if (!check_font()) error("Failed to install IsabelleText fonts") |
|
34044
09afb1d49fe7
slightly more robust and less ambitious version of install_fonts;
wenzelm
parents:
34027
diff
changeset
|
350 |
} |
34024 | 351 |
} |
27919 | 352 |
} |