src/Pure/Tools/isabelle_system.scala
author wenzelm
Tue, 20 Jan 2009 18:05:21 +0100
changeset 29570 10fca82e688a
parent 29180 62513d4d34c2
permissions -rw-r--r--
IsabelleSystem: provide Symbol.Interpretation;
     1 /*  Title:      Pure/Tools/isabelle_system.scala
     2     Author:     Makarius
     3 
     4 Isabelle system support -- basic Cygwin/Posix compatibility.
     5 */
     6 
     7 package isabelle
     8 
     9 import java.util.regex.{Pattern, Matcher}
    10 import java.io.{BufferedReader, InputStreamReader, FileInputStream, File, IOException}
    11 
    12 import scala.io.Source
    13 
    14 
    15 class IsabelleSystem {
    16 
    17   val charset = "UTF-8"
    18 
    19 
    20   /* Isabelle environment settings */
    21 
    22   private val environment = System.getenv
    23 
    24   def getenv(name: String) = {
    25     val value = environment.get(if (name == "HOME") "HOME_JVM" else name)
    26     if (value != null) value else ""
    27   }
    28 
    29   def getenv_strict(name: String) = {
    30     val value = environment.get(name)
    31     if (value != "") value else error("Undefined environment variable: " + name)
    32   }
    33 
    34   val is_cygwin = Pattern.matches(".*-cygwin", getenv_strict("ML_PLATFORM"))
    35 
    36 
    37   /* file path specifications */
    38 
    39   private val cygdrive_pattern = Pattern.compile("/cygdrive/([a-zA-Z])($|/.*)")
    40 
    41   def platform_path(source_path: String) = {
    42     val result_path = new StringBuilder
    43 
    44     def init(path: String) = {
    45       val cygdrive = cygdrive_pattern.matcher(path)
    46       if (cygdrive.matches) {
    47         result_path.length = 0
    48         result_path.append(cygdrive.group(1))
    49         result_path.append(":")
    50         result_path.append(File.separator)
    51         cygdrive.group(2)
    52       }
    53       else if (path.startsWith("/")) {
    54         result_path.length = 0
    55         result_path.append(getenv_strict("ISABELLE_ROOT_JVM"))
    56         path.substring(1)
    57       }
    58       else path
    59     }
    60     def append(path: String) = {
    61       for (p <- init(path).split("/")) {
    62         if (p != "") {
    63           val len = result_path.length
    64           if (len > 0 && result_path(len - 1) != File.separatorChar)
    65             result_path.append(File.separator)
    66           result_path.append(p)
    67         }
    68       }
    69     }
    70     for (p <- init(source_path).split("/")) {
    71       if (p.startsWith("$")) append(getenv_strict(p.substring(1)))
    72       else if (p == "~") append(getenv_strict("HOME"))
    73       else if (p == "~~") append(getenv_strict("ISABELLE_HOME"))
    74       else append(p)
    75     }
    76     result_path.toString
    77   }
    78 
    79   def platform_file(path: String) =
    80     new File(platform_path(path))
    81 
    82 
    83   /* processes */
    84 
    85   def execute(redirect: Boolean, args: String*): Process = {
    86     val cmdline = new java.util.LinkedList[String]
    87     if (is_cygwin) cmdline.add(platform_path("/bin/env"))
    88     for (s <- args) cmdline.add(s)
    89 
    90     val proc = new ProcessBuilder(cmdline)
    91     proc.environment.clear
    92     proc.environment.putAll(environment)
    93     proc.redirectErrorStream(redirect)
    94     proc.start
    95   }
    96 
    97 
    98   /* Isabelle tools (non-interactive) */
    99 
   100   def isabelle_tool(args: String*) = {
   101     val proc =
   102       try { execute(true, (List(getenv_strict("ISABELLE_TOOL")) ++ args): _*) }
   103       catch { case e: IOException => error(e.getMessage) }
   104     proc.getOutputStream.close
   105     val output = Source.fromInputStream(proc.getInputStream, charset).mkString
   106     val rc = proc.waitFor
   107     (output, rc)
   108   }
   109 
   110 
   111   /* named pipes */
   112 
   113   def mk_fifo() = {
   114     val (result, rc) = isabelle_tool("mkfifo")
   115     if (rc == 0) result.trim
   116     else error(result)
   117   }
   118 
   119   def rm_fifo(fifo: String) = {
   120     val (result, rc) = isabelle_tool("rmfifo", fifo)
   121     if (rc != 0) error(result)
   122   }
   123 
   124   def fifo_reader(fifo: String) = {
   125     // blocks until writer is ready
   126     val stream =
   127       if (is_cygwin) execute(false, "cat", fifo).getInputStream
   128       else new FileInputStream(fifo)
   129     new BufferedReader(new InputStreamReader(stream, charset))
   130   }
   131 
   132 
   133   /* find logics */
   134 
   135   def find_logics() = {
   136     val ml_ident = getenv_strict("ML_IDENTIFIER")
   137     var logics: Set[String] = Set()
   138     for (dir <- getenv_strict("ISABELLE_PATH").split(":")) {
   139       val files = platform_file(dir + "/" + ml_ident).listFiles()
   140       if (files != null) {
   141         for (file <- files if file.isFile) logics += file.getName
   142       }
   143     }
   144     logics.toList.sort(_ < _)
   145   }
   146 
   147 
   148   /* symbols */
   149 
   150   private def read_symbols(path: String) = {
   151     val file = new File(platform_path(path))
   152     if (file.canRead) Source.fromFile(file).getLines
   153     else Iterator.empty
   154   }
   155   val symbols = new Symbol.Interpretation(
   156     read_symbols("$ISABELLE_HOME/etc/symbols") ++
   157     read_symbols("$ISABELLE_HOME_USER/etc/symbols"))
   158 }