src/Pure/System/standard_system.scala
author wenzelm
Tue Sep 21 21:53:15 2010 +0200 (2010-09-21)
changeset 39578 b75164153c37
parent 39522 01aade784da9
child 39582 a873158542d0
permissions -rw-r--r--
added Standard_System.slurp convenience;
tuned;
wenzelm@34201
     1
/*  Title:      Pure/System/standard_system.scala
wenzelm@34201
     2
    Author:     Makarius
wenzelm@34201
     3
wenzelm@34201
     4
Standard system operations, with basic Cygwin/Posix compatibility.
wenzelm@34201
     5
*/
wenzelm@34201
     6
wenzelm@34201
     7
package isabelle
wenzelm@34201
     8
wenzelm@34201
     9
import java.util.regex.Pattern
wenzelm@34201
    10
import java.util.Locale
wenzelm@34201
    11
import java.io.{BufferedWriter, OutputStreamWriter, FileOutputStream,
wenzelm@39578
    12
  BufferedInputStream, InputStream, FileInputStream, BufferedReader, InputStreamReader,
wenzelm@34298
    13
  File, FileFilter, IOException}
wenzelm@34201
    14
wenzelm@36011
    15
import scala.io.{Source, Codec}
wenzelm@34201
    16
import scala.util.matching.Regex
wenzelm@34201
    17
import scala.collection.mutable
wenzelm@34201
    18
wenzelm@34201
    19
wenzelm@34201
    20
object Standard_System
wenzelm@34201
    21
{
wenzelm@38264
    22
  /* UTF-8 charset */
wenzelm@38264
    23
wenzelm@34201
    24
  val charset = "UTF-8"
wenzelm@36015
    25
  def codec(): Codec = Codec(charset)
wenzelm@34201
    26
wenzelm@38264
    27
  def string_bytes(s: String): Array[Byte] = s.getBytes(charset)
wenzelm@38264
    28
wenzelm@34201
    29
wenzelm@34201
    30
  /* permissive UTF-8 decoding */
wenzelm@34201
    31
wenzelm@34201
    32
  // see also http://en.wikipedia.org/wiki/UTF-8#Description
wenzelm@34201
    33
  // overlong encodings enable byte-stuffing
wenzelm@34201
    34
wenzelm@34201
    35
  def decode_permissive_utf8(text: CharSequence): String =
wenzelm@34201
    36
  {
wenzelm@34201
    37
    val buf = new java.lang.StringBuilder(text.length)
wenzelm@34201
    38
    var code = -1
wenzelm@34201
    39
    var rest = 0
wenzelm@34201
    40
    def flush()
wenzelm@34201
    41
    {
wenzelm@34201
    42
      if (code != -1) {
wenzelm@34201
    43
        if (rest == 0 && Character.isValidCodePoint(code))
wenzelm@34201
    44
          buf.appendCodePoint(code)
wenzelm@34201
    45
        else buf.append('\uFFFD')
wenzelm@34201
    46
        code = -1
wenzelm@34201
    47
        rest = 0
wenzelm@34201
    48
      }
wenzelm@34201
    49
    }
wenzelm@34201
    50
    def init(x: Int, n: Int)
wenzelm@34201
    51
    {
wenzelm@34201
    52
      flush()
wenzelm@34201
    53
      code = x
wenzelm@34201
    54
      rest = n
wenzelm@34201
    55
    }
wenzelm@34201
    56
    def push(x: Int)
wenzelm@34201
    57
    {
wenzelm@34201
    58
      if (rest <= 0) init(x, -1)
wenzelm@34201
    59
      else {
wenzelm@34201
    60
        code <<= 6
wenzelm@34201
    61
        code += x
wenzelm@34201
    62
        rest -= 1
wenzelm@34201
    63
      }
wenzelm@34201
    64
    }
wenzelm@34201
    65
    for (i <- 0 until text.length) {
wenzelm@34201
    66
      val c = text.charAt(i)
wenzelm@34201
    67
      if (c < 128) { flush(); buf.append(c) }
wenzelm@34201
    68
      else if ((c & 0xC0) == 0x80) push(c & 0x3F)
wenzelm@34201
    69
      else if ((c & 0xE0) == 0xC0) init(c & 0x1F, 1)
wenzelm@34201
    70
      else if ((c & 0xF0) == 0xE0) init(c & 0x0F, 2)
wenzelm@34201
    71
      else if ((c & 0xF8) == 0xF0) init(c & 0x07, 3)
wenzelm@34201
    72
    }
wenzelm@34201
    73
    flush()
wenzelm@34201
    74
    buf.toString
wenzelm@34201
    75
  }
wenzelm@34201
    76
wenzelm@34201
    77
wenzelm@34201
    78
  /* basic file operations */
wenzelm@34201
    79
wenzelm@39578
    80
  def slurp(reader: BufferedReader): String =
wenzelm@34201
    81
  {
wenzelm@39578
    82
    val output = new StringBuilder(100)
wenzelm@39578
    83
    var c = -1
wenzelm@39578
    84
    while ({ c = reader.read; c != -1 }) output += c.toChar
wenzelm@39578
    85
    reader.close
wenzelm@39578
    86
    output.toString
wenzelm@34201
    87
  }
wenzelm@34201
    88
wenzelm@39578
    89
  def slurp(stream: InputStream): String =
wenzelm@39578
    90
    slurp(new BufferedReader(new InputStreamReader(stream, charset)))
wenzelm@39578
    91
wenzelm@39578
    92
  def read_file(file: File): String = slurp(new FileInputStream(file))
wenzelm@34201
    93
wenzelm@34201
    94
  def write_file(file: File, text: CharSequence)
wenzelm@34201
    95
  {
wenzelm@34201
    96
    val writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset))
