author | ballarin |
Tue, 30 Dec 2008 11:10:01 +0100 | |
changeset 29252 | ea97aa6aeba2 |
parent 29180 | 62513d4d34c2 |
child 29570 | 10fca82e688a |
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 |
|
29177 | 22 |
private val environment = System.getenv |
23 |
||
27953 | 24 |
def getenv(name: String) = { |
29177 | 25 |
val value = environment.get(if (name == "HOME") "HOME_JVM" else name) |
27953 | 26 |
if (value != null) value else "" |
27 |
} |
|
27919 | 28 |
|
27953 | 29 |
def getenv_strict(name: String) = { |
29177 | 30 |
val value = environment.get(name) |
27993
6dd90ef9f927
simplified exceptions: use plain error function / RuntimeException;
wenzelm
parents:
27974
diff
changeset
|
31 |
if (value != "") value else error("Undefined environment variable: " + name) |
27919 | 32 |
} |
33 |
||
29177 | 34 |
val is_cygwin = Pattern.matches(".*-cygwin", getenv_strict("ML_PLATFORM")) |
28046 | 35 |
|
27936 | 36 |
|
27974 | 37 |
/* file path specifications */ |
27936 | 38 |
|
39 |
private val cygdrive_pattern = Pattern.compile("/cygdrive/([a-zA-Z])($|/.*)") |
|
40 |
||
41 |
def platform_path(source_path: String) = { |
|
42 |
val result_path = new StringBuilder |
|
27919 | 43 |
|
27936 | 44 |
def init(path: String) = { |
45 |
val cygdrive = cygdrive_pattern.matcher(path) |
|
46 |
if (cygdrive.matches) { |
|
27953 | 47 |
result_path.length = 0 |
27936 | 48 |
result_path.append(cygdrive.group(1)) |
49 |
result_path.append(":") |
|
50 |
result_path.append(File.separator) |
|
51 |
cygdrive.group(2) |
|
52 |
} |
|
53 |
else if (path.startsWith("/")) { |
|
27953 | 54 |
result_path.length = 0 |
55 |
result_path.append(getenv_strict("ISABELLE_ROOT_JVM")) |
|
27936 | 56 |
path.substring(1) |
57 |
} |
|
58 |
else path |
|
59 |
} |
|
60 |
def append(path: String) = { |
|
61 |
for (p <- init(path).split("/")) { |
|
62 |
if (p != "") { |
|
63 |
val len = result_path.length |
|
64 |
if (len > 0 && result_path(len - 1) != File.separatorChar) |
|
65 |
result_path.append(File.separator) |
|
66 |
result_path.append(p) |
|
67 |
} |
|
68 |
} |
|
69 |
} |
|
70 |
for (p <- init(source_path).split("/")) { |
|
27953 | 71 |
if (p.startsWith("$")) append(getenv_strict(p.substring(1))) |
72 |
else if (p == "~") append(getenv_strict("HOME")) |
|
73 |
else if (p == "~~") append(getenv_strict("ISABELLE_HOME")) |
|
27936 | 74 |
else append(p) |
75 |
} |
|
76 |
result_path.toString |
|
77 |
} |
|
27953 | 78 |
|
29152 | 79 |
def platform_file(path: String) = |
80 |
new File(platform_path(path)) |
|
81 |
||
27953 | 82 |
|
27974 | 83 |
/* processes */ |
27953 | 84 |
|
29177 | 85 |
def execute(redirect: Boolean, args: String*): Process = { |
86 |
val cmdline = new java.util.LinkedList[String] |
|
87 |
if (is_cygwin) cmdline.add(platform_path("/bin/env")) |
|
88 |
for (s <- args) cmdline.add(s) |
|
27974 | 89 |
|
29177 | 90 |
val proc = new ProcessBuilder(cmdline) |
29180 | 91 |
proc.environment.clear |
92 |
proc.environment.putAll(environment) |
|
93 |
proc.redirectErrorStream(redirect) |
|
94 |
proc.start |
|
28046 | 95 |
} |
96 |
||
28063 | 97 |
|
98 |
/* Isabelle tools (non-interactive) */ |
|
99 |
||
28496
4cff10648928
renamed isatool to isabelle_tool in programming interfaces;
wenzelm
parents:
28063
diff
changeset
|
100 |
def isabelle_tool(args: String*) = { |
28063 | 101 |
val proc = |
29177 | 102 |
try { execute(true, (List(getenv_strict("ISABELLE_TOOL")) ++ args): _*) } |
28063 | 103 |
catch { case e: IOException => error(e.getMessage) } |
104 |
proc.getOutputStream.close |
|
29174 | 105 |
val output = Source.fromInputStream(proc.getInputStream, charset).mkString |
28063 | 106 |
val rc = proc.waitFor |
107 |
(output, rc) |
|
108 |
} |
|
109 |
||
110 |
||
111 |
/* named pipes */ |
|
112 |
||
113 |
def mk_fifo() = { |
|
28496
4cff10648928
renamed isatool to isabelle_tool in programming interfaces;
wenzelm
parents:
28063
diff
changeset
|
114 |
val (result, rc) = isabelle_tool("mkfifo") |
28063 | 115 |
if (rc == 0) result.trim |
116 |
else error(result) |
|
117 |
} |
|
118 |
||
119 |
def rm_fifo(fifo: String) = { |
|
28496
4cff10648928
renamed isatool to isabelle_tool in programming interfaces;
wenzelm
parents:
28063
diff
changeset
|
120 |
val (result, rc) = isabelle_tool("rmfifo", fifo) |
28063 | 121 |
if (rc != 0) error(result) |
122 |
} |
|
123 |
||
29177 | 124 |
def fifo_reader(fifo: String) = { |
125 |
// blocks until writer is ready |
|
126 |
val stream = |
|
127 |
if (is_cygwin) execute(false, "cat", fifo).getInputStream |
|
128 |
else new FileInputStream(fifo) |
|
129 |
new BufferedReader(new InputStreamReader(stream, charset)) |
|
130 |
} |
|
28063 | 131 |
|
29152 | 132 |
|
133 |
/* find logics */ |
|
134 |
||
135 |
def find_logics() = { |
|
136 |
val ml_ident = getenv_strict("ML_IDENTIFIER") |
|
137 |
var logics: Set[String] = Set() |
|
138 |
for (dir <- getenv_strict("ISABELLE_PATH").split(":")) { |
|
139 |
val files = platform_file(dir + "/" + ml_ident).listFiles() |
|
140 |
if (files != null) { |
|
141 |
for (file <- files if file.isFile) logics += file.getName |
|
142 |
} |
|
143 |
} |
|
144 |
logics.toList.sort(_ < _) |
|
145 |
} |
|
27919 | 146 |
} |