author | wenzelm |
Fri, 01 Apr 2022 17:06:10 +0200 | |
changeset 75393 | 87ebf5a50283 |
parent 74306 | a117c076aa22 |
child 77243 | 629dce95bb5c |
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 { |
74306 | 13 |
val ok = 0 |
14 |
val error = 1 |
|
15 |
val failure = 2 |
|
16 |
val interrupt = 130 |
|
17 |
val timeout = 142 |
|
71749 | 18 |
|
75393 | 19 |
private def text(rc: Int): String = { |
74306 | 20 |
val txt = |
21 |
rc match { |
|
22 |
case `ok` => "OK" |
|
23 |
case `error` => "ERROR" |
|
24 |
case `failure` => "FAILURE" |
|
25 |
case 127 => "COMMAND NOT FOUND" |
|
26 |
case `interrupt` => "INTERRUPT" |
|
27 |
case 131 => "QUIT SIGNAL" |
|
28 |
case 137 => "KILL SIGNAL" |
|
29 |
case 138 => "BUS ERROR" |
|
30 |
case 139 => "SEGMENTATION VIOLATION" |
|
31 |
case `timeout` => "TIMEOUT" |
|
32 |
case 143 => "TERMINATION SIGNAL" |
|
33 |
case _ => "" |
|
34 |
} |
|
35 |
if (txt.isEmpty) txt else " (" + txt + ")" |
|
71749 | 36 |
} |
74067 | 37 |
|
74306 | 38 |
def print_long(rc: Int): String = "Return code: " + rc + text(rc) |
39 |
def print(rc: Int): String = "return code " + rc + text(rc) |
|
40 |
} |
|
71747 | 41 |
} |
42 |
||
62401 | 43 |
final case class Process_Result( |
62402 | 44 |
rc: Int, |
45 |
out_lines: List[String] = Nil, |
|
46 |
err_lines: List[String] = Nil, |
|
75393 | 47 |
timing: Timing = Timing.zero |
48 |
) { |
|
73277
0110e2e2964c
clarified signature: always trim_line of Process_Result.out/err, uniformly in ML and Scala;
wenzelm
parents:
73134
diff
changeset
|
49 |
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
|
50 |
def err: String = Library.trim_line(cat_lines(err_lines)) |
71631 | 51 |
|
71646 | 52 |
def output(outs: List[String]): Process_Result = |
53 |
copy(out_lines = out_lines ::: outs.flatMap(split_lines)) |
|
54 |
def errors(errs: List[String]): Process_Result = |
|
55 |
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
|
56 |
def error(err: String): Process_Result = |
5c4800f6b25a
more robust protocol for "Timing ..." messages, notably for pide_session=true;
wenzelm
parents:
71749
diff
changeset
|
57 |
if (err.isEmpty) this else errors(List(err)) |
62400 | 58 |
|
74306 | 59 |
def ok: Boolean = rc == Process_Result.RC.ok |
60 |
def interrupted: Boolean = rc == Process_Result.RC.interrupt |
|
62400 | 61 |
|
74306 | 62 |
def timeout_rc: Process_Result = copy(rc = Process_Result.RC.timeout) |
63 |
def timeout: Boolean = rc == Process_Result.RC.timeout |
|
73134
8a8ffe78eee7
clarified return code: re-use SIGALRM for soft timeout;
wenzelm
parents:
72726
diff
changeset
|
64 |
|
74306 | 65 |
def error_rc: Process_Result = |
66 |
if (interrupted) this else copy(rc = rc max Process_Result.RC.error) |
|
68927 | 67 |
|
72726
ec6a27bbdab8
proper return code for more errors (amending d892f6d66402);
wenzelm
parents:
72556
diff
changeset
|
68 |
def errors_rc(errs: List[String]): Process_Result = |
ec6a27bbdab8
proper return code for more errors (amending d892f6d66402);
wenzelm
parents:
72556
diff
changeset
|
69 |
if (errs.isEmpty) this else errors(errs).error_rc |
ec6a27bbdab8
proper return code for more errors (amending d892f6d66402);
wenzelm
parents:
72556
diff
changeset
|
70 |
|
64408
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64138
diff
changeset
|
71 |
def check_rc(pred: Int => Boolean): Process_Result = |
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64138
diff
changeset
|
72 |
if (pred(rc)) this |
62400 | 73 |
else if (interrupted) throw Exn.Interrupt() |
62492 | 74 |
else Exn.error(err) |
62400 | 75 |
|
74306 | 76 |
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
|
77 |
|
74306 | 78 |
def print_return_code: String = Process_Result.RC.print_long(rc) |
79 |
def print_rc: String = Process_Result.RC.print(rc) |
|
71747 | 80 |
|
75393 | 81 |
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
|
82 |
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
|
83 |
Output.writeln(out) |
62404
13a0f537e232
retain tail out_lines as printed, but not the whole log content;
wenzelm
parents:
62402
diff
changeset
|
84 |
copy(out_lines = Nil, err_lines = Nil) |
62400 | 85 |
} |
62553 | 86 |
|
75393 | 87 |
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
|
88 |
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
|
89 |
Output.writeln(out, stdout = true) |
62553 | 90 |
copy(out_lines = Nil, err_lines = Nil) |
91 |
} |
|
62400 | 92 |
} |