src/Pure/System/standard_system.scala
author wenzelm
Thu May 24 23:44:16 2012 +0200 (2012-05-24)
changeset 47998 969457d93f75
parent 47113 b5a5662528fb
child 48027 69ba790960ba
permissions -rw-r--r--
discontinued obsolete Cygwin module;
wenzelm@34201
     1
/*  Title:      Pure/System/standard_system.scala
wenzelm@45673
     2
    Module:     PIDE
wenzelm@34201
     3
    Author:     Makarius
wenzelm@34201
     4
wenzelm@34201
     5
Standard system operations, with basic Cygwin/Posix compatibility.
wenzelm@34201
     6
*/
wenzelm@34201
     7
wenzelm@34201
     8
package isabelle
wenzelm@34201
     9
wenzelm@43520
    10
import java.lang.System
wenzelm@39732
    11
import java.util.zip.{ZipEntry, ZipInputStream}
wenzelm@34201
    12
import java.util.regex.Pattern
wenzelm@34201
    13
import java.util.Locale
wenzelm@39705
    14
import java.net.URL
wenzelm@39705
    15
import java.io.{BufferedWriter, OutputStreamWriter, FileOutputStream, BufferedOutputStream,
wenzelm@39578
    16
  BufferedInputStream, InputStream, FileInputStream, BufferedReader, InputStreamReader,
wenzelm@34298
    17
  File, FileFilter, IOException}
wenzelm@43516
    18
import java.nio.charset.Charset
wenzelm@34201
    19
wenzelm@36011
    20
import scala.io.{Source, Codec}
wenzelm@34201
    21
import scala.util.matching.Regex
wenzelm@34201
    22
import scala.collection.mutable
wenzelm@34201
    23
wenzelm@34201
    24
wenzelm@34201
    25
