From fb9f3a3a46a40dff1122c1410291a0eaf3353331 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 26 Jan 2017 08:52:23 -0800 Subject: awk macro: provide name of current input via fname. * share/txr/stdlib/awk.tl (sys:awk-state): New slot, file-name. (sys:awk-state loop): Set file-name from current input name, or else from the stream name property if the current input is a stream object. (sys:awk-mac-let): New awk macro, fname. * txr.1: Documented fname. --- share/txr/stdlib/awk.tl | 5 +++++ txr.1 | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/share/txr/stdlib/awk.tl b/share/txr/stdlib/awk.tl index 9732ea24..05567956 100644 --- a/share/txr/stdlib/awk.tl +++ b/share/txr/stdlib/awk.tl @@ -35,6 +35,7 @@ (inputs) (output *stdout*) (file-num 0) + file-name (file-rec-num 0) (rec-num 0) rec orig-rec fields nf @@ -119,6 +120,9 @@ (whilet ((in (pop aws.inputs))) (block :awk-file (inc aws.file-num) + (set aws.file-name (if (streamp in) + (stream-get-prop in :name) + in)) (when beg-file-func [beg-file-func aws]) (let* ((*stdin* (if (streamp in) in (open-file in))) @@ -268,6 +272,7 @@ (nr (qref ,aws-sym rec-num)) (fnr (qref ,aws-sym file-rec-num)) (arg (qref ,aws-sym file-num)) + (fname (qref ,aws-sym file-name)) (rs (qref ,aws-sym rs)) (krs (qref ,aws-sym krs)) (fs (qref ,aws-sym fs)) diff --git a/txr.1 b/txr.1 index c9cbe389..f5a2c8c7 100644 --- a/txr.1 +++ b/txr.1 @@ -43122,6 +43122,30 @@ Thereafter, it is incremented whenever .code awk switches to a new input source. +.coNP Variable @ fname +.desc +The awk variable +.code fname +provides access to a character string which, if the current input is +a file stream, is the name of the underlying file. Assigning to this +variable changes its value, but has no effect on the input stream. +Whenever a new input source is used by +.code awk +it sets this variable either from the file name on which it is opening +a stream.. When using an existing stream rather than opening a file, +.code awk +sets this variable from the +.code :name +property of the stream. + +Note that the redirection macros +.code <- +and +.code