author | wenzelm |
Sun, 28 Jun 2009 16:59:43 +0200 | |
changeset 31829 | 6906e114c898 |
parent 31826 | 7f311da87d5a |
child 31927 | 9a0f28bcc81d |
permissions | -rwxr-xr-x |
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} |
12 |
||
13 |
import scala.io.Source |
|
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
14 |
import scala.util.matching.Regex |
27936 | 15 |
|
27919 | 16 |
|
31796 | 17 |
object Isabelle_System |
31498 | 18 |
{ |
28057 | 19 |
val charset = "UTF-8" |
20 |
||
31822 | 21 |
|
31498 | 22 |
/* shell processes */ |
23 |
||
24 |
private def raw_execute(env: Map[String, String], redirect: Boolean, args: String*): Process = |
|
25 |
{ |
|
26 |
val cmdline = new java.util.LinkedList[String] |
|
27 |
for (s <- args) cmdline.add(s) |
|
28 |
||
29 |
val proc = new ProcessBuilder(cmdline) |
|
30 |
proc.environment.clear |
|
31 |
for ((x, y) <- env) proc.environment.put(x, y) |
|
32 |
proc.redirectErrorStream(redirect) |
|
33 |
||
34 |
try { proc.start } |
|
35 |
catch { case e: IOException => error(e.getMessage) } |
|
36 |
} |
|
37 |
||
38 |
private def process_output(proc: Process): (String, Int) = |
|
39 |
{ |
|
40 |
proc.getOutputStream.close |
|
41 |
val output = Source.fromInputStream(proc.getInputStream, charset).mkString |
|
42 |
val rc = proc.waitFor |
|
43 |
(output, rc) |
|
44 |
} |
|
45 |
||
46 |
} |
|
47 |
||
48 |
||
31796 | 49 |
class Isabelle_System |
31498 | 50 |
{ |
28057 | 51 |
|
31796 | 52 |
/** unique ids **/ |
31443 | 53 |
|
54 |
private var id_count: BigInt = 0 |
|
55 |
def id(): String = synchronized { id_count += 1; "j" + id_count } |
|
56 |
||
57 |
||
31796 | 58 |
|
59 |
/** Isabelle environment **/ |
|
60 |
||
61 |
/* platform prefixes */ |
|
31498 | 62 |
|
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
63 |
private val (platform_root, drive_prefix, shell_prefix) = |
31500 | 64 |
{ |
31825 | 65 |
if (Platform.is_windows) { |
31829 | 66 |
val (root, drive) = Cygwin.config() |
31826 | 67 |
if (!Cygwin.check(root)) error("Bad Cygwin installation: " + root) |
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
68 |
val shell = List(root + "\\bin\\bash", "-l") |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
69 |
(root, drive, shell) |
31500 | 70 |
} |
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
71 |
else ("/", "", Nil) |
31500 | 72 |
} |
73 |
||
31796 | 74 |
|
75 |
/* bash environment */ |
|
76 |
||
31498 | 77 |
private val environment: Map[String, String] = |
78 |
{ |
|
79 |
import scala.collection.jcl.Conversions._ |
|
80 |
||
81 |
val env0 = Map(java.lang.System.getenv.toList: _*) |
|
27919 | 82 |
|
31498 | 83 |
val isabelle = |
84 |
env0.get("ISABELLE_TOOL") match { |
|
85 |
case None | Some("") => |
|
86 |
val isabelle = java.lang.System.getProperty("isabelle.tool") |
|
87 |
if (isabelle == null || isabelle == "") "isabelle" |
|
88 |
else isabelle |
|
89 |
case Some(isabelle) => isabelle |
|
90 |
} |
|
29177 | 91 |
|
31498 | 92 |
val dump = File.createTempFile("isabelle", null) |
93 |
try { |
|
31500 | 94 |
val cmdline = shell_prefix ::: List(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 |
||
31796 | 246 |
/** system tools **/ |
247 |
||
31498 | 248 |
/* external processes */ |
27953 | 249 |
|
31498 | 250 |
def execute(redirect: Boolean, args: String*): Process = |
251 |
{ |
|
252 |
val cmdline = |
|
31825 | 253 |
if (Platform.is_windows) List(platform_path("/bin/env")) ++ args |
31498 | 254 |
else args |
31796 | 255 |
Isabelle_System.raw_execute(environment, redirect, cmdline: _*) |
28046 | 256 |
} |
257 |
||
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
258 |
def isabelle_tool(name: String, args: String*): (String, Int) = |
31498 | 259 |
{ |
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
260 |
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
|
261 |
val file = platform_file(dir + "/" + name) |
31824 | 262 |
try { file.isFile && file.canRead } // file.canExecute requires Java 1.6 |
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
263 |
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
|
264 |
}) match { |
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
265 |
case Some(dir) => |
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
31803
diff
changeset
|
266 |
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
|
267 |
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
|
268 |
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
|
269 |
} |
28063 | 270 |
} |
271 |
||
272 |
||
273 |
/* named pipes */ |
|
274 |
||
31498 | 275 |
def mk_fifo(): String = |
276 |
{ |
|
28496
4cff10648928
renamed isatool to isabelle_tool in programming interfaces;
wenzelm
parents:
28063
diff
changeset
|
277 |
val (result, rc) = isabelle_tool("mkfifo") |
28063 | 278 |
if (rc == 0) result.trim |
279 |
else error(result) |
|
280 |
} |
|
281 |
||
31498 | 282 |
def rm_fifo(fifo: String) |
283 |
{ |
|
28496
4cff10648928
renamed isatool to isabelle_tool in programming interfaces;
wenzelm
parents:
28063
diff
changeset
|
284 |
val (result, rc) = isabelle_tool("rmfifo", fifo) |
28063 | 285 |
if (rc != 0) error(result) |
286 |
} |
|
287 |
||
31498 | 288 |
def fifo_reader(fifo: String): BufferedReader = |
289 |
{ |
|
29177 | 290 |
// blocks until writer is ready |
291 |
val stream = |
|
31825 | 292 |
if (Platform.is_windows) execute(false, "cat", fifo).getInputStream |
29177 | 293 |
else new FileInputStream(fifo) |
31796 | 294 |
new BufferedReader(new InputStreamReader(stream, Isabelle_System.charset)) |
29177 | 295 |
} |
28063 | 296 |
|
29152 | 297 |
|
31796 | 298 |
/** Isabelle resources **/ |
299 |
||
29152 | 300 |
/* find logics */ |
301 |
||
31498 | 302 |
def find_logics(): List[String] = |
303 |
{ |
|
29152 | 304 |
val ml_ident = getenv_strict("ML_IDENTIFIER") |
305 |
var logics: Set[String] = Set() |
|
306 |
for (dir <- getenv_strict("ISABELLE_PATH").split(":")) { |
|
307 |
val files = platform_file(dir + "/" + ml_ident).listFiles() |
|
308 |
if (files != null) { |
|
309 |
for (file <- files if file.isFile) logics += file.getName |
|
310 |
} |
|
311 |
} |
|
312 |
logics.toList.sort(_ < _) |
|
313 |
} |
|
29570 | 314 |
|
315 |
||
316 |
/* symbols */ |
|
317 |
||
31498 | 318 |
private def read_symbols(path: String): Iterator[String] = |
319 |
{ |
|
29570 | 320 |
val file = new File(platform_path(path)) |
31436 | 321 |
if (file.isFile) Source.fromFile(file).getLines |
29570 | 322 |
else Iterator.empty |
323 |
} |
|
324 |
val symbols = new Symbol.Interpretation( |
|
325 |
read_symbols("$ISABELLE_HOME/etc/symbols") ++ |
|
326 |
read_symbols("$ISABELLE_HOME_USER/etc/symbols")) |
|
27919 | 327 |
} |