author | nipkow |
Tue, 17 Jun 2025 14:11:40 +0200 | |
changeset 82733 | 8b537e1af2ec |
parent 81832 | 7c92343b5408 |
permissions | -rw-r--r-- |
62400 | 1 |
/* Title: Pure/System/process_result.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Result of system process. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
75393 | 9 |
object Process_Result { |
74306 | 10 |
/* return code */ |
11 |
||
75393 | 12 |
object RC { |
77253
792dad9cb04f
clarified data structure: absorb Option[Process_Result] into Process_Result, e.g. to simplify database storage;
wenzelm
parents:
77243
diff
changeset
|
13 |
val undefined = -1 |
74306 | 14 |
val ok = 0 |
15 |
val error = 1 |
|
16 |
val failure = 2 |
|
77243 | 17 |
val startup_failure = 127 |
74306 | 18 |
val interrupt = 130 |
19 |
val timeout = 142 |
|
71749 | 20 |
|
81832 | 21 |
val regular: Set[Int] = Set(ok, error) |
22 |
||
75393 | 23 |
private def text(rc: Int): String = { |
74306 | 24 |
val txt = |
25 |
rc match { |
|
77253
792dad9cb04f
clarified data structure: absorb Option[Process_Result] into Process_Result, e.g. to simplify database storage;
wenzelm
parents:
77243
diff
changeset
|
26 |
case `undefined` => "UNDEFINED" |
74306 | 27 |
case `ok` => "OK" |
28 |
case `error` => "ERROR" |
|
29 |
case `failure` => "FAILURE" |
|
78438 | 30 |
case `startup_failure` => "COMMAND NOT FOUND" |
74306 | 31 |
case `interrupt` => "INTERRUPT" |
32 |
case 131 => "QUIT SIGNAL" |
|
33 |
case 137 => "KILL SIGNAL" |
|
34 |
case 138 => "BUS ERROR" |
|
35 |
case 139 => "SEGMENTATION VIOLATION" |
|
36 |
case `timeout` => "TIMEOUT" |
|
37 |
case 143 => "TERMINATION SIGNAL" |
|
38 |
case _ => "" |
|
39 |
} |
|
40 |
if (txt.isEmpty) txt else " (" + txt + ")" |
|
71749 | 41 |
} |
74067 | 42 |
|
74306 | 43 |
def print_long(rc: Int): String = "Return code: " + rc + text(rc) |
44 |
def print(rc: Int): String = "return code " + rc + text(rc) |
|
78438 | 45 |
|
46 |
def merge(rc1: Int, rc2: Int): Int = |
|
47 |
if (rc1 == interrupt || rc2 == interrupt) interrupt |
|
48 |
else rc1 max rc2 |
|
49 |
||
50 |
def merge(rcs: IterableOnce[Int]): Int = rcs.iterator.foldLeft(ok)(merge) |
|
51 |
||
78442 | 52 |
def apply(ok: Boolean): Int = if (ok) RC.ok else RC.error |
53 |
def apply(exn: Throwable): Int = if (Exn.is_interrupt(exn)) interrupt else error |
|
78438 | 54 |
def apply(result: Exn.Result[Process_Result]): Int = |
55 |
result match { |
|
56 |
case Exn.Res(res) => res.rc |
|
78442 | 57 |
case Exn.Exn(exn) => apply(exn) |
78438 | 58 |
} |
74306 | 59 |
} |
77243 | 60 |
|
77253
792dad9cb04f
clarified data structure: absorb Option[Process_Result] into Process_Result, e.g. to simplify database storage;
wenzelm
parents:
77243
diff
changeset
|
61 |
val undefined: Process_Result = Process_Result(RC.undefined) |
77243 | 62 |
val ok: Process_Result = Process_Result(RC.ok) |
63 |
val error: Process_Result = Process_Result(RC.error) |
|
64 |
val startup_failure: Process_Result = Process_Result(RC.startup_failure) |
|
71747 | 65 |
} |
66 |
||
62401 | 67 |
final case class Process_Result( |
62402 | 68 |
rc: Int, |
69 |
out_lines: List[String] = Nil, |
|
70 |
err_lines: List[String] = Nil, |
|
75393 | 71 |
timing: Timing = Timing.zero |
72 |
) { |
|
73277
0110e2e2964c
clarified signature: always trim_line of Process_Result.out/err, uniformly in ML and Scala;
wenzelm
parents:
73134
diff
changeset
|
73 |
def out: String = Library.trim_line(cat_lines(out_lines)) |
0110e2e2964c
clarified signature: always trim_line of Process_Result.out/err, uniformly in ML and Scala;
wenzelm
parents:
73134
diff
changeset
|
74 |
def err: String = Library.trim_line(cat_lines(err_lines)) |
71631 | 75 |
|
71646 | 76 |
def output(outs: List[String]): Process_Result = |
77 |
copy(out_lines = out_lines ::: outs.flatMap(split_lines)) |
|
78 |
def errors(errs: List[String]): Process_Result = |
|
79 |
copy(err_lines = err_lines ::: errs.flatMap(split_lines)) |
|
72002
5c4800f6b25a
more robust protocol for "Timing ..." messages, notably for pide_session=true;
wenzelm
parents:
71749
diff
changeset
|
80 |
def error(err: String): Process_Result = |
5c4800f6b25a
more robust protocol for "Timing ..." messages, notably for pide_session=true;
wenzelm
parents:
71749
diff
changeset
|
81 |
if (err.isEmpty) this else errors(List(err)) |
62400 | 82 |
|
74306 | 83 |
def ok: Boolean = rc == Process_Result.RC.ok |
84 |
def interrupted: Boolean = rc == Process_Result.RC.interrupt |
|
62400 | 85 |
|
77253
792dad9cb04f
clarified data structure: absorb Option[Process_Result] into Process_Result, e.g. to simplify database storage;
wenzelm
parents:
77243
diff
changeset
|
86 |
def defined: Boolean = rc > Process_Result.RC.undefined |
792dad9cb04f
clarified data structure: absorb Option[Process_Result] into Process_Result, e.g. to simplify database storage;
wenzelm
parents:
77243
diff
changeset
|
87 |
def strict: Process_Result = if (defined) this else copy(rc = Process_Result.RC.error) |
792dad9cb04f
clarified data structure: absorb Option[Process_Result] into Process_Result, e.g. to simplify database storage;
wenzelm
parents:
77243
diff
changeset
|
88 |
|
74306 | 89 |
def timeout_rc: Process_Result = copy(rc = Process_Result.RC.timeout) |
90 |
def timeout: Boolean = rc == Process_Result.RC.timeout |
|
73134
8a8ffe78eee7
clarified return code: re-use SIGALRM for soft timeout;
wenzelm
parents:
72726
diff
changeset
|
91 |
|
74306 | 92 |
def error_rc: Process_Result = |
93 |
if (interrupted) this else copy(rc = rc max Process_Result.RC.error) |
|
68927 | 94 |
|
72726
ec6a27bbdab8
proper return code for more errors (amending d892f6d66402);
wenzelm
parents:
72556
diff
changeset
|
95 |
def errors_rc(errs: List[String]): Process_Result = |
ec6a27bbdab8
proper return code for more errors (amending d892f6d66402);
wenzelm
parents:
72556
diff
changeset
|
96 |
if (errs.isEmpty) this else errors(errs).error_rc |
ec6a27bbdab8
proper return code for more errors (amending d892f6d66402);
wenzelm
parents:
72556
diff
changeset
|
97 |
|
64408
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64138
diff
changeset
|
98 |
def check_rc(pred: Int => Boolean): Process_Result = |
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64138
diff
changeset
|
99 |
if (pred(rc)) this |
62400 | 100 |
else if (interrupted) throw Exn.Interrupt() |
62492 | 101 |
else Exn.error(err) |
62400 | 102 |
|
74306 | 103 |
def check: Process_Result = check_rc(_ == Process_Result.RC.ok) |
64408
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64138
diff
changeset
|
104 |
|
74306 | 105 |
def print_return_code: String = Process_Result.RC.print_long(rc) |
106 |
def print_rc: String = Process_Result.RC.print(rc) |
|
71747 | 107 |
|
75393 | 108 |
def print: Process_Result = { |
64138
cf0c8c5782af
eliminated extra trim_line: Process_Result.out/err are based on cat_lines, without trailing newline;
wenzelm
parents:
64024
diff
changeset
|
109 |
Output.warning(err) |
cf0c8c5782af
eliminated extra trim_line: Process_Result.out/err are based on cat_lines, without trailing newline;
wenzelm
parents:
64024
diff
changeset
|
110 |
Output.writeln(out) |
62404
13a0f537e232
retain tail out_lines as printed, but not the whole log content;
wenzelm
parents:
62402
diff
changeset
|
111 |
copy(out_lines = Nil, err_lines = Nil) |
62400 | 112 |
} |
62553 | 113 |
|
75393 | 114 |
def print_stdout: Process_Result = { |
64138
cf0c8c5782af
eliminated extra trim_line: Process_Result.out/err are based on cat_lines, without trailing newline;
wenzelm
parents:
64024
diff
changeset
|
115 |
Output.warning(err, stdout = true) |
cf0c8c5782af
eliminated extra trim_line: Process_Result.out/err are based on cat_lines, without trailing newline;
wenzelm
parents:
64024
diff
changeset
|
116 |
Output.writeln(out, stdout = true) |
62553 | 117 |
copy(out_lines = Nil, err_lines = Nil) |
118 |
} |
|
62400 | 119 |
} |