author | wenzelm |
Thu, 18 Jun 2009 17:22:41 +0200 | |
changeset 31704 | b8628ac68b73 |
parent 31703 | 4e6064759aeb |
child 31796 | 117300d72398 |
permissions | -rwxr-xr-x |
30175 | 1 |
/* Title: Pure/System/isabelle_system.scala |
27919 | 2 |
Author: Makarius |
3 |
||
27974 | 4 |
Isabelle system support -- 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 |
28063 | 10 |
import java.io.{BufferedReader, InputStreamReader, FileInputStream, File, IOException} |
11 |
||
12 |
import scala.io.Source |
|
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
13 |
import scala.util.matching.Regex |
27936 | 14 |
|
27919 | 15 |
|
31498 | 16 |
object IsabelleSystem |
17 |
{ |
|
27919 | 18 |
|
28057 | 19 |
val charset = "UTF-8" |
20 |
||
31498 | 21 |
val is_cygwin = System.getProperty("os.name").startsWith("Windows") |
22 |
||
23 |
||
24 |
/* shell processes */ |
|
25 |
||
26 |
private def raw_execute(env: Map[String, String], redirect: Boolean, args: String*): Process = |
|
27 |
{ |
|
28 |
val cmdline = new java.util.LinkedList[String] |
|
29 |
for (s <- args) cmdline.add(s) |
|
30 |
||
31 |
val proc = new ProcessBuilder(cmdline) |
|
32 |
proc.environment.clear |
|
33 |
for ((x, y) <- env) proc.environment.put(x, y) |
|
34 |
proc.redirectErrorStream(redirect) |
|
35 |
||
36 |
try { proc.start } |
|
37 |
catch { case e: IOException => error(e.getMessage) } |
|
38 |
} |
|
39 |
||
40 |
private def process_output(proc: Process): (String, Int) = |
|
41 |
{ |
|
42 |
proc.getOutputStream.close |
|
43 |
val output = Source.fromInputStream(proc.getInputStream, charset).mkString |
|
44 |
val rc = proc.waitFor |
|
45 |
(output, rc) |
|
46 |
} |
|
47 |
||
48 |
} |
|
49 |
||
50 |
||
51 |
class IsabelleSystem |
|
52 |
{ |
|
28057 | 53 |
|
31443 | 54 |
/* unique ids */ |
55 |
||
56 |
private var id_count: BigInt = 0 |
|
57 |
def id(): String = synchronized { id_count += 1; "j" + id_count } |
|
58 |
||
59 |
||
31498 | 60 |
/* Isabelle settings environment */ |
61 |
||
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
62 |
private val (platform_root, drive_prefix, shell_prefix) = |
31500 | 63 |
{ |
64 |
if (IsabelleSystem.is_cygwin) { |
|
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
65 |
val root = Cygwin.root() getOrElse "C:\\cygwin" |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
66 |
val drive = Cygwin.cygdrive() getOrElse "/cygdrive" |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
67 |
val shell = List(root + "\\bin\\bash", "-l") |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
68 |
(root, drive, shell) |
31500 | 69 |
} |
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
70 |
else ("/", "", Nil) |
31500 | 71 |
} |
72 |
||
31498 | 73 |
private val environment: Map[String, String] = |
74 |
{ |
|
75 |
import scala.collection.jcl.Conversions._ |
|
76 |
||
77 |
val env0 = Map(java.lang.System.getenv.toList: _*) |
|
27919 | 78 |
|
31498 | 79 |
val isabelle = |
80 |
env0.get("ISABELLE_TOOL") match { |
|
81 |
case None | Some("") => |
|
82 |
val isabelle = java.lang.System.getProperty("isabelle.tool") |
|
83 |
if (isabelle == null || isabelle == "") "isabelle" |
|
84 |
else isabelle |
|
85 |
case Some(isabelle) => isabelle |
|
86 |
} |
|
29177 | 87 |
|
31498 | 88 |
val dump = File.createTempFile("isabelle", null) |
89 |
try { |
|
31500 | 90 |
val cmdline = shell_prefix ::: List(isabelle, "getenv", "-d", dump.toString) |
31498 | 91 |
val proc = IsabelleSystem.raw_execute(env0, true, cmdline: _*) |
92 |
val (output, rc) = IsabelleSystem.process_output(proc) |
|
93 |
if (rc != 0) error(output) |
|
94 |
||
95 |
val entries = |
|
96 |
for (entry <- Source.fromFile(dump).mkString split "\0" if entry != "") yield { |
|
97 |
val i = entry.indexOf('=') |
|
98 |
if (i <= 0) (entry -> "") |
|
99 |
else (entry.substring(0, i) -> entry.substring(i + 1)) |
|
100 |
} |
|
31704
b8628ac68b73
environment: keep original HOME and PATH (required for Cygwin integrity);
wenzelm
parents:
31703
diff
changeset
|
101 |
Map(entries: _*) + |
b8628ac68b73
environment: keep original HOME and PATH (required for Cygwin integrity);
wenzelm
parents:
31703
diff
changeset
|
102 |
("HOME" -> java.lang.System.getenv("HOME")) + |
b8628ac68b73
environment: keep original HOME and PATH (required for Cygwin integrity);
wenzelm
parents:
31703
diff
changeset
|
103 |
("PATH" -> java.lang.System.getenv("PATH")) |
31498 | 104 |
} |
105 |
finally { dump.delete } |
|
27953 | 106 |
} |
27919 | 107 |
|
31498 | 108 |
def getenv(name: String): String = |
109 |
{ |
|
110 |
environment.getOrElse(if (name == "HOME") "HOME_JVM" else name, "") |
|
111 |
} |
|
112 |
||
113 |
def getenv_strict(name: String): String = |
|
114 |
{ |
|
31234
6ce6801129de
getenv_strict needs to be based on getenv (accidentally broken in 0e88d33e8d19);
wenzelm
parents:
30175
diff
changeset
|
115 |
val value = getenv(name) |
27993
6dd90ef9f927
simplified exceptions: use plain error function / RuntimeException;
wenzelm
parents:
27974
diff
changeset
|
116 |
if (value != "") value else error("Undefined environment variable: " + name) |
27919 | 117 |
} |
118 |
||
31703 | 119 |
override def toString = getenv("ISABELLE_HOME") |
120 |
||
27936 | 121 |
|
27974 | 122 |
/* file path specifications */ |
27936 | 123 |
|
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
124 |
private val Cygdrive = new Regex( |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
125 |
if (drive_prefix == "") "\0" |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
126 |
else Pattern.quote(drive_prefix) + "/([a-zA-Z])($|/.*)") |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
127 |
|
31498 | 128 |
def platform_path(source_path: String): String = |
129 |
{ |
|
27936 | 130 |
val result_path = new StringBuilder |
27919 | 131 |
|
31500 | 132 |
def init(path: String): String = |
133 |
{ |
|
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
134 |
path match { |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
135 |
case Cygdrive(drive, rest) => |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
136 |
result_path.length = 0 |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
137 |
result_path.append(drive) |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
138 |
result_path.append(":") |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
139 |
result_path.append(File.separator) |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
140 |
rest |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
141 |
case _ if (path.startsWith("/")) => |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
142 |
result_path.length = 0 |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
143 |
result_path.append(platform_root) |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
144 |
path.substring(1) |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
145 |
case _ => path |
31500 | 146 |
} |
147 |
} |
|
31498 | 148 |
def append(path: String) |
149 |
{ |
|
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
150 |
for (p <- init(path) split "/") { |
27936 | 151 |
if (p != "") { |
152 |
val len = result_path.length |
|
153 |
if (len > 0 && result_path(len - 1) != File.separatorChar) |
|
154 |
result_path.append(File.separator) |
|
155 |
result_path.append(p) |
|
156 |
} |
|
157 |
} |
|
158 |
} |
|
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
159 |
for (p <- init(source_path) split "/") { |
27953 | 160 |
if (p.startsWith("$")) append(getenv_strict(p.substring(1))) |
161 |
else if (p == "~") append(getenv_strict("HOME")) |
|
162 |
else if (p == "~~") append(getenv_strict("ISABELLE_HOME")) |
|
27936 | 163 |
else append(p) |
164 |
} |
|
165 |
result_path.toString |
|
166 |
} |
|
27953 | 167 |
|
31498 | 168 |
def platform_file(path: String) = new File(platform_path(path)) |
29152 | 169 |
|
27953 | 170 |
|
31436 | 171 |
/* source files */ |
172 |
||
173 |
private def try_file(file: File) = if (file.isFile) Some(file) else None |
|
174 |
||
31498 | 175 |
def source_file(path: String): Option[File] = |
176 |
{ |
|
177 |
if (path.startsWith("/") || path == "") |
|
31436 | 178 |
try_file(platform_file(path)) |
179 |
else { |
|
180 |
val pure_file = platform_file("~~/src/Pure/" + path) |
|
181 |
if (pure_file.isFile) Some(pure_file) |
|
182 |
else if (getenv("ML_SOURCES") != "") |
|
183 |
try_file(platform_file("$ML_SOURCES/" + path)) |
|
184 |
else None |
|
185 |
} |
|
186 |
} |
|
187 |
||
188 |
||
31498 | 189 |
/* external processes */ |
27953 | 190 |
|
31498 | 191 |
def execute(redirect: Boolean, args: String*): Process = |
192 |
{ |
|
193 |
val cmdline = |
|
194 |
if (IsabelleSystem.is_cygwin) List(platform_path("/bin/env")) ++ args |
|
195 |
else args |
|
196 |
IsabelleSystem.raw_execute(environment, redirect, cmdline: _*) |
|
28046 | 197 |
} |
198 |
||
31498 | 199 |
def isabelle_tool(args: String*): (String, Int) = |
200 |
{ |
|
201 |
val proc = execute(true, (List(getenv_strict("ISABELLE_TOOL")) ++ args): _*) |
|
202 |
IsabelleSystem.process_output(proc) |
|
28063 | 203 |
} |
204 |
||
205 |
||
206 |
/* named pipes */ |
|
207 |
||
31498 | 208 |
def mk_fifo(): String = |
209 |
{ |
|
28496
4cff10648928
renamed isatool to isabelle_tool in programming interfaces;
wenzelm
parents:
28063
diff
changeset
|
210 |
val (result, rc) = isabelle_tool("mkfifo") |
28063 | 211 |
if (rc == 0) result.trim |
212 |
else error(result) |
|
213 |
} |
|
214 |
||
31498 | 215 |
def rm_fifo(fifo: String) |
216 |
{ |
|
28496
4cff10648928
renamed isatool to isabelle_tool in programming interfaces;
wenzelm
parents:
28063
diff
changeset
|
217 |
val (result, rc) = isabelle_tool("rmfifo", fifo) |
28063 | 218 |
if (rc != 0) error(result) |
219 |
} |
|
220 |
||
31498 | 221 |
def fifo_reader(fifo: String): BufferedReader = |
222 |
{ |
|
29177 | 223 |
// blocks until writer is ready |
224 |
val stream = |
|
31498 | 225 |
if (IsabelleSystem.is_cygwin) execute(false, "cat", fifo).getInputStream |
29177 | 226 |
else new FileInputStream(fifo) |
31498 | 227 |
new BufferedReader(new InputStreamReader(stream, IsabelleSystem.charset)) |
29177 | 228 |
} |
28063 | 229 |
|
29152 | 230 |
|
231 |
/* find logics */ |
|
232 |
||
31498 | 233 |
def find_logics(): List[String] = |
234 |
{ |
|
29152 | 235 |
val ml_ident = getenv_strict("ML_IDENTIFIER") |
236 |
var logics: Set[String] = Set() |
|
237 |
for (dir <- getenv_strict("ISABELLE_PATH").split(":")) { |
|
238 |
val files = platform_file(dir + "/" + ml_ident).listFiles() |
|
239 |
if (files != null) { |
|
240 |
for (file <- files if file.isFile) logics += file.getName |
|
241 |
} |
|
242 |
} |
|
243 |
logics.toList.sort(_ < _) |
|
244 |
} |
|
29570 | 245 |
|
246 |
||
247 |
/* symbols */ |
|
248 |
||
31498 | 249 |
private def read_symbols(path: String): Iterator[String] = |
250 |
{ |
|
29570 | 251 |
val file = new File(platform_path(path)) |
31436 | 252 |
if (file.isFile) Source.fromFile(file).getLines |
29570 | 253 |
else Iterator.empty |
254 |
} |
|
255 |
val symbols = new Symbol.Interpretation( |
|
256 |
read_symbols("$ISABELLE_HOME/etc/symbols") ++ |
|
257 |
read_symbols("$ISABELLE_HOME_USER/etc/symbols")) |
|
27919 | 258 |
} |