object Standard_System
wenzelm@34201
    26
{
wenzelm@38264
    27
  /* UTF-8 charset */
wenzelm@38264
    28
wenzelm@43516
    29
  val charset_name: String = "UTF-8"
wenzelm@43516
    30
  val charset: Charset = Charset.forName(charset_name)
wenzelm@36015
    31
  def codec(): Codec = Codec(charset)
wenzelm@34201
    32
wenzelm@38264
    33
  def string_bytes(s: String): Array[Byte] = s.getBytes(charset)
wenzelm@38264
    34
wenzelm@34201
    35
wenzelm@34201
    36
  /* permissive UTF-8 decoding */
wenzelm@34201
    37
wenzelm@34201
    38
  // see also http://en.wikipedia.org/wiki/UTF-8#Description
wenzelm@34201
    39
  // overlong encodings enable byte-stuffing
wenzelm@34201
    40
wenzelm@34201
    41
  def decode_permissive_utf8(text: CharSequence): String =
wenzelm@34201
    42
  {
wenzelm@34201
    43
    val buf = new java.lang.StringBuilder(text.length)
wenzelm@34201
    44
    var code = -1
wenzelm@34201
    45
    var rest = 0
wenzelm@34201
    46
    def flush()
wenzelm@34201
    47
    {
wenzelm@34201
    48
      if (code != -1) {
wenzelm@34201
    49
        if (rest == 0 && Character.isValidCodePoint(code))
wenzelm@34201
    50
          buf.appendCodePoint(code)
wenzelm@34201
    51
        else buf.append('\uFFFD')
wenzelm@34201
    52
        code = -1
wenzelm@34201
    53
        rest = 0
wenzelm@34201
    54
      }
wenzelm@34201
    55
    }
wenzelm@34201
    56
    def init(x: Int, n: Int)
wenzelm@34201
    57
    {
wenzelm@34201
    58
      flush()
wenzelm@34201
    59
      code = x
wenzelm@34201
    60
      rest = n
wenzelm@34201
    61
    }
wenzelm@34201
    62
    def push(x: Int)
wenzelm@34201
    63
    {
wenzelm@34201
    64
      if (rest <= 0) init(x, -1)
wenzelm@34201
    65
      else {
wenzelm@34201
    66
        code <<= 6
wenzelm@34201
    67
        code += x
wenzelm@34201
    68
        rest -= 1
wenzelm@34201
    69
      }
wenzelm@34201
    70
    }
wenzelm@34201
    71
    for (i <- 0 until text.length) {
wenzelm@34201
    72
      val c = text.charAt(i)
wenzelm@34201
    73
      if (c < 128) { flush(); buf.append(c) }
wenzelm@34201
    74
      else if ((c & 0xC0) == 0x80) push(c & 0x3F)
wenzelm@34201
    75
      else if ((c & 0xE0) == 0xC0) init(c & 0x1F, 1)
wenzelm@34201
    76
      else if ((c & 0xF0) == 0xE0) init(c & 0x0F, 2)
wenzelm@34201
    77
      else if ((c & 0xF8) == 0xF0) init(c & 0x07, 3)
wenzelm@34201
    78
    }
wenzelm@34201
    79
    flush()
wenzelm@34201
    80
    buf.toString
wenzelm@34201
    81
  }
wenzelm@34201
    82
wenzelm@43746
    83
  private class Decode_Chars(decode: String => String,
wenzelm@43746
    84
    buffer: Array[Byte], start: Int, end: Int) extends CharSequence
wenzelm@43746
    85
  {
wenzelm@43746
    86
    def length: Int = end - start
wenzelm@43746
    87
    def charAt(i: Int): Char = (buffer(start + i).asInstanceOf[Int] & 0xFF).asInstanceOf[Char]
wenzelm@43746
    88
    def subSequence(i: Int, j: Int): CharSequence =
wenzelm@43746
    89
      new Decode_Chars(decode, buffer, start + i, start + j)
wenzelm@43746
    90
wenzelm@43746
    91
    // toString with adhoc decoding: abuse of CharSequence interface
wenzelm@43746
    92
    override def toString: String = decode(decode_permissive_utf8(this))
wenzelm@43746
    93
  }
wenzelm@43746
    94
wenzelm@43746
    95
  def decode_chars(decode: String => String,
wenzelm@43746
    96
    buffer: Array[Byte], start: Int, end: Int): CharSequence =
wenzelm@43746
    97
  {
wenzelm@43746
    98
    require(0 <= start && start <= end && end <= buffer.length)
wenzelm@43746
    99
    new Decode_Chars(decode, buffer, start, end)
wenzelm@43746
   100
  }
wenzelm@43746
   101
wenzelm@34201
   102
wenzelm@34201
   103
  /* basic file operations */
wenzelm@34201
   104
wenzelm@39578
   105
  def slurp(reader: BufferedReader): String =
wenzelm@34201
   106
  {
wenzelm@39578
   107
    val output = new StringBuilder(100)
wenzelm@39578
   108
    var c = -1
wenzelm@39578
   109
    while ({ c = reader.read; c != -1 }) output += c.toChar
wenzelm@39578
   110
    reader.close
wenzelm@39578
   111
    output.toString
wenzelm@34201
   112
  }
wenzelm@34201
   113
wenzelm@39578
   114
  def slurp(stream: InputStream): String =
wenzelm@39578
   115
    slurp(new BufferedReader(new InputStreamReader(stream, charset)))
wenzelm@39578
   116
wenzelm@39578
   117
  def read_file(file: File): String = slurp(new FileInputStream(file))
wenzelm@34201
   118
wenzelm@34201
   119
  def write_file(file: File, text: CharSequence)
wenzelm@34201
   120
  {
wenzelm@43516
   121
    val writer =
wenzelm@43516
   122
      new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset))
wenzelm@34201
   123
    try { writer.append(text) }
wenzelm@34201
   124
    finally { writer.close }
wenzelm@34201
   125
  }
wenzelm@34201
   126
wenzelm@39578
   127
  def with_tmp_file[A](prefix: String)(body: File => A): A =
wenzelm@39578
   128
  {
wenzelm@39578
   129
    val file = File.createTempFile(prefix, null)
wenzelm@39582
   130
    file.deleteOnExit
wenzelm@39578
   131
    try { body(file) } finally { file.delete }
wenzelm@39578
   132
  }
wenzelm@39578
   133
wenzelm@34300
   134
  // FIXME handle (potentially cyclic) directory graph
wenzelm@34298
   135
  def find_files(start: File, ok: File => Boolean): List[File] =
wenzelm@34298
   136
  {
wenzelm@34298
   137
    val files = new mutable.ListBuffer[File]
wenzelm@34298
   138
    val filter = new FileFilter { def accept(entry: File) = entry.isDirectory || ok(entry) }
wenzelm@34298
   139
    def find_entry(entry: File)
wenzelm@34298
   140
    {
wenzelm@34298
   141
      if (ok(entry)) files += entry
wenzelm@34298
   142
      if (entry.isDirectory) entry.listFiles(filter).foreach(find_entry)
wenzelm@34298
   143
    }
wenzelm@34298
   144
    find_entry(start)
wenzelm@34298
   145
    files.toList
wenzelm@34298
   146
  }
