| author | wenzelm |
| Thu, 28 Aug 2008 19:29:59 +0200 | |
| changeset 28046 | 1447932b1b13 |
| parent 27993 | 6dd90ef9f927 |
| child 28057 | 6b90e6532d51 |
| permissions | -rw-r--r-- |
| 27919 | 1 |
/* Title: Pure/Tools/isabelle_system.scala |
2 |
ID: $Id$ |
|
3 |
Author: Makarius |
|
4 |
||
| 27974 | 5 |
Isabelle system support -- basic Cygwin/Posix compatibility. |
| 27919 | 6 |
*/ |
7 |
||
8 |
package isabelle |
|
9 |
||
| 27936 | 10 |
import java.util.regex.{Pattern, Matcher}
|
11 |
import java.io.File |
|
12 |
||
| 27919 | 13 |
|
14 |
object IsabelleSystem {
|
|
15 |
||
| 28046 | 16 |
/* Isabelle environment settings */ |
| 27919 | 17 |
|
| 27953 | 18 |
def getenv(name: String) = {
|
19 |
val value = System.getenv(name) |
|
20 |
if (value != null) value else "" |
|
21 |
} |
|
| 27919 | 22 |
|
| 27953 | 23 |
def getenv_strict(name: String) = {
|
24 |
val value = getenv(name) |
|
|
27993
6dd90ef9f927
simplified exceptions: use plain error function / RuntimeException;
wenzelm
parents:
27974
diff
changeset
|
25 |
if (value != "") value else error("Undefined environment variable: " + name)
|
| 27919 | 26 |
} |
27 |
||
| 28046 | 28 |
def is_cygwin() = Pattern.matches(".*-cygwin", getenv_strict("ML_PLATFORM"))
|
29 |
||
| 27936 | 30 |
|
| 27974 | 31 |
/* file path specifications */ |
| 27936 | 32 |
|
33 |
private val cygdrive_pattern = Pattern.compile("/cygdrive/([a-zA-Z])($|/.*)")
|
|
34 |
||
35 |
def platform_path(source_path: String) = {
|
|
36 |
val result_path = new StringBuilder |
|
| 27919 | 37 |
|
| 27936 | 38 |
def init(path: String) = {
|
39 |
val cygdrive = cygdrive_pattern.matcher(path) |
|
40 |
if (cygdrive.matches) {
|
|
| 27953 | 41 |
result_path.length = 0 |
| 27936 | 42 |
result_path.append(cygdrive.group(1)) |
43 |
result_path.append(":")
|
|
44 |
result_path.append(File.separator) |
|
45 |
cygdrive.group(2) |
|
46 |
} |
|
47 |
else if (path.startsWith("/")) {
|
|
| 27953 | 48 |
result_path.length = 0 |
49 |
result_path.append(getenv_strict("ISABELLE_ROOT_JVM"))
|
|
| 27936 | 50 |
path.substring(1) |
51 |
} |
|
52 |
else path |
|
53 |
} |
|
54 |
def append(path: String) = {
|
|
55 |
for (p <- init(path).split("/")) {
|
|
56 |
if (p != "") {
|
|
57 |
val len = result_path.length |
|
58 |
if (len > 0 && result_path(len - 1) != File.separatorChar) |
|
59 |
result_path.append(File.separator) |
|
60 |
result_path.append(p) |
|
61 |
} |
|
62 |
} |
|
63 |
} |
|
64 |
for (p <- init(source_path).split("/")) {
|
|
| 27953 | 65 |
if (p.startsWith("$")) append(getenv_strict(p.substring(1)))
|
66 |
else if (p == "~") append(getenv_strict("HOME"))
|
|
67 |
else if (p == "~~") append(getenv_strict("ISABELLE_HOME"))
|
|
| 27936 | 68 |
else append(p) |
69 |
} |
|
70 |
result_path.toString |
|
71 |
} |
|
| 27953 | 72 |
|
73 |
||
| 27974 | 74 |
/* processes */ |
| 27953 | 75 |
|
| 28046 | 76 |
private def posix_prefix() = if (is_cygwin()) List(platform_path("/bin/env")) else Nil
|
| 27953 | 77 |
|
| 27974 | 78 |
def exec(args: List[String]) = Runtime.getRuntime.exec((posix_prefix() ++ args).toArray) |
79 |
||
| 28046 | 80 |
def exec2(args: List[String]) = {
|
81 |
val cmdline = new java.util.LinkedList[String] |
|
82 |
for (s <- posix_prefix() ++ args) cmdline.add(s) |
|
83 |
new ProcessBuilder(cmdline).redirectErrorStream(true).start |
|
84 |
} |
|
85 |
||
| 27919 | 86 |
} |