From 6b8ef68111c89c0adbb048ea22ac7d2d6c29ca4e Mon Sep 17 00:00:00 2001 From: Yaroslav Tykhiy Date: Sun, 1 Apr 2007 13:25:03 +0000 Subject: [PATCH] This trivial change should fix at least 3 similar bugs. All of them are related to the `c' function's need to know if we are at the actual end of the address range. (It must print the text not earlier than the whole pattern space was deleted.) It appears the only sed function with this requirement. There is `lastaddr' set by applies(), which is to notify the `c' function, but it can't always help because it's false when we are hitting the end of file early. There is also a bug in applies() due to which `lastaddr' isn't set to true on degenerate ranges such as `$,$' or `N,$' if N appears the last line number. Handling early EOF condition in applies() could look more logical, but it would effectively revert sed to the unreasonable behaviour rev. 1.26 of main.c fought against, as it would require lastline() be called for each line within each address range. So it's better to call lastline() only if needed by the `c' function. Together with this change to sed go regression tests for the bugs fixed (c1-c3). A basic test of `c' (c0) is also added as it helped me to spot my own error. Discussed with: dds Tested by: the regression tests MFC after: 1 week --- tools/regression/usr.bin/sed/regress.c0.out | 4 ++++ tools/regression/usr.bin/sed/regress.c1.out | 4 ++++ tools/regression/usr.bin/sed/regress.c2.out | 3 +++ tools/regression/usr.bin/sed/regress.c3.out | 3 +++ tools/regression/usr.bin/sed/regress.sh | 14 +++++++++++++- usr.bin/sed/process.c | 2 +- 6 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 tools/regression/usr.bin/sed/regress.c0.out create mode 100644 tools/regression/usr.bin/sed/regress.c1.out create mode 100644 tools/regression/usr.bin/sed/regress.c2.out create mode 100644 tools/regression/usr.bin/sed/regress.c3.out diff --git a/tools/regression/usr.bin/sed/regress.c0.out b/tools/regression/usr.bin/sed/regress.c0.out new file mode 100644 index 00000000000..a1f894413ef --- /dev/null +++ b/tools/regression/usr.bin/sed/regress.c0.out @@ -0,0 +1,4 @@ +foo +foo +foo +foo diff --git a/tools/regression/usr.bin/sed/regress.c1.out b/tools/regression/usr.bin/sed/regress.c1.out new file mode 100644 index 00000000000..21af01e3aaa --- /dev/null +++ b/tools/regression/usr.bin/sed/regress.c1.out @@ -0,0 +1,4 @@ +input +data +for validation +foo diff --git a/tools/regression/usr.bin/sed/regress.c2.out b/tools/regression/usr.bin/sed/regress.c2.out new file mode 100644 index 00000000000..6c54a8d978f --- /dev/null +++ b/tools/regression/usr.bin/sed/regress.c2.out @@ -0,0 +1,3 @@ +input +data +foo diff --git a/tools/regression/usr.bin/sed/regress.c3.out b/tools/regression/usr.bin/sed/regress.c3.out new file mode 100644 index 00000000000..6c54a8d978f --- /dev/null +++ b/tools/regression/usr.bin/sed/regress.c3.out @@ -0,0 +1,3 @@ +input +data +foo diff --git a/tools/regression/usr.bin/sed/regress.sh b/tools/regression/usr.bin/sed/regress.sh index 45d8f85e2a4..3e7ba5fcc19 100644 --- a/tools/regression/usr.bin/sed/regress.sh +++ b/tools/regression/usr.bin/sed/regress.sh @@ -2,7 +2,7 @@ REGRESSION_START($1) -echo '1..11' +echo '1..15' REGRESSION_TEST(`G', `sed G < regress.in') REGRESSION_TEST(`P', `sed P < regress.in') @@ -15,5 +15,17 @@ REGRESSION_TEST(`s4', `echo foo | sed s/,*/,/4') REGRESSION_TEST(`s5', `echo foo | sed s/,*/,/5') REGRESSION_TEST(`hanoi', `echo ":abcd: : :" | sed -f hanoi.sed') REGRESSION_TEST(`math', `echo "4+7*3+2^7/3" | sed -f math.sed') +REGRESSION_TEST(`c0', `sed ''`c\ +foo +''`< regress.in') +REGRESSION_TEST(`c1', `sed ''`4,$c\ +foo +''`< regress.in') +REGRESSION_TEST(`c2', `sed ''`3,9c\ +foo +''`< regress.in') +REGRESSION_TEST(`c3', `sed ''`3,/no such string/c\ +foo +''`< regress.in') REGRESSION_END() diff --git a/usr.bin/sed/process.c b/usr.bin/sed/process.c index 83fe325b1e5..ee9334c2451 100644 --- a/usr.bin/sed/process.c +++ b/usr.bin/sed/process.c @@ -128,7 +128,7 @@ redirect: case 'c': pd = 1; psl = 0; - if (cp->a2 == NULL || lastaddr) + if (cp->a2 == NULL || lastaddr || lastline()) (void)fprintf(outfile, "%s", cp->t); break; case 'd':