wenzelm@34298
   147
wenzelm@34201
   148
wenzelm@34201
   149
  /* shell processes */
wenzelm@34201
   150
wenzelm@34219
   151
  def raw_execute(cwd: File, env: Map[String, String], redirect: Boolean, args: String*): Process =
wenzelm@34201
   152
  {
wenzelm@34201
   153
    val cmdline = new java.util.LinkedList[String]
wenzelm@34201
   154
    for (s <- args) cmdline.add(s)
wenzelm@34201
   155
wenzelm@34201
   156
    val proc = new ProcessBuilder(cmdline)
wenzelm@34219
   157
    if (cwd != null) proc.directory(cwd)
wenzelm@34202
   158
    if (env != null) {
wenzelm@34202
   159
      proc.environment.clear
wenzelm@34202
   160
      for ((x, y) <- env) proc.environment.put(x, y)
wenzelm@34202
   161
    }
wenzelm@34201
   162
    proc.redirectErrorStream(redirect)
wenzelm@39522
   163
    proc.start
wenzelm@34201
   164
  }
wenzelm@34201
   165
wenzelm@34201
   166
  def process_output(proc: Process): (String, Int) =
wenzelm@34201
   167
  {
wenzelm@34201
   168
    proc.getOutputStream.close
wenzelm@39578
   169
    val output = slurp(proc.getInputStream)
wenzelm@34201
   170
    val rc =
wenzelm@34201
   171
      try { proc.waitFor }
wenzelm@34201
   172
      finally {
wenzelm@34201
   173
        proc.getInputStream.close
wenzelm@34201
   174
        proc.getErrorStream.close
wenzelm@34201
   175
        proc.destroy
wenzelm@34201
   176
        Thread.interrupted
wenzelm@34201
   177
      }
wenzelm@34201
   178
    (output, rc)
wenzelm@34201
   179
  }
wenzelm@34258
   180
wenzelm@39522
   181
  def raw_exec(cwd: File, env: Map[String, String], redirect: Boolean, args: String*)
wenzelm@39522
   182
    : (String, Int) = process_output(raw_execute(cwd, env, redirect, args: _*))
wenzelm@39705
   183
wenzelm@39705
   184
wenzelm@39732
   185
  /* unpack zip archive -- platform file-system */
wenzelm@39732
   186
wenzelm@39732
   187
  def unzip(url: URL, root: File)
wenzelm@39732
   188
  {
wenzelm@39732
   189
    import scala.collection.JavaConversions._
wenzelm@39732
   190
wenzelm@39732
   191
    val buffer = new Array[Byte](4096)
wenzelm@39732
   192
wenzelm@39732
   193
    val zip_stream = new ZipInputStream(new BufferedInputStream(url.openStream))
wenzelm@39732
   194
    var entry: ZipEntry = null
wenzelm@39732
   195
    try {
wenzelm@39732
   196
      while ({ entry = zip_stream.getNextEntry; entry != null }) {
wenzelm@39732
   197
        val file = new File(root, entry.getName.replace('/', File.separatorChar))
wenzelm@39732
   198
        val dir = file.getParentFile
wenzelm@39732
   199
        if (dir != null && !dir.isDirectory && !dir.mkdirs)
wenzelm@39732
   200
          error("Failed to create directory: " + dir)
wenzelm@39732
   201
wenzelm@39732
   202
        var len = 0
wenzelm@39732
   203
        val out_stream = new BufferedOutputStream(new FileOutputStream(file))
wenzelm@39732
   204
        try {
wenzelm@39732
   205
          while ({ len = zip_stream.read(buffer); len != -1 })
wenzelm@39732
   206
            out_stream.write(buffer, 0, len)
wenzelm@39732
   207
        }
wenzelm@39732
   208
        finally { out_stream.close }
wenzelm@39732
   209
      }
wenzelm@39732
   210
    }
wenzelm@39732
   211
    finally { zip_stream.close }
wenzelm@39732
   212
  }
wenzelm@39732
   213
wenzelm@39732
   214
wenzelm@39732
   215
  /* unpack tar archive -- POSIX file-system */
wenzelm@39705
   216
wenzelm@39708
   217
  def posix_untar(url: URL, root: File, gunzip: Boolean = false,
wenzelm@39709
   218
    tar: String = "tar", gzip: String = "", progress: Int => Unit = _ => ()): String =
