simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
tuned implicit build/init messages;
1 /* Title: Pure/System/standard_system.scala
4 Standard system operations, with basic Cygwin/Posix compatibility.
9 import java.lang.System
10 import java.util.zip.{ZipEntry, ZipInputStream}
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, FileFilter, IOException}
17 import java.nio.charset.Charset
19 import scala.io.{Source, Codec}
20 import scala.util.matching.Regex
21 import scala.collection.mutable
24 object Standard_System
28 val charset_name: String = "UTF-8"
29 val charset: Charset = Charset.forName(charset_name)
30 def codec(): Codec = Codec(charset)
32 def string_bytes(s: String): Array[Byte] = s.getBytes(charset)
35 /* permissive UTF-8 decoding */
37 // see also http://en.wikipedia.org/wiki/UTF-8#Description
38 // overlong encodings enable byte-stuffing
40 def decode_permissive_utf8(text: CharSequence): String =
42 val buf = new java.lang.StringBuilder(text.length)
48 if (rest == 0 && Character.isValidCodePoint(code))
49 buf.appendCodePoint(code)
50 else buf.append('\uFFFD')
55 def init(x: Int, n: Int)
63 if (rest <= 0) init(x, -1)
70 for (i <- 0 until text.length) {
71 val c = text.charAt(i)
72 if (c < 128) { flush(); buf.append(c) }
73 else if ((c & 0xC0) == 0x80) push(c & 0x3F)
74 else if ((c & 0xE0) == 0xC0) init(c & 0x1F, 1)
75 else if ((c & 0xF0) == 0xE0) init(c & 0x0F, 2)
76 else if ((c & 0xF8) == 0xF0) init(c & 0x07, 3)
83 /* basic file operations */
85 def slurp(reader: BufferedReader): String =
87 val output = new StringBuilder(100)
89 while ({ c = reader.read; c != -1 }) output += c.toChar
94 def slurp(stream: InputStream): String =
95 slurp(new BufferedReader(new InputStreamReader(stream, charset)))
97 def read_file(file: File): String = slurp(new FileInputStream(file))
99 def write_file(file: File, text: CharSequence)
102 new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset))
103 try { writer.append(text) }
104 finally { writer.close }
107 def with_tmp_file[A](prefix: String)(body: File => A): A =
109 val file = File.createTempFile(prefix, null)
111 try { body(file) } finally { file.delete }
114 // FIXME handle (potentially cyclic) directory graph
115 def find_files(start: File, ok: File => Boolean): List[File] =
117 val files = new mutable.ListBuffer[File]
118 val filter = new FileFilter { def accept(entry: File) = entry.isDirectory || ok(entry) }
119 def find_entry(entry: File)
121 if (ok(entry)) files += entry
122 if (entry.isDirectory) entry.listFiles(filter).foreach(find_entry)
129 /* shell processes */
131 def raw_execute(cwd: File, env: Map[String, String], redirect: Boolean, args: String*): Process =
133 val cmdline = new java.util.LinkedList[String]
134 for (s <- args) cmdline.add(s)
136 val proc = new ProcessBuilder(cmdline)
137 if (cwd != null) proc.directory(cwd)
139 proc.environment.clear
140 for ((x, y) <- env) proc.environment.put(x, y)
142 proc.redirectErrorStream(redirect)
146 def process_output(proc: Process): (String, Int) =
148 proc.getOutputStream.close
149 val output = slurp(proc.getInputStream)
153 proc.getInputStream.close
154 proc.getErrorStream.close
161 def raw_exec(cwd: File, env: Map[String, String], redirect: Boolean, args: String*)
162 : (String, Int) = process_output(raw_execute(cwd, env, redirect, args: _*))
165 /* unpack zip archive -- platform file-system */
167 def unzip(url: URL, root: File)
169 import scala.collection.JavaConversions._
171 val buffer = new Array[Byte](4096)
173 val zip_stream = new ZipInputStream(new BufferedInputStream(url.openStream))
174 var entry: ZipEntry = null
176 while ({ entry = zip_stream.getNextEntry; entry != null }) {
177 val file = new File(root, entry.getName.replace('/', File.separatorChar))
178 val dir = file.getParentFile
179 if (dir != null && !dir.isDirectory && !dir.mkdirs)
180 error("Failed to create directory: " + dir)
183 val out_stream = new BufferedOutputStream(new FileOutputStream(file))
185 while ({ len = zip_stream.read(buffer); len != -1 })
186 out_stream.write(buffer, 0, len)
188 finally { out_stream.close }
191 finally { zip_stream.close }
195 /* unpack tar archive -- POSIX file-system */
197 def posix_untar(url: URL, root: File, gunzip: Boolean = false,
198 tar: String = "tar", gzip: String = "", progress: Int => Unit = _ => ()): String =
200 if (!root.isDirectory && !root.mkdirs)
201 error("Failed to create root directory: " + root)
203 val connection = url.openConnection
205 val length = connection.getContentLength.toLong
206 require(length >= 0L)
208 val stream = new BufferedInputStream(connection.getInputStream)
209 val progress_stream = new InputStream {
210 private val total = length max 1L
211 private var index = 0L
212 private var percentage = 0L
213 override def read(): Int =
218 val p = index / total
219 if (percentage != p) { percentage = p; progress(percentage.toInt) }
223 override def available(): Int = stream.available
224 override def close() { stream.close }
228 List(tar, "-o", "-x", "-f-") :::
229 (if (!gunzip) Nil else if (gzip == "") List("-z") else List("-I", gzip))
231 val proc = raw_execute(root, null, false, cmdline:_*)
232 val stdout = Simple_Thread.future("tar_stdout") { slurp(proc.getInputStream) }
233 val stderr = Simple_Thread.future("tar_stderr") { slurp(proc.getErrorStream) }
234 val stdin = new BufferedOutputStream(proc.getOutputStream)
239 try { while ({ c = progress_stream.read; c != -1 }) stdin.write(c); false }
240 catch { case e: IOException => true }
243 val rc = try { proc.waitFor } finally { Thread.interrupted }
244 if (io_err || rc != 0) error(stderr.join.trim) else stdout.join
247 progress_stream.close
255 class Standard_System
259 val platform_root = if (Platform.is_windows) Cygwin.check_root() else "/"
264 private val Cygdrive = new Regex("/cygdrive/([a-zA-Z])($|/.*)")
265 private val Named_Root = new Regex("//+([^/]*)(.*)")
267 def jvm_path(posix_path: String): String =
268 if (Platform.is_windows) {
269 val result_path = new StringBuilder
272 case Cygdrive(drive, rest) =>
273 result_path ++= (drive + ":" + File.separator)
275 case Named_Root(root, rest) =>
276 result_path ++= File.separator
277 result_path ++= File.separator
280 case path if path.startsWith("/") =>
281 result_path ++= platform_root
285 for (p <- space_explode('/', rest) if p != "") {
286 val len = result_path.length
287 if (len > 0 && result_path(len - 1) != File.separatorChar)
288 result_path += File.separatorChar
298 private val Platform_Root = new Regex("(?i)" +
299 Pattern.quote(platform_root) + """(?:\\+|\z)(.*)""")
301 private val Drive = new Regex("""([a-zA-Z]):\\*(.*)""")
303 def posix_path(jvm_path: String): String =
304 if (Platform.is_windows) {
305 jvm_path.replace('/', '\\') match {
306 case Platform_Root(rest) => "/" + rest.replace('\\', '/')
307 case Drive(letter, rest) =>
308 "/cygdrive/" + letter.toLowerCase(Locale.ENGLISH) +
309 (if (rest == "") "" else "/" + rest.replace('\\', '/'))
310 case path => path.replace('\\', '/')
316 /* this_java executable */
318 def this_java(): String =
320 val java_home = System.getProperty("java.home")
322 if (Platform.is_windows) new File(java_home + "\\bin\\java.exe")
323 else new File(java_home + "/bin/java")
324 if (!java_exe.isFile) error("Expected this Java executable: " + java_exe.toString)
325 posix_path(java_exe.getAbsolutePath)