awk: fix FS assignment behavior. Closes 5108
authorDenys Vlasenko <vda.linux@googlemail.com>
Tue, 10 Jul 2012 23:27:15 +0000 (01:27 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Tue, 10 Jul 2012 23:27:15 +0000 (01:27 +0200)
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
editors/awk.c
testsuite/awk.tests

index d69720d6473fea753db54ff3d37debee6b742449..42f6ef866e90f40f30394ef7f1da45cf6b7a7d05 100644 (file)
@@ -1812,6 +1812,18 @@ static void handle_special(var *v)
                is_f0_split = FALSE;
 
        } else if (v == intvar[FS]) {
+               /*
+                * The POSIX-2008 standard says that changing FS should have no effect on the
+                * current input line, but only on the next one. The language is:
+                *
+                * > Before the first reference to a field in the record is evaluated, the record
+                * > shall be split into fields, according to the rules in Regular Expressions,
+                * > using the value of FS that was current at the time the record was read.
+                *
+                * So, split up current line before assignment to FS:
+                */
+               split_f0();
+
                mk_splitter(getvar_s(v), &fsplitter);
 
        } else if (v == intvar[RS]) {
index d4c390d3120a4f3c59151673d4a16d75a6247237..f9c3b6b4d07299d115514134fd7681e16bcb456a 100755 (executable)
@@ -217,4 +217,11 @@ end d
 testing "awk handles empty ()" \
        "awk 'BEGIN {print()}' 2>&1" "awk: cmd. line:1: Empty sequence\n" "" ""
 
+testing "awk FS assignment" "awk '{FS=\":\"; print \$1}'" \
+       "a:b\ne\n" \
+       "" \
+       "a:b c:d\ne:f g:h"
+
+# testing "description" "command" "result" "infile" "stdin"
+
 exit $FAILCOUNT