wenzelm@39705
   219
  {
wenzelm@39708
   220
    if (!root.isDirectory && !root.mkdirs)
wenzelm@39708
   221
      error("Failed to create root directory: " + root)
wenzelm@39705
   222
wenzelm@39705
   223
    val connection = url.openConnection
wenzelm@39705
   224
wenzelm@39705
   225
    val length = connection.getContentLength.toLong
wenzelm@39705
   226
    require(length >= 0L)
wenzelm@39705
   227
wenzelm@39708
   228
    val stream = new BufferedInputStream(connection.getInputStream)
wenzelm@39705
   229
    val progress_stream = new InputStream {
wenzelm@39705
   230
      private val total = length max 1L
wenzelm@39705
   231
      private var index = 0L
wenzelm@39705
   232
      private var percentage = 0L
wenzelm@39705
   233
      override def read(): Int =
wenzelm@39705
   234
      {
wenzelm@39708
   235
        val c = stream.read
wenzelm@39705
   236
        if (c != -1) {
wenzelm@39705
   237
          index += 100
wenzelm@39705
   238
          val p = index / total
wenzelm@39705
   239
          if (percentage != p) { percentage = p; progress(percentage.toInt) }
wenzelm@39705
   240
        }
wenzelm@39705
   241
        c
wenzelm@39705
   242
      }
wenzelm@39708
   243
      override def available(): Int = stream.available
wenzelm@39708
   244
      override def close() { stream.close }
wenzelm@39705
   245
    }
wenzelm@39705
   246
wenzelm@39709
   247
    val cmdline =
wenzelm@39709
   248
      List(tar, "-o", "-x", "-f-") :::
wenzelm@39709
   249
        (if (!gunzip) Nil else if (gzip == "") List("-z") else List("-I", gzip))
wenzelm@39709
   250
wenzelm@39709
   251
    val proc = raw_execute(root, null, false, cmdline:_*)
wenzelm@39705
   252
    val stdout = Simple_Thread.future("tar_stdout") { slurp(proc.getInputStream) }
wenzelm@39705
   253
    val stderr = Simple_Thread.future("tar_stderr") { slurp(proc.getErrorStream) }
wenzelm@39705
   254
    val stdin = new BufferedOutputStream(proc.getOutputStream)
wenzelm@39705
   255
wenzelm@39705
   256
    try {
wenzelm@39705
   257
      var c = -1
wenzelm@39706
   258
      val io_err =
wenzelm@39708
   259
        try { while ({ c = progress_stream.read; c != -1 }) stdin.write(c); false }
wenzelm@39706
   260
        catch { case e: IOException => true }
wenzelm@39705
   261
      stdin.close
wenzelm@39705
   262
wenzelm@39705
   263
      val rc = try { proc.waitFor } finally { Thread.interrupted }
wenzelm@39706
   264
      if (io_err || rc != 0) error(stderr.join.trim) else stdout.join
wenzelm@39705
   265
    }
wenzelm@39705
   266
    finally {
wenzelm@39708
   267
      progress_stream.close
wenzelm@39705
   268
      stdin.close
wenzelm@39705
   269
      proc.destroy
wenzelm@39705
   270
    }
wenzelm@39705
   271
  }
wenzelm@47998
   272
wenzelm@47998
   273
wenzelm@47998
   274
  /* cygwin_root */
wenzelm@47998
   275
wenzelm@47998
   276
  def cygwin_root(): String =
wenzelm@47998
   277
  {
wenzelm@47998
   278
    val cygwin_root1 = System.getenv("CYGWIN_ROOT")
wenzelm@47998
   279
    val cygwin_root2 = System.getProperty("cygwin.root")
wenzelm@47998
   280
    val root =
wenzelm@47998
   281
      if (cygwin_root1 != null && cygwin_root1 != "") cygwin_root1
wenzelm@47998
   282
      else if (cygwin_root2 != null && cygwin_root2 != "") cygwin_root2
wenzelm@47998
   283
      else error("Bad Cygwin installation: unknown root")
wenzelm@47998
   284
wenzelm@47998
   285
    val root_file = new File(root)
wenzelm@47998
   286
    if (!new File(root_file, "bin\\bash.exe").isFile ||
wenzelm@47998
   287
        !new File(root_file, "bin\\env.exe").isFile ||
wenzelm@47998
   288
        !new File(root_file, "bin\\tar.exe").isFile)
wenzelm@47998
   289
      error("Bad Cygwin installation: " + quote(root))
wenzelm@47998
   290
wenzelm@47998
   291
    root
wenzelm@47998
   292
  }
wenzelm@34201
   293
}
wenzelm@34201
   294