wenzelm@34201
    97
    try { writer.append(text) }
wenzelm@34201
    98
    finally { writer.close }
wenzelm@34201
    99
  }
wenzelm@34201
   100
wenzelm@39578
   101
  def with_tmp_file[A](prefix: String)(body: File => A): A =
wenzelm@39578
   102
  {
wenzelm@39578
   103
    val file = File.createTempFile(prefix, null)
wenzelm@39578
   104
    try { body(file) } finally { file.delete }
wenzelm@39578
   105
  }
wenzelm@39578
   106
wenzelm@34300
   107
  // FIXME handle (potentially cyclic) directory graph
wenzelm@34298
   108
  def find_files(start: File, ok: File => Boolean): List[File] =
wenzelm@34298
   109
  {
wenzelm@34298
   110
    val files = new mutable.ListBuffer[File]
wenzelm@34298
   111
    val filter = new FileFilter { def accept(entry: File) = entry.isDirectory || ok(entry) }
wenzelm@34298
   112
    def find_entry(entry: File)
wenzelm@34298
   113
    {
wenzelm@34298
   114
      if (ok(entry)) files += entry
wenzelm@34298
   115
      if (entry.isDirectory) entry.listFiles(filter).foreach(find_entry)
wenzelm@34298
   116
    }
wenzelm@34298
   117
    find_entry(start)
wenzelm@34298
   118
    files.toList
wenzelm@34298
   119
  }
wenzelm@34298
   120
wenzelm@34201
   121
wenzelm@34201
   122
  /* shell processes */
wenzelm@34201
   123
wenzelm@34219
   124
  def raw_execute(cwd: File, env: Map[String, String], redirect: Boolean, args: String*): Process =
wenzelm@34201
   125
  {
wenzelm@34201
   126
    val cmdline = new java.util.LinkedList[String]
wenzelm@34201
   127
    for (s <- args) cmdline.add(s)
wenzelm@34201
   128
wenzelm@34201
   129
    val proc = new ProcessBuilder(cmdline)
wenzelm@34219
   130
    if (cwd != null) proc.directory(cwd)
wenzelm@34202
   131
    if (env != null) {
wenzelm@34202
   132
      proc.environment.clear
wenzelm@34202
   133
      for ((x, y) <- env) proc.environment.put(x, y)
wenzelm@34202
   134
    }
wenzelm@34201
   135
    proc.redirectErrorStream(redirect)
wenzelm@39522
   136
    proc.start
wenzelm@34201
   137
  }
wenzelm@34201
   138
wenzelm@34201
   139
  def process_output(proc: Process): (String, Int) =
wenzelm@34201
   140
  {
wenzelm@34201
   141
    proc.getOutputStream.close
wenzelm@39578
   142
    val output = slurp(proc.getInputStream)
wenzelm@34201
   143
    val rc =
wenzelm@34201
   144
      try { proc.waitFor }
wenzelm@34201
   145
      finally {
wenzelm@34201
   146
        proc.getInputStream.close
wenzelm@34201
   147
        proc.getErrorStream.close
wenzelm@34201
   148
        proc.destroy
wenzelm@34201
   149
        Thread.interrupted
wenzelm@34201
   150
      }
wenzelm@34201
   151
    (output, rc)
wenzelm@34201
   152
  }
wenzelm@34258
   153
wenzelm@39522
   154
  def raw_exec(cwd: File, env: Map[String, String], redirect: Boolean, args: String*)
