src/Pure/System/isabelle_system.scala
changeset 31825 d47a9dc1f064
parent 31824 28f5ed40ecab
child 31826 7f311da87d5a
equal deleted inserted replaced
31824:28f5ed40ecab 31825:d47a9dc1f064
    15 
    15 
    16 
    16 
    17 object Isabelle_System
    17 object Isabelle_System
    18 {
    18 {
    19   val charset = "UTF-8"
    19   val charset = "UTF-8"
    20 
       
    21 
       
    22   /* platform identification */
       
    23 
       
    24   val is_cygwin = System.getProperty("os.name").startsWith("Windows")
       
    25 
       
    26   private val X86 = new Regex("i.86|x86")
       
    27   private val X86_64 = new Regex("amd64|x86_64")
       
    28   private val Sparc = new Regex("sparc")
       
    29   private val PPC = new Regex("PowerPC|ppc")
       
    30 
       
    31   private val Solaris = new Regex("SunOS|Solaris")
       
    32   private val Linux = new Regex("Linux")
       
    33   private val Darwin = new Regex("Mac OS X")
       
    34   private val Cygwin = new Regex("Windows.*")
       
    35 
       
    36   val default_platform: Option[String] =
       
    37   {
       
    38     val name =
       
    39       java.lang.System.getProperty("os.name") match {
       
    40         case Solaris() => "solaris"
       
    41         case Linux() => "linux"
       
    42         case Darwin() => "darwin"
       
    43         case Cygwin() => "cygwin"
       
    44         case _ => ""
       
    45       }
       
    46     val arch =
       
    47       java.lang.System.getProperty("os.arch") match {
       
    48         case X86() | X86_64() => "x86"
       
    49         case Sparc() => "sparc"
       
    50         case PPC() => "ppc"
       
    51         case _ => ""
       
    52       }
       
    53     if (arch == "" || name == "") None
       
    54     else Some(arch + "-" + name)
       
    55   }
       
    56 
    20 
    57 
    21 
    58   /* shell processes */
    22   /* shell processes */
    59 
    23 
    60   private def raw_execute(env: Map[String, String], redirect: Boolean, args: String*): Process =
    24   private def raw_execute(env: Map[String, String], redirect: Boolean, args: String*): Process =
    96 
    60 
    97   /* platform prefixes */
    61   /* platform prefixes */
    98 
    62 
    99   private val (platform_root, drive_prefix, shell_prefix) =
    63   private val (platform_root, drive_prefix, shell_prefix) =
   100   {
    64   {
   101     if (Isabelle_System.is_cygwin) {
    65     if (Platform.is_windows) {
   102       val root = Cygwin.root() getOrElse "C:\\cygwin"
    66       val root = Cygwin.root() getOrElse "C:\\cygwin"
   103       val drive = Cygwin.cygdrive() getOrElse "/cygdrive"
    67       val drive = Cygwin.cygdrive() getOrElse "/cygdrive"
   104       val shell = List(root + "\\bin\\bash", "-l")
    68       val shell = List(root + "\\bin\\bash", "-l")
   105       (root, drive, shell)
    69       (root, drive, shell)
   106     }
    70     }
   218   def platform_path(isabelle_path: String): String =
   182   def platform_path(isabelle_path: String): String =
   219   {
   183   {
   220     val result_path = new StringBuilder
   184     val result_path = new StringBuilder
   221     val rest =
   185     val rest =
   222       expand_path(isabelle_path) match {
   186       expand_path(isabelle_path) match {
   223         case Cygdrive(drive, rest) if Isabelle_System.is_cygwin =>
   187         case Cygdrive(drive, rest) if Platform.is_windows =>
   224           result_path ++= (drive + ":" + File.separator)
   188           result_path ++= (drive + ":" + File.separator)
   225           rest
   189           rest
   226         case path if path.startsWith("/") =>
   190         case path if path.startsWith("/") =>
   227           result_path ++= platform_root
   191           result_path ++= platform_root
   228           path
   192           path
   246     Pattern.quote(platform_root) + """(?:\\+|\z)(.*)""")
   210     Pattern.quote(platform_root) + """(?:\\+|\z)(.*)""")
   247   private val Drive = new Regex("""([a-zA-Z]):\\*(.*)""")
   211   private val Drive = new Regex("""([a-zA-Z]):\\*(.*)""")
   248 
   212 
   249   def isabelle_path(platform_path: String): String =
   213   def isabelle_path(platform_path: String): String =
   250   {
   214   {
   251     if (Isabelle_System.is_cygwin) {
   215     if (Platform.is_windows) {
   252       platform_path.replace('/', '\\') match {
   216       platform_path.replace('/', '\\') match {
   253         case Platform_Root(rest) => "/" + rest.replace('\\', '/')
   217         case Platform_Root(rest) => "/" + rest.replace('\\', '/')
   254         case Drive(letter, rest) =>
   218         case Drive(letter, rest) =>
   255           drive_prefix + "/" + letter.toLowerCase(Locale.ENGLISH) +
   219           drive_prefix + "/" + letter.toLowerCase(Locale.ENGLISH) +
   256             (if (rest == "") "" else "/" + rest.replace('\\', '/'))
   220             (if (rest == "") "" else "/" + rest.replace('\\', '/'))
   284   /* external processes */
   248   /* external processes */
   285 
   249 
   286   def execute(redirect: Boolean, args: String*): Process =
   250   def execute(redirect: Boolean, args: String*): Process =
   287   {
   251   {
   288     val cmdline =
   252     val cmdline =
   289       if (Isabelle_System.is_cygwin) List(platform_path("/bin/env")) ++ args
   253       if (Platform.is_windows) List(platform_path("/bin/env")) ++ args
   290       else args
   254       else args
   291     Isabelle_System.raw_execute(environment, redirect, cmdline: _*)
   255     Isabelle_System.raw_execute(environment, redirect, cmdline: _*)
   292   }
   256   }
   293 
   257 
   294   def isabelle_tool(name: String, args: String*): (String, Int) =
   258   def isabelle_tool(name: String, args: String*): (String, Int) =
   323 
   287 
   324   def fifo_reader(fifo: String): BufferedReader =
   288   def fifo_reader(fifo: String): BufferedReader =
   325   {
   289   {
   326     // blocks until writer is ready
   290     // blocks until writer is ready
   327     val stream =
   291     val stream =
   328       if (Isabelle_System.is_cygwin) execute(false, "cat", fifo).getInputStream
   292       if (Platform.is_windows) execute(false, "cat", fifo).getInputStream
   329       else new FileInputStream(fifo)
   293       else new FileInputStream(fifo)
   330     new BufferedReader(new InputStreamReader(stream, Isabelle_System.charset))
   294     new BufferedReader(new InputStreamReader(stream, Isabelle_System.charset))
   331   }
   295   }
   332 
   296 
   333 
   297