author | wenzelm |
Mon, 28 Dec 2009 20:24:09 +0100 | |
changeset 34200 | ca5f522fa233 |
parent 34199 | 1e40a1009ac1 |
child 34201 | c95dcd12f48a |
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 |
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 |
|
31796 | 21 |
object Isabelle_System |
31498 | 22 |
{ |
28057 | 23 |
val charset = "UTF-8" |
24 |
||
31822 | 25 |
|
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
26 |
/* permissive UTF-8 decoding */ |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
27 |
|
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
28 |
// see also http://en.wikipedia.org/wiki/UTF-8#Description |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
29 |
def decode_permissive_utf8(text: CharSequence): String = |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
30 |
{ |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
31 |
val buf = new java.lang.StringBuilder(text.length) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
32 |
var code = -1 |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
33 |
var rest = 0 |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
34 |
def flush() |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
35 |
{ |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
36 |
if (code != -1) { |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
37 |
if (rest == 0 && Character.isValidCodePoint(code)) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
38 |
buf.appendCodePoint(code) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
39 |
else buf.append('\uFFFD') |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
40 |
code = -1 |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
41 |
rest = 0 |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
42 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
43 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
44 |
def init(x: Int, n: Int) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
45 |
{ |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
46 |
flush() |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
47 |
code = x |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
48 |
rest = n |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
49 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
50 |
def push(x: Int) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
51 |
{ |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
52 |
if (rest <= 0) init(x, -1) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
53 |
else { |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
54 |
code <<= 6 |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
55 |
code += x |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
56 |
rest -= 1 |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
57 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
58 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
59 |
for (i <- 0 until text.length) { |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
60 |
val c = text.charAt(i) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
61 |
if (c < 128) { flush(); buf.append(c) } |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
62 |
else if ((c & 0xC0) == 0x80) push(c & 0x3F) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
63 |
else if ((c & 0xE0) == 0xC0) init(c & 0x1F, 1) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
64 |
else if ((c & 0xF0) == 0xE0) init(c & 0x0F, 2) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
65 |
else if ((c & 0xF8) == 0xF0) init(c & 0x07, 3) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
66 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
67 |
flush() |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
68 |
buf.toString |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
69 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
70 |
|
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
71 |
|
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
72 |
/* basic file operations */ |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
73 |
|
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
74 |
def with_tmp_file[A](prefix: String)(body: File => A): A = |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
75 |
{ |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
76 |
val file = File.createTempFile(prefix, null) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
77 |
val result = |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
78 |
try { body(file) } |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
79 |
finally { file.delete } |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
80 |
result |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
81 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
82 |
|
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
83 |
def read_file(file: File): String = |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
84 |
{ |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
85 |
val buf = new StringBuilder(file.length.toInt) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
86 |
val reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset)) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
87 |
var c = reader.read |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
88 |
while (c != -1) { |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
89 |
buf.append(c.toChar) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
90 |
c = reader.read |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
91 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
92 |
reader.close |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
93 |
buf.toString |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
94 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
95 |
|
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
96 |
def write_file(file: File, text: CharSequence) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
97 |
{ |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
98 |
val writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
99 |
try { writer.append(text) } |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
100 |
finally { writer.close } |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
101 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
102 |
|
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
103 |
|
31498 | 104 |
/* shell processes */ |
105 |
||
106 |
private def raw_execute(env: Map[String, String], redirect: Boolean, args: String*): Process = |
|
107 |
{ |
|
108 |
val cmdline = new java.util.LinkedList[String] |
|
109 |
for (s <- args) cmdline.add(s) |
|
110 |
||
111 |
val proc = new ProcessBuilder(cmdline) |
|
112 |
proc.environment.clear |
|
113 |
for ((x, y) <- env) proc.environment.put(x, y) |
|
114 |
proc.redirectErrorStream(redirect) |
|
115 |
||
116 |
try { proc.start } |
|
117 |
catch { case e: IOException => error(e.getMessage) } |
|
118 |
} |
|
119 |
||
120 |
private def process_output(proc: Process): (String, Int) = |
|
121 |
{ |
|
122 |
proc.getOutputStream.close |
|
123 |
val output = Source.fromInputStream(proc.getInputStream, charset).mkString |
|
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
|
124 |
val rc = |
001321ca185c
slightly more paranoid cleanup of process (cf. http://kylecartmell.com/?p=9 "Five Common java.lang.Process Pitfalls");
wenzelm
parents:
34186
diff
changeset
|
125 |
try { proc.waitFor } |
001321ca185c
slightly more paranoid cleanup of process (cf. http://kylecartmell.com/?p=9 "Five Common java.lang.Process Pitfalls");
wenzelm
parents:
34186
diff
changeset
|
126 |
finally { |
001321ca185c
slightly more paranoid cleanup of process (cf. http://kylecartmell.com/?p=9 "Five Common java.lang.Process Pitfalls");
wenzelm
parents:
34186
diff
changeset
|
127 |
proc.getInputStream.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
|
128 |
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
|
129 |
proc.destroy |
001321ca185c
slightly more paranoid cleanup of process (cf. http://kylecartmell.com/?p=9 "Five Common java.lang.Process Pitfalls");
wenzelm
parents:
34186
diff
changeset
|
130 |
Thread.interrupted |
001321ca185c
slightly more paranoid cleanup of process (cf. http://kylecartmell.com/?p=9 "Five Common java.lang.Process Pitfalls");
wenzelm
parents:
34186
diff
changeset
|
131 |
} |
31498 | 132 |
(output, rc) |
133 |
} |
|
134 |
} |
|
135 |
||
136 |
||
31796 | 137 |
class Isabelle_System |
31498 | 138 |
{ |
31796 | 139 |
/** Isabelle environment **/ |
140 |
||
141 |
/* platform prefixes */ |
|
31498 | 142 |
|
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
143 |
private val (platform_root, drive_prefix, shell_prefix) = |
31500 | 144 |
{ |
31825 | 145 |
if (Platform.is_windows) { |
34045
bc71778a327d
simplified Cygwin setup, assuming 1.7 registry layout (version 1.5 suffers from upcaseenv problem anyway);
wenzelm
parents:
34044
diff
changeset
|
146 |
val root = Cygwin.check_root() |
bc71778a327d
simplified Cygwin setup, assuming 1.7 registry layout (version 1.5 suffers from upcaseenv problem anyway);
wenzelm
parents:
34044
diff
changeset
|
147 |
val drive = "/cygdrive" |
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
148 |
val shell = List(root + "\\bin\\bash", "-l") |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
149 |
(root, drive, shell) |
31500 | 150 |
} |
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
151 |
else ("/", "", Nil) |
31500 | 152 |
} |
153 |
||
31796 | 154 |
|
155 |
/* bash environment */ |
|
156 |
||
31498 | 157 |
private val environment: Map[String, String] = |
158 |
{ |
|
159 |
import scala.collection.jcl.Conversions._ |
|
160 |
||
161 |
val env0 = Map(java.lang.System.getenv.toList: _*) |
|
27919 | 162 |
|
31927
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
163 |
val isabelle_home = |
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
164 |
env0.get("ISABELLE_HOME") match { |
31498 | 165 |
case None | Some("") => |
31927
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
166 |
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
|
167 |
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
|
168 |
else path |
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
169 |
case Some(path) => path |
31498 | 170 |
} |
29177 | 171 |
|
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
172 |
Isabelle_System.with_tmp_file("isabelle_settings") { dump => |
31927
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
173 |
val cmdline = shell_prefix ::: |
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
174 |
List(isabelle_home + "/bin/isabelle", "getenv", "-d", dump.toString) |
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
|
175 |
val (output, rc) = |
001321ca185c
slightly more paranoid cleanup of process (cf. http://kylecartmell.com/?p=9 "Five Common java.lang.Process Pitfalls");
wenzelm
parents:
34186
diff
changeset
|
176 |
Isabelle_System.process_output(Isabelle_System.raw_execute(env0, true, cmdline: _*)) |
31498 | 177 |
if (rc != 0) error(output) |
178 |
||
179 |
val entries = |
|
180 |
for (entry <- Source.fromFile(dump).mkString split "\0" if entry != "") yield { |
|
181 |
val i = entry.indexOf('=') |
|
182 |
if (i <= 0) (entry -> "") |
|
183 |
else (entry.substring(0, i) -> entry.substring(i + 1)) |
|
184 |
} |
|
31704
b8628ac68b73
environment: keep original HOME and PATH (required for Cygwin integrity);
wenzelm
parents:
31703
diff
changeset
|
185 |
Map(entries: _*) + |
b8628ac68b73
environment: keep original HOME and PATH (required for Cygwin integrity);
wenzelm
parents:
31703
diff
changeset
|
186 |
("HOME" -> java.lang.System.getenv("HOME")) + |
b8628ac68b73
environment: keep original HOME and PATH (required for Cygwin integrity);
wenzelm
parents:
31703
diff
changeset
|
187 |
("PATH" -> java.lang.System.getenv("PATH")) |
31498 | 188 |
} |
27953 | 189 |
} |
27919 | 190 |
|
31796 | 191 |
|
192 |
/* getenv */ |
|
193 |
||
31498 | 194 |
def getenv(name: String): String = |
195 |
environment.getOrElse(if (name == "HOME") "HOME_JVM" else name, "") |
|
196 |
||
197 |
def getenv_strict(name: String): String = |
|
198 |
{ |
|
31234
6ce6801129de
getenv_strict needs to be based on getenv (accidentally broken in 0e88d33e8d19);
wenzelm
parents:
30175
diff
changeset
|
199 |
val value = getenv(name) |
27993
6dd90ef9f927
simplified exceptions: use plain error function / RuntimeException;
wenzelm
parents:
27974
diff
changeset
|
200 |
if (value != "") value else error("Undefined environment variable: " + name) |
27919 | 201 |
} |
202 |
||
31703 | 203 |
override def toString = getenv("ISABELLE_HOME") |
204 |
||
27936 | 205 |
|
31796 | 206 |
|
207 |
/** file path specifications **/ |
|
208 |
||
31820 | 209 |
/* expand_path */ |
31796 | 210 |
|
31820 | 211 |
def expand_path(isabelle_path: String): String = |
31796 | 212 |
{ |
213 |
val result_path = new StringBuilder |
|
214 |
def init(path: String) |
|
215 |
{ |
|
216 |
if (path.startsWith("/")) { |
|
217 |
result_path.clear |
|
218 |
result_path += '/' |
|
219 |
} |
|
220 |
} |
|
221 |
def append(path: String) |
|
222 |
{ |
|
223 |
init(path) |
|
31803 | 224 |
for (p <- path.split("/") if p != "" && p != ".") { |
225 |
if (p == "..") { |
|
226 |
val result = result_path.toString |
|
227 |
val i = result.lastIndexOf("/") |
|
228 |
if (result == "") |
|
229 |
result_path ++= ".." |
|
230 |
else if (result.substring(i + 1) == "..") |
|
231 |
result_path ++= "/.." |
|
232 |
else if (i < 1) |
|
233 |
result_path.length = i + 1 |
|
234 |
else |
|
235 |
result_path.length = i |
|
236 |
} |
|
237 |
else { |
|
238 |
val len = result_path.length |
|
239 |
if (len > 0 && result_path(len - 1) != '/') |
|
240 |
result_path += '/' |
|
241 |
result_path ++= p |
|
242 |
} |
|
31796 | 243 |
} |
244 |
} |
|
31820 | 245 |
init(isabelle_path) |
246 |
for (p <- isabelle_path.split("/")) { |
|
31796 | 247 |
if (p.startsWith("$")) append(getenv_strict(p.substring(1))) |
248 |
else if (p == "~") append(getenv_strict("HOME")) |
|
249 |
else if (p == "~~") append(getenv_strict("ISABELLE_HOME")) |
|
250 |
else append(p) |
|
251 |
} |
|
252 |
result_path.toString |
|
253 |
} |
|
254 |
||
255 |
||
31820 | 256 |
/* platform_path */ |
27936 | 257 |
|
31820 | 258 |
private val Cygdrive = |
259 |
new Regex(Pattern.quote(drive_prefix) + "/([a-zA-Z])($|/.*)") |
|
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
260 |
|
31820 | 261 |
def platform_path(isabelle_path: String): String = |
31498 | 262 |
{ |
27936 | 263 |
val result_path = new StringBuilder |
31796 | 264 |
val rest = |
31820 | 265 |
expand_path(isabelle_path) match { |
31825 | 266 |
case Cygdrive(drive, rest) if Platform.is_windows => |
31796 | 267 |
result_path ++= (drive + ":" + File.separator) |
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
268 |
rest |
31796 | 269 |
case path if path.startsWith("/") => |
270 |
result_path ++= platform_root |
|
271 |
path |
|
272 |
case path => path |
|
31500 | 273 |
} |
31796 | 274 |
for (p <- rest.split("/") if p != "") { |
275 |
val len = result_path.length |
|
276 |
if (len > 0 && result_path(len - 1) != File.separatorChar) |
|
277 |
result_path += File.separatorChar |
|
278 |
result_path ++= p |
|
27936 | 279 |
} |
280 |
result_path.toString |
|
281 |
} |
|
27953 | 282 |
|
31498 | 283 |
def platform_file(path: String) = new File(platform_path(path)) |
29152 | 284 |
|
27953 | 285 |
|
31820 | 286 |
/* isabelle_path */ |
287 |
||
288 |
private val Platform_Root = new Regex("(?i)" + |
|
31821
ce6be870a5d3
isabelle_path: slightly more liberal root pattern;
wenzelm
parents:
31820
diff
changeset
|
289 |
Pattern.quote(platform_root) + """(?:\\+|\z)(.*)""") |
31820 | 290 |
private val Drive = new Regex("""([a-zA-Z]):\\*(.*)""") |
291 |
||
292 |
def isabelle_path(platform_path: String): String = |
|
293 |
{ |
|
31825 | 294 |
if (Platform.is_windows) { |
31820 | 295 |
platform_path.replace('/', '\\') match { |
296 |
case Platform_Root(rest) => "/" + rest.replace('\\', '/') |
|
297 |
case Drive(letter, rest) => |
|
298 |
drive_prefix + "/" + letter.toLowerCase(Locale.ENGLISH) + |
|
299 |
(if (rest == "") "" else "/" + rest.replace('\\', '/')) |
|
300 |
case path => path.replace('\\', '/') |
|
301 |
} |
|
302 |
} |
|
303 |
else platform_path |
|
304 |
} |
|
305 |
||
306 |
||
31436 | 307 |
/* source files */ |
308 |
||
309 |
private def try_file(file: File) = if (file.isFile) Some(file) else None |
|
310 |
||
31498 | 311 |
def source_file(path: String): Option[File] = |
312 |
{ |
|
313 |
if (path.startsWith("/") || path == "") |
|
31436 | 314 |
try_file(platform_file(path)) |
315 |
else { |
|
316 |
val pure_file = platform_file("~~/src/Pure/" + path) |
|
317 |
if (pure_file.isFile) Some(pure_file) |
|
318 |
else if (getenv("ML_SOURCES") != "") |
|
319 |
try_file(platform_file("$ML_SOURCES/" + path)) |
|
320 |
else None |
|
321 |
} |
|
322 |
} |
|
323 |
||
324 |
||
32450 | 325 |
|
31796 | 326 |
/** system tools **/ |
327 |
||
31498 | 328 |
/* external processes */ |
27953 | 329 |
|
31498 | 330 |
def execute(redirect: Boolean, args: String*): Process = |
331 |
{ |
|
332 |
val cmdline = |
|
31825 | 333 |
if (Platform.is_windows) List(platform_path("/bin/env")) ++ args |
31498 | 334 |
else args |
31796 | 335 |
Isabelle_System.raw_execute(environment, redirect, cmdline: _*) |
28046 | 336 |
} |
337 |
||
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
338 |
def system_out(script: String): (String, Int) = |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
339 |
{ |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
340 |
Isabelle_System.with_tmp_file("isabelle_script") { script_file => |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
341 |
Isabelle_System.with_tmp_file("isabelle_pid") { pid_file => |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
342 |
Isabelle_System.with_tmp_file("isabelle_output") { output_file => |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
343 |
|
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
344 |
Isabelle_System.write_file(script_file, script) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
345 |
|
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
346 |
val proc = execute(true, "perl", "-w", |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
347 |
expand_path("$ISABELLE_HOME/lib/scripts/system.pl"), "group", |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
348 |
script_file.getPath, pid_file.getPath, output_file.getPath) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
349 |
|
34199 | 350 |
def kill(strict: Boolean) = |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
351 |
{ |
34199 | 352 |
val pid = |
353 |
try { Some(Isabelle_System.read_file(pid_file)) } |
|
354 |
catch { case _: IOException => None } |
|
355 |
if (pid.isDefined) { |
|
356 |
var running = true |
|
357 |
var count = 10 |
|
358 |
while (running && count > 0) { |
|
359 |
if (execute(true, "kill", "-INT", "-" + pid.get).waitFor != 0) |
|
360 |
running = false |
|
361 |
else { |
|
362 |
Thread.sleep(100) |
|
363 |
if (!strict) count -= 1 |
|
364 |
} |
|
365 |
} |
|
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
366 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
367 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
368 |
|
34199 | 369 |
val shutdown_hook = new Thread { override def run = kill(true) } |
34198
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
370 |
Runtime.getRuntime.addShutdownHook(shutdown_hook) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
371 |
|
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
372 |
def cleanup() = |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
373 |
try { Runtime.getRuntime.removeShutdownHook(shutdown_hook) } |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
374 |
catch { case _: IllegalStateException => } |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
375 |
|
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
376 |
val rc = |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
377 |
try { |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
378 |
try { proc.waitFor } // FIXME read stderr (!??) |
34199 | 379 |
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
|
380 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
381 |
finally { |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
382 |
proc.getOutputStream.close |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
383 |
proc.getInputStream.close |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
384 |
proc.getErrorStream.close |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
385 |
proc.destroy |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
386 |
cleanup() |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
387 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
388 |
|
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
389 |
val output = |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
390 |
try { Isabelle_System.read_file(output_file) } |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
391 |
catch { case _: IOException => "" } |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
392 |
|
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
393 |
(output, rc) |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
394 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
395 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
396 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
397 |
} |
ff5486262cd6
moved Library.decode_permissive_utf8 to Isabelle_System;
wenzelm
parents:
34196
diff
changeset
|
398 |
|
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
399 |
def isabelle_tool(name: String, args: String*): (String, Int) = |
31498 | 400 |
{ |
34200 | 401 |
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
|
402 |
val file = platform_file(dir + "/" + name) |
34025 | 403 |
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
|
404 |
catch { case _: SecurityException => false } |
34200 | 405 |
} match { |
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
406 |
case Some(dir) => |
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
|
407 |
Isabelle_System.process_output( |
34195 | 408 |
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
|
409 |
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
|
410 |
} |
28063 | 411 |
} |
412 |
||
413 |
||
414 |
/* named pipes */ |
|
415 |
||
31498 | 416 |
def mk_fifo(): String = |
417 |
{ |
|
28496
4cff10648928
renamed isatool to isabelle_tool in programming interfaces;
wenzelm
parents:
28063
diff
changeset
|
418 |
val (result, rc) = isabelle_tool("mkfifo") |
28063 | 419 |
if (rc == 0) result.trim |
420 |
else error(result) |
|
421 |
} |
|
422 |
||
31498 | 423 |
def rm_fifo(fifo: String) |
424 |
{ |
|
28496
4cff10648928
renamed isatool to isabelle_tool in programming interfaces;
wenzelm
parents:
28063
diff
changeset
|
425 |
val (result, rc) = isabelle_tool("rmfifo", fifo) |
28063 | 426 |
if (rc != 0) error(result) |
427 |
} |
|
428 |
||
34100 | 429 |
def fifo_stream(fifo: String): BufferedInputStream = |
31498 | 430 |
{ |
29177 | 431 |
// blocks until writer is ready |
432 |
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
|
433 |
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
|
434 |
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
|
435 |
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
|
436 |
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
|
437 |
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
|
438 |
} |
29177 | 439 |
else new FileInputStream(fifo) |
34100 | 440 |
new BufferedInputStream(stream) |
29177 | 441 |
} |
28063 | 442 |
|
29152 | 443 |
|
32450 | 444 |
|
31796 | 445 |
/** Isabelle resources **/ |
446 |
||
32328 | 447 |
/* components */ |
448 |
||
449 |
def components(): List[String] = |
|
450 |
getenv("ISABELLE_COMPONENTS").split(":").toList |
|
451 |
||
452 |
||
29152 | 453 |
/* find logics */ |
454 |
||
31498 | 455 |
def find_logics(): List[String] = |
456 |
{ |
|
29152 | 457 |
val ml_ident = getenv_strict("ML_IDENTIFIER") |
34163 | 458 |
val logics = new mutable.ListBuffer[String] |
29152 | 459 |
for (dir <- getenv_strict("ISABELLE_PATH").split(":")) { |
460 |
val files = platform_file(dir + "/" + ml_ident).listFiles() |
|
461 |
if (files != null) { |
|
462 |
for (file <- files if file.isFile) logics += file.getName |
|
463 |
} |
|
464 |
} |
|
465 |
logics.toList.sort(_ < _) |
|
466 |
} |
|
29570 | 467 |
|
468 |
||
469 |
/* symbols */ |
|
470 |
||
34137 | 471 |
private def read_symbols(path: String): List[String] = |
31498 | 472 |
{ |
34195 | 473 |
val file = platform_file(path) |
34137 | 474 |
if (file.isFile) Source.fromFile(file).getLines.toList |
475 |
else Nil |
|
29570 | 476 |
} |
477 |
val symbols = new Symbol.Interpretation( |
|
34137 | 478 |
read_symbols("$ISABELLE_HOME/etc/symbols") ::: |
29570 | 479 |
read_symbols("$ISABELLE_HOME_USER/etc/symbols")) |
34024 | 480 |
|
481 |
||
482 |
/* fonts */ |
|
483 |
||
484 |
val font_family = "IsabelleText" |
|
485 |
||
34044
09afb1d49fe7
slightly more robust and less ambitious version of install_fonts;
wenzelm
parents:
34027
diff
changeset
|
486 |
private def check_font(): Boolean = |
09afb1d49fe7
slightly more robust and less ambitious version of install_fonts;
wenzelm
parents:
34027
diff
changeset
|
487 |
new Font(font_family, Font.PLAIN, 1).getFamily == font_family |
09afb1d49fe7
slightly more robust and less ambitious version of install_fonts;
wenzelm
parents:
34027
diff
changeset
|
488 |
|
34024 | 489 |
private def create_font(name: String) = |
490 |
Font.createFont(Font.TRUETYPE_FONT, platform_file(name)) |
|
491 |
||
34044
09afb1d49fe7
slightly more robust and less ambitious version of install_fonts;
wenzelm
parents:
34027
diff
changeset
|
492 |
def install_fonts() |
09afb1d49fe7
slightly more robust and less ambitious version of install_fonts;
wenzelm
parents:
34027
diff
changeset
|
493 |
{ |
09afb1d49fe7
slightly more robust and less ambitious version of install_fonts;
wenzelm
parents:
34027
diff
changeset
|
494 |
if (!check_font()) { |
09afb1d49fe7
slightly more robust and less ambitious version of install_fonts;
wenzelm
parents:
34027
diff
changeset
|
495 |
val ge = GraphicsEnvironment.getLocalGraphicsEnvironment() |
09afb1d49fe7
slightly more robust and less ambitious version of install_fonts;
wenzelm
parents:
34027
diff
changeset
|
496 |
ge.registerFont(create_font("$ISABELLE_HOME/lib/fonts/IsabelleText.ttf")) |
09afb1d49fe7
slightly more robust and less ambitious version of install_fonts;
wenzelm
parents:
34027
diff
changeset
|
497 |
ge.registerFont(create_font("$ISABELLE_HOME/lib/fonts/IsabelleTextBold.ttf")) |
09afb1d49fe7
slightly more robust and less ambitious version of install_fonts;
wenzelm
parents:
34027
diff
changeset
|
498 |
if (!check_font()) |
09afb1d49fe7
slightly more robust and less ambitious version of install_fonts;
wenzelm
parents:
34027
diff
changeset
|
499 |
error("Failed to install IsabelleText fonts") |
09afb1d49fe7
slightly more robust and less ambitious version of install_fonts;
wenzelm
parents:
34027
diff
changeset
|
500 |
} |
34024 | 501 |
} |
27919 | 502 |
} |