src/Pure/System/cygwin.scala
author haftmann
Tue Oct 20 16:13:01 2009 +0200 (2009-10-20)
changeset 33037 b22e44496dc2
parent 31826 7f311da87d5a
child 34043 7129fab1fe4f
permissions -rw-r--r--
replaced old_style infixes eq_set, subset, union, inter and variants by generic versions
wenzelm@31499
     1
/*  Title:      Pure/System/cygwin.scala
wenzelm@31499
     2
    Author:     Makarius
wenzelm@31499
     3
wenzelm@31499
     4
Accessing the Cygwin installation.
wenzelm@31499
     5
*/
wenzelm@31499
     6
wenzelm@31499
     7
package isabelle
wenzelm@31499
     8
wenzelm@31499
     9
import java.lang.reflect.Method
wenzelm@31826
    10
import java.io.File
wenzelm@31499
    11
wenzelm@31499
    12
wenzelm@31499
    13
object Cygwin
wenzelm@31499
    14
{
wenzelm@31499
    15
  /* registry access */
wenzelm@31499
    16
wenzelm@31499
    17
  // Some black magic involving private WindowsPreferences from Sun, cf.
wenzelm@31499
    18
  // http://www.docjar.com/html/api/java/util/prefs/WindowsPreferences.java.html
wenzelm@31499
    19
wenzelm@31499
    20
  private val WindowsPreferences = Class.forName("java.util.prefs.WindowsPreferences")
wenzelm@31499
    21
wenzelm@31499
    22
  private val HKEY_CURRENT_USER = 0x80000001
wenzelm@31499
    23
  private val HKEY_LOCAL_MACHINE = 0x80000002
wenzelm@31499
    24
  private val KEY_READ = 0x20019
wenzelm@31499
    25
  private val NATIVE_HANDLE = 0
wenzelm@31499
    26
  private val ERROR_CODE = 1
wenzelm@31499
    27
wenzelm@31499
    28
  private def C_string(s: String): Array[Byte] =
wenzelm@31499
    29
    (s + "\0").getBytes("US-ASCII")
wenzelm@31499
    30
wenzelm@31499
    31
  private def J_string(bs: Array[Byte]): String =
wenzelm@31499
    32
    new String(bs, 0, bs.length - 1, "US-ASCII")
wenzelm@31499
    33
wenzelm@31499
    34
  private val INT = Integer.TYPE
wenzelm@31499
    35
  private val BYTES = (new Array[Byte](0)).getClass
wenzelm@31499
    36
wenzelm@31499
    37
  private def open_key(handle: Int, subkey: Array[Byte], mask: Int): Array[Int] =
wenzelm@31499
    38
  {
wenzelm@31499
    39
    val m = WindowsPreferences.getDeclaredMethod("WindowsRegOpenKey", INT, BYTES, INT)
wenzelm@31499
    40
    m.setAccessible(true)
wenzelm@31499
    41
    m.invoke(null, handle.asInstanceOf[Object], subkey.asInstanceOf[Object],
wenzelm@31499
    42
      mask.asInstanceOf[Object]).asInstanceOf[Array[Int]]
wenzelm@31499
    43
  }
wenzelm@31499
    44
wenzelm@31499
    45
  private def close_key(handle: Int): Int =
wenzelm@31499
    46
  {
wenzelm@31499
    47
    val m = WindowsPreferences.getDeclaredMethod("WindowsRegCloseKey", INT)
wenzelm@31499
    48
    m.setAccessible(true)
wenzelm@31499
    49
    m.invoke(null, handle.asInstanceOf[Object]).asInstanceOf[Int]
wenzelm@31499
    50
  }
wenzelm@31499
    51
wenzelm@31499
    52
  private def query(handle: Int, name: Array[Byte]) =
wenzelm@31499
    53
  {
wenzelm@31499
    54
    val m = WindowsPreferences.getDeclaredMethod("WindowsRegQueryValueEx", INT, BYTES)
wenzelm@31499
    55
    m.setAccessible(true)
wenzelm@31499
    56
    m.invoke(null, handle.asInstanceOf[Object], name.asInstanceOf[Object]).
wenzelm@31499
    57
      asInstanceOf[Array[Byte]]
wenzelm@31499
    58
  }
wenzelm@31499
    59
wenzelm@31499
    60
  def query_registry(sys: Boolean, path: String, name: String): Option[String] =
wenzelm@31499
    61
  {
wenzelm@31499
    62
    val handle = if (sys) HKEY_LOCAL_MACHINE else HKEY_CURRENT_USER
wenzelm@31499
    63
    val result = open_key(handle, C_string(path), KEY_READ)
wenzelm@31499
    64
    if (result(ERROR_CODE) != 0) None
wenzelm@31499
    65
    else {
wenzelm@31499
    66
      val res = query(result(NATIVE_HANDLE), C_string(name))
wenzelm@31499
    67
      if (res == null) None
wenzelm@31499
    68
      else Some(J_string(res))
wenzelm@31499
    69
    }
wenzelm@31499
    70
  }
wenzelm@31499
    71
wenzelm@31499
    72
  def query_registry(path: String, name: String): Option[String] =
wenzelm@31499
    73
    query_registry(false, path, name) orElse
wenzelm@31499
    74
      query_registry(true, path, name)
wenzelm@31499
    75
wenzelm@31499
    76
wenzelm@31499
    77
  /* Cygwin installation */
wenzelm@31499
    78
wenzelm@31826
    79
  // old-style mount points (Cygwin 1.5)
wenzelm@31499
    80
  private val CYGWIN_MOUNTS = "Software\\Cygnus Solutions\\Cygwin\\mounts v2"
wenzelm@31499
    81
wenzelm@31826
    82
  // new-style setup (Cygwin 1.7)
wenzelm@31826
    83
  private val CYGWIN_SETUP1 = "Software\\Cygwin\\setup"
wenzelm@31826
    84
  private val CYGWIN_SETUP2 = "Software\\Wow6432Node\\Cygwin\\setup"  // !?
wenzelm@31499
    85
wenzelm@31826
    86
  def config(): (String, String) =
wenzelm@31826
    87
  {
wenzelm@31826
    88
    query_registry(CYGWIN_SETUP1, "rootdir") match {
wenzelm@31826
    89
      case Some(root) => (root, "/cygdrive")
wenzelm@31826
    90
      case None =>
wenzelm@31826
    91
        val root =
wenzelm@31826
    92
          query_registry(CYGWIN_MOUNTS + "\\/", "native") getOrElse "C:\\cygwin"
wenzelm@31826
    93
        val cygdrive =
wenzelm@31826
    94
          query_registry(CYGWIN_MOUNTS, "cygdrive prefix") getOrElse "cygdrive"
wenzelm@31826
    95
        (root, cygdrive)
wenzelm@31826
    96
    }
wenzelm@31826
    97
  }
wenzelm@31826
    98
wenzelm@31826
    99
wenzelm@31826
   100
  /* basic sanity check */
wenzelm@31826
   101
wenzelm@31826
   102
  def check(root: String): Boolean =
wenzelm@31826
   103
    new File(root + "\\bin\\bash.exe").isFile &&
wenzelm@31826
   104
    new File(root + "\\bin\\env.exe").isFile
wenzelm@31499
   105
}
wenzelm@31499
   106