| author | wenzelm | 
| Wed, 01 May 2024 20:26:20 +0200 | |
| changeset 80166 | 825f35bae74b | 
| parent 78442 | c38aebdf1a3d | 
| child 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: 
77243diff
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 | |
| 75393 | 21 |     private def text(rc: Int): String = {
 | 
| 74306 | 22 | val txt = | 
| 23 |         rc match {
 | |
| 77253 
792dad9cb04f
clarified data structure: absorb Option[Process_Result] into Process_Result, e.g. to simplify database storage;
 wenzelm parents: 
77243diff
changeset | 24 | case `undefined` => "UNDEFINED" | 
| 74306 | 25 | case `ok` => "OK" | 
| 26 | case `error` => "ERROR" | |
| 27 | case `failure` => "FAILURE" | |
| 78438 | 28 | case `startup_failure` => "COMMAND NOT FOUND" | 
| 74306 | 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) | |
| 78438 | 43 | |
| 44 | def merge(rc1: Int, rc2: Int): Int = | |
| 45 | if (rc1 == interrupt || rc2 == interrupt) interrupt | |
| 46 | else rc1 max rc2 | |
| 47 | ||
| 48 | def merge(rcs: IterableOnce[Int]): Int = rcs.iterator.foldLeft(ok)(merge) | |
| 49 | ||
| 78442 | 50 | def apply(ok: Boolean): Int = if (ok) RC.ok else RC.error | 
| 51 | def apply(exn: Throwable): Int = if (Exn.is_interrupt(exn)) interrupt else error | |
| 78438 | 52 | def apply(result: Exn.Result[Process_Result]): Int = | 
| 53 |       result match {
 | |
| 54 | case Exn.Res(res) => res.rc | |
| 78442 | 55 | case Exn.Exn(exn) => apply(exn) | 
| 78438 | 56 | } | 
| 74306 | 57 | } | 
| 77243 | 58 | |
| 77253 
792dad9cb04f
clarified data structure: absorb Option[Process_Result] into Process_Result, e.g. to simplify database storage;
 wenzelm parents: 
77243diff
changeset | 59 | val undefined: Process_Result = Process_Result(RC.undefined) | 
| 77243 | 60 | val ok: Process_Result = Process_Result(RC.ok) | 
| 61 | val error: Process_Result = Process_Result(RC.error) | |
| 62 | val startup_failure: Process_Result = Process_Result(RC.startup_failure) | |
| 71747 | 63 | } | 
| 64 | ||
| 62401 | 65 | final case class Process_Result( | 
| 62402 | 66 | rc: Int, | 
| 67 | out_lines: List[String] = Nil, | |
| 68 | err_lines: List[String] = Nil, | |
| 75393 | 69 | timing: Timing = Timing.zero | 
| 70 | ) {
 | |
| 73277 
0110e2e2964c
clarified signature: always trim_line of Process_Result.out/err, uniformly in ML and Scala;
 wenzelm parents: 
73134diff
changeset | 71 | 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 | 72 | def err: String = Library.trim_line(cat_lines(err_lines)) | 
| 71631 | 73 | |
| 71646 | 74 | def output(outs: List[String]): Process_Result = | 
| 75 | copy(out_lines = out_lines ::: outs.flatMap(split_lines)) | |
| 76 | def errors(errs: List[String]): Process_Result = | |
| 77 | 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 | 78 | def error(err: String): Process_Result = | 
| 
5c4800f6b25a
more robust protocol for "Timing ..." messages, notably for pide_session=true;
 wenzelm parents: 
71749diff
changeset | 79 | if (err.isEmpty) this else errors(List(err)) | 
| 62400 | 80 | |
| 74306 | 81 | def ok: Boolean = rc == Process_Result.RC.ok | 
| 82 | def interrupted: Boolean = rc == Process_Result.RC.interrupt | |
| 62400 | 83 | |
| 77253 
792dad9cb04f
clarified data structure: absorb Option[Process_Result] into Process_Result, e.g. to simplify database storage;
 wenzelm parents: 
77243diff
changeset | 84 | 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: 
77243diff
changeset | 85 | 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: 
77243diff
changeset | 86 | |
| 74306 | 87 | def timeout_rc: Process_Result = copy(rc = Process_Result.RC.timeout) | 
| 88 | def timeout: Boolean = rc == Process_Result.RC.timeout | |
| 73134 
8a8ffe78eee7
clarified return code: re-use SIGALRM for soft timeout;
 wenzelm parents: 
72726diff
changeset | 89 | |
| 74306 | 90 | def error_rc: Process_Result = | 
| 91 | if (interrupted) this else copy(rc = rc max Process_Result.RC.error) | |
| 68927 | 92 | |
| 72726 
ec6a27bbdab8
proper return code for more errors (amending d892f6d66402);
 wenzelm parents: 
72556diff
changeset | 93 | def errors_rc(errs: List[String]): Process_Result = | 
| 
ec6a27bbdab8
proper return code for more errors (amending d892f6d66402);
 wenzelm parents: 
72556diff
changeset | 94 | if (errs.isEmpty) this else errors(errs).error_rc | 
| 
ec6a27bbdab8
proper return code for more errors (amending d892f6d66402);
 wenzelm parents: 
72556diff
changeset | 95 | |
| 64408 
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
 wenzelm parents: 
64138diff
changeset | 96 | def check_rc(pred: Int => Boolean): Process_Result = | 
| 
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
 wenzelm parents: 
64138diff
changeset | 97 | if (pred(rc)) this | 
| 62400 | 98 | else if (interrupted) throw Exn.Interrupt() | 
| 62492 | 99 | else Exn.error(err) | 
| 62400 | 100 | |
| 74306 | 101 | 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 | 102 | |
| 74306 | 103 | def print_return_code: String = Process_Result.RC.print_long(rc) | 
| 104 | def print_rc: String = Process_Result.RC.print(rc) | |
| 71747 | 105 | |
| 75393 | 106 |   def print: Process_Result = {
 | 
| 64138 
cf0c8c5782af
eliminated extra trim_line: Process_Result.out/err are based on cat_lines, without trailing newline;
 wenzelm parents: 
64024diff
changeset | 107 | Output.warning(err) | 
| 
cf0c8c5782af
eliminated extra trim_line: Process_Result.out/err are based on cat_lines, without trailing newline;
 wenzelm parents: 
64024diff
changeset | 108 | Output.writeln(out) | 
| 62404 
13a0f537e232
retain tail out_lines as printed, but not the whole log content;
 wenzelm parents: 
62402diff
changeset | 109 | copy(out_lines = Nil, err_lines = Nil) | 
| 62400 | 110 | } | 
| 62553 | 111 | |
| 75393 | 112 |   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: 
64024diff
changeset | 113 | 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 | 114 | Output.writeln(out, stdout = true) | 
| 62553 | 115 | copy(out_lines = Nil, err_lines = Nil) | 
| 116 | } | |
| 62400 | 117 | } |