diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2017-01-26 08:52:23 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2017-01-26 08:52:23 -0800 |
commit | fb9f3a3a46a40dff1122c1410291a0eaf3353331 (patch) | |
tree | 986d721b6725ab52831bc8c2e7c8f7fbd024d079 | |
parent | 6899c7cd2b6890acd8ef86f4f2301708c6f59c1d (diff) | |
download | txr-fb9f3a3a46a40dff1122c1410291a0eaf3353331.tar.gz txr-fb9f3a3a46a40dff1122c1410291a0eaf3353331.tar.bz2 txr-fb9f3a3a46a40dff1122c1410291a0eaf3353331.zip |
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.
-rw-r--r-- | share/txr/stdlib/awk.tl | 5 | ||||
-rw-r--r-- | txr.1 | 24 |
2 files changed, 29 insertions, 0 deletions
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)) @@ -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 <! +have no effect on this variable. Within their scope, +.code fname +retains its value. + .coNP Variable @ rs .desc The awk variable |