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.{BufferedWriter, OutputStreamWriter, FileOutputStream, BufferedOutputStream,
15 BufferedInputStream, InputStream, FileInputStream, BufferedReader, InputStreamReader,
16 File => JFile, FileFilter, IOException}
17 import java.nio.charset.Charset
18 import java.util.zip.GZIPOutputStream
21 import scala.util.matching.Regex
22 import scala.collection.mutable
25 object Standard_System
29 val charset_name: String = "UTF-8"
30 val charset: Charset = Charset.forName(charset_name)
31 def codec(): Codec = Codec(charset)
33 def string_bytes(s: String): Array[Byte] = s.getBytes(charset)
36 /* permissive UTF-8 decoding */
38 // see also http://en.wikipedia.org/wiki/UTF-8#Description
39 // overlong encodings enable byte-stuffing
41 def decode_permissive_utf8(text: CharSequence): String =
43 val buf = new java.lang.StringBuilder(text.length)
49 if (rest == 0 && Character.isValidCodePoint(code))
50 buf.appendCodePoint(code)
51 else buf.append('\uFFFD')
56 def init(x: Int, n: Int)
64 if (rest <= 0) init(x, -1)
71 for (i <- 0 until text.length) {
72 val c = text.charAt(i)
73 if (c < 128) { flush(); buf.append(c) }
74 else if ((c & 0xC0) == 0x80) push(c & 0x3F)
75 else if ((c & 0xE0) == 0xC0) init(c & 0x1F, 1)
76 else if ((c & 0xF0) == 0xE0) init(c & 0x0F, 2)
77 else if ((c & 0xF8) == 0xF0) init(c & 0x07, 3)
83 private class Decode_Chars(decode: String => String,
84 buffer: Array[Byte], start: Int, end: Int) extends CharSequence
86 def length: Int = end - start
87 def charAt(i: Int): Char = (buffer(start + i).asInstanceOf[Int] & 0xFF).asInstanceOf[Char]
88 def subSequence(i: Int, j: Int): CharSequence =
89 new Decode_Chars(decode, buffer, start + i, start + j)
91 // toString with adhoc decoding: abuse of CharSequence interface
92 override def toString: String = decode(decode_permissive_utf8(this))
95 def decode_chars(decode: String => String,
96 buffer: Array[Byte], start: Int, end: Int): CharSequence =
98 require(0 <= start && start <= end && end <= buffer.length)
99 new Decode_Chars(decode, buffer, start, end)
103 /* basic file operations */
105 def slurp(reader: BufferedReader): String =
107 val output = new StringBuilder(100)
109 while ({ c = reader.read; c != -1 }) output += c.toChar
114 def slurp(stream: InputStream): String =
115 slurp(new BufferedReader(new InputStreamReader(stream, charset)))
117 def read_file(file: JFile): String = slurp(new FileInputStream(file))
119 def write_file(file: JFile, text: CharSequence, zip: Boolean = false)
121 val stream1 = new FileOutputStream(file)
122 val stream2 = if (zip) new GZIPOutputStream(new BufferedOutputStream(stream1)) else stream1
123 val writer = new BufferedWriter(new OutputStreamWriter(stream2, charset))
124 try { writer.append(text) }
125 finally { writer.close }
128 def eq_file(file1: JFile, file2: JFile): Boolean =
129 file1.getCanonicalPath == file2.getCanonicalPath // FIXME prefer java.nio.file.Files.isSameFile of Java 1.7
131 def copy_file(src: JFile, dst: JFile) =
132 if (!eq_file(src, dst)) {
133 val in = new FileInputStream(src)
135 val out = new FileOutputStream(dst)
137 val buf = new Array[Byte](65536)
140 m = in.read(buf, 0, buf.length)
141 if (m != -1) out.write(buf, 0, m)
144 finally { out.close }
149 def with_tmp_file[A](prefix: String)(body: JFile => A): A =
151 val file = JFile.createTempFile(prefix, null)
153 try { body(file) } finally { file.delete }
156 // FIXME handle (potentially cyclic) directory graph
157 def find_files(start: JFile, ok: JFile => Boolean): List[JFile] =
159 val files = new mutable.ListBuffer[JFile]
160 val filter = new FileFilter { def accept(entry: JFile) = entry.isDirectory || ok(entry) }
161 def find_entry(entry: JFile)
163 if (ok(entry)) files += entry
164 if (entry.isDirectory) entry.listFiles(filter).foreach(find_entry)
171 /* shell processes */
173 def raw_execute(cwd: JFile, env: Map[String, String], redirect: Boolean, args: String*): Process =
175 val cmdline = new java.util.LinkedList[String]
176 for (s <- args) cmdline.add(s)
178 val proc = new ProcessBuilder(cmdline)
179 if (cwd != null) proc.directory(cwd)
181 proc.environment.clear
182 for ((x, y) <- env) proc.environment.put(x, y)
184 proc.redirectErrorStream(redirect)
188 def process_output(proc: Process): (String, Int) =
190 proc.getOutputStream.close
191 val output = slurp(proc.getInputStream)
195 proc.getInputStream.close
196 proc.getErrorStream.close
203 def raw_exec(cwd: JFile, env: Map[String, String], redirect: Boolean, args: String*)
204 : (String, Int) = process_output(raw_execute(cwd, env, redirect, args: _*))
209 def cygwin_root(): String =
211 val cygwin_root1 = System.getenv("CYGWIN_ROOT")
212 val cygwin_root2 = System.getProperty("cygwin.root")
214 if (cygwin_root1 != null && cygwin_root1 != "") cygwin_root1
215 else if (cygwin_root2 != null && cygwin_root2 != "") cygwin_root2
216 else error("Bad Cygwin installation: unknown root")
218 val root_file = new JFile(root)
219 if (!new JFile(root_file, "bin\\bash.exe").isFile ||
220 !new JFile(root_file, "bin\\env.exe").isFile ||
221 !new JFile(root_file, "bin\\tar.exe").isFile)
222 error("Bad Cygwin installation: " + quote(root))
229 class Standard_System
233 val platform_root = if (Platform.is_windows) Standard_System.cygwin_root() else "/"
238 private val Cygdrive = new Regex("/cygdrive/([a-zA-Z])($|/.*)")
239 private val Named_Root = new Regex("//+([^/]*)(.*)")
241 def jvm_path(posix_path: String): String =
242 if (Platform.is_windows) {
243 val result_path = new StringBuilder
246 case Cygdrive(drive, rest) =>
247 result_path ++= (drive.toUpperCase(Locale.ENGLISH) + ":" + JFile.separator)
249 case Named_Root(root, rest) =>
250 result_path ++= JFile.separator
251 result_path ++= JFile.separator
254 case path if path.startsWith("/") =>
255 result_path ++= platform_root
259 for (p <- space_explode('/', rest) if p != "") {
260 val len = result_path.length
261 if (len > 0 && result_path(len - 1) != JFile.separatorChar)
262 result_path += JFile.separatorChar
272 private val Platform_Root = new Regex("(?i)" +
273 Pattern.quote(platform_root) + """(?:\\+|\z)(.*)""")
275 private val Drive = new Regex("""([a-zA-Z]):\\*(.*)""")
277 def posix_path(jvm_path: String): String =
278 if (Platform.is_windows) {
279 jvm_path.replace('/', '\\') match {
280 case Platform_Root(rest) => "/" + rest.replace('\\', '/')
281 case Drive(letter, rest) =>
282 "/cygdrive/" + letter.toLowerCase(Locale.ENGLISH) +
283 (if (rest == "") "" else "/" + rest.replace('\\', '/'))
284 case path => path.replace('\\', '/')
290 /* JDK home of running JVM */
292 def this_jdk_home(): String =
294 val java_home = System.getProperty("java.home")
295 val home = new JFile(java_home)
296 val parent = home.getParent
298 if (home.getName == "jre" && parent != null &&
299 (new JFile(new JFile(parent, "bin"), "javac")).exists) parent