author | wenzelm |
Tue, 09 Jun 2009 01:32:57 +0200 | |
changeset 31520 | 0a99c8716312 |
parent 31500 | eced346b2231 |
child 31703 | 4e6064759aeb |
permissions | -rw-r--r-- |
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 |
} |
|
101 |
Map(entries: _*) |
|
102 |
} |
|
103 |
finally { dump.delete } |
|
27953 | 104 |
} |
27919 | 105 |
|
31498 | 106 |
def getenv(name: String): String = |
107 |
{ |
|
108 |
environment.getOrElse(if (name == "HOME") "HOME_JVM" else name, "") |
|
109 |
} |
|
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 |
||
27936 | 117 |
|
27974 | 118 |
/* file path specifications */ |
27936 | 119 |
|
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
120 |
private val Cygdrive = new Regex( |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
121 |
if (drive_prefix == "") "\0" |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
122 |
else Pattern.quote(drive_prefix) + "/([a-zA-Z])($|/.*)") |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
123 |
|
31498 | 124 |
def platform_path(source_path: String): String = |
125 |
{ |
|
27936 | 126 |
val result_path = new StringBuilder |
27919 | 127 |
|
31500 | 128 |
def init(path: String): String = |
129 |
{ |
|
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
130 |
path match { |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
131 |
case Cygdrive(drive, rest) => |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
132 |
result_path.length = 0 |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
133 |
result_path.append(drive) |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
134 |
result_path.append(":") |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
135 |
result_path.append(File.separator) |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
136 |
rest |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
137 |
case _ if (path.startsWith("/")) => |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
138 |
result_path.length = 0 |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
139 |
result_path.append(platform_root) |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
140 |
path.substring(1) |
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
141 |
case _ => path |
31500 | 142 |
} |
143 |
} |
|
31498 | 144 |
def append(path: String) |
145 |
{ |
|
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
146 |
for (p <- init(path) split "/") { |
27936 | 147 |
if (p != "") { |
148 |
val len = result_path.length |
|
149 |
if (len > 0 && result_path(len - 1) != File.separatorChar) |
|
150 |
result_path.append(File.separator) |
|
151 |
result_path.append(p) |
|
152 |
} |
|
153 |
} |
|
154 |
} |
|
31520
0a99c8716312
simplified IsabelleSystem.platform_path for cygwin;
wenzelm
parents:
31500
diff
changeset
|
155 |
for (p <- init(source_path) split "/") { |
27953 | 156 |
if (p.startsWith("$")) append(getenv_strict(p.substring(1))) |
157 |
else if (p == "~") append(getenv_strict("HOME")) |
|
158 |
else if (p == "~~") append(getenv_strict("ISABELLE_HOME")) |
|
27936 | 159 |
else append(p) |
160 |
} |
|
161 |
result_path.toString |
|
162 |
} |
|
27953 | 163 |
|
31498 | 164 |
def platform_file(path: String) = new File(platform_path(path)) |
29152 | 165 |
|
27953 | 166 |
|
31436 | 167 |
/* source files */ |
168 |
||
169 |
private def try_file(file: File) = if (file.isFile) Some(file) else None |
|
170 |
||
31498 | 171 |
def source_file(path: String): Option[File] = |
172 |
{ |
|
173 |
if (path.startsWith("/") || path == "") |
|
31436 | 174 |
try_file(platform_file(path)) |
175 |
else { |
|
176 |
val pure_file = platform_file("~~/src/Pure/" + path) |
|
177 |
if (pure_file.isFile) Some(pure_file) |
|
178 |
else if (getenv("ML_SOURCES") != "") |
|
179 |
try_file(platform_file("$ML_SOURCES/" + path)) |
|
180 |
else None |
|
181 |
} |
|
182 |
} |
|
183 |
||
184 |
||
31498 | 185 |
/* external processes */ |
27953 | 186 |
|
31498 | 187 |
def execute(redirect: Boolean, args: String*): Process = |
188 |
{ |
|
189 |
val cmdline = |
|
190 |
if (IsabelleSystem.is_cygwin) List(platform_path("/bin/env")) ++ args |
|
191 |
else args |
|
192 |
IsabelleSystem.raw_execute(environment, redirect, cmdline: _*) |
|
28046 | 193 |
} |
194 |
||
31498 | 195 |
def isabelle_tool(args: String*): (String, Int) = |
196 |
{ |
|
197 |
val proc = execute(true, (List(getenv_strict("ISABELLE_TOOL")) ++ args): _*) |
|
198 |
IsabelleSystem.process_output(proc) |
|
28063 | 199 |
} |
200 |
||
201 |
||
202 |
/* named pipes */ |
|
203 |
||
31498 | 204 |
def mk_fifo(): String = |
205 |
{ |
|
28496
4cff10648928
renamed isatool to isabelle_tool in programming interfaces;
wenzelm
parents:
28063
diff
changeset
|
206 |
val (result, rc) = isabelle_tool("mkfifo") |
28063 | 207 |
if (rc == 0) result.trim |
208 |
else error(result) |
|
209 |
} |
|
210 |
||
31498 | 211 |
def rm_fifo(fifo: String) |
212 |
{ |
|
28496
4cff10648928
renamed isatool to isabelle_tool in programming interfaces;
wenzelm
parents:
28063
diff
changeset
|
213 |
val (result, rc) = isabelle_tool("rmfifo", fifo) |
28063 | 214 |
if (rc != 0) error(result) |
215 |
} |
|
216 |
||
31498 | 217 |
def fifo_reader(fifo: String): BufferedReader = |
218 |
{ |
|
29177 | 219 |
// blocks until writer is ready |
220 |
val stream = |
|
31498 | 221 |
if (IsabelleSystem.is_cygwin) execute(false, "cat", fifo).getInputStream |
29177 | 222 |
else new FileInputStream(fifo) |
31498 | 223 |
new BufferedReader(new InputStreamReader(stream, IsabelleSystem.charset)) |
29177 | 224 |
} |
28063 | 225 |
|
29152 | 226 |
|
227 |
/* find logics */ |
|
228 |
||
31498 | 229 |
def find_logics(): List[String] = |
230 |
{ |
|
29152 | 231 |
val ml_ident = getenv_strict("ML_IDENTIFIER") |
232 |
var logics: Set[String] = Set() |
|
233 |
for (dir <- getenv_strict("ISABELLE_PATH").split(":")) { |
|
234 |
val files = platform_file(dir + "/" + ml_ident).listFiles() |
|
235 |
if (files != null) { |
|
236 |
for (file <- files if file.isFile) logics += file.getName |
|
237 |
} |
|
238 |
} |
|
239 |
logics.toList.sort(_ < _) |
|
240 |
} |
|
29570 | 241 |
|
242 |
||
243 |
/* symbols */ |
|
244 |
||
31498 | 245 |
private def read_symbols(path: String): Iterator[String] = |
246 |
{ |
|
29570 | 247 |
val file = new File(platform_path(path)) |
31436 | 248 |
if (file.isFile) Source.fromFile(file).getLines |
29570 | 249 |
else Iterator.empty |
250 |
} |
|
251 |
val symbols = new Symbol.Interpretation( |
|
252 |
read_symbols("$ISABELLE_HOME/etc/symbols") ++ |
|
253 |
read_symbols("$ISABELLE_HOME_USER/etc/symbols")) |
|
27919 | 254 |
} |