src/Pure/Tools/isabelle_process.scala
author wenzelm
Thu Aug 28 19:29:58 2008 +0200 (2008-08-28 ago)
changeset 28045 1a6f273108ae
parent 27999 c26e0373c24f
child 28056 18dbe34f6c5d
permissions -rw-r--r--
join stdout/stderr, eliminated Kind.STDERR;
messages are read from separate fifo (ensures that spurious stdout from shell wrappers, Poly/ML runtime system etc. do not spoil message protocol);
private output, output_raw;
wenzelm@27963
     1
/*  Title:      Pure/Tools/isabelle_process.ML
wenzelm@27949
     2
    ID:         $Id$
wenzelm@27949
     3
    Author:     Makarius
wenzelm@27963
     4
    Options:    :folding=explicit:collapseFolds=1:
wenzelm@27949
     5
wenzelm@27949
     6
Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm@27949
     7
*/
wenzelm@27949
     8
wenzelm@27949
     9
package isabelle
wenzelm@27949
    10
wenzelm@27949
    11
import java.util.Properties
wenzelm@27955
    12
import java.util.concurrent.LinkedBlockingQueue
wenzelm@28045
    13
import java.io.{BufferedReader, BufferedWriter, InputStreamReader, OutputStreamWriter,
wenzelm@28045
    14
  InputStream, OutputStream, FileInputStream, IOException}
wenzelm@27949
    15
wenzelm@27963
    16
import isabelle.{Symbol, XML}
wenzelm@27949
    17
wenzelm@27949
    18
wenzelm@27973
    19
object IsabelleProcess {
wenzelm@27973
    20
wenzelm@28045
    21
  private val charset = "UTF-8"
wenzelm@28045
    22
wenzelm@28045
    23
wenzelm@27949
    24
  /* results */
wenzelm@27949
    25
wenzelm@27949
    26
  object Kind extends Enumeration {
wenzelm@27949
    27
    //{{{ values
wenzelm@27949
    28
    // Posix channels/events
wenzelm@27949
    29
    val STDIN = Value("STDIN")
wenzelm@27949
    30
    val STDOUT = Value("STDOUT")
wenzelm@27949
    31
    val SIGNAL = Value("SIGNAL")
wenzelm@27949
    32
    val EXIT = Value("EXIT")
wenzelm@27949
    33
    // Isabelle messages
wenzelm@27949
    34
    val WRITELN = Value("WRITELN")
wenzelm@27949
    35
    val PRIORITY = Value("PRIORITY")
wenzelm@27949
    36
    val TRACING = Value("TRACING")
wenzelm@27949
    37
    val WARNING = Value("WARNING")
wenzelm@27949
    38
    val ERROR = Value("ERROR")
wenzelm@27949
    39
    val DEBUG = Value("DEBUG")
wenzelm@27949
    40
    val PROMPT = Value("PROMPT")
wenzelm@27949
    41
    val INIT = Value("INIT")
wenzelm@27949
    42
    val STATUS = Value("STATUS")
wenzelm@27949
    43
    // internal system notification
wenzelm@27949
    44
    val SYSTEM = Value("SYSTEM")
wenzelm@27949
    45
    //}}}
wenzelm@27949
    46
    def is_raw(kind: Value) =
wenzelm@28045
    47
      kind == STDOUT
wenzelm@27990
    48
    def is_control(kind: Value) =
wenzelm@27990
    49
      kind == SIGNAL ||
wenzelm@27990
    50
      kind == EXIT ||
wenzelm@27990
    51
      kind == SYSTEM
wenzelm@27949
    52
    def is_system(kind: Value) =
wenzelm@27949
    53
      kind == STDIN ||
wenzelm@27949
    54
      kind == SIGNAL ||
wenzelm@27949
    55
      kind == EXIT ||
wenzelm@27949
    56
      kind == PROMPT ||
wenzelm@27949
    57
      kind == STATUS ||
wenzelm@27949
    58
      kind == SYSTEM
wenzelm@27949
    59
  }
wenzelm@27949
    60
wenzelm@27973
    61
  class Result(val kind: Kind.Value, val props: Properties, val result: String) {
wenzelm@27949
    62
    override def toString = {
wenzelm@27963
    63
      val res = XML.content(YXML.parse_failsafe(result)).mkString("")
wenzelm@27955
    64
      if (props == null) kind.toString + " [[" + res + "]]"
wenzelm@27955
    65
      else kind.toString + " " + props.toString + " [[" + res + "]]"
wenzelm@27949
    66
    }
wenzelm@27973
    67
    def is_raw() = Kind.is_raw(kind)
wenzelm@27990
    68
    def is_control() = Kind.is_control(kind)
wenzelm@27973
    69
    def is_system() = Kind.is_system(kind)
wenzelm@27973
    70
  }
wenzelm@27949
    71
wenzelm@27973
    72
}
wenzelm@27973
    73
