src/Pure/System/standard_system.scala
author wenzelm
Tue, 05 Jul 2011 10:54:05 +0200
changeset 43664 47af50b0c8c5
parent 43661 39fdbd814c7f
child 43670 7f933761764b
permissions -rw-r--r--
tuned;
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
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
     3
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
     4
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
     5
*/
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
package isabelle
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
     8
43520
cec9b95fa35d explicit import java.lang.System to prevent odd scope problems;
wenzelm
parents: 43516
diff changeset
     9
import java.lang.System
39732
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
    10
import java.util.zip.{ZipEntry, ZipInputStream}
34201
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    11
import java.util.regex.Pattern
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    12
import java.util.Locale
39705
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
    13
import java.net.URL
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
    14
import java.io.{BufferedWriter, OutputStreamWriter, FileOutputStream, BufferedOutputStream,
39578
b75164153c37 added Standard_System.slurp convenience;
wenzelm
parents: 39522
diff changeset
    15
  BufferedInputStream, InputStream, FileInputStream, BufferedReader, InputStreamReader,
34298
13e9f1f4acd9 added find_files;
wenzelm
parents: 34258
diff changeset
    16
  File, FileFilter, IOException}
43516
1c4736b9396a prefer actual charset over charset name;
wenzelm
parents: 39732
diff changeset
    17
import java.nio.charset.Charset
34201
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    18
36011
3ff725ac13a4 adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents: 34300
diff changeset
    19
