author | wenzelm |
Tue, 08 Dec 2009 17:55:07 +0100 | |
changeset 34027 | ce25a3b37111 |
parent 34025 | 7996b488a9b5 |
child 34044 | 09afb1d49fe7 |
permissions | -rw-r--r-- |
30175 | 1 |
/* Title: Pure/System/isabelle_system.scala |
27919 | 2 |
Author: Makarius |
3 |
||
31796 | 4 |
Isabelle system support, with basic Cygwin/Posix compatibility. |
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 |
28063 | 11 |
import java.io.{BufferedReader, InputStreamReader, FileInputStream, File, IOException} |
34024 | 12 |
import java.awt.{GraphicsEnvironment, Font} |
28063 | 13 |
|
14 |
import scala.io.Source |
|
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
15 |
import scala.util.matching.Regex |
27936 | 16 |
|
27919 | 17 |
|
31796 | 18 |
object Isabelle_System |
31498 | 19 |
{ |
28057 | 20 |
val charset = "UTF-8" |
21 |
||
31822 | 22 |
|
31498 | 23 |
/* shell processes */ |
24 |
||
25 |
private def raw_execute(env: Map[String, String], redirect: Boolean, args: String*): Process = |
|
26 |
{ |
|
27 |
val cmdline = new java.util.LinkedList[String] |
|
28 |
for (s <- args) cmdline.add(s) |
|
29 |
||
30 |
val proc = new ProcessBuilder(cmdline) |
|
31 |
proc.environment.clear |
|
32 |
for ((x, y) <- env) proc.environment.put(x, y) |
|
33 |
proc.redirectErrorStream(redirect) |
|
34 |
||
35 |
try { proc.start } |
|
36 |
catch { case e: IOException => error(e.getMessage) } |
|
37 |
} |
|
38 |
||
39 |
private def process_output(proc: Process): (String, Int) = |
|
40 |
{ |
|
41 |
proc.getOutputStream.close |
|
42 |
val output = Source.fromInputStream(proc.getInputStream, charset).mkString |
|
43 |
val rc = proc.waitFor |
|
44 |
(output, rc) |
|
45 |
} |
|
46 |
} |
|
47 |
||
48 |
||
31796 | 49 |
class Isabelle_System |
31498 | 50 |
{ |
31796 | 51 |
/** unique ids **/ |
31443 | 52 |
|
53 |
private var id_count: BigInt = 0 |
|
54 |
def id(): String = synchronized { id_count += 1; "j" + id_count } |
|
55 |
||
56 |
||
31796 | 57 |
|
58 |
/** Isabelle environment **/ |
|
59 |
||
60 |
/* platform prefixes */ |
|
31498 | 61 |
|
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
62 |
private val (platform_root, drive_prefix, shell_prefix) = |
31500 | 63 |
{ |
31825 | 64 |
if (Platform.is_windows) { |
31829 | 65 |
val (root, drive) = Cygwin.config() |
31826 | 66 |
if (!Cygwin.check(root)) error("Bad Cygwin installation: " + root) |
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
67 |
val shell = List(root + "\\bin\\bash", "-l") |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
68 |
(root, drive, shell) |
31500 | 69 |
} |
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
70 |
else ("/", "", Nil) |
31500 | 71 |
} |
72 |
||
31796 | 73 |
|
74 |
/* bash environment */ |
|
75 |
||
31498 | 76 |
private val environment: Map[String, String] = |
77 |
{ |
|
78 |
import scala.collection.jcl.Conversions._ |
|
79 |
||
80 |
val env0 = Map(java.lang.System.getenv.toList: _*) |
|
27919 | 81 |
|
31927
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
82 |
val isabelle_home = |
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
83 |
env0.get("ISABELLE_HOME") match { |
31498 | 84 |
case None | Some("") => |
31927
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
85 |
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
|
86 |
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
|
87 |
else path |
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
88 |
case Some(path) => path |
31498 | 89 |
} |
29177 | 90 |
|
31498 | 91 |
val dump = File.createTempFile("isabelle", null) |
92 |
try { |
|
31927
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
93 |
val cmdline = shell_prefix ::: |
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
94 |
List(isabelle_home + "/bin/isabelle", "getenv", "-d", dump.toString) |
31796 | 95 |
val proc = Isabelle_System.raw_execute(env0, true, cmdline: _*) |
96 |
val (output, rc) = Isabelle_System.process_output(proc) |
|
31498 | 97 |
if (rc != 0) error(output) |
98 |
||
99 |
val entries = |
|
100 |
for (entry <- Source.fromFile(dump).mkString split "\0" if entry != "") yield { |
|
101 |
val i = entry.indexOf('=') |
|
102 |
if (i <= 0) (entry -> "") |
|
103 |
else (entry.substring(0, i) -> entry.substring(i + 1)) |
|
104 |
} |
|
31704
b8628ac68b73
environment: keep original HOME and PATH (required for Cygwin integrity);
wenzelm
parents:
31703
diff
changeset
|
105 |
Map(entries: _*) + |
b8628ac68b73
environment: keep original HOME and PATH (required for Cygwin integrity);
wenzelm
parents:
31703
diff
changeset
|
106 |
("HOME" -> java.lang.System.getenv("HOME")) + |
b8628ac68b73
environment: keep original HOME and PATH (required for Cygwin integrity);
wenzelm
parents:
31703
diff
changeset
|
107 |
("PATH" -> java.lang.System.getenv("PATH")) |
31498 | 108 |
} |
109 |
finally { dump.delete } |
|
27953 | 110 |
} |
27919 | 111 |
|
31796 | 112 |
|
113 |
/* getenv */ |
|
114 |
||
31498 | 115 |
def getenv(name: String): String = |
116 |
environment.getOrElse(if (name == "HOME") "HOME_JVM" else name, "") |
|
117 |
||
118 |
def getenv_strict(name: String): String = |
|
119 |
{ |
|
31234
6ce6801129de
getenv_strict needs to be based on getenv (accidentally broken in 0e88d33e8d19);
wenzelm
parents:
30175
diff
changeset
|
120 |
val value = getenv(name) |
27993
6dd90ef9f927
simplified exceptions: use plain error function / RuntimeException;
wenzelm
parents:
27974
diff
changeset
|
121 |
if (value != "") value else error("Undefined environment variable: " + name) |
27919 | 122 |
} |
123 |
||
31703 | 124 |
override def toString = getenv("ISABELLE_HOME") |
125 |
||
27936 | 126 |
|
31796 | 127 |
|
128 |
/** file path specifications **/ |
|
129 |
||
31820 | 130 |
/* expand_path */ |
31796 | 131 |
|
31820 | 132 |
def expand_path(isabelle_path: String): String = |
31796 | 133 |
{ |
134 |
val result_path = new StringBuilder |
|
135 |
def init(path: String) |
|
136 |
{ |
|
137 |
if (path.startsWith("/")) { |
|
138 |
result_path.clear |
|
139 |
result_path += '/' |
|
140 |
} |
|
141 |
} |
|
142 |
def append(path: String) |
|
143 |
{ |
|
144 |
init(path) |
|
31803 | 145 |
for (p <- path.split("/") if p != "" && p != ".") { |
146 |
if (p == "..") { |
|
147 |
val result = result_path.toString |
|
148 |
val i = result.lastIndexOf("/") |
|
149 |
if (result == "") |
|
150 |
result_path ++= ".." |
|
151 |
else if (result.substring(i + 1) == "..") |
|
152 |
result_path ++= "/.." |
|
153 |
else if (i < 1) |
|
154 |
result_path.length = i + 1 |
|
155 |
else |
|
156 |
result_path.length = i |
|
157 |
} |
|
158 |
else { |
|
159 |
val len = result_path.length |
|
160 |
if (len > 0 && result_path(len - 1) != '/') |
|
161 |
result_path += '/' |
|
162 |
result_path ++= p |
|
163 |
} |
|
31796 | 164 |
} |
165 |
} |
|
31820 | 166 |
init(isabelle_path) |
167 |
for (p <- isabelle_path.split("/")) { |
|
31796 | 168 |
if (p.startsWith("$")) append(getenv_strict(p.substring(1))) |
169 |
else if (p == "~") append(getenv_strict("HOME")) |
|
170 |
else if (p == "~~") append(getenv_strict("ISABELLE_HOME")) |
|
171 |
else append(p) |
|
172 |
} |
|
173 |
result_path.toString |
|
174 |
} |
|
175 |
||
176 |
||
31820 | 177 |
/* platform_path */ |
27936 | 178 |
|
31820 | 179 |
private val Cygdrive = |
180 |
new Regex(Pattern.quote(drive_prefix) + "/([a-zA-Z])($|/.*)") |
|
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
181 |
|
31820 | 182 |
def platform_path(isabelle_path: String): String = |
31498 | 183 |
{ |
27936 | 184 |
val result_path = new StringBuilder |
31796 | 185 |
val rest = |
31820 | 186 |
expand_path(isabelle_path) match { |
31825 | 187 |
case Cygdrive(drive, rest) if Platform.is_windows => |
31796 | 188 |
result_path ++= (drive + ":" + File.separator) |
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
189 |
rest |
31796 | 190 |
case path if path.startsWith("/") => |
191 |
result_path ++= platform_root |
|
192 |
path |
|
193 |
case path => path |
|
31500 | 194 |
} |
31796 | 195 |
for (p <- rest.split("/") if p != "") { |
196 |
val len = result_path.length |
|
197 |
if (len > 0 && result_path(len - 1) != File.separatorChar) |
|
198 |
result_path += File.separatorChar |
|
199 |
result_path ++= p |
|
27936 | 200 |
} |
201 |
result_path.toString |
|
202 |
} |
|
27953 | 203 |
|
31498 | 204 |
def platform_file(path: String) = new File(platform_path(path)) |
29152 | 205 |
|
27953 | 206 |
|
31820 | 207 |
/* isabelle_path */ |
208 |
||
209 |
private val Platform_Root = new Regex("(?i)" + |
|
31821
ce6be870a5d3
isabelle_path: slightly more liberal root pattern;
wenzelm
parents:
31820
diff
changeset
|
210 |
Pattern.quote(platform_root) + """(?:\\+|\z)(.*)""") |
31820 | 211 |
private val Drive = new Regex("""([a-zA-Z]):\\*(.*)""") |
212 |
||
213 |
def isabelle_path(platform_path: String): String = |
|
214 |
{ |
|
31825 | 215 |
if (Platform.is_windows) { |
31820 | 216 |
platform_path.replace('/', '\\') match { |
217 |
case Platform_Root(rest) => "/" + rest.replace('\\', '/') |
|
218 |
case Drive(letter, rest) => |
|
219 |
drive_prefix + "/" + letter.toLowerCase(Locale.ENGLISH) + |
|
220 |
(if (rest == "") "" else "/" + rest.replace('\\', '/')) |
|
221 |
case path => path.replace('\\', '/') |
|
222 |
} |
|
223 |
} |
|
224 |
else platform_path |
|
225 |
} |
|
226 |
||
227 |
||
31436 | 228 |
/* source files */ |
229 |
||
230 |
private def try_file(file: File) = if (file.isFile) Some(file) else None |
|
231 |
||
31498 | 232 |
def source_file(path: String): Option[File] = |
233 |
{ |
|
234 |
if (path.startsWith("/") || path == "") |
|
31436 | 235 |
try_file(platform_file(path)) |
236 |
else { |
|
237 |
val pure_file = platform_file("~~/src/Pure/" + path) |
|
238 |
if (pure_file.isFile) Some(pure_file) |
|
239 |
else if (getenv("ML_SOURCES") != "") |
|
240 |
try_file(platform_file("$ML_SOURCES/" + path)) |
|
241 |
else None |
|
242 |
} |
|
243 |
} |
|
244 |
||
245 |
||
32450 | 246 |
|
31796 | 247 |
/** system tools **/ |
248 |
||
31498 | 249 |
/* external processes */ |
27953 | 250 |
|
31498 | 251 |
def execute(redirect: Boolean, args: String*): Process = |
252 |
{ |
|
253 |
val cmdline = |
|
31825 | 254 |
if (Platform.is_windows) List(platform_path("/bin/env")) ++ args |
31498 | 255 |
else args |
31796 | 256 |
Isabelle_System.raw_execute(environment, redirect, cmdline: _*) |
28046 | 257 |
} |
258 |
||
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
259 |
def isabelle_tool(name: String, args: String*): (String, Int) = |
31498 | 260 |
{ |
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
261 |
getenv_strict("ISABELLE_TOOLS").split(":").find(dir => { |
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
262 |
val file = platform_file(dir + "/" + name) |
34025 | 263 |
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
|
264 |
catch { case _: SecurityException => false } |
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
265 |
}) match { |
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
266 |
case Some(dir) => |
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
267 |
val proc = execute(true, (List(platform_path(dir + "/" + name)) ++ args): _*) |
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
268 |
Isabelle_System.process_output(proc) |
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
269 |
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
|
270 |
} |
28063 | 271 |
} |
272 |
||
273 |
||
274 |
/* named pipes */ |
|
275 |
||
31498 | 276 |
def mk_fifo(): String = |
277 |
{ |
|
28496
4cff10648928
renamed isatool to isabelle_tool in programming interfaces;
wenzelm
parents:
28063
diff
changeset
|
278 |
val (result, rc) = isabelle_tool("mkfifo") |
28063 | 279 |
if (rc == 0) result.trim |
280 |
else error(result) |
|
281 |
} |
|
282 |
||
31498 | 283 |
def rm_fifo(fifo: String) |
284 |
{ |
|
28496
4cff10648928
renamed isatool to isabelle_tool in programming interfaces;
wenzelm
parents:
28063
diff
changeset
|
285 |
val (result, rc) = isabelle_tool("rmfifo", fifo) |
28063 | 286 |
if (rc != 0) error(result) |
287 |
} |
|
288 |
||
31498 | 289 |
def fifo_reader(fifo: String): BufferedReader = |
290 |
{ |
|
29177 | 291 |
// blocks until writer is ready |
292 |
val stream = |
|
31825 | 293 |
if (Platform.is_windows) execute(false, "cat", fifo).getInputStream |
29177 | 294 |
else new FileInputStream(fifo) |
31796 | 295 |
new BufferedReader(new InputStreamReader(stream, Isabelle_System.charset)) |
29177 | 296 |
} |
28063 | 297 |
|
29152 | 298 |
|
32450 | 299 |
|
31796 | 300 |
/** Isabelle resources **/ |
301 |
||
32328 | 302 |
/* components */ |
303 |
||
304 |
def components(): List[String] = |
|
305 |
getenv("ISABELLE_COMPONENTS").split(":").toList |
|
306 |
||
307 |
||
29152 | 308 |
/* find logics */ |
309 |
||
31498 | 310 |
def find_logics(): List[String] = |
311 |
{ |
|
29152 | 312 |
val ml_ident = getenv_strict("ML_IDENTIFIER") |
313 |
var logics: Set[String] = Set() |
|
314 |
for (dir <- getenv_strict("ISABELLE_PATH").split(":")) { |
|
315 |
val files = platform_file(dir + "/" + ml_ident).listFiles() |
|
316 |
if (files != null) { |
|
317 |
for (file <- files if file.isFile) logics += file.getName |
|
318 |
} |
|
319 |
} |
|
320 |
logics.toList.sort(_ < _) |
|
321 |
} |
|
29570 | 322 |
|
323 |
||
324 |
/* symbols */ |
|
325 |
||
31498 | 326 |
private def read_symbols(path: String): Iterator[String] = |
327 |
{ |
|
29570 | 328 |
val file = new File(platform_path(path)) |
31436 | 329 |
if (file.isFile) Source.fromFile(file).getLines |
29570 | 330 |
else Iterator.empty |
331 |
} |
|
332 |
val symbols = new Symbol.Interpretation( |
|
333 |
read_symbols("$ISABELLE_HOME/etc/symbols") ++ |
|
334 |
read_symbols("$ISABELLE_HOME_USER/etc/symbols")) |
|
34024 | 335 |
|
336 |
||
337 |
/* fonts */ |
|
338 |
||
339 |
val font_family = "IsabelleText" |
|
340 |
||
341 |
private def create_font(name: String) = |
|
342 |
Font.createFont(Font.TRUETYPE_FONT, platform_file(name)) |
|
343 |
||
34027 | 344 |
def register_fonts() { |
34024 | 345 |
val ge = GraphicsEnvironment.getLocalGraphicsEnvironment() |
346 |
val ok1 = ge.registerFont(create_font("~~/lib/fonts/IsabelleText.ttf")) |
|
347 |
val ok2 = ge.registerFont(create_font("~~/lib/fonts/IsabelleTextBold.ttf")) |
|
34027 | 348 |
if (!(ok1 && ok2) && !ge.getAvailableFontFamilyNames.contains(font_family)) |
349 |
error("Font family " + font_family + " unavailable") |
|
34024 | 350 |
} |
27919 | 351 |
} |