From: Richard W.M. Jones Date: Mon, 20 Jan 2020 19:52:21 +0000 (+0000) Subject: Implement %tmpdir. X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=a1b5f8cc0dae6adfdb306d701fc6fd579504ffe6;hp=5e13f1e2a3dc1237fcf2fa141d8379bdd36bde61;p=goals.git Implement %tmpdir. --- diff --git a/docs/Goalfile.pod b/docs/Goalfile.pod index ec84bae..5355fe0 100644 --- a/docs/Goalfile.pod +++ b/docs/Goalfile.pod @@ -12,6 +12,15 @@ Goalfile - introduction, tutorial, and reference for writing goal files =head1 REFERENCE +=head2 Standard Variables + +=head3 %tmpdir + +The location of a temporary directory which is created by goals when +it starts and is deleted when it exits (either on success or failure). +You can use this to store any temporary files that you want +automatically cleaned up. + =head2 Standard Functions =head3 basename (path) diff --git a/src/main.ml b/src/main.ml index cee1dcf..7e1c2f0 100644 --- a/src/main.ml +++ b/src/main.ml @@ -33,6 +33,19 @@ let main () = (* Change directory (-C option). *) Sys.chdir (Cmdline.directory ()); + (* Create a temporary directory which is always cleaned up at exit. *) + let tmpdir = + let temp_dir = try Unix.getenv "TMPDIR" with Not_found -> "/var/tmp" in + let t = Filename.temp_file ~temp_dir "goals" ".d" in + Unix.unlink t; + Unix.mkdir t 0o700; + at_exit ( + fun () -> + let cmd = sprintf "rm -rf %s" (Filename.quote t) in + ignore (Sys.command cmd) + ); + t in + (* Create the initial environment, containing the system environment * and a few other standard strings. *) @@ -43,6 +56,8 @@ let main () = Ast.Env.add k (Ast.EConstant (Ast.noloc, Ast.CString v)) env ) Ast.Env.empty (Unix.environment ()) in let env = + Ast.Env.add "tmpdir" (Ast.EConstant (Ast.noloc, Ast.CString tmpdir)) env in + let env = Ast.Env.add "stdlib" (Ast.EConstant (Ast.noloc, Ast.CString Cmdline.stdlibdir)) env in