From 5ff4246327fd802923faa77a3421d2384d643735 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 21 Jul 2022 20:15:52 -0700 Subject: compile-file: distinguish nonexistence errors. The compile-file function must only try a different path, such as with a suffix, if a given path fails to open due to non-existence. If the failure is for another reason, like permissions. In that case we want to propagate the failure. * stdlib/compiler.tl (ign-notfound): New macro. This lets through all errors, catching only path-not-found, converting that to a nil result. (open-compile-streams): Use ign-notfound instead of ignerr when trying to open an input file for reading. Also, we lose the ignerr wrapping around the open-file. We let any error whatsoever just bubble out so that the user is better informed about what went wrong. The requirement to close the input stream is handled by the obvious unwind-protect. --- stdlib/compiler.tl | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'stdlib') diff --git a/stdlib/compiler.tl b/stdlib/compiler.tl index b82f85a2..bea78e33 100644 --- a/stdlib/compiler.tl +++ b/stdlib/compiler.tl @@ -2240,6 +2240,9 @@ intern unintern rehome-sym use-sym unuse-sym)) +(defmacro ign-notfound (form) + ^(usr:catch ,form (path-not-found (. rest)))) + (defun open-compile-streams (in-path out-path test-fn) (if (and (nullify in-path) (find [in-path -1] path-sep-chars)) @@ -2253,10 +2256,10 @@ in-stream out-stream) (casequal suff (".txr" (error "~s: cannot compile TXR files" 'compile-file)) - (".tl" (set in-stream (ignerr (open-file in-path)) + (".tl" (set in-stream (ign-notfound (open-file in-path)) out-path (or out-path `@{ip-nosuff}.tlo`))) - (t (set in-stream (or (ignerr (open-file in-path)) - (ignerr (open-file `@{in-path}.tl`))) + (t (set in-stream (or (ign-notfound (open-file in-path)) + (ign-notfound (open-file `@{in-path}.tl`))) out-path (or out-path `@{in-path}.tlo`)))) (unless in-stream @@ -2266,11 +2269,10 @@ (close-stream in-stream) (return-from open-compile-streams nil)) - (set out-stream (ignerr (open-file out-path "w"))) - - (unless out-stream - (close-stream in-stream) - (error "~s: unable to open output file ~s" 'compile-file out-path)) + (unwind-protect + (set out-stream (open-file out-path "w")) + (unless out-stream + (close-stream in-stream))) (list in-stream out-stream out-path))) -- cgit v1.2.3