| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Support for system calls as part of reflection |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Reflection.External where |
| 10 |
|
| 11 |
import Agda.Builtin.Reflection.External as Builtin |
| 12 |
|
| 13 |
open import Data.Nat.Base using (ℕ; suc; zero; NonZero) |
| 14 |
open import Data.List.Base using (List; _∷_; []) |
| 15 |
open import Data.Product.Base using (_×_; _,_) |
| 16 |
open import Data.String.Base as String using (String; _++_) |
| 17 |
open import Data.Sum.Base using (_⊎_; inj₁; inj₂; [_,_]) |
| 18 |
open import Data.Unit.Base using (⊤; tt) |
| 19 |
open import Function.Base using (case_of_; _$_; _∘_) |
| 20 |
open import Reflection hiding (name) |
| 21 |
|
| 22 |
-- Type aliases for the various strings. |
| 23 |
|
| 24 |
CmdName = String |
| 25 |
StdIn = String |
| 26 |
StdErr = String |
| 27 |
StdOut = String |
| 28 |
|
| 29 |
-- Representation for exit codes, assuming 0 is consistently used to |
| 30 |
-- indicate success across platforms. |
| 31 |
data ExitCode : Set where |
| 32 |
exitSuccess : ExitCode |
| 33 |
exitFailure : (n : ℕ) {n≢0 : NonZero n} → ExitCode |
| 34 |
|
| 35 |
-- Specification for a command. |
| 36 |
record CmdSpec : Set where |
| 37 |
constructor cmdSpec |
| 38 |
field |
| 39 |
name : CmdName -- ^ Executable name (see ~/.agda/executables) |
| 40 |
args : List String -- ^ Command-line arguments for executable |
| 41 |
input : StdIn -- ^ Contents of standard input |
| 42 |
|
| 43 |
-- Result of running a command. |
| 44 |
record Result : Set where |
| 45 |
constructor result |
| 46 |
field |
| 47 |
exitCode : ExitCode -- ^ Exit code returned by the process |
| 48 |
output : StdOut -- ^ Contents of standard output |
| 49 |
error : StdErr -- ^ Contents of standard error |
| 50 |
|
| 51 |
-- Convert a natural number to an exit code. |
| 52 |
toExitCode : ℕ → ExitCode |
| 53 |
toExitCode zero = exitSuccess |
| 54 |
toExitCode (suc n) = exitFailure (suc n) |
| 55 |
|
| 56 |
-- Quote an exit code as an Agda term. |
| 57 |
quoteExitCode : ExitCode → Term |
| 58 |
quoteExitCode exitSuccess = |
| 59 |
con (quote exitSuccess) [] |
| 60 |
quoteExitCode (exitFailure n) = |
| 61 |
con (quote exitFailure) (vArg (lit (nat n)) ∷ hArg (con (quote tt) []) ∷ []) |
| 62 |
|
| 63 |
-- Quote a result as an Agda term. |
| 64 |
quoteResult : Result → Term |
| 65 |
quoteResult (result exitCode output error) = |
| 66 |
con (quote result) $ vArg (quoteExitCode exitCode) |
| 67 |
∷ vArg (lit (string output)) |
| 68 |
∷ vArg (lit (string error)) |
| 69 |
∷ [] |
| 70 |
|
| 71 |
-- Run command from specification and return the full result. |
| 72 |
-- |
| 73 |
-- NOTE: If the command fails, this macro still succeeds, and returns the |
| 74 |
-- full result, including exit code and the contents of stderr. |
| 75 |
-- |
| 76 |
unsafeRunCmdTC : CmdSpec → TC Result |
| 77 |
unsafeRunCmdTC c = do |
| 78 |
(exitCode , (stdOut , stdErr)) |
| 79 |
← Builtin.execTC (CmdSpec.name c) (CmdSpec.args c) (CmdSpec.input c) |
| 80 |
pure $ result (toExitCode exitCode) stdOut stdErr |
| 81 |
|
| 82 |
macro |
| 83 |
unsafeRunCmd : CmdSpec → Term → TC ⊤ |
| 84 |
unsafeRunCmd c hole = unsafeRunCmdTC c >>= unify hole ∘ quoteResult |
| 85 |
|
| 86 |
|
| 87 |
-- Show a command for the user. |
| 88 |
showCmdSpec : CmdSpec → String |
| 89 |
showCmdSpec c = String.unwords $ CmdSpec.name c ∷ CmdSpec.args c |
| 90 |
|
| 91 |
|
| 92 |
private |
| 93 |
-- Helper function for throwing an error from reflection. |
| 94 |
userError : ∀ {a} {A : Set a} → CmdSpec → StdOut × StdErr → TC A |
| 95 |
userError c (stdout , stderr) = typeError (strErr errMsg ∷ []) |
| 96 |
where |
| 97 |
errMsg : String |
| 98 |
errMsg = String.unlines |
| 99 |
$ ("Error while running command '" ++ showCmdSpec c ++ "'") |
| 100 |
∷ ("Input:\n" ++ CmdSpec.input c) |
| 101 |
∷ ("Output:\n" ++ stdout) |
| 102 |
∷ ("Error:\n" ++ stderr) |
| 103 |
∷ [] |
| 104 |
|
| 105 |
|
| 106 |
-- Run command from specification. If the command succeeds, it returns the |
| 107 |
-- contents of stdout. Otherwise, it throws a type error with the contents |
| 108 |
-- of stderr. |
| 109 |
runCmdTC : CmdSpec → TC StdOut |
| 110 |
runCmdTC c = do |
| 111 |
r ← unsafeRunCmdTC c |
| 112 |
let debugPrefix = ("user." ++ CmdSpec.name c) |
| 113 |
case Result.exitCode r of λ |
| 114 |
{ exitSuccess → do |
| 115 |
debugPrint (debugPrefix ++ ".stderr") 10 (strErr (Result.error r) ∷ []) |
| 116 |
pure $ Result.output r |
| 117 |
; (exitFailure n) → do |
| 118 |
userError c (Result.output r , Result.error r) |
| 119 |
} |
| 120 |
|
| 121 |
macro |
| 122 |
runCmd : CmdSpec → Term → TC ⊤ |
| 123 |
runCmd c hole = runCmdTC c >>= unify hole ∘ lit ∘ string |