separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
1 /* Title: Pure/System/standard_system.scala
4 Standard system operations, with basic Cygwin/Posix compatibility.
9 import java.util.regex.Pattern
10 import java.util.Locale
11 import java.io.{BufferedWriter, OutputStreamWriter, FileOutputStream,
12 BufferedInputStream, FileInputStream, BufferedReader, InputStreamReader,
15 import scala.io.Source
16 import scala.util.matching.Regex
17 import scala.collection.mutable
20 object Standard_System
25 /* permissive UTF-8 decoding */
27 // see also http://en.wikipedia.org/wiki/UTF-8#Description
28 // overlong encodings enable byte-stuffing
30 def decode_permissive_utf8(text: CharSequence): String =
32 val buf = new java.lang.StringBuilder(text.length)
38 if (rest == 0 && Character.isValidCodePoint(code))
39 buf.appendCodePoint(code)
40 else buf.append('\uFFFD')
45 def init(x: Int, n: Int)
53 if (rest <= 0) init(x, -1)
60 for (i <- 0 until text.length) {
61 val c = text.charAt(i)
62 if (c < 128) { flush(); buf.append(c) }
63 else if ((c & 0xC0) == 0x80) push(c & 0x3F)
64 else if ((c & 0xE0) == 0xC0) init(c & 0x1F, 1)
65 else if ((c & 0xF0) == 0xE0) init(c & 0x0F, 2)
66 else if ((c & 0xF8) == 0xF0) init(c & 0x07, 3)
73 /* basic file operations */
75 def with_tmp_file[A](prefix: String)(body: File => A): A =
77 val file = File.createTempFile(prefix, null)
78 try { body(file) } finally { file.delete }
81 def read_file(file: File): String =
83 val buf = new StringBuilder(file.length.toInt)
84 val reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset))
94 def write_file(file: File, text: CharSequence)
96 val writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset))
97 try { writer.append(text) }
98 finally { writer.close }
102 /* shell processes */
104 def raw_execute(env: Map[String, String], redirect: Boolean, args: String*): Process =
106 val cmdline = new java.util.LinkedList[String]
107 for (s <- args) cmdline.add(s)
109 val proc = new ProcessBuilder(cmdline)
110 proc.environment.clear
111 for ((x, y) <- env) proc.environment.put(x, y)
112 proc.redirectErrorStream(redirect)
115 catch { case e: IOException => error(e.getMessage) }
118 def process_output(proc: Process): (String, Int) =
120 proc.getOutputStream.close
121 val output = Source.fromInputStream(proc.getInputStream, charset).mkString // FIXME
125 proc.getInputStream.close
126 proc.getErrorStream.close
135 class Standard_System
137 val platform_root = if (Platform.is_windows) Cygwin.check_root() else "/"
138 override def toString = platform_root
143 private val Cygdrive = new Regex("/cygdrive/([a-zA-Z])($|/.*)")
145 def jvm_path(posix_path: String): String =
146 if (Platform.is_windows) {
147 val result_path = new StringBuilder
150 case Cygdrive(drive, rest) =>
151 result_path ++= (drive + ":" + File.separator)
153 case path if path.startsWith("/") =>
154 result_path ++= platform_root
158 for (p <- rest.split("/") if p != "") {
159 val len = result_path.length
160 if (len > 0 && result_path(len - 1) != File.separatorChar)
161 result_path += File.separatorChar
171 private val Platform_Root = new Regex("(?i)" +
172 Pattern.quote(platform_root) + """(?:\\+|\z)(.*)""")
174 private val Drive = new Regex("""([a-zA-Z]):\\*(.*)""")
176 def posix_path(jvm_path: String): String =
177 if (Platform.is_windows) {
178 jvm_path.replace('/', '\\') match {
179 case Platform_Root(rest) => "/" + rest.replace('\\', '/')
180 case Drive(letter, rest) =>
181 "/cygdrive/" + letter.toLowerCase(Locale.ENGLISH) +
182 (if (rest == "") "" else "/" + rest.replace('\\', '/'))
183 case path => path.replace('\\', '/')