src/Pure/System/standard_system.scala
author wenzelm
Sat Jan 09 23:22:56 2010 +0100 (2010-01-09 ago)
changeset 34300 3f2e25dc99ab
parent 34298 13e9f1f4acd9
child 36011 3ff725ac13a4
permissions -rw-r--r--
misc tuning;
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@34201
    12
  BufferedInputStream, FileInputStream, BufferedReader, InputStreamReader,
wenzelm@34298
    13
  File, FileFilter, IOException}
wenzelm@34201
    14
wenzelm@34201
    15
import scala.io.Source
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@34201
    22
  val charset = "UTF-8"
wenzelm@34201
    23
wenzelm@34201
    24
wenzelm@34201
    25
  /* permissive UTF-8 decoding */
wenzelm@34201
    26
wenzelm@34201
    27
  // see also http://en.wikipedia.org/wiki/UTF-8#Description
wenzelm@34201
    28
  // overlong encodings enable byte-stuffing
wenzelm@34201
    29
wenzelm@34201
    30
  def decode_permissive_utf8(text: CharSequence): String =
wenzelm@34201
    31
  {
wenzelm@34201
    32
    val buf = new java.lang.StringBuilder(text.length)
wenzelm@34201
    33
    var code = -1
wenzelm@34201
    34
    var rest = 0
wenzelm@34201
    35
    def flush()
wenzelm@34201
    36
    {
wenzelm@34201
    37
      if (code != -1) {
wenzelm@34201
    38
        if (rest == 0 && Character.isValidCodePoint(code))
wenzelm@34201
    39
          buf.appendCodePoint(code)
wenzelm@34201
    40
        else buf.append('\uFFFD')
wenzelm@34201
    41
        code = -1
wenzelm@34201
    42
        rest = 0
wenzelm@34201
    43
      }
wenzelm@34201
    44
    }
wenzelm@34201
    45
    def init(x: Int, n: Int)
wenzelm@34201
    46
    {
wenzelm@34201
    47
      flush()
wenzelm@34201
    48
      code = x
wenzelm@34201
    49
      rest = n
wenzelm@34201
    50
    }
wenzelm@34201
    51
    def push(x: Int)
wenzelm@34201
    52
    {
wenzelm@34201
    53
      if (rest <= 0) init(x, -1)
wenzelm@34201
    54
      else {
wenzelm@34201
    55
        code <<= 6
wenzelm@34201
    56
        code += x
wenzelm@34201
    57
        rest -= 1
wenzelm@34201
    58
      }
wenzelm@34201
    59
    }
wenzelm@34201
    60
    for (i <- 0 until text.length) {
wenzelm@34201
    61
      val c = text.charAt(i)
wenzelm@34201
    62
      if (c < 128) { flush(); buf.append(c) }
wenzelm@34201
    63
      else if ((c & 0xC0) == 0x80) push(c & 0x3F)
wenzelm@34201
    64
      else if ((c & 0xE0) == 0xC0) init(c & 0x1F, 1)
wenzelm@34201
    65
      else if ((c & 0xF0) == 0xE0) init(c & 0x0F, 2)
wenzelm@34201
    66
      else if ((c & 0xF8) == 0xF0) init(c & 0x07, 3)
wenzelm@34201
    67
    }
wenzelm@34201
    68
    flush()
wenzelm@34201
    69
    buf.toString
wenzelm@34201
    70
  }
wenzelm@34201
    71
wenzelm@34201
    72
wenzelm@34201
    73
  /* basic file operations */
wenzelm@34201
    74
wenzelm@34201
    75
  def with_tmp_file[A](prefix: String)(body: File => A): A =
wenzelm@34201
    76
  {
wenzelm@34201
    77
    val file = File.createTempFile(prefix, null)
wenzelm@34201
    78
    try { body(file) } finally { file.delete }
wenzelm@34201
    79
  }
wenzelm@34201
    80
wenzelm@34201
    81
  def read_file(file: File): String =
wenzelm@34201
    82
  {
wenzelm@34201
    83
    val buf = new StringBuilder(file.length.toInt)
wenzelm@34201
    84
    val reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset))
wenzelm@34201
    85
    var c = reader.read
wenzelm@34201
    86
    while (c != -1) {
wenzelm@34201
    87
      buf.append(c.toChar)
wenzelm@34201
    88
      c = reader.read
wenzelm@34201
    89
    }
wenzelm@34201
    90
    reader.close
wenzelm@34201
    91
    buf.toString
wenzelm@34201
    92
  }
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@34300
   101
  // FIXME handle (potentially cyclic) directory graph
wenzelm@34298
   102
  def find_files(start: File, ok: File => Boolean): List[File] =
wenzelm@34298
   103
  {
wenzelm@34298
   104
    val files = new mutable.ListBuffer[File]
wenzelm@34298
   105
    val filter = new FileFilter { def accept(entry: File) = entry.isDirectory || ok(entry) }
wenzelm@34298
   106
    def find_entry(entry: File)
wenzelm@34298
   107
    {
wenzelm@34298
   108
      if (ok(entry)) files += entry
wenzelm@34298
   109
      if (entry.isDirectory) entry.listFiles(filter).foreach(find_entry)
wenzelm@34298
   110
    }
wenzelm@34298
   111
    find_entry(start)
wenzelm@34298
   112
    files.toList
wenzelm@34298
   113
  }
wenzelm@34298
   114
wenzelm@34201
   115
wenzelm@34201
   116
  /* shell processes */
