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