src/Pure/System/standard_system.scala
author wenzelm
Mon Jan 04 22:43:07 2010 +0100 (2010-01-04)
changeset 34258 e936d3c35ce0
parent 34219 d37cfca69887
child 34298 13e9f1f4acd9
permissions -rw-r--r--
Standard_System.raw_exec;
more robust root.mkdirs;
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@34201
    13
  File, 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@34201
   101
wenzelm@34201
   102
  /* shell processes */
wenzelm@34201
   103
wenzelm@34219
   104
  def raw_execute(cwd: File, env: Map[String, String], redirect: Boolean, args: String*): Process =
wenzelm@34201
   105
  {
wenzelm@34201
   106
    val cmdline = new java.util.LinkedList[String]
wenzelm@34201
   107
    for (s <- args) cmdline.add(s)
wenzelm@34201
   108
wenzelm@34201
   109
    val proc = new ProcessBuilder(cmdline)
wenzelm@34219
   110
    if (cwd != null) proc.directory(cwd)
wenzelm@34202
   111
    if (env != null) {
wenzelm@34202
   112
      proc.environment.clear
wenzelm@34202
   113
      for ((x, y) <- env) proc.environment.put(x, y)
wenzelm@34202
   114
    }
wenzelm@34201
   115
    proc.redirectErrorStream(redirect)
wenzelm@34201
   116
wenzelm@34201
   117
    try { proc.start }
wenzelm@34201
   118
    catch { case e: IOException => error(e.getMessage) }
wenzelm@34201
   119
  }
wenzelm@34201
   120
wenzelm@34201
   121
  def process_output(proc: Process): (String, Int) =
wenzelm@34201
   122
  {
wenzelm@34201
   123
    proc.getOutputStream.close
wenzelm@34201
   124
    val output = Source.fromInputStream(proc.getInputStream, charset).mkString  // FIXME
wenzelm@34201
   125
    val rc =
wenzelm@34201
   126
      try { proc.waitFor }
wenzelm@34201
   127
      finally {
wenzelm@34201
   128
        proc.getInputStream.close
wenzelm@34201
   129
        proc.getErrorStream.close
wenzelm@34201
   130
        proc.destroy
wenzelm@34201
   131
        Thread.interrupted
wenzelm@34201
   132
      }
wenzelm@34201
   133
    (output, rc)
wenzelm@34201
   134
  }
wenzelm@34258
   135
wenzelm@34258
   136
  def raw_exec(cwd: File, env: Map[String, String], redirect: Boolean, args: String*):
wenzelm@34258
   137
    (String, Int) = process_output(raw_execute(cwd, env, redirect, args: _*))
wenzelm@34201
   138
}
wenzelm@34201
   139
wenzelm@34201
   140
wenzelm@34201
   141
class Standard_System
wenzelm@34201
   142
{
wenzelm@34201
   143
  val platform_root = if (Platform.is_windows) Cygwin.check_root() else "/"
wenzelm@34201
   144
  override def toString = platform_root
wenzelm@34201
   145
wenzelm@34201
   146
wenzelm@34201
   147
  /* jvm_path */
wenzelm@34201
   148
wenzelm@34201
   149
  private val Cygdrive = new Regex("/cygdrive/([a-zA-Z])($|/.*)")
wenzelm@34201
   150
wenzelm@34201
   151
  def jvm_path(posix_path: String): String =
wenzelm@34201
   152
    if (Platform.is_windows) {
wenzelm@34201
   153
      val result_path = new StringBuilder
wenzelm@34201
   154
      val rest =
wenzelm@34201
   155
        posix_path match {
wenzelm@34201
   156
          case Cygdrive(drive, rest) =>
wenzelm@34201
   157
            result_path ++= (drive + ":" + File.separator)
wenzelm@34201
   158
            rest
wenzelm@34201
   159
          case path if path.startsWith("/") =>
wenzelm@34201
   160
            result_path ++= platform_root
wenzelm@34201
   161
            path
wenzelm@34201
   162
          case path => path
wenzelm@34201
   163
        }
wenzelm@34201
   164
      for (p <- rest.split("/") if p != "") {
wenzelm@34201
   165
        val len = result_path.length
wenzelm@34201
   166
        if (len > 0 && result_path(len - 1) != File.separatorChar)
wenzelm@34201
   167
          result_path += File.separatorChar
wenzelm@34201
   168
        result_path ++= p
wenzelm@34201
   169
      }
wenzelm@34201
   170
      result_path.toString
wenzelm@34201
   171
    }
wenzelm@34201
   172
    else posix_path
wenzelm@34201
   173
wenzelm@34201
   174
wenzelm@34201
   175
  /* posix_path */
wenzelm@34201
   176
wenzelm@34201
   177
  private val Platform_Root = new Regex("(?i)" +
wenzelm@34201
   178
    Pattern.quote(platform_root) + """(?:\\+|\z)(.*)""")
wenzelm@34201
   179
wenzelm@34201
   180
  private val Drive = new Regex("""([a-zA-Z]):\\*(.*)""")
wenzelm@34201
   181
wenzelm@34201
   182
  def posix_path(jvm_path: String): String =
wenzelm@34201
   183
    if (Platform.is_windows) {
wenzelm@34201
   184
      jvm_path.replace('/', '\\') match {
wenzelm@34201
   185
        case Platform_Root(rest) => "/" + rest.replace('\\', '/')
wenzelm@34201
   186
        case Drive(letter, rest) =>
wenzelm@34201
   187
          "/cygdrive/" + letter.toLowerCase(Locale.ENGLISH) +
wenzelm@34201
   188
            (if (rest == "") "" else "/" + rest.replace('\\', '/'))
wenzelm@34201
   189
        case path => path.replace('\\', '/')
wenzelm@34201
   190
      }
wenzelm@34201
   191
    }
wenzelm@34201
   192
    else jvm_path
wenzelm@34201
   193
}