src/Pure/General/system_process.ML
author wenzelm
Sat, 16 Feb 2008 16:43:54 +0100
changeset 26078 4fc74eb2842b
child 26085 4ce22e7a81bd
permissions -rw-r--r--
System shell processes, with static input/output and propagation of interrupts.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26078
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/General/system_process.ML
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
     4
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
     5
System shell processes, with static input/output and propagation of interrupts.
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
     6
*)
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
     7
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
     8
signature SYSTEM_PROCESS =
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
     9
sig
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    10
  val system_out: string -> string * int
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    11
  val system: string -> int
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    12
end;
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    13
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    14
structure SystemProcess: SYSTEM_PROCESS =
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    15
struct
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    16
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    17
fun system_out cmdline = uninterruptible (fn restore_attributes => fn () =>
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    18
  let
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    19
    val _ = Secure.deny_secure "Cannot execute shell commands in secure mode";
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    20
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    21
    val proc = Unix.execute (cmdline, []);
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    22
    val (proc_stdout, proc_stdin) = Unix.streamsOf proc;
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    23
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    24
    fun read buf =
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    25
      (case Exn.capture (restore_attributes TextIO.input) proc_stdout of
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    26
        Exn.Exn Interrupt => (Unix.kill (proc, Posix.Signal.int); read buf)
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    27
      | Exn.Exn _ => buf
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    28
      | Exn.Result "" => buf
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    29
      | Exn.Result txt => read (Buffer.add txt buf));
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    30
    val output = read Buffer.empty;
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    31
    val status = Unix.reap proc;
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    32
    val rc = Unix.fromStatus status;
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    33
  in
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    34
    tracing (PolyML.makestring (Unix.fromStatus status));
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    35
    if rc = Unix.W_SIGNALED Posix.Signal.int orelse rc = Unix.W_EXITSTATUS 0wx82
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    36
    then raise Interrupt
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    37
    else (Buffer.content output, if OS.Process.isSuccess status then 0 else 1)
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    38
  end) ();
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    39
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    40
fun system cmdline =
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    41
  let val (output, status) = system_out cmdline in writeln output; status end;
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    42
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    43
end;
4fc74eb2842b System shell processes, with static input/output and propagation of interrupts.
wenzelm
parents:
diff changeset
    44