wenzelm@27973
    74
wenzelm@27973
    75
class IsabelleProcess(args: String*) {
wenzelm@27973
    76
wenzelm@27973
    77
  import IsabelleProcess._
wenzelm@27973
    78
wenzelm@27973
    79
wenzelm@27973
    80
  /* process information */
wenzelm@27973
    81
wenzelm@27973
    82
  private var proc: Process = null
wenzelm@27973
    83
  private var closing = false
wenzelm@27973
    84
  private var pid: String = null
wenzelm@27973
    85
  private var the_session: String = null
wenzelm@27973
    86
  def session() = the_session
wenzelm@27973
    87
wenzelm@27973
    88
wenzelm@27973
    89
  /* results */
wenzelm@27949
    90
wenzelm@27990
    91
  private val results = new LinkedBlockingQueue[Result]
wenzelm@27949
    92
wenzelm@27949
    93
  private def put_result(kind: Kind.Value, props: Properties, result: String) {
wenzelm@27963
    94
    if (kind == Kind.INIT && props != null) {
wenzelm@27973
    95
      pid = props.getProperty(Markup.PID)
wenzelm@27973
    96
      the_session = props.getProperty(Markup.SESSION)
wenzelm@27963
    97
    }
wenzelm@27955
    98
    results.put(new Result(kind, props, result))
wenzelm@27949
    99
  }
wenzelm@27949
   100
wenzelm@27990
   101
  def get_result() = results.take
wenzelm@27973
   102
wenzelm@27992
   103
  def try_result() = {
wenzelm@27992
   104
    val res = results.poll
wenzelm@27992
   105
    if (res != null) Some(res) else None
wenzelm@27992
   106
  }
wenzelm@27992
   107
wenzelm@27973
   108
wenzelm@27973
   109
  /* signals */
wenzelm@27973
   110
wenzelm@27973
   111
  def interrupt() = synchronized {
wenzelm@27993
   112
    if (proc == null) error("Cannot interrupt Isabelle: no process")
wenzelm@27973
   113
    if (pid == null) put_result(Kind.SYSTEM, null, "Cannot interrupt: unknown pid")
wenzelm@27973
   114
    else {
wenzelm@27973
   115
      try {
wenzelm@27990
   116
        if (IsabelleSystem.exec(List("kill", "-INT", pid)).waitFor == 0)
wenzelm@27990
   117
          put_result(Kind.SIGNAL, null, "INT")
wenzelm@27990
   118
        else
wenzelm@27973
   119
          put_result(Kind.SYSTEM, null, "Cannot interrupt: kill command failed")
wenzelm@27973
   120
      }
wenzelm@27993
   121
      catch { case e: IOException => error("Cannot interrupt Isabelle: " + e.getMessage) }
wenzelm@27973
   122
    }
wenzelm@27973
   123
  }
wenzelm@27973
   124
wenzelm@27973
   125
  def kill() = synchronized {
wenzelm@27993
   126
    if (proc == 0) error("Cannot kill Isabelle: no process")
wenzelm@27973
   127
    else {
wenzelm@27973
   128
      try_close()
wenzelm@27990
   129
      Thread.sleep(500)
wenzelm@27973
   130
      put_result(Kind.SIGNAL, null, "KILL")
wenzelm@27973
   131
      proc.destroy
wenzelm@27973
   132
      proc = null
wenzelm@27990
   133
      pid = null
wenzelm@27973
   134
    }
wenzelm@27973
   135
  }
wenzelm@27973
   136
wenzelm@27949
   137
wenzelm@27949
   138
  /* output being piped into the process */
wenzelm@27949
   139
wenzelm@27990
   140
  private val output = new LinkedBlockingQueue[String]
wenzelm@27949
   141
wenzelm@28045
   142
  private def output_raw(text: String) = synchronized {
wenzelm@27993
   143
    if (proc == null) error("Cannot output to Isabelle: no process")
wenzelm@27993
   144
    if (closing) error("Cannot output to Isabelle: already closing")
wenzelm@27955
   145
    output.put(text)
wenzelm@27949
   146
  }
wenzelm@27949
   147
wenzelm@28045
   148
  private def output_sync(text: String) =
wenzelm@28045
   149
    output_raw(" \\<^sync>\n; " + text + " \\<^sync>;\n")
wenzelm@27949
   150
wenzelm@27963
   151
wenzelm@27963
   152
  def command(text: String) =
wenzelm@27949
   153
    output_sync("Isabelle.command " + IsabelleSyntax.encode_string(text))
wenzelm@27949
   154
wenzelm@27963
   155
  def command(props: Properties, text: String) =
wenzelm@27949
   156
    output_sync("Isabelle.command " + IsabelleSyntax.encode_properties(props) + " " +
wenzelm@27949
   157
      IsabelleSyntax.encode_string(text))
wenzelm@27949
   158
wenzelm@27963
   159
  def ML(text: String) =
wenzelm@27949
   160
    output_sync("ML_val " + IsabelleSyntax.encode_string(text))
wenzelm@27949
   161
wenzelm@27949
   162
  def close() = synchronized {    // FIXME watchdog/timeout
wenzelm@27949
   163
    output_raw("\u0000")
wenzelm@27949
   164
    closing = true
wenzelm@27949
   165
  }
wenzelm@27949
   166
wenzelm@27949
   167
  def try_close() = synchronized {
wenzelm@27949
   168
    if (proc != null && !closing) {
wenzelm@27949
   169
      try { close() }
wenzelm@27993
   170
      catch { case _: RuntimeException => }
wenzelm@27949
   171
    }
wenzelm@27949
   172
  }
wenzelm@27949
   173
wenzelm@27949
   174
wenzelm@27949
   175
  /* stdin */
wenzelm@27949
   176
wenzelm@28045
   177
  private class StdinThread(out_stream: OutputStream) extends Thread("isabelle: stdin") {
wenzelm@27949
   178
    override def run() = {
wenzelm@28045
   179
      val writer = new BufferedWriter(new OutputStreamWriter(out_stream, charset))
wenzelm@27949
   180
      var finished = false
wenzelm@27949
   181
      while (!finished) {
wenzelm@27949
   182
        try {
wenzelm@27949
   183
          //{{{
wenzelm@27955
   184
          val s = output.take
wenzelm@27949
   185
          if (s == "\u0000") {
wenzelm@27949
   186
            writer.close
wenzelm@27949
   187
            finished = true
wenzelm@27949
   188
          }
wenzelm@27949
   189
          else {
wenzelm@27949
   190
            put_result(Kind.STDIN, null, s)
wenzelm@27949
   191
            writer.write(s)
wenzelm@27949
   192
            writer.flush
wenzelm@27949
   193
          }
wenzelm@27949
   194
          //}}}
wenzelm@27949
   195
        }
wenzelm@27949
   196
        catch {
wenzelm@27949
   197
          case e: IOException => put_result(Kind.SYSTEM, null, "Stdin thread: " + e.getMessage)
wenzelm@27949
   198
        }
wenzelm@27949
   199
      }
wenzelm@27949
   200
      put_result(Kind.SYSTEM, null, "Stdin thread terminated")
wenzelm@27949
   201
    }
wenzelm@27949
   202
  }
wenzelm@27949
   203
wenzelm@27949
   204
wenzelm@27949
   205
  /* stdout */
wenzelm@27949
   206
wenzelm@28045
   207
  private class StdoutThread(in_stream: InputStream) extends Thread("isabelle: stdout") {
wenzelm@27949
   208
    override def run() = {
wenzelm@28045
   209
      val reader = new BufferedReader(new InputStreamReader(in_stream, charset))
wenzelm@28045
   210
      var result = new StringBuilder(100)
wenzelm@27949
   211
wenzelm@27949
   212
      var finished = false
wenzelm@27949
   213
      while (!finished) {
wenzelm@27949
   214
        try {
wenzelm@28045
   215
          //{{{
wenzelm@28045
   216
          var c = -1
wenzelm@28045
   217
          var done = false
wenzelm@28045
   218
          while (!done && (result.length == 0 || reader.ready)) {
wenzelm@28045
   219
            c = reader.read
wenzelm@28045
   220
            if (c >= 0) result.append(c.asInstanceOf[Char])
wenzelm@28045
   221
            else done = true
wenzelm@28045
   222
          }
wenzelm@28045
   223
          if (result.length > 0) {
wenzelm@28045
   224
            put_result(Kind.STDOUT, null, result.toString)
wenzelm@28045
   225
            result.length = 0
wenzelm@27949
   226
          }
wenzelm@27949
   227
          else {
wenzelm@28045
   228
            reader.close
wenzelm@28045
   229
            finished = true
wenzelm@28045
   230
            try_close()
wenzelm@27949
   231
          }
wenzelm@28045
   232
          //}}}
wenzelm@27963
   233
        }
wenzelm@27963
   234
        catch {
wenzelm@27949
   235
          case e: IOException => put_result(Kind.SYSTEM, null, "Stdout thread: " + e.getMessage)
wenzelm@27949
   236
        }
wenzelm@27949
   237
      }
wenzelm@27949
   238
      put_result(Kind.SYSTEM, null, "Stdout thread terminated")
wenzelm@27949
   239
    }
wenzelm@27949
   240
  }
wenzelm@27949
   241
wenzelm@27949
   242
wenzelm@28045
   243
  /* messages */
wenzelm@27949
   244
wenzelm@28045
   245
  private class MessageThread(fifo: String) extends Thread("isabelle: messages") {
wenzelm@27949
   246
    override def run() = {
wenzelm@28045
   247
      val reader = new BufferedReader(new InputStreamReader(new FileInputStream(fifo), charset))
wenzelm@28045
   248
      var kind: Kind.Value = null
wenzelm@28045
   249
      var props: Properties = null
wenzelm@28045
   250
      var result = new StringBuilder
wenzelm@27949
   251
wenzelm@27949
   252
      var finished = false
wenzelm@27949
   253
      while (!finished) {
wenzelm@27949
   254
        try {
wenzelm@28045
   255
          try {
wenzelm@28045
   256
            if (kind == null) {
wenzelm@28045
   257
              //{{{ Char mode -- resync
wenzelm@28045
   258
              var c = -1
wenzelm@28045
   259
              do {
wenzelm@28045
   260
                c = reader.read
wenzelm@28045
   261
                if (c >= 0 && c != 2) result.append(c.asInstanceOf[Char])
wenzelm@28045
   262
              } while (c >= 0 && c != 2)
wenzelm@28045
   263
  
wenzelm@28045
   264
              if (result.length > 0) {
wenzelm@28045
   265
                put_result(Kind.SYSTEM, null, "Malformed message:\n" + result.toString)
wenzelm@28045
   266
                result.length = 0
wenzelm@28045
   267
              }
wenzelm@28045
   268
              if (c < 0) {
wenzelm@28045
   269
                reader.close
wenzelm@28045
   270
                finished = true
wenzelm@28045
   271
                try_close()
wenzelm@28045
   272
              }
wenzelm@28045
   273
              else {
wenzelm@28045
   274
                reader.read match {
wenzelm@28045
   275
                  case 'A' => kind = Kind.WRITELN
wenzelm@28045
   276
                  case 'B' => kind = Kind.PRIORITY
wenzelm@28045
   277
                  case 'C' => kind = Kind.TRACING
wenzelm@28045
   278
                  case 'D' => kind = Kind.WARNING
wenzelm@28045
   279
                  case 'E' => kind = Kind.ERROR
wenzelm@28045
   280
                  case 'F' => kind = Kind.DEBUG
wenzelm@28045
   281
                  case 'G' => kind = Kind.PROMPT
wenzelm@28045
   282
                  case 'H' => kind = Kind.INIT
wenzelm@28045
   283
                  case 'I' => kind = Kind.STATUS
wenzelm@28045
   284
                  case _ => kind = null
wenzelm@28045
   285
                }
wenzelm@28045
   286
                props = null
wenzelm@28045
   287
              }
wenzelm@28045
   288
              //}}}
wenzelm@28045
   289
            }
wenzelm@28045
   290
            else {
wenzelm@28045
   291
              //{{{ Line mode
wenzelm@28045
   292
              val line = reader.readLine
wenzelm@28045
   293
              if (line == null) {
wenzelm@28045
   294
                reader.close
wenzelm@28045
   295
                finished = true
wenzelm@28045
   296
                try_close()
wenzelm@28045
   297
              }
wenzelm@28045
   298
              else {
wenzelm@28045
   299
                val len = line.length
wenzelm@28045
   300
                // property
wenzelm@28045
   301
                if (line.endsWith("\u0002,")) {
wenzelm@28045
   302
                  val i = line.indexOf('=')
wenzelm@28045
   303
                  if (i > 0) {
wenzelm@28045
   304
                    val name = line.substring(0, i)
wenzelm@28045
   305
                    val value = line.substring(i + 1, len - 2)
wenzelm@28045
   306
                    if (props == null) props = new Properties
wenzelm@28045
   307
                    if (!props.containsKey(name)) props.setProperty(name, value)
wenzelm@28045
   308
                  }
wenzelm@28045
   309
                }
wenzelm@28045
   310
                // last text line
wenzelm@28045
   311
                else if (line.endsWith("\u0002.")) {
wenzelm@28045
   312
                  result.append(line.substring(0, len - 2))
wenzelm@28045
   313
                  put_result(kind, props, result.toString)
wenzelm@28045
   314
                  result.length = 0
wenzelm@28045
   315
                  kind = null
wenzelm@28045
   316
                }
wenzelm@28045
   317
                // text line
wenzelm@28045
   318
                else {
wenzelm@28045
   319
                  result.append(line)
wenzelm@28045
   320
                  result.append('\n')
wenzelm@28045
   321
                }
wenzelm@28045
   322
              }
wenzelm@28045
   323
              //}}}
wenzelm@28045
   324
            }
wenzelm@27949
   325
          }
wenzelm@28045
   326
          catch {
wenzelm@28045
   327
            case e: IOException => put_result(Kind.SYSTEM, null, "Message thread: " + e.getMessage)
wenzelm@27949
   328
          }
wenzelm@27963
   329
        }
wenzelm@28045
   330
        catch { case _: InterruptedException => finished = true }
wenzelm@27949
   331
      }
wenzelm@28045
   332
      try { put_result(Kind.SYSTEM, null, "Message thread terminated") }
wenzelm@28045
   333
      catch { case _ : InterruptedException => }
wenzelm@27949
   334
    }
wenzelm@27949
   335
  }
wenzelm@27949
   336
wenzelm@27949
   337
wenzelm@27949
   338
  /** main **/
wenzelm@27949
   339
wenzelm@27949
   340
  {
wenzelm@28045
   341
wenzelm@28045
   342
    /* message fifo */
wenzelm@28045
   343
wenzelm@28045
   344
    val fifo =
wenzelm@28045
   345
      try {
wenzelm@28045
   346
        val mkfifo = IsabelleSystem.exec(List(IsabelleSystem.getenv_strict("ISATOOL"), "mkfifo"))
wenzelm@28045
   347
        val fifo = new BufferedReader(new InputStreamReader(mkfifo.getInputStream, charset)).readLine
wenzelm@28045
   348
        if (mkfifo.waitFor == 0) fifo
wenzelm@28045
   349
        else error("Failed to create message fifo")
wenzelm@28045
   350
      }
wenzelm@28045
   351
      catch {
wenzelm@28045
   352
        case e: IOException => error("Failed to create message fifo: " + e.getMessage)
wenzelm@28045
   353
      }
wenzelm@28045
   354
wenzelm@28045
   355
    val message_thread = new MessageThread(fifo)
wenzelm@28045
   356
wenzelm@28045
   357
wenzelm@27973
   358
    /* exec process */
wenzelm@27963
   359
wenzelm@27973
   360
    try {
wenzelm@28045
   361
      proc = IsabelleSystem.exec2(List(
wenzelm@28045
   362
        IsabelleSystem.getenv_strict("ISABELLE_HOME") + "/bin/isabelle-process", "-W", fifo) ++ args)
wenzelm@27949
   363
    }
wenzelm@27993
   364
    catch {
wenzelm@27993
   365
      case e: IOException => error("Failed to execute Isabelle process: " + e.getMessage)
wenzelm@27993
   366
    }
wenzelm@27949
   367
wenzelm@27949
   368
wenzelm@28045
   369
    /* stdin/stdout */
wenzelm@28045
   370
wenzelm@28045
   371
    new StdinThread(proc.getOutputStream).start
wenzelm@28045
   372
    new StdoutThread(proc.getInputStream).start
wenzelm@28045
   373
wenzelm@28045
   374
wenzelm@28045
   375
    /* exit */
wenzelm@27949
   376
wenzelm@28045
   377
    class ExitThread extends Thread("isabelle: exit") {
wenzelm@28045
   378
      override def run() = {
wenzelm@28045
   379
        val rc = proc.waitFor()
wenzelm@28045
   380
        Thread.sleep(300)
wenzelm@28045
   381
        put_result(Kind.SYSTEM, null, "Exit thread terminated")
wenzelm@28045
   382
        put_result(Kind.EXIT, null, Integer.toString(rc))
wenzelm@28045
   383
        message_thread.interrupt
wenzelm@28045
   384
      }
wenzelm@28045
   385
    }
wenzelm@28045
   386
    message_thread.start
wenzelm@27949
   387
    new ExitThread().start
wenzelm@27949
   388
  }
wenzelm@27949
   389
wenzelm@27949
   390
}