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