1 /* Title: Pure/System/standard_system.scala
5 Standard system operations, with basic Cygwin/Posix compatibility.
10 import java.lang.System
11 import java.util.regex.Pattern
12 import java.util.Locale
14 import java.io.{File => JFile}
16 import scala.util.matching.Regex
19 object Standard_System
23 def raw_execute(cwd: JFile, env: Map[String, String], redirect: Boolean, args: String*): Process =
25 val cmdline = new java.util.LinkedList[String]
26 for (s <- args) cmdline.add(s)
28 val proc = new ProcessBuilder(cmdline)
29 if (cwd != null) proc.directory(cwd)
31 proc.environment.clear
32 for ((x, y) <- env) proc.environment.put(x, y)
34 proc.redirectErrorStream(redirect)
38 def process_output(proc: Process): (String, Int) =
40 proc.getOutputStream.close
41 val output = File.read(proc.getInputStream)
45 proc.getInputStream.close
46 proc.getErrorStream.close
53 def raw_exec(cwd: JFile, env: Map[String, String], redirect: Boolean, args: String*)
54 : (String, Int) = process_output(raw_execute(cwd, env, redirect, args: _*))
59 def cygwin_root(): String =
61 val cygwin_root1 = System.getenv("CYGWIN_ROOT")
62 val cygwin_root2 = System.getProperty("cygwin.root")
64 if (cygwin_root1 != null && cygwin_root1 != "") cygwin_root1
65 else if (cygwin_root2 != null && cygwin_root2 != "") cygwin_root2
66 else error("Bad Cygwin installation: unknown root")
68 val root_file = new JFile(root)
69 if (!new JFile(root_file, "bin\\bash.exe").isFile ||
70 !new JFile(root_file, "bin\\env.exe").isFile ||
71 !new JFile(root_file, "bin\\tar.exe").isFile)
72 error("Bad Cygwin installation: " + quote(root))
83 val platform_root = if (Platform.is_windows) Standard_System.cygwin_root() else "/"
88 private val Cygdrive = new Regex("/cygdrive/([a-zA-Z])($|/.*)")
89 private val Named_Root = new Regex("//+([^/]*)(.*)")
91 def jvm_path(posix_path: String): String =
92 if (Platform.is_windows) {
93 val result_path = new StringBuilder
96 case Cygdrive(drive, rest) =>
97 result_path ++= (drive.toUpperCase(Locale.ENGLISH) + ":" + JFile.separator)
99 case Named_Root(root, rest) =>
100 result_path ++= JFile.separator
101 result_path ++= JFile.separator
104 case path if path.startsWith("/") =>
105 result_path ++= platform_root
109 for (p <- space_explode('/', rest) if p != "") {
110 val len = result_path.length
111 if (len > 0 && result_path(len - 1) != JFile.separatorChar)
112 result_path += JFile.separatorChar
122 private val Platform_Root = new Regex("(?i)" +
123 Pattern.quote(platform_root) + """(?:\\+|\z)(.*)""")
125 private val Drive = new Regex("""([a-zA-Z]):\\*(.*)""")
127 def posix_path(jvm_path: String): String =
128 if (Platform.is_windows) {
129 jvm_path.replace('/', '\\') match {
130 case Platform_Root(rest) => "/" + rest.replace('\\', '/')
131 case Drive(letter, rest) =>
132 "/cygdrive/" + letter.toLowerCase(Locale.ENGLISH) +
133 (if (rest == "") "" else "/" + rest.replace('\\', '/'))
134 case path => path.replace('\\', '/')
140 /* JDK home of running JVM */
142 def this_jdk_home(): String =
144 val java_home = System.getProperty("java.home")
145 val home = new JFile(java_home)
146 val parent = home.getParent
148 if (home.getName == "jre" && parent != null &&
149 (new JFile(new JFile(parent, "bin"), "javac")).exists) parent