From 3e00170c1ca93f6677c1cc9f68b120bdc3f9da1f Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Mon, 19 Jun 2017 19:07:31 -0700 Subject: awk macro: bugfix: :output file stream not closed. In the case when the :output clause of the awk macro opens a stream, it isn't closing it. Why don't we treat such a stream uniformly with redirections. * share/txr/stdlib/awk.tl (sys:awk-state :postinit): If a file is opened for output, then don't just install this file as self.output. Also place it into the self.streams hash, identified by using the key (:outf ), just like a (-> "name") redirection. This ensures that it will be closed when the awk macro terminates. It's also potentially useful in that output redirections referring to the name will use the same stream. * txr.1: Documented that streams opened by :output are closed, and that they are treated uniformly with redirections. --- share/txr/stdlib/awk.tl | 4 +++- txr.1 | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/share/txr/stdlib/awk.tl b/share/txr/stdlib/awk.tl index 7c4033e6..aab6a4eb 100644 --- a/share/txr/stdlib/awk.tl +++ b/share/txr/stdlib/awk.tl @@ -50,7 +50,9 @@ (if (plusp self.rng-n) (set self.rng-vec (vector self.rng-n))) (unless (streamp self.output) - (set self.output (open-file self.output "w"))))) + (let ((stream (open-file self.output "w"))) + (set [self.streams ^(:outf ,self.output)] stream + self.output stream))))) (defstruct sys:awk-compile-time () inputs output name lets diff --git a/txr.1 b/txr.1 index bd1eeed8..a0cbd8e8 100644 --- a/txr.1 +++ b/txr.1 @@ -44734,6 +44734,38 @@ can rebind causing the definition provided by .code :output to be shadowed. + +In the case when the +.code :output +argument is a string such that a new stream is opened +on the file, the +.code awk +macro will close that stream when it finishes executing. +Moreover, that stream is treated uniformly as a member of +the set of streams that are implicitly managed by the +redirection macros in the same +.code awk +macro invocation. In brief, the implication is that if +.code :output +creates a stream for the file path name +.str "out.txt" +and somewhere in the same +.code awk +macro, there is a redirection of the form, or equivalent to +.cblk +(-> "out.txt") +.cble +then this redirection shall refer to the same stream +that was established by +.codn :output . +Note also that in this example situation, the expression +.cblk +(-> "out.txt" :close) +.cble +has the effect of closing the +.code :output +stream. + .meIP (:begin << form *) All .code :begin -- cgit v1.2.3