import scala.io.{Source, Codec}
34201
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    20
import scala.util.matching.Regex
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    21
import scala.collection.mutable
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    22
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
object Standard_System
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    25
{
38264
205b74a1bb18 added string_bytes convenience;
wenzelm
parents: 36193
diff changeset
    26
  /* UTF-8 charset */
205b74a1bb18 added string_bytes convenience;
wenzelm
parents: 36193
diff changeset
    27
43516
1c4736b9396a prefer actual charset over charset name;
wenzelm
parents: 39732
diff changeset
    28
  val charset_name: String = "UTF-8"
1c4736b9396a prefer actual charset over charset name;
wenzelm
parents: 39732
diff changeset
    29
  val charset: Charset = Charset.forName(charset_name)
36015
6111de7c916a adapted to Scala 2.8.0 Beta 1;
wenzelm
parents: 36011
diff changeset
    30
  def codec(): Codec = Codec(charset)
34201
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    31
38264
205b74a1bb18 added string_bytes convenience;
wenzelm
parents: 36193
diff changeset
    32
  def string_bytes(s: String): Array[Byte] = s.getBytes(charset)
205b74a1bb18 added string_bytes convenience;
wenzelm
parents: 36193
diff changeset
    33
34201
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    34
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    35
  /* permissive UTF-8 decoding */
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    36
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    37
  // 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
    38
  // overlong encodings enable byte-stuffing
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    39
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    40
  def decode_permissive_utf8(text: CharSequence): String =
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    41
  {
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    42
    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
    43
    var code = -1
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    44
    var rest = 0
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    45
    def flush()
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    46
    {
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    47
      if (code != -1) {
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    48
        if (rest == 0 && Character.isValidCodePoint(code))
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    49
          buf.appendCodePoint(code)
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    50
        else buf.append('\uFFFD')
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    51
        code = -1
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    52
        rest = 0
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    53
      }
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
    def init(x: Int, n: Int)
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    56
    {
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    57
      flush()
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    58
      code = x
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    59
      rest = n
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    60
    }
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    61
    def push(x: Int)
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    62
    {
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    63
      if (rest <= 0) init(x, -1)
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    64
      else {
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    65
        code <<= 6
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    66
        code += x
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    67
        rest -= 1
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    68
      }
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
    for (i <- 0 until text.length) {
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    71
      val c = text.charAt(i)
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    72
      if (c < 128) { flush(); buf.append(c) }
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    73
      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
    74
      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
    75
      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
    76
      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
    77
    }
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    78
    flush()
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    79
    buf.toString
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    80
  }
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
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    83
  /* basic file operations */
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    84
39578
b75164153c37 added Standard_System.slurp convenience;
wenzelm
parents: 39522
diff changeset
    85
  def slurp(reader: BufferedReader): String =
34201
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    86
  {
39578
b75164153c37 added Standard_System.slurp convenience;
wenzelm
parents: 39522
diff changeset
    87
    val output = new StringBuilder(100)
b75164153c37 added Standard_System.slurp convenience;
wenzelm
parents: 39522
diff changeset
    88
    var c = -1
b75164153c37 added Standard_System.slurp convenience;
wenzelm
parents: 39522
diff changeset
    89
    while ({ c = reader.read; c != -1 }) output += c.toChar
b75164153c37 added Standard_System.slurp convenience;
wenzelm
parents: 39522
diff changeset
    90
    reader.close
b75164153c37 added Standard_System.slurp convenience;
wenzelm
parents: 39522
diff changeset
    91
    output.toString
34201
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    92
  }
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
    93
39578
b75164153c37 added Standard_System.slurp convenience;
wenzelm
parents: 39522
diff changeset
    94
  def slurp(stream: InputStream): String =
b75164153c37 added Standard_System.slurp convenience;
wenzelm
parents: 39522
diff changeset
    95
    slurp(new BufferedReader(new InputStreamReader(stream, charset)))
b75164153c37 added Standard_System.slurp convenience;
wenzelm
parents: 39522
diff changeset
    96
b75164153c37 added Standard_System.slurp convenience;
wenzelm
parents: 39522
diff changeset
    97
  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
    98
43661
39fdbd814c7f quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents: 43520
diff changeset
    99
  def try_read(files: Seq[File]): String =
39fdbd814c7f quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents: 43520
diff changeset
   100
  {
39fdbd814c7f quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents: 43520
diff changeset
   101
    val buf = new StringBuilder
39fdbd814c7f quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents: 43520
diff changeset
   102
    for {
39fdbd814c7f quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents: 43520
diff changeset
   103
      file <- files if file.isFile
39fdbd814c7f quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents: 43520
diff changeset
   104
      c <- (Source.fromFile(file) ++ Iterator.single('\n'))
39fdbd814c7f quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents: 43520
diff changeset
   105
    } buf.append(c)
39fdbd814c7f quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents: 43520
diff changeset
   106
    buf.toString
39fdbd814c7f quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents: 43520
diff changeset
   107
  }
39fdbd814c7f quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents: 43520
diff changeset
   108
34201
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   109
  def write_file(file: File, text: CharSequence)
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   110
  {
43516
1c4736b9396a prefer actual charset over charset name;
wenzelm
parents: 39732
diff changeset
   111
    val writer =
1c4736b9396a prefer actual charset over charset name;
wenzelm
parents: 39732
diff changeset
   112
      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
   113
    try { writer.append(text) }
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   114
    finally { writer.close }
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   115
  }
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   116
39578
b75164153c37 added Standard_System.slurp convenience;
wenzelm
parents: 39522
diff changeset
   117
  def with_tmp_file[A](prefix: String)(body: File => A): A =
b75164153c37 added Standard_System.slurp convenience;
wenzelm
parents: 39522
diff changeset
   118
  {
b75164153c37 added Standard_System.slurp convenience;
wenzelm
parents: 39522
diff changeset
   119
    val file = File.createTempFile(prefix, null)
39582
a873158542d0 Standard_System.with_tmp_file: deleteOnExit to make double sure;
wenzelm
parents: 39578
diff changeset
   120
    file.deleteOnExit
39578
b75164153c37 added Standard_System.slurp convenience;
wenzelm
parents: 39522
diff changeset
   121
    try { body(file) } finally { file.delete }
b75164153c37 added Standard_System.slurp convenience;
wenzelm
parents: 39522
diff changeset
   122
  }
b75164153c37 added Standard_System.slurp convenience;
wenzelm
parents: 39522
diff changeset
   123
34300
3f2e25dc99ab misc tuning;
wenzelm
parents: 34298
diff changeset
   124
  // FIXME handle (potentially cyclic) directory graph
34298
13e9f1f4acd9 added find_files;
wenzelm
parents: 34258
diff changeset
   125
  def find_files(start: File, ok: File => Boolean): List[File] =
13e9f1f4acd9 added find_files;
wenzelm
parents: 34258
diff changeset
   126
  {
13e9f1f4acd9 added find_files;
wenzelm
parents: 34258
diff changeset
   127
    val files = new mutable.ListBuffer[File]
13e9f1f4acd9 added find_files;
wenzelm
parents: 34258
diff changeset
   128
    val filter = new FileFilter { def accept(entry: File) = entry.isDirectory || ok(entry) }
13e9f1f4acd9 added find_files;
wenzelm
parents: 34258
diff changeset
   129
    def find_entry(entry: File)
13e9f1f4acd9 added find_files;
wenzelm
parents: 34258
diff changeset
   130
    {
13e9f1f4acd9 added find_files;
wenzelm
parents: 34258
diff changeset
   131
      if (ok(entry)) files += entry
13e9f1f4acd9 added find_files;
wenzelm
parents: 34258
diff changeset
   132
      if (entry.isDirectory) entry.listFiles(filter).foreach(find_entry)
13e9f1f4acd9 added find_files;
wenzelm
parents: 34258
diff changeset
   133
    }
13e9f1f4acd9 added find_files;
wenzelm
parents: 34258
diff changeset
   134
    find_entry(start)
13e9f1f4acd9 added find_files;
wenzelm
parents: 34258
diff changeset
   135
    files.toList
13e9f1f4acd9 added find_files;
wenzelm
parents: 34258
diff changeset
   136
  }
13e9f1f4acd9 added find_files;
wenzelm
parents: 34258
diff changeset
   137
34201
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   138
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   139
  /* shell processes */
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   140
34219
d37cfca69887 Standard_System.raw_execute: optional cwd;
wenzelm
parents: 34202
diff changeset
   141
  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
   142
  {
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   143
    val cmdline = new java.util.LinkedList[String]
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   144
    for (s <- args) cmdline.add(s)
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   145
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   146
    val proc = new ProcessBuilder(cmdline)
34219
d37cfca69887 Standard_System.raw_execute: optional cwd;
wenzelm
parents: 34202
diff changeset
   147
    if (cwd != null) proc.directory(cwd)
34202
99241daf807d ignore undefined environment;
wenzelm
parents: 34201
diff changeset
   148
    if (env != null) {
99241daf807d ignore undefined environment;
wenzelm
parents: 34201
diff changeset
   149
      proc.environment.clear
99241daf807d ignore undefined environment;
wenzelm
parents: 34201
diff changeset
   150
      for ((x, y) <- env) proc.environment.put(x, y)
99241daf807d ignore undefined environment;
wenzelm
parents: 34201
diff changeset
   151
    }
34201
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   152
    proc.redirectErrorStream(redirect)
39522
01aade784da9 raw_execute: let IOException pass-through unhindered (again);
wenzelm
parents: 38264
diff changeset
   153
    proc.start
34201
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   154
  }
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
  def process_output(proc: Process): (String, Int) =
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   157
  {
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   158
    proc.getOutputStream.close
39578
b75164153c37 added Standard_System.slurp convenience;
wenzelm
parents: 39522
diff changeset
   159
    val output = slurp(proc.getInputStream)
34201
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   160
    val rc =
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   161
      try { proc.waitFor }
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   162
      finally {
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   163
        proc.getInputStream.close
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   164
        proc.getErrorStream.close
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   165
        proc.destroy
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   166
        Thread.interrupted
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
    (output, rc)
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   169
  }
34258
e936d3c35ce0 Standard_System.raw_exec;
wenzelm
parents: 34219
diff changeset
   170
39522
01aade784da9 raw_execute: let IOException pass-through unhindered (again);
wenzelm
parents: 38264
diff changeset
   171
  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
   172
    : (String, Int) = process_output(raw_execute(cwd, env, redirect, args: _*))
39705
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   173
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   174
39732
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   175
  /* unpack zip archive -- platform file-system */
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   176
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   177
  def unzip(url: URL, root: File)
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   178
  {
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   179
    import scala.collection.JavaConversions._
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   180
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   181
    val buffer = new Array[Byte](4096)
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   182
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   183
    val zip_stream = new ZipInputStream(new BufferedInputStream(url.openStream))
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   184
    var entry: ZipEntry = null
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   185
    try {
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   186
      while ({ entry = zip_stream.getNextEntry; entry != null }) {
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   187
        val file = new File(root, entry.getName.replace('/', File.separatorChar))
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   188
        val dir = file.getParentFile
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   189
        if (dir != null && !dir.isDirectory && !dir.mkdirs)
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   190
          error("Failed to create directory: " + dir)
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   191
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   192
        var len = 0
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   193
        val out_stream = new BufferedOutputStream(new FileOutputStream(file))
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   194
        try {
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   195
          while ({ len = zip_stream.read(buffer); len != -1 })
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   196
            out_stream.write(buffer, 0, len)
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   197
        }
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   198
        finally { out_stream.close }
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   199
      }
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   200
    }
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   201
    finally { zip_stream.close }
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   202
  }
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   203
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   204
4dbc72759706 added Standard_System.unzip (for platform file-system);
wenzelm
parents: 39709
diff changeset
   205
  /* unpack tar archive -- POSIX file-system */
39705
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   206
39708
c3239be617f4 more efficient posix_untar -- avoid really slow java.util.zip.GZIPInputStream;
wenzelm
parents: 39706
diff changeset
   207
  def posix_untar(url: URL, root: File, gunzip: Boolean = false,
39709
1fa4c5c7d534 some more options to robustify posix_untar;
wenzelm
parents: 39708
diff changeset
   208
    tar: String = "tar", gzip: String = "", progress: Int => Unit = _ => ()): String =
39705
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   209
  {
39708
c3239be617f4 more efficient posix_untar -- avoid really slow java.util.zip.GZIPInputStream;
wenzelm
parents: 39706
diff changeset
   210
    if (!root.isDirectory && !root.mkdirs)
c3239be617f4 more efficient posix_untar -- avoid really slow java.util.zip.GZIPInputStream;
wenzelm
parents: 39706
diff changeset
   211
      error("Failed to create root directory: " + root)
39705
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   212
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   213
    val connection = url.openConnection
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   214
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   215
    val length = connection.getContentLength.toLong
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   216
    require(length >= 0L)
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   217
39708
c3239be617f4 more efficient posix_untar -- avoid really slow java.util.zip.GZIPInputStream;
wenzelm
parents: 39706
diff changeset
   218
    val stream = new BufferedInputStream(connection.getInputStream)
39705
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   219
    val progress_stream = new InputStream {
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   220
      private val total = length max 1L
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   221
      private var index = 0L
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   222
      private var percentage = 0L
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   223
      override def read(): Int =
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   224
      {
39708
c3239be617f4 more efficient posix_untar -- avoid really slow java.util.zip.GZIPInputStream;
wenzelm
parents: 39706
diff changeset
   225
        val c = stream.read
39705
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   226
        if (c != -1) {
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   227
          index += 100
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   228
          val p = index / total
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   229
          if (percentage != p) { percentage = p; progress(percentage.toInt) }
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   230
        }
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   231
        c
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   232
      }
39708
c3239be617f4 more efficient posix_untar -- avoid really slow java.util.zip.GZIPInputStream;
wenzelm
parents: 39706
diff changeset
   233
      override def available(): Int = stream.available
c3239be617f4 more efficient posix_untar -- avoid really slow java.util.zip.GZIPInputStream;
wenzelm
parents: 39706
diff changeset
   234
      override def close() { stream.close }
39705
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   235
    }
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   236
39709
1fa4c5c7d534 some more options to robustify posix_untar;
wenzelm
parents: 39708
diff changeset
   237
    val cmdline =
1fa4c5c7d534 some more options to robustify posix_untar;
wenzelm
parents: 39708
diff changeset
   238
      List(tar, "-o", "-x", "-f-") :::
1fa4c5c7d534 some more options to robustify posix_untar;
wenzelm
parents: 39708
diff changeset
   239
        (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
   240
1fa4c5c7d534 some more options to robustify posix_untar;
wenzelm
parents: 39708
diff changeset
   241
    val proc = raw_execute(root, null, false, cmdline:_*)
39705
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   242
    val stdout = Simple_Thread.future("tar_stdout") { slurp(proc.getInputStream) }
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   243
    val stderr = Simple_Thread.future("tar_stderr") { slurp(proc.getErrorStream) }
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   244
    val stdin = new BufferedOutputStream(proc.getOutputStream)
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   245
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   246
    try {
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   247
      var c = -1
39706
6e74fb2d4374 raw_untar.raw_execute with native cwd, to avoid cross-platform complications;
wenzelm
parents: 39705
diff changeset
   248
      val io_err =
39708
c3239be617f4 more efficient posix_untar -- avoid really slow java.util.zip.GZIPInputStream;
wenzelm
parents: 39706
diff changeset
   249
        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
   250
        catch { case e: IOException => true }
39705
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   251
      stdin.close
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   252
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   253
      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
   254
      if (io_err || rc != 0) error(stderr.join.trim) else stdout.join
39705
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   255
    }
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   256
    finally {
39708
c3239be617f4 more efficient posix_untar -- avoid really slow java.util.zip.GZIPInputStream;
wenzelm
parents: 39706
diff changeset
   257
      progress_stream.close
39705
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   258
      stdin.close
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   259
      proc.destroy
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   260
    }
41e9f69c553d added Standard_System.raw_untar;
wenzelm
parents: 39582
diff changeset
   261
  }
34201
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   262
}
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   263
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   264
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   265
class Standard_System
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   266
{
43664
wenzelm
parents: 43661
diff changeset
   267
  /* platform_root */
wenzelm
parents: 43661
diff changeset
   268
34201
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   269
  val platform_root = if (Platform.is_windows) Cygwin.check_root() else "/"
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   270
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   271
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   272
  /* jvm_path */
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   273
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   274
  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
   275
  private val Named_Root = new Regex("//+([^/]*)(.*)")
34201
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   276
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   277
  def jvm_path(posix_path: String): String =
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   278
    if (Platform.is_windows) {
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   279
      val result_path = new StringBuilder
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   280
      val rest =
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   281
        posix_path match {
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   282
          case Cygdrive(drive, rest) =>
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   283
            result_path ++= (drive + ":" + File.separator)
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   284
            rest
36136
89b1a136edef more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents: 36015
diff changeset
   285
          case Named_Root(root, rest) =>
89b1a136edef more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents: 36015
diff changeset
   286
            result_path ++= File.separator
89b1a136edef more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents: 36015
diff changeset
   287
            result_path ++= File.separator
89b1a136edef more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents: 36015
diff changeset
   288
            result_path ++= root
89b1a136edef more precise treatment of UNC server prefix, e.g. //foo;
wenzelm
parents: 36015
diff changeset
   289
            rest
34201
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   290
          case path if path.startsWith("/") =>
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   291
            result_path ++= platform_root
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   292
            path
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   293
          case path => path
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
      for (p <- rest.split("/") if p != "") {
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   296
        val len = result_path.length
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   297
        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
   298
          result_path += File.separatorChar
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   299
        result_path ++= p
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   300
      }
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   301
      result_path.toString
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
    else posix_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
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   306
  /* posix_path */
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
  private val Platform_Root = new Regex("(?i)" +
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   309
    Pattern.quote(platform_root) + """(?:\\+|\z)(.*)""")
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   310
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   311
  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
   312
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   313
  def posix_path(jvm_path: String): String =
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   314
    if (Platform.is_windows) {
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   315
      jvm_path.replace('/', '\\') match {
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   316
        case Platform_Root(rest) => "/" + rest.replace('\\', '/')
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   317
        case Drive(letter, rest) =>
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   318
          "/cygdrive/" + letter.toLowerCase(Locale.ENGLISH) +
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   319
            (if (rest == "") "" else "/" + rest.replace('\\', '/'))
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   320
        case path => path.replace('\\', '/')
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   321
      }
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   322
    }
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   323
    else jvm_path
36193
067a01827fca improved ISABELLE_JAVA, based on THIS_JAVA of the actually running JVM;
wenzelm
parents: 36136
diff changeset
   324
067a01827fca improved ISABELLE_JAVA, based on THIS_JAVA of the actually running JVM;
wenzelm
parents: 36136
diff changeset
   325
067a01827fca improved ISABELLE_JAVA, based on THIS_JAVA of the actually running JVM;
wenzelm
parents: 36136
diff changeset
   326
  /* this_java executable */
067a01827fca improved ISABELLE_JAVA, based on THIS_JAVA of the actually running JVM;
wenzelm
parents: 36136
diff changeset
   327
067a01827fca improved ISABELLE_JAVA, based on THIS_JAVA of the actually running JVM;
wenzelm
parents: 36136
diff changeset
   328
  def this_java(): String =
067a01827fca improved ISABELLE_JAVA, based on THIS_JAVA of the actually running JVM;
wenzelm
parents: 36136
diff changeset
   329
  {
067a01827fca improved ISABELLE_JAVA, based on THIS_JAVA of the actually running JVM;
wenzelm
parents: 36136
diff changeset
   330
    val java_home = System.getProperty("java.home")
067a01827fca improved ISABELLE_JAVA, based on THIS_JAVA of the actually running JVM;
wenzelm
parents: 36136
diff changeset
   331
    val java_exe =
067a01827fca improved ISABELLE_JAVA, based on THIS_JAVA of the actually running JVM;
wenzelm
parents: 36136
diff changeset
   332
      if (Platform.is_windows) new File(java_home + "\\bin\\java.exe")
067a01827fca improved ISABELLE_JAVA, based on THIS_JAVA of the actually running JVM;
wenzelm
parents: 36136
diff changeset
   333
      else new File(java_home + "/bin/java")
067a01827fca improved ISABELLE_JAVA, based on THIS_JAVA of the actually running JVM;
wenzelm
parents: 36136
diff changeset
   334
    if (!java_exe.isFile) error("Expected this Java executable: " + java_exe.toString)
067a01827fca improved ISABELLE_JAVA, based on THIS_JAVA of the actually running JVM;
wenzelm
parents: 36136
diff changeset
   335
    posix_path(java_exe.getAbsolutePath)
067a01827fca improved ISABELLE_JAVA, based on THIS_JAVA of the actually running JVM;
wenzelm
parents: 36136
diff changeset
   336
  }
34201
c95dcd12f48a separate Standard_System (Cygwin/Posix compatibility) vs. Isabelle_System (settings environment etc.);
wenzelm
parents:
diff changeset
   337
}