aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/scalam/m/interpretation/Interpreter.scala
blob: 93411c31dc172deff0c01d8fb179172aa36e9491 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package scalam.m.interpretation

import scalax.file.Path
import scala.sys.process._
import scala.io._
import java.io._
import scala.concurrent._

class Interpreter(command: String, pwd: Path) {
  private val inputStream = new SyncVar[OutputStream];

  val process = Process(command, pwd.fileOption, "" -> "").run(
    new ProcessIO(
      stdin => inputStream.put(stdin),
      stdout => Source.fromInputStream(stdout).getLines.foreach(println),
      stderr => Source.fromInputStream(stderr).getLines.foreach(println)));

  def write(s: String): Unit = synchronized {
    inputStream.get.write((s).getBytes)
    inputStream.get.flush()
  }

  def close(): Unit = {
    inputStream.get.close
  }
  
  def kill(): Unit = {
    close()
    process.destroy()
  }
}