<commit_before>/*  Title:      Pure/Tools/isabelle_system.scala
    ID:         $Id$
    Author:     Makarius

Isabelle system support -- basic Cygwin/Posix compatibility.
*/

package isabelle

import java.util.regex.{Pattern, Matcher}
import java.io.File


object IsabelleSystem {

  /* Isabelle environment settings */

  def getenv(name: String) = {
    val value = System.getenv(name)
    if (value != null) value else ""
  }

  def getenv_strict(name: String) = {
    val value = getenv(name)
    if (value != "") value else error("Undefined environment variable: " + name)
  }

  def is_cygwin() = Pattern.matches(".*-cygwin", getenv_strict("ML_PLATFORM"))


  /* file path specifications */

  private val cygdrive_pattern = Pattern.compile("/cygdrive/([a-zA-Z])($|/.*)")

  def platform_path(source_path: String) = {
    val result_path = new StringBuilder

    def init(path: String) = {
      val cygdrive = cygdrive_pattern.matcher(path)
      if (cygdrive.matches) {
        result_path.length = 0
        result_path.append(cygdrive.group(1))
        result_path.append(":")
        result_path.append(File.separator)
        cygdrive.group(2)
      }
      else if (path.startsWith("/")) {
        result_path.length = 0
        result_path.append(getenv_strict("ISABELLE_ROOT_JVM"))
        path.substring(1)
      }
      else path
    }
    def append(path: String) = {
      for (p <- init(path).split("/")) {
        if (p != "") {
          val len = result_path.length
          if (len > 0 && result_path(len - 1) != File.separatorChar)
            result_path.append(File.separator)
          result_path.append(p)
        }
      }
    }
    for (p <- init(source_path).split("/")) {
      if (p.startsWith("$")) append(getenv_strict(p.substring(1)))
      else if (p == "~") append(getenv_strict("HOME"))
      else if (p == "~~") append(getenv_strict("ISABELLE_HOME"))
      else append(p)
    }
    result_path.toString
  }


  /* processes */

  private def posix_prefix() = if (is_cygwin()) List(platform_path("/bin/env")) else Nil

  def exec(args: List[String]) = Runtime.getRuntime.exec((posix_prefix() ++ args).toArray)

  def exec2(args: List[String]) = {
    val cmdline = new java.util.LinkedList[String]
    for (s <- posix_prefix() ++ args) cmdline.add(s)
    new ProcessBuilder(cmdline).redirectErrorStream(true).start
  }

}
<commit_msg>added charset (from isabelle_process.scala); added fifo_reader -- also works on Cygwin;<commit_after>/*  Title:      Pure/Tools/isabelle_system.scala
    ID:         $Id$
    Author:     Makarius

Isabelle system support -- basic Cygwin/Posix compatibility.
*/

package isabelle

import java.util.regex.{Pattern, Matcher}
import java.io.{BufferedReader, InputStreamReader, FileInputStream, File}


object IsabelleSystem {

  val charset = "UTF-8"


  /* Isabelle environment settings */

  def getenv(name: String) = {
    val value = System.getenv(if (name == "HOME") "HOME_JVM" else name)
    if (value != null) value else ""
  }

  def getenv_strict(name: String) = {
    val value = getenv(name)
    if (value != "") value else error("Undefined environment variable: " + name)
  }

  def is_cygwin() = Pattern.matches(".*-cygwin", getenv_strict("ML_PLATFORM"))


  /* file path specifications */

  private val cygdrive_pattern = Pattern.compile("/cygdrive/([a-zA-Z])($|/.*)")

  def platform_path(source_path: String) = {
    val result_path = new StringBuilder

    def init(path: String) = {
      val cygdrive = cygdrive_pattern.matcher(path)
      if (cygdrive.matches) {
        result_path.length = 0
        result_path.append(cygdrive.group(1))
        result_path.append(":")
        result_path.append(File.separator)
        cygdrive.group(2)
      }
      else if (path.startsWith("/")) {
        result_path.length = 0
        result_path.append(getenv_strict("ISABELLE_ROOT_JVM"))
        path.substring(1)
      }
      else path
    }
    def append(path: String) = {
      for (p <- init(path).split("/")) {
        if (p != "") {
          val len = result_path.length
          if (len > 0 && result_path(len - 1) != File.separatorChar)
            result_path.append(File.separator)
          result_path.append(p)
        }
      }
    }
    for (p <- init(source_path).split("/")) {
      if (p.startsWith("$")) append(getenv_strict(p.substring(1)))
      else if (p == "~") append(getenv_strict("HOME"))
      else if (p == "~~") append(getenv_strict("ISABELLE_HOME"))
      else append(p)
    }
    result_path.toString
  }


  /* named pipes */

  def fifo_reader(fifo: String) =
    if (is_cygwin()) new BufferedReader(new InputStreamReader(Runtime.getRuntime.exec(
      Array(platform_path("/bin/cat"), fifo)).getInputStream, charset))
    else new BufferedReader(new InputStreamReader(new FileInputStream(fifo), charset))


  /* processes */

  private def posix_prefix() = if (is_cygwin()) List(platform_path("/bin/env")) else Nil

  def exec(args: List[String]) = Runtime.getRuntime.exec((posix_prefix() ++ args).toArray)

  def exec2(args: List[String]) = {
    val cmdline = new java.util.LinkedList[String]
    for (s <- posix_prefix() ++ args) cmdline.add(s)
    new ProcessBuilder(cmdline).redirectErrorStream(true).start
  }

}

{'fraction_non_alphanumeric': 0.14484467314655994, 'fraction_numerical': 0.0036211168286639983, 'mean_word_length': 2.92814371257485, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4609244', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1711, 'n_words': 349}