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