wenzelm@34201
   295
wenzelm@34201
   296
class Standard_System
wenzelm@34201
   297
{
wenzelm@43664
   298
  /* platform_root */
wenzelm@43664
   299
wenzelm@47998
   300
  val platform_root = if (Platform.is_windows) Standard_System.cygwin_root() else "/"
wenzelm@34201
   301
wenzelm@34201
   302
wenzelm@34201
   303
  /* jvm_path */
wenzelm@34201
   304
wenzelm@34201
   305
  private val Cygdrive = new Regex("/cygdrive/([a-zA-Z])($|/.*)")
wenzelm@36136
   306
  private val Named_Root = new Regex("//+([^/]*)(.*)")
wenzelm@34201
   307
wenzelm@34201
   308
  def jvm_path(posix_path: String): String =
wenzelm@34201
   309
    if (Platform.is_windows) {
wenzelm@34201
   310
      val result_path = new StringBuilder
wenzelm@34201
   311
      val rest =
wenzelm@34201
   312
        posix_path match {
wenzelm@34201
   313
          case Cygdrive(drive, rest) =>
wenzelm@45072
   314
            result_path ++= (drive.toUpperCase(Locale.ENGLISH) + ":" + File.separator)
wenzelm@34201
   315
            rest
wenzelm@36136
   316
          case Named_Root(root, rest) =>
wenzelm@36136
   317
            result_path ++= File.separator
wenzelm@36136
   318
            result_path ++= File.separator
wenzelm@36136
   319
            result_path ++= root
wenzelm@36136
   320
            rest
wenzelm@34201
   321
          case path if path.startsWith("/") =>
wenzelm@34201
   322
            result_path ++= platform_root
wenzelm@34201
   323
            path
wenzelm@34201
   324
          case path => path
wenzelm@34201
   325
        }
wenzelm@43670
   326
      for (p <- space_explode('/', rest) if p != "") {
wenzelm@34201
   327
        val len = result_path.length
wenzelm@34201
   328
        if (len > 0 && result_path(len - 1) != File.separatorChar)
wenzelm@34201
   329
          result_path += File.separatorChar
wenzelm@34201
   330
        result_path ++= p
wenzelm@34201
   331
      }
wenzelm@34201
   332
      result_path.toString
wenzelm@34201
   333
    }
wenzelm@34201
   334
    else posix_path
wenzelm@34201
   335
wenzelm@34201
   336
wenzelm@34201
   337
  /* posix_path */
wenzelm@34201
   338
wenzelm@34201
   339
  private val Platform_Root = new Regex("(?i)" +
wenzelm@34201
   340
    Pattern.quote(platform_root) + """(?:\\+|\z)(.*)""")
wenzelm@34201
   341
wenzelm@34201
   342
  private val Drive = new Regex("""([a-zA-Z]):\\*(.*)""")
wenzelm@34201
   343
wenzelm@34201
   344
  def posix_path(jvm_path: String): String =
wenzelm@34201
   345
    if (Platform.is_windows) {
wenzelm@34201
   346
      jvm_path.replace('/', '\\') match {
wenzelm@34201
   347
        case Platform_Root(rest) => "/" + rest.replace('\\', '/')
wenzelm@34201
   348
        case Drive(letter, rest) =>
wenzelm@34201
   349
          "/cygdrive/" + letter.toLowerCase(Locale.ENGLISH) +
wenzelm@34201
   350
            (if (rest == "") "" else "/" + rest.replace('\\', '/'))
wenzelm@34201
   351
        case path => path.replace('\\', '/')
wenzelm@34201
   352
      }
wenzelm@34201
   353
    }
wenzelm@34201
   354
    else jvm_path
wenzelm@36193
   355
wenzelm@36193
   356
wenzelm@47113
   357
  /* JDK home of running JVM */
wenzelm@36193
   358
wenzelm@47113
   359
  def this_jdk_home(): String =
wenzelm@36193
   360
  {
wenzelm@36193
   361
    val java_home = System.getProperty("java.home")
wenzelm@47113
   362
    val home = new File(java_home)
wenzelm@47113
   363
    val parent = home.getParent
wenzelm@47113
   364
    val jdk_home =
wenzelm@47113
   365
      if (home.getName == "jre" && parent != null &&
wenzelm@47113
   366
          (new File(new File(parent, "bin"), "javac")).exists) parent
wenzelm@47113
   367
      else java_home
wenzelm@47113
   368
    posix_path(jdk_home)
wenzelm@36193
   369
  }
wenzelm@34201
   370
}