Pau Espin Pedrol | 5badb21 | 2024-05-15 14:36:53 +0200 | [diff] [blame] | 1 | module PIPEasp_Templates { |
| 2 | |
| 3 | import from PIPEasp_PortType all; |
| 4 | import from PIPEasp_Types all; |
| 5 | |
Pau Espin Pedrol | a674d61 | 2024-05-14 19:56:33 +0200 | [diff] [blame] | 6 | template (value) ASP_PExecute ts_PExecute(template (value) charstring command, |
| 7 | template (value) charstring stdin) := { |
| 8 | command := command, |
| 9 | stdin := stdin |
| 10 | } |
| 11 | |
| 12 | template (present) ASP_PResult tr_PResult(template (present) charstring stdout, |
| 13 | template (present) charstring stderr, |
| 14 | template (present) integer code) := { |
| 15 | stdout := stdout, |
| 16 | stderr := stderr, |
| 17 | code := code |
| 18 | } |
| 19 | |
Pau Espin Pedrol | 5badb21 | 2024-05-15 14:36:53 +0200 | [diff] [blame] | 20 | template (value) ASP_PExecuteBackground ts_ExecBg(charstring cmd) := { |
| 21 | command := cmd |
| 22 | } |
| 23 | |
| 24 | template (present) ASP_PStdout tr_Stdout(template (present) charstring line) := { |
| 25 | stdout := line |
| 26 | } |
| 27 | |
| 28 | template (present) ASP_PStderr tr_Stderr(template (present) charstring line) := { |
| 29 | stderr := line |
| 30 | } |
| 31 | |
| 32 | template (value) ASP_PStdin ts_Stdin(template (value) charstring line) := { |
| 33 | stdin :=line |
| 34 | } |
| 35 | |
| 36 | /* Ignore output from stderr: */ |
| 37 | altstep as_ignore_stderr(PIPEasp_PT pt) { |
| 38 | [] pt.receive(tr_Stderr(?)) { repeat; } |
| 39 | } |
| 40 | |
Pau Espin Pedrol | a674d61 | 2024-05-14 19:56:33 +0200 | [diff] [blame] | 41 | /* User should map(component_name:PIPE, system:PIPE) before using this function. */ |
| 42 | function f_PIPEasp_exec_sync_PResult(PIPEasp_PT pt, |
| 43 | charstring cmdline, |
| 44 | template (present) ASP_PResult res_exp := tr_PResult(?,?,0), |
| 45 | float time_out := 10.0) return ASP_PResult { |
| 46 | var ASP_PResult res; |
| 47 | timer t; |
| 48 | |
| 49 | if (time_out > 0.0) { |
| 50 | t.start(time_out); |
| 51 | } |
| 52 | |
| 53 | log ("Executing: ", cmdline); |
| 54 | pt.send(ts_PExecute(cmdline, "")); |
| 55 | |
| 56 | alt { |
| 57 | [] pt.receive(res_exp) -> value res; |
| 58 | [time_out > 0.0] t.timeout { |
| 59 | setverdict(fail, "Timeout: ", cmdline); |
| 60 | mtc.stop; |
| 61 | } |
| 62 | } |
| 63 | log ("Result: ", res); |
| 64 | return res; |
| 65 | } |
| 66 | |
| 67 | function f_PIPEasp_exec_sync(PIPEasp_PT pt, |
| 68 | charstring cmdline, |
| 69 | template (present) integer rc := 0) return ASP_PResult { |
| 70 | return f_PIPEasp_exec_sync_PResult(pt, cmdline, tr_PResult(?, ?, rc)); |
| 71 | } |
| 72 | |
Pau Espin Pedrol | 5badb21 | 2024-05-15 14:36:53 +0200 | [diff] [blame] | 73 | } |