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