src/Pure/Concurrent/bash_sequential.ML
author wenzelm
Mon, 16 Apr 2012 23:07:40 +0200
changeset 47499 4b0daca2bf88
parent 44054 da5fcc0f6c52
permissions -rw-r--r--
redirect bash stderr to Isabelle warning as appropriate -- avoid raw process error output which may either get ignored or overload PIDE syslog in extreme cases;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40748
591b6778d076 removed bash from ML system bootstrap, and past the Secure ML barrier;
wenzelm
parents: 39583
diff changeset
     1
(*  Title:      Pure/Concurrent/bash_sequential.ML
26220
d34b68c21f9a common setup for system_out/system;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
d34b68c21f9a common setup for system_out/system;
wenzelm
parents:
diff changeset
     3
35010
d6e492cea6e4 renamed system/system_out to bash/bash_output -- to emphasized that this is really GNU bash, not some undefined POSIX sh;
wenzelm
parents: 29564
diff changeset
     4
Generic GNU bash processes (no provisions to propagate interrupts, but
d6e492cea6e4 renamed system/system_out to bash/bash_output -- to emphasized that this is really GNU bash, not some undefined POSIX sh;
wenzelm
parents: 29564
diff changeset
     5
could work via the controlling tty).
26220
d34b68c21f9a common setup for system_out/system;
wenzelm
parents:
diff changeset
     6
*)
d34b68c21f9a common setup for system_out/system;
wenzelm
parents:
diff changeset
     7
44054
da5fcc0f6c52 proper signature;
wenzelm
parents: 43850
diff changeset
     8
signature BASH =
da5fcc0f6c52 proper signature;
wenzelm
parents: 43850
diff changeset
     9
sig
47499
4b0daca2bf88 redirect bash stderr to Isabelle warning as appropriate -- avoid raw process error output which may either get ignored or overload PIDE syslog in extreme cases;
wenzelm
parents: 44054
diff changeset
    10
  val process: string -> {out: string, err: string, rc: int, terminate: unit -> unit}
44054
da5fcc0f6c52 proper signature;
wenzelm
parents: 43850
diff changeset
    11
end;
da5fcc0f6c52 proper signature;
wenzelm
parents: 43850
diff changeset
    12
da5fcc0f6c52 proper signature;
wenzelm
parents: 43850
diff changeset
    13
structure Bash: BASH =
43850
7f2cbc713344 moved bash operations to Isabelle_System (cf. Scala version);
wenzelm
parents: 43847
diff changeset
    14
struct
7f2cbc713344 moved bash operations to Isabelle_System (cf. Scala version);
wenzelm
parents: 43847
diff changeset
    15
7f2cbc713344 moved bash operations to Isabelle_System (cf. Scala version);
wenzelm
parents: 43847
diff changeset
    16
fun process script =
26220
d34b68c21f9a common setup for system_out/system;
wenzelm
parents:
diff changeset
    17
  let
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    18
    val id = serial_string ();
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    19
    val script_path = File.tmp_path (Path.basic ("bash_script" ^ id));
47499
4b0daca2bf88 redirect bash stderr to Isabelle warning as appropriate -- avoid raw process error output which may either get ignored or overload PIDE syslog in extreme cases;
wenzelm
parents: 44054
diff changeset
    20
    val out_path = File.tmp_path (Path.basic ("bash_out" ^ id));
4b0daca2bf88 redirect bash stderr to Isabelle warning as appropriate -- avoid raw process error output which may either get ignored or overload PIDE syslog in extreme cases;
wenzelm
parents: 44054
diff changeset
    21
    val err_path = File.tmp_path (Path.basic ("bash_err" ^ id));
4b0daca2bf88 redirect bash stderr to Isabelle warning as appropriate -- avoid raw process error output which may either get ignored or overload PIDE syslog in extreme cases;
wenzelm
parents: 44054
diff changeset
    22
    fun cleanup () = (try File.rm script_path; try File.rm out_path; try File.rm err_path);
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    23
  in
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    24
    let
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    25
      val _ = File.write script_path script;
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    26
      val status =
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    27
        OS.Process.system
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    28
          ("exec \"$ISABELLE_HOME/lib/scripts/process\" no_group /dev/null" ^
47499
4b0daca2bf88 redirect bash stderr to Isabelle warning as appropriate -- avoid raw process error output which may either get ignored or overload PIDE syslog in extreme cases;
wenzelm
parents: 44054
diff changeset
    29
            " script \"exec bash " ^ File.shell_path script_path ^
4b0daca2bf88 redirect bash stderr to Isabelle warning as appropriate -- avoid raw process error output which may either get ignored or overload PIDE syslog in extreme cases;
wenzelm
parents: 44054
diff changeset
    30
            " > " ^ File.shell_path out_path ^
4b0daca2bf88 redirect bash stderr to Isabelle warning as appropriate -- avoid raw process error output which may either get ignored or overload PIDE syslog in extreme cases;
wenzelm
parents: 44054
diff changeset
    31
            " 2> " ^ File.shell_path err_path ^ "\"");
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    32
      val rc =
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    33
        (case Posix.Process.fromStatus status of
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    34
          Posix.Process.W_EXITED => 0
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    35
        | Posix.Process.W_EXITSTATUS w => Word8.toInt w
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    36
        | Posix.Process.W_SIGNALED s => 256 + LargeWord.toInt (Posix.Signal.toWord s)
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    37
        | Posix.Process.W_STOPPED s => 512 + LargeWord.toInt (Posix.Signal.toWord s));
26220
d34b68c21f9a common setup for system_out/system;
wenzelm
parents:
diff changeset
    38
47499
4b0daca2bf88 redirect bash stderr to Isabelle warning as appropriate -- avoid raw process error output which may either get ignored or overload PIDE syslog in extreme cases;
wenzelm
parents: 44054
diff changeset
    39
      val out = the_default "" (try File.read out_path);
4b0daca2bf88 redirect bash stderr to Isabelle warning as appropriate -- avoid raw process error output which may either get ignored or overload PIDE syslog in extreme cases;
wenzelm
parents: 44054
diff changeset
    40
      val err = the_default "" (try File.read err_path);
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    41
      val _ = cleanup ();
47499
4b0daca2bf88 redirect bash stderr to Isabelle warning as appropriate -- avoid raw process error output which may either get ignored or overload PIDE syslog in extreme cases;
wenzelm
parents: 44054
diff changeset
    42
    in {out = out, err = err, rc = rc, terminate = fn () => ()} end
40749
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    43
    handle exn => (cleanup (); reraise exn)
cb6698d2dbaf prefer Isabelle/ML concurrency elements;
wenzelm
parents: 40748
diff changeset
    44
  end;
26220
d34b68c21f9a common setup for system_out/system;
wenzelm
parents:
diff changeset
    45
43850
7f2cbc713344 moved bash operations to Isabelle_System (cf. Scala version);
wenzelm
parents: 43847
diff changeset
    46
end;
7f2cbc713344 moved bash operations to Isabelle_System (cf. Scala version);
wenzelm
parents: 43847
diff changeset
    47