author | wenzelm |
Fri, 03 Jul 2009 21:14:16 +0200 | |
changeset 31927 | 9a0f28bcc81d |
parent 31829 | 6906e114c898 |
child 32328 | f2fd9da84bac |
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} |
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 |
|
31927
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
83 |
val isabelle_home = |
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
84 |
env0.get("ISABELLE_HOME") match { |
31498 | 85 |
case None | Some("") => |
31927
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
86 |
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
|
87 |
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
|
88 |
else path |
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
89 |
case Some(path) => path |
31498 | 90 |
} |
29177 | 91 |
|
31498 | 92 |
val dump = File.createTempFile("isabelle", null) |
93 |
try { |
|
31927
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
94 |
val cmdline = shell_prefix ::: |
9a0f28bcc81d
init isabelle home from existing setting or hint via system property;
wenzelm
parents:
31829
diff
changeset
|
95 |
List(isabelle_home + "/bin/isabelle", "getenv", "-d", dump.toString) |
31796 | 96 |
val proc = Isabelle_System.raw_execute(env0, true, cmdline: _*) |
97 |
val (output, rc) = Isabelle_System.process_output(proc) |
|
31498 | 98 |
if (rc != 0) error(output) |
99 |
||
100 |
val entries = |
|
101 |
for (entry <- Source.fromFile(dump).mkString split "\0" if entry != "") yield { |
|
102 |
val i = entry.indexOf('=') |
|
103 |
if (i <= 0) (entry -> "") |
|
104 |
else (entry.substring(0, i) -> entry.substring(i + 1)) |
|
105 |
} |
|
31704
b8628ac68b73
environment: keep original HOME and PATH (required for Cygwin integrity);
wenzelm
parents:
31703
diff
changeset
|
106 |
Map(entries: _*) + |
b8628ac68b73
environment: keep original HOME and PATH (required for Cygwin integrity);
wenzelm
parents:
31703
diff
changeset
|
107 |
("HOME" -> java.lang.System.getenv("HOME")) + |
b8628ac68b73
environment: keep original HOME and PATH (required for Cygwin integrity);
wenzelm
parents:
31703
diff
changeset
|
108 |
("PATH" -> java.lang.System.getenv("PATH")) |
31498 | 109 |
} |
110 |
finally { dump.delete } |
|
27953 | 111 |
} |
27919 | 112 |
|
31796 | 113 |
|
114 |
/* getenv */ |
|
115 |
||
31498 | 116 |
def getenv(name: String): String = |
117 |
environment.getOrElse(if (name == "HOME") "HOME_JVM" else name, "") |
|
118 |
||
119 |
def getenv_strict(name: String): String = |
|
120 |
{ |
|
31234
6ce6801129de
getenv_strict needs to be based on getenv (accidentally broken in 0e88d33e8d19);
wenzelm
parents:
30175
diff
changeset
|
121 |
val value = getenv(name) |
27993
6dd90ef9f927
simplified exceptions: use plain error function / RuntimeException;
wenzelm
parents:
27974
diff
changeset
|
122 |
if (value != "") value else error("Undefined environment variable: " + name) |
27919 | 123 |
} |
124 |
||
31703 | 125 |
override def toString = getenv("ISABELLE_HOME") |
126 |
||
27936 | 127 |
|
31796 | 128 |
|
129 |
/** file path specifications **/ |
|
130 |
||
31820 | 131 |
/* expand_path */ |
31796 | 132 |
|
31820 | 133 |
def expand_path(isabelle_path: String): String = |
31796 | 134 |
{ |
135 |
val result_path = new StringBuilder |
|
136 |
def init(path: String) |
|
137 |
{ |
|
138 |
if (path.startsWith("/")) { |
|
139 |
result_path.clear |
|
140 |
result_path += '/' |
|
141 |
} |
|
142 |
} |
|
143 |
def append(path: String) |
|
144 |
{ |
|
145 |
init(path) |
|
31803 | 146 |
for (p <- path.split("/") if p != "" && p != ".") { |
147 |
if (p == "..") { |
|
148 |
val result = result_path.toString |
|
149 |
val i = result.lastIndexOf("/") |
|
150 |
if (result == "") |
|
151 |
result_path ++= ".." |
|
152 |
else if (result.substring(i + 1) == "..") |
|
153 |
result_path ++= "/.." |
|
154 |
else if (i < 1) |
|
155 |
result_path.length = i + 1 |
|
156 |
else |
|
157 |
result_path.length = i |
|
158 |
} |
|
159 |
else { |
|
160 |
val len = result_path.length |
|
161 |
if (len > 0 && result_path(len - 1) != '/') |
|
162 |
result_path += '/' |
|
163 |
result_path ++= p |
|
164 |
} |
|
31796 | 165 |
} |
166 |
} |
|
31820 | 167 |
init(isabelle_path) |
168 |
for (p <- isabelle_path.split("/")) { |
|
31796 | 169 |
if (p.startsWith("$")) append(getenv_strict(p.substring(1))) |
170 |
else if (p == "~") append(getenv_strict("HOME")) |
|
171 |
else if (p == "~~") append(getenv_strict("ISABELLE_HOME")) |
|
172 |
else append(p) |
|
173 |
} |
|
174 |
result_path.toString |
|
175 |
} |
|
176 |
||
177 |
||
31820 | 178 |
/* platform_path */ |
27936 | 179 |
|
31820 | 180 |
private val Cygdrive = |
181 |
new Regex(Pattern.quote(drive_prefix) + "/([a-zA-Z])($|/.*)") |
|
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
182 |
|
31820 | 183 |
def platform_path(isabelle_path: String): String = |
31498 | 184 |
{ |
27936 | 185 |
val result_path = new StringBuilder |
31796 | 186 |
val rest = |
31820 | 187 |
expand_path(isabelle_path) match { |
31825 | 188 |
case Cygdrive(drive, rest) if Platform.is_windows => |
31796 | 189 |
result_path ++= (drive + ":" + File.separator) |
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
190 |
rest |
31796 | 191 |
case path if path.startsWith("/") => |
192 |
result_path ++= platform_root |
|
193 |
path |
|
194 |
case path => path |
|
31500 | 195 |
} |
31796 | 196 |
for (p <- rest.split("/") if p != "") { |
197 |
val len = result_path.length |
|
198 |
if (len > 0 && result_path(len - 1) != File.separatorChar) |
|
199 |
result_path += File.separatorChar |
|
200 |
result_path ++= p |
|
27936 | 201 |
} |
202 |
result_path.toString |
|
203 |
} |
|
27953 | 204 |
|
31498 | 205 |
def platform_file(path: String) = new File(platform_path(path)) |
29152 | 206 |
|
27953 | 207 |
|
31820 | 208 |
/* isabelle_path */ |
209 |
||
210 |
private val Platform_Root = new Regex("(?i)" + |
|
31821
ce6be870a5d3
isabelle_path: slightly more liberal root pattern;
wenzelm
parents:
31820
diff
changeset
|
211 |
Pattern.quote(platform_root) + """(?:\\+|\z)(.*)""") |
31820 | 212 |
private val Drive = new Regex("""([a-zA-Z]):\\*(.*)""") |
213 |
||
214 |
def isabelle_path(platform_path: String): String = |
|
215 |
{ |
|
31825 | 216 |
if (Platform.is_windows) { |
31820 | 217 |
platform_path.replace('/', '\\') match { |
218 |
case Platform_Root(rest) => "/" + rest.replace('\\', '/') |
|
219 |
case Drive(letter, rest) => |
|
220 |
drive_prefix + "/" + letter.toLowerCase(Locale.ENGLISH) + |
|
221 |
(if (rest == "") "" else "/" + rest.replace('\\', '/')) |
|
222 |
case path => path.replace('\\', '/') |
|
223 |
} |
|
224 |
} |
|
225 |
else platform_path |
|
226 |
} |
|
227 |
||
228 |
||
31436 | 229 |
/* source files */ |
230 |
||
231 |
private def try_file(file: File) = if (file.isFile) Some(file) else None |
|
232 |
||
31498 | 233 |
def source_file(path: String): Option[File] = |
234 |
{ |
|
235 |
if (path.startsWith("/") || path == "") |
|
31436 | 236 |
try_file(platform_file(path)) |
237 |
else { |
|
238 |
val pure_file = platform_file("~~/src/Pure/" + path) |
|
239 |
if (pure_file.isFile) Some(pure_file) |
|
240 |
else if (getenv("ML_SOURCES") != "") |
|
241 |
try_file(platform_file("$ML_SOURCES/" + path)) |
|
242 |
else None |
|
243 |
} |
|
244 |
} |
|
245 |
||
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) |
31824 | 263 |
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
|
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 |
|
31796 | 299 |
/** Isabelle resources **/ |
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 |
||
31498 | 319 |
private def read_symbols(path: String): Iterator[String] = |
320 |
{ |
|
29570 | 321 |
val file = new File(platform_path(path)) |
31436 | 322 |
if (file.isFile) Source.fromFile(file).getLines |
29570 | 323 |
else Iterator.empty |
324 |
} |
|
325 |
val symbols = new Symbol.Interpretation( |
|
326 |
read_symbols("$ISABELLE_HOME/etc/symbols") ++ |
|
327 |
read_symbols("$ISABELLE_HOME_USER/etc/symbols")) |
|
27919 | 328 |
} |