wenzelm@39522
   155
    : (String, Int) = process_output(raw_execute(cwd, env, redirect, args: _*))
wenzelm@34201
   156
}
wenzelm@34201
   157
wenzelm@34201
   158
wenzelm@34201
   159
class Standard_System
wenzelm@34201
   160
{
wenzelm@34201
   161
  val platform_root = if (Platform.is_windows) Cygwin.check_root() else "/"
wenzelm@34201
   162
  override def toString = platform_root
wenzelm@34201
   163
wenzelm@34201
   164
wenzelm@34201
   165
  /* jvm_path */
wenzelm@34201
   166
wenzelm@34201
   167
  private val Cygdrive = new Regex("/cygdrive/([a-zA-Z])($|/.*)")
wenzelm@36136
   168
  private val Named_Root = new Regex("//+([^/]*)(.*)")
wenzelm@34201
   169
wenzelm@34201
   170
  def jvm_path(posix_path: String): String =
wenzelm@34201
   171
    if (Platform.is_windows) {
wenzelm@34201
   172
      val result_path = new StringBuilder
wenzelm@34201
   173
      val rest =
wenzelm@34201
   174
        posix_path match {
wenzelm@34201
   175
          case Cygdrive(drive, rest) =>
wenzelm@34201
   176
            result_path ++= (drive + ":" + File.separator)
wenzelm@34201
   177
            rest
wenzelm@36136
   178
          case Named_Root(root, rest) =>
wenzelm@36136
   179
            result_path ++= File.separator
wenzelm@36136
   180
            result_path ++= File.separator
wenzelm@36136
   181
            result_path ++= root
wenzelm@36136
   182
            rest
wenzelm@34201
   183
          case path if path.startsWith("/") =>
wenzelm@34201
   184
            result_path ++= platform_root
wenzelm@34201
   185
            path
wenzelm@34201
   186
          case path => path
wenzelm@34201
   187
        }
wenzelm@34201
   188
      for (p <- rest.split("/") if p != "") {
wenzelm@34201
   189
        val len = result_path.length
wenzelm@34201
   190
        if (len > 0 && result_path(len - 1) != File.separatorChar)
wenzelm@34201
   191
          result_path += File.separatorChar
wenzelm@34201
   192
        result_path ++= p
wenzelm@34201
   193
      }
wenzelm@34201
   194
      result_path.toString
wenzelm@34201
   195
    }
wenzelm@34201
   196
    else posix_path
wenzelm@34201
   197
wenzelm@34201
   198
wenzelm@34201
   199
  /* posix_path */
wenzelm@34201
   200
wenzelm@34201
   201
  private val Platform_Root = new Regex("(?i)" +
wenzelm@34201
   202
    Pattern.quote(platform_root) + """(?:\\+|\z)(.*)""")
wenzelm@34201
   203
wenzelm@34201
   204
  private val Drive = new Regex("""([a-zA-Z]):\\*(.*)""")
wenzelm@34201
   205
wenzelm@34201
   206
  def posix_path(jvm_path: String): String =
wenzelm@34201
   207
    if (Platform.is_windows) {
wenzelm@34201
   208
      jvm_path.replace('/', '\\') match {
wenzelm@34201
   209
        case Platform_Root(rest) => "/" + rest.replace('\\', '/')
wenzelm@34201
   210
        case Drive(letter, rest) =>
wenzelm@34201
   211
          "/cygdrive/" + letter.toLowerCase(Locale.ENGLISH) +
wenzelm@34201
   212
            (if (rest == "") "" else "/" + rest.replace('\\', '/'))
wenzelm@34201
   213
        case path => path.replace('\\', '/')
wenzelm@34201
   214
      }
wenzelm@34201
   215
    }
wenzelm@34201
   216
    else jvm_path
wenzelm@36193
   217
wenzelm@36193
   218
wenzelm@36193
   219
  /* this_java executable */
wenzelm@36193
   220
wenzelm@36193
   221
  def this_java(): String =
wenzelm@36193
   222
  {
wenzelm@36193
   223
    val java_home = System.getProperty("java.home")
wenzelm@36193
   224
    val java_exe =
wenzelm@36193
   225
      if (Platform.is_windows) new File(java_home + "\\bin\\java.exe")
wenzelm@36193
   226
      else new File(java_home + "/bin/java")
wenzelm@36193
   227
    if (!java_exe.isFile) error("Expected this Java executable: " + java_exe.toString)
wenzelm@36193
   228
    posix_path(java_exe.getAbsolutePath)
wenzelm@36193
   229
  }
wenzelm@34201
   230
}