author | wenzelm |
Sat, 27 Dec 2008 14:57:30 +0100 | |
changeset 29174 | d4058295affb |
parent 29152 | 89b0803404d7 |
child 29177 | 0e88d33e8d19 |
permissions | -rw-r--r-- |
27919 | 1 |
/* Title: Pure/Tools/isabelle_system.scala |
2 |
Author: Makarius |
|
3 |
||
27974 | 4 |
Isabelle system support -- basic Cygwin/Posix compatibility. |
27919 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
27936 | 9 |
import java.util.regex.{Pattern, Matcher} |
28063 | 10 |
import java.io.{BufferedReader, InputStreamReader, FileInputStream, File, IOException} |
11 |
||
12 |
import scala.io.Source |
|
27936 | 13 |
|
27919 | 14 |
|
29174 | 15 |
class IsabelleSystem { |
27919 | 16 |
|
28057 | 17 |
val charset = "UTF-8" |
18 |
||
19 |
||
28046 | 20 |
/* Isabelle environment settings */ |
27919 | 21 |
|
27953 | 22 |
def getenv(name: String) = { |
28057 | 23 |
val value = System.getenv(if (name == "HOME") "HOME_JVM" else name) |
27953 | 24 |
if (value != null) value else "" |
25 |
} |
|
27919 | 26 |
|
27953 | 27 |
def getenv_strict(name: String) = { |
28 |
val value = getenv(name) |
|
27993
6dd90ef9f927
simplified exceptions: use plain error function / RuntimeException;
wenzelm
parents:
27974
diff
changeset
|
29 |
if (value != "") value else error("Undefined environment variable: " + name) |
27919 | 30 |
} |
31 |
||
28046 | 32 |
def is_cygwin() = Pattern.matches(".*-cygwin", getenv_strict("ML_PLATFORM")) |
33 |
||
27936 | 34 |
|
27974 | 35 |
/* file path specifications */ |
27936 | 36 |
|
37 |
private val cygdrive_pattern = Pattern.compile("/cygdrive/([a-zA-Z])($|/.*)") |
|
38 |
||
39 |
def platform_path(source_path: String) = { |
|
40 |
val result_path = new StringBuilder |
|
27919 | 41 |
|
27936 | 42 |
def init(path: String) = { |
43 |
val cygdrive = cygdrive_pattern.matcher(path) |
|
44 |
if (cygdrive.matches) { |
|
27953 | 45 |
result_path.length = 0 |
27936 | 46 |
result_path.append(cygdrive.group(1)) |
47 |
result_path.append(":") |
|
48 |
result_path.append(File.separator) |
|
49 |
cygdrive.group(2) |
|
50 |
} |
|
51 |
else if (path.startsWith("/")) { |
|
27953 | 52 |
result_path.length = 0 |
53 |
result_path.append(getenv_strict("ISABELLE_ROOT_JVM")) |
|
27936 | 54 |
path.substring(1) |
55 |
} |
|
56 |
else path |
|
57 |
} |
|
58 |
def append(path: String) = { |
|
59 |
for (p <- init(path).split("/")) { |
|
60 |
if (p != "") { |
|
61 |
val len = result_path.length |
|
62 |
if (len > 0 && result_path(len - 1) != File.separatorChar) |
|
63 |
result_path.append(File.separator) |
|
64 |
result_path.append(p) |
|
65 |
} |
|
66 |
} |
|
67 |
} |
|
68 |
for (p <- init(source_path).split("/")) { |
|
27953 | 69 |
if (p.startsWith("$")) append(getenv_strict(p.substring(1))) |
70 |
else if (p == "~") append(getenv_strict("HOME")) |
|
71 |
else if (p == "~~") append(getenv_strict("ISABELLE_HOME")) |
|
27936 | 72 |
else append(p) |
73 |
} |
|
74 |
result_path.toString |
|
75 |
} |
|
27953 | 76 |
|
29152 | 77 |
def platform_file(path: String) = |
78 |
new File(platform_path(path)) |
|
79 |
||
27953 | 80 |
|
27974 | 81 |
/* processes */ |
27953 | 82 |
|
28046 | 83 |
private def posix_prefix() = if (is_cygwin()) List(platform_path("/bin/env")) else Nil |
27953 | 84 |
|
28063 | 85 |
def exec(args: String*): Process = Runtime.getRuntime.exec((posix_prefix() ++ args).toArray) |
27974 | 86 |
|
28063 | 87 |
def exec2(args: String*): Process = { |
28046 | 88 |
val cmdline = new java.util.LinkedList[String] |
89 |
for (s <- posix_prefix() ++ args) cmdline.add(s) |
|
90 |
new ProcessBuilder(cmdline).redirectErrorStream(true).start |
|
91 |
} |
|
92 |
||
28063 | 93 |
|
94 |
/* Isabelle tools (non-interactive) */ |
|
95 |
||
28496
4cff10648928
renamed isatool to isabelle_tool in programming interfaces;
wenzelm
parents:
28063
diff
changeset
|
96 |
def isabelle_tool(args: String*) = { |
28063 | 97 |
val proc = |
28500 | 98 |
try { exec2((List(getenv_strict("ISABELLE_TOOL")) ++ args): _*) } |
28063 | 99 |
catch { case e: IOException => error(e.getMessage) } |
100 |
proc.getOutputStream.close |
|
29174 | 101 |
val output = Source.fromInputStream(proc.getInputStream, charset).mkString |
28063 | 102 |
val rc = proc.waitFor |
103 |
(output, rc) |
|
104 |
} |
|
105 |
||
106 |
||
107 |
/* named pipes */ |
|
108 |
||
109 |
def mk_fifo() = { |
|
28496
4cff10648928
renamed isatool to isabelle_tool in programming interfaces;
wenzelm
parents:
28063
diff
changeset
|
110 |
val (result, rc) = isabelle_tool("mkfifo") |
28063 | 111 |
if (rc == 0) result.trim |
112 |
else error(result) |
|
113 |
} |
|
114 |
||
115 |
def rm_fifo(fifo: String) = { |
|
28496
4cff10648928
renamed isatool to isabelle_tool in programming interfaces;
wenzelm
parents:
28063
diff
changeset
|
116 |
val (result, rc) = isabelle_tool("rmfifo", fifo) |
28063 | 117 |
if (rc != 0) error(result) |
118 |
} |
|
119 |
||
120 |
def fifo_reader(fifo: String) = // blocks until writer is ready |
|
121 |
if (is_cygwin()) new BufferedReader(new InputStreamReader(Runtime.getRuntime.exec( |
|
122 |
Array(platform_path("/bin/cat"), fifo)).getInputStream, charset)) |
|
123 |
else new BufferedReader(new InputStreamReader(new FileInputStream(fifo), charset)) |
|
124 |
||
29152 | 125 |
|
126 |
/* find logics */ |
|
127 |
||
128 |
def find_logics() = { |
|
129 |
val ml_ident = getenv_strict("ML_IDENTIFIER") |
|
130 |
var logics: Set[String] = Set() |
|
131 |
for (dir <- getenv_strict("ISABELLE_PATH").split(":")) { |
|
132 |
val files = platform_file(dir + "/" + ml_ident).listFiles() |
|
133 |
if (files != null) { |
|
134 |
for (file <- files if file.isFile) logics += file.getName |
|
135 |
} |
|
136 |
} |
|
137 |
logics.toList.sort(_ < _) |
|
138 |
} |
|
27919 | 139 |
} |