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