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