From b5abca70b167173913f8e1030071e61bce70b4f0 Mon Sep 17 00:00:00 2001
From: Ray Gardner <raygard@gmail.com>
Date: Thu, 8 Aug 2024 17:47:23 -0600
Subject: [PATCH 1/7] Fix nextfile; add tests

The nextfile statement was failing because it left unprocessed input in the
record buffer from the current file. Mod to reset the buffer so it will get
data from the next file immediately. Add a test for the nextfile fix and also
for the preceding commit that fixed the 'getline var' bug (not setting it as a
numeric string if needed).
---
 toys/pending/awk.c             | 3 ++-
 tests/awk.test | 4 ++++

diff --git a/toys/pending/awk.c b/toys/pending/awk.c
index 8cffd9e..dfbeb24 100644
--- a/toys/pending/awk.c
+++ b/toys/pending/awk.c
@@ -3290,11 +3290,12 @@ static int next_fp(void)
   } else if (fn) {
     if (!(TT.cfile->fp = fopen(fn, "r"))) FFATAL("can't open %s\n", fn);
     zvalue_copy(&STACK[FILENAME], &TT.rgl.cur_arg);
-    set_num(&STACK[FNR], 0);
   } else {
     TT.rgl.eof = 1;
     return 0;
   }
+  set_num(&STACK[FNR], 0);
+  TT.cfile->recoffs = TT.cfile->endoffs = 0;  // reset record buffer
   return 1;
 }
 
diff --git a/tests/awk.test b/tests/awk.test
index 90148b0..a9ea76d 100644
--- a/tests/awk.test
+++ b/tests/awk.test
@@ -420,6 +420,10 @@ testcmd "awk -v myvar=val -f file1 file" "-v myvar=$2 -f test.awk testfile1.txt
 
 # 2024: New tests -- not in Divya Kothari's original ...
 
+testcmd "nextfile" " '{print NR, FNR, \$0};/ghi jkl/{nextfile}/ghi,jkl/{nextfile}' testfile1.txt testfile2.txt" "1 1 abc def ghi 5\n2 2 ghi jkl mno 10\n3 1 abc,def,ghi,5\n4 2 ghi,jkl,mno,10\n" "" ""
+
+testcmd "getline var numeric string bug fixed 20240514"  "'BEGIN{getline var; print (var < 10.0)}'" "1\n" "" "5.0\n"
+
 testcmd "lshift()" "'BEGIN{print lshift(3,2)}'" "12\n" "" ""
 testcmd "lshift() 64 bit" "'BEGIN{print lshift(1,40)}'" "1099511627776\n" "" ""
 testcmd "rshift()" "'BEGIN{print rshift(12, 1)}'" "6\n" "" ""
-- 
2.43.0