wenzelm@34201
   117
wenzelm@34219
   118
  def raw_execute(cwd: File, env: Map[String, String], redirect: Boolean, args: String*): Process =
wenzelm@34201
   119
  {
wenzelm@34201
   120
    val cmdline = new java.util.LinkedList[String]
wenzelm@34201
   121
    for (s <- args) cmdline.add(s)
wenzelm@34201
   122
wenzelm@34201
   123
    val proc = new ProcessBuilder(cmdline)
wenzelm@34219
   124
    if (cwd != null) proc.directory(cwd)
wenzelm@34202
   125
    if (env != null) {
wenzelm@34202
   126
      proc.environment.clear
wenzelm@34202
   127
      for ((x, y) <- env) proc.environment.put(x, y)
wenzelm@34202
   128
    }
wenzelm@34201
   129
    proc.redirectErrorStream(redirect)
wenzelm@34201
   130
wenzelm@34201
   131
    try { proc.start }
wenzelm@34201
   132
    catch { case e: IOException => error(e.getMessage) }
wenzelm@34201
   133
  }
wenzelm@34201
   134
wenzelm@34201
   135
  def process_output(proc: Process): (String, Int) =
wenzelm@34201
   136
  {
wenzelm@34201
   137
    proc.getOutputStream.close
wenzelm@34201
   138
    val output = Source.fromInputStream(proc.getInputStream, charset).mkString  // FIXME
wenzelm@34201
   139
    val rc =
wenzelm@34201
   140
      try { proc.waitFor }
wenzelm@34201
   141
      finally {
wenzelm@34201
   142
        proc.getInputStream.close
wenzelm@34201
   143
        proc.getErrorStream.close
wenzelm@34201
   144
        proc.destroy
wenzelm@34201
   145
        Thread.interrupted
wenzelm@34201
   146
      }
wenzelm@34201
   147
    (output, rc)
wenzelm@34201
   148
  }
wenzelm@34258
   149
wenzelm@34258
   150
  def raw_exec(cwd: File, env: Map[String, String], redirect: Boolean, args: String*):
wenzelm@34258
   151
    (String, Int) = process_output(raw_execute(cwd, env, redirect, args: _*))
wenzelm@34201
   152
}
wenzelm@34201
   153
wenzelm@34201
   154
wenzelm@34201
   155
class Standard_System
wenzelm@34201
   156
{
wenzelm@34201
   157
  val platform_root = if (Platform.is_windows) Cygwin.check_root() else "/"
wenzelm@34201
   158
  override def toString = platform_root
wenzelm@34201
   159
wenzelm@34201
   160
wenzelm@34201
   161
  /* jvm_path */
wenzelm@34201
   162
wenzelm@34201
   163
  private val Cygdrive = new Regex("/cygdrive/([a-zA-Z])($|/.*)")
wenzelm@34201
   164
wenzelm@34201
   165
  def jvm_path(posix_path: String): String =
wenzelm@34201
   166
    if (Platform.is_windows) {
wenzelm@34201
   167
      val result_path = new StringBuilder
wenzelm@34201
   168
      val rest =
wenzelm@34201
   169
        posix_path match {
wenzelm@34201
   170
          case Cygdrive(drive, rest) =>
wenzelm@34201
   171
            result_path ++= (drive + ":" + File.separator)
wenzelm@34201
   172
            rest
wenzelm@34201
   173
          case path if path.startsWith("/") =>
wenzelm@34201
   174
            result_path ++= platform_root
wenzelm@34201
   175
            path
wenzelm@34201
   176
          case path => path
wenzelm@34201
   177
        }
wenzelm@34201
   178
      for (p <- rest.split("/") if p != "") {
wenzelm@34201
   179
        val len = result_path.length
wenzelm@34201
   180
        if (len > 0 && result_path(len - 1) != File.separatorChar)
wenzelm@34201
   181
          result_path += File.separatorChar
wenzelm@34201
   182
        result_path ++= p
wenzelm@34201
   183
      }
wenzelm@34201
   184
      result_path.toString
wenzelm@34201
   185
    }
wenzelm@34201
   186
    else posix_path
wenzelm@34201
   187
wenzelm@34201
   188
wenzelm@34201
   189
  /* posix_path */
wenzelm@34201
   190
wenzelm@34201
   191
  private val Platform_Root = new Regex("(?i)" +
wenzelm@34201
   192
    Pattern.quote(platform_root) + """(?:\\+|\z)(.*)""")
wenzelm@34201
   193
wenzelm@34201
   194
  private val Drive = new Regex("""([a-zA-Z]):\\*(.*)""")
wenzelm@34201
   195
wenzelm@34201
   196
  def posix_path(jvm_path: String): String =
wenzelm@34201
   197
    if (Platform.is_windows) {
wenzelm@34201
   198
      jvm_path.replace('/', '\\') match {
wenzelm@34201
   199
        case Platform_Root(rest) => "/" + rest.replace('\\', '/')
wenzelm@34201
   200
        case Drive(letter, rest) =>
wenzelm@34201
   201
          "/cygdrive/" + letter.toLowerCase(Locale.ENGLISH) +
wenzelm@34201
   202
            (if (rest == "") "" else "/" + rest.replace('\\', '/'))
wenzelm@34201
   203
        case path => path.replace('\\', '/')
wenzelm@34201
   204
      }
wenzelm@34201
   205
    }
wenzelm@34201
   206
    else jvm_path
wenzelm@34201
   207
}