| author | wenzelm |
| Fri, 19 Dec 2008 20:37:29 +0100 | |
| changeset 29140 | e7ac5bb20aed |
| parent 28500 | 4b79e5d3d0aa |
| child 29152 | 89b0803404d7 |
| 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 |
|
15 |
object IsabelleSystem {
|
|
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 |
|
77 |
||
| 27974 | 78 |
/* processes */ |
| 27953 | 79 |
|
| 28046 | 80 |
private def posix_prefix() = if (is_cygwin()) List(platform_path("/bin/env")) else Nil
|
| 27953 | 81 |
|
| 28063 | 82 |
def exec(args: String*): Process = Runtime.getRuntime.exec((posix_prefix() ++ args).toArray) |
| 27974 | 83 |
|
| 28063 | 84 |
def exec2(args: String*): Process = {
|
| 28046 | 85 |
val cmdline = new java.util.LinkedList[String] |
86 |
for (s <- posix_prefix() ++ args) cmdline.add(s) |
|
87 |
new ProcessBuilder(cmdline).redirectErrorStream(true).start |
|
88 |
} |
|
89 |
||
| 28063 | 90 |
|
91 |
/* Isabelle tools (non-interactive) */ |
|
92 |
||
|
28496
4cff10648928
renamed isatool to isabelle_tool in programming interfaces;
wenzelm
parents:
28063
diff
changeset
|
93 |
def isabelle_tool(args: String*) = {
|
| 28063 | 94 |
val proc = |
| 28500 | 95 |
try { exec2((List(getenv_strict("ISABELLE_TOOL")) ++ args): _*) }
|
| 28063 | 96 |
catch { case e: IOException => error(e.getMessage) }
|
97 |
proc.getOutputStream.close |
|
98 |
val output = Source.fromInputStream(proc.getInputStream, charset).mkString("")
|
|
99 |
val rc = proc.waitFor |
|
100 |
(output, rc) |
|
101 |
} |
|
102 |
||
103 |
||
104 |
/* named pipes */ |
|
105 |
||
106 |
def mk_fifo() = {
|
|
|
28496
4cff10648928
renamed isatool to isabelle_tool in programming interfaces;
wenzelm
parents:
28063
diff
changeset
|
107 |
val (result, rc) = isabelle_tool("mkfifo")
|
| 28063 | 108 |
if (rc == 0) result.trim |
109 |
else error(result) |
|
110 |
} |
|
111 |
||
112 |
def rm_fifo(fifo: String) = {
|
|
|
28496
4cff10648928
renamed isatool to isabelle_tool in programming interfaces;
wenzelm
parents:
28063
diff
changeset
|
113 |
val (result, rc) = isabelle_tool("rmfifo", fifo)
|
| 28063 | 114 |
if (rc != 0) error(result) |
115 |
} |
|
116 |
||
117 |
def fifo_reader(fifo: String) = // blocks until writer is ready |
|
118 |
if (is_cygwin()) new BufferedReader(new InputStreamReader(Runtime.getRuntime.exec( |
|
119 |
Array(platform_path("/bin/cat"), fifo)).getInputStream, charset))
|
|
120 |
else new BufferedReader(new InputStreamReader(new FileInputStream(fifo), charset)) |
|
121 |
||
| 27919 | 122 |
} |