From 0ad39c713f1d0239abda6cece563c0964a1ea5e0 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sun, 25 Sep 2016 07:06:41 -0700 Subject: awk macro: handle dynamic changes in rs variable. * awk.tl (sys:awk-state loop): The loop now notices when rs or krs changes and switches to a new record-adapter or to the raw stream as necessary. * txr.1: Notes added about changes to rs and krs. --- share/txr/stdlib/awk.tl | 48 ++++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) (limited to 'share') diff --git a/share/txr/stdlib/awk.tl b/share/txr/stdlib/awk.tl index eb07cdcc..404c3aab 100644 --- a/share/txr/stdlib/awk.tl +++ b/share/txr/stdlib/awk.tl @@ -82,26 +82,34 @@ (inc aws.file-num) (when beg-file-func [beg-file-func aws]) - (unwind-protect - (let* ((stin (if (streamp in) in (open-file in))) - (recin (if (and (equal aws.rs "\n") - (not aws.krs)) - stin - (record-adapter (if (regexp aws.rs) - aws.rs - (regex-compile aws.rs)) - stin - aws.krs)))) - (set aws.file-rec-num 0) - (whilet ((rec (get-line recin))) - (set aws.rec rec aws.orig-rec rec) - (inc aws.rec-num) - (inc aws.file-rec-num) - aws.(rec-to-f) - (block :awk-rec - [func aws]))) - (when end-file-func - [end-file-func aws]))))) + (flet ((get-stream (stin) + (if (and (equal aws.rs "\n") + (not aws.krs)) + stin + (record-adapter (if (regexp aws.rs) + aws.rs + (regex-compile aws.rs)) + stin + aws.krs)))) + (unwind-protect + (let* ((stin (if (streamp in) in (open-file in))) + (recin (get-stream stin)) + (noted-rs aws.rs) + (noted-krs aws.krs)) + (set aws.file-rec-num 0) + (whilet ((rec (get-line recin))) + (set aws.rec rec aws.orig-rec rec) + (inc aws.rec-num) + (inc aws.file-rec-num) + aws.(rec-to-f) + (block :awk-rec + [func aws]) + (unless (and (equal noted-rs aws.rs) + (eq noted-krs aws.krs)) + (set recin (get-stream stin)) + (set noted-rs aws.rs noted-krs aws.krs)))) + (when end-file-func + [end-file-func aws])))))) (defmeth sys:awk-state prn (self . args) (cond -- cgit v1.2.3