src/Pure/System/isabelle_process.scala
author wenzelm
Mon, 07 Mar 2016 22:40:43 +0100
changeset 62554 56449c2d20db
parent 62548 f8ebb715e06d
child 62555 fd6e64133684
permissions -rw-r--r--
prospective command line entry point for simplified isabelle_process;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43283
446e6621762d updated headers;
wenzelm
parents: 40848
diff changeset
     1
/*  Title:      Pure/System/isabelle_process.scala
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
     3
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
     4
Isabelle process wrapper.
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
     5
*/
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
     6
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
     7
package isabelle
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
     8
55618
995162143ef4 tuned imports;
wenzelm
parents: 54443
diff changeset
     9
57917
8ce97e5d545f tuned signature;
wenzelm
parents: 57916
diff changeset
    10
object Isabelle_Process
8ce97e5d545f tuned signature;
wenzelm
parents: 57916
diff changeset
    11
{
8ce97e5d545f tuned signature;
wenzelm
parents: 57916
diff changeset
    12
  def apply(
8ce97e5d545f tuned signature;
wenzelm
parents: 57916
diff changeset
    13
    receiver: Prover.Message => Unit = Console.println(_),
62545
8ebffdaf2ce2 Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents: 62309
diff changeset
    14
    prover_args: List[String] = Nil): Isabelle_Process =
57917
8ce97e5d545f tuned signature;
wenzelm
parents: 57916
diff changeset
    15
  {
8ce97e5d545f tuned signature;
wenzelm
parents: 57916
diff changeset
    16
    val system_channel = System_Channel()
8ce97e5d545f tuned signature;
wenzelm
parents: 57916
diff changeset
    17
    val system_process =
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
    18
      try {
62545
8ebffdaf2ce2 Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents: 62309
diff changeset
    19
        val process =
62548
f8ebb715e06d clarified treatment of DEL;
wenzelm
parents: 62545
diff changeset
    20
          Bash.process("\"$ISABELLE_PROCESS\" -P " + File.bash_string(system_channel.server_name) +
f8ebb715e06d clarified treatment of DEL;
wenzelm
parents: 62545
diff changeset
    21
            " " + File.bash_args(prover_args))
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
    22
        process.stdin.close
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
    23
        process
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
    24
      }
60215
5fb4990dfc73 misc tuning, based on warnings by IntelliJ IDEA;
wenzelm
parents: 57917
diff changeset
    25
      catch { case exn @ ERROR(_) => system_channel.accepted(); throw exn }
57917
8ce97e5d545f tuned signature;
wenzelm
parents: 57916
diff changeset
    26
8ce97e5d545f tuned signature;
wenzelm
parents: 57916
diff changeset
    27
    new Isabelle_Process(receiver, system_channel, system_process)
8ce97e5d545f tuned signature;
wenzelm
parents: 57916
diff changeset
    28
  }
62554
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    29
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    30
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    31
  /* command line entry point */
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    32
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    33
  def main(args: Array[String])
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    34
  {
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    35
    Command_Line.tool {
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    36
      var secure = false
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    37
      var ml_args: List[String] = Nil
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    38
      var modes: List[String] = Nil
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    39
      var options = Options.init()
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    40
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    41
      val getopts = Getopts("""
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    42
Usage: isabelle_process [OPTIONS] [HEAP]
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    43
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    44
  Options are:
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    45
    -S           secure mode -- disallow critical operations
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    46
    -e ML_EXPR   evaluate ML expression on startup
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    47
    -f ML_FILE   evaluate ML file on startup
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    48
    -m MODE      add print mode for output
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    49
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    50
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    51
  If HEAP is a plain name (default $ISABELLE_LOGIC), it is searched in
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    52
  $ISABELLE_PATH; if it contains a slash, it is taken as literal file;
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    53
  if it is RAW_ML_SYSTEM, the initial ML heap is used.
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    54
""",
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    55
        "S" -> (_ => secure = true),
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    56
        "e:" -> (arg => ml_args = ml_args ::: List("--eval", arg)),
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    57
        "f:" -> (arg => ml_args = ml_args ::: List("--use", arg)),
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    58
        "m:" -> (arg => modes = arg :: modes),
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    59
        "o:" -> (arg => options = options + arg))
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    60
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    61
      val heap =
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    62
        getopts(args) match {
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    63
          case Nil => ""
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    64
          case List(heap) => heap
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    65
          case _ => getopts.usage()
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    66
        }
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    67
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    68
      ML_Process(options, heap = heap, args = ml_args, secure = secure, modes = modes).
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    69
        result().print_stdout.rc
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    70
    }
56449c2d20db prospective command line entry point for simplified isabelle_process;
wenzelm
parents: 62548
diff changeset
    71
  }
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
    72
}
27949
6eb0327c0b9b Isabelle process management -- always reactive due to multi-threaded I/O.
wenzelm
parents:
diff changeset
    73
57917
8ce97e5d545f tuned signature;
wenzelm
parents: 57916
diff changeset
    74
class Isabelle_Process private(
8ce97e5d545f tuned signature;
wenzelm
parents: 57916
diff changeset
    75
    receiver: Prover.Message => Unit,
8ce97e5d545f tuned signature;
wenzelm
parents: 57916
diff changeset
    76
    system_channel: System_Channel,
8ce97e5d545f tuned signature;
wenzelm
parents: 57916
diff changeset
    77
    system_process: Prover.System_Process)
8ce97e5d545f tuned signature;
wenzelm
parents: 57916
diff changeset
    78
  extends Prover(receiver, system_channel, system_process)
62309
96c9a259d275 tuned whitespace;
wenzelm
parents: 62308
diff changeset
    79
{
96c9a259d275 tuned whitespace;
wenzelm
parents: 62308
diff changeset
    80
  def encode(s: String): String = Symbol.encode(s)
96c9a259d275 tuned whitespace;
wenzelm
parents: 62308
diff changeset
    81
  def decode(s: String): String = Symbol.decode(s)
96c9a259d275 tuned whitespace;
wenzelm
parents: 62308
diff changeset
    82
}