On 01/05/16 12:36, Pádraig Brady wrote:
On 01/05/16 04:04, KOBAYASHI Takashi wrote:
Hello,

Regarding nl command, I think that behaving like 'p' option is always
enabled even if I do not enable this option.
In other words, the number is not reset for each logical pages in the
default setting.
This behavior is far from a long time ago, but is it a bug?
Version of the testing command is “nl (GNU coreutils) 8.25.5-632ed”.

Example:

$ cat test.txt
\:\:\:
header line 1
\:\:
section 1; body line 1 (total 1)
section 1; body line 2 (total 2)
\:\:
section 2; body line 1 (total 3)
section 2; body line 2 (total 4)
\:\:
section 3; body line 1 (total 5)
section 3; body line 2 (total 6)
\:
footer line 1

$ nl test.txt

      header line 1

    1  section 1; body line 1 (total 1)
    2  section 1; body line 2 (total 2)

    3  section 2; body line 1 (total 3)
    4  section 2; body line 2 (total 4)

    5  section 3; body line 1 (total 5)
    6  section 3; body line 2 (total 6)

      footer line 1

Expected output:

      header line 1

    1  section 1; body line 1 (total 1)
    2  section 1; body line 2 (total 2)

    1  section 2; body line 1 (total 3)
    2  section 2; body line 2 (total 4)

    1  section 3; body line 1 (total 5)
    2  section 3; body line 2 (total 6)

      footer line 1


I made a patch for this issue.

diff --git src/nl.c src/nl.c
index a4a48bc..be1b197 100644
--- src/nl.c
+++ src/nl.c
@@ -286,8 +286,6 @@ proc_header (void)
   {
     current_type = header_type;
     current_regex = &header_regex;
-  if (reset_numbers)
- line_no = starting_line_number;
     putchar ('\n');
   }

@@ -298,6 +296,8 @@ proc_body (void)
   {
     current_type = body_type;
     current_regex = &body_regex;
+  if (reset_numbers)
+ line_no = starting_line_number;
     putchar ('\n');
   }


Best Regards :)

KOBAYASHI, Takashi

Wow that's an old bug!
I agree that -p controls renumbering across page _delimiters_
rather than just pages. POSIX says the same thing.

I'll apply this in your name and add a test.

I adjusted a bit to number for each section.
That's what POSIX states and what FreeBSD does at least.

Proposed patch is attached.

thanks,
Pádraig.

>From 441c655cc8c8662bade5164d1bb07f565637294f Mon Sep 17 00:00:00 2001
From: KOBAYASHI Takashi <[email protected]>
Date: Sun, 1 May 2016 13:47:25 +0100
Subject: [PATCH] nl: reset numbering for each section

* NEWS: Mention the bug fix.
* src/nl.c (proc_body, proc_footer): Reset numbering here too.
* doc/coreutils.texi (nl invocation): Adjust for behavior change.
* tests/misc/nl.sh: Add a test case.
---
 NEWS               |  3 +++
 doc/coreutils.texi |  8 ++++----
 src/nl.c           | 16 ++++++++++------
 tests/misc/nl.sh   | 15 +++++++++++++++
 4 files changed, 32 insertions(+), 10 deletions(-)

diff --git a/NEWS b/NEWS
index 6018aa9..a4abba7 100644
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,9 @@ GNU coreutils NEWS                                    -*- outline -*-
    System V style platforms where this information is available only
    in the global variable 'tzname'. [bug introduced in coreutils-8.24]
 
+   nl now resets numbering for each page section rather than just for each page.
+   [This bug was present in "the beginning".]
+
    stty --help no longer outputs extraneous gettext header lines
    for translated languages. [bug introduced in coreutils-8.24]
 
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 5630201..6a671bb 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -1741,10 +1741,10 @@ nl [@var{option}]@dots{} [@var{file}]@dots{}
 @end example
 
 @cindex logical pages, numbering on
-@command{nl} decomposes its input into (logical) pages; by default, the
-line number is reset to 1 at the top of each logical page.  @command{nl}
-treats all of the input files as a single document; it does not reset
-line numbers or logical pages between files.
+@command{nl} decomposes its input into (logical) page sections;
+by default, the line number is reset to 1 at each logical page section.
+@command{nl} treats all of the input files as a single document;
+it does not reset line numbers or logical pages between files.
 
 @cindex headers, numbering
 @cindex body, numbering
diff --git a/src/nl.c b/src/nl.c
index a4a48bc..b276f09 100644
--- a/src/nl.c
+++ b/src/nl.c
@@ -185,7 +185,7 @@ Write each FILE to standard output, with line numbers added.\n\
 
       fputs (_("\
   -b, --body-numbering=STYLE      use STYLE for numbering body lines\n\
-  -d, --section-delimiter=CC      use CC for separating logical pages\n\
+  -d, --section-delimiter=CC      use CC for logical page delimiters\n\
   -f, --footer-numbering=STYLE    use STYLE for numbering footer lines\n\
 "), stdout);
       fputs (_("\
@@ -193,20 +193,20 @@ Write each FILE to standard output, with line numbers added.\n\
   -i, --line-increment=NUMBER     line number increment at each line\n\
   -l, --join-blank-lines=NUMBER   group of NUMBER empty lines counted as one\n\
   -n, --number-format=FORMAT      insert line numbers according to FORMAT\n\
-  -p, --no-renumber               do not reset line numbers at logical pages\n\
+  -p, --no-renumber               do not reset line numbers for each section\n\
   -s, --number-separator=STRING   add STRING after (possible) line number\n\
 "), stdout);
       fputs (_("\
-  -v, --starting-line-number=NUMBER  first line number on each logical page\n\
+  -v, --starting-line-number=NUMBER  first line number for each section\n\
   -w, --number-width=NUMBER       use NUMBER columns for line numbers\n\
 "), stdout);
       fputs (HELP_OPTION_DESCRIPTION, stdout);
       fputs (VERSION_OPTION_DESCRIPTION, stdout);
       fputs (_("\
 \n\
-By default, selects -v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn.  CC are\n\
-two delimiter characters for separating logical pages, a missing\n\
-second character implies :.  Type \\\\ for \\.  STYLE is one of:\n\
+By default, selects -v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn.\n\
+CC are two delimiter characters used to construct logical page delimiters,\n\
+a missing second character implies :.  Type \\\\ for \\.  STYLE is one of:\n\
 "), stdout);
       fputs (_("\
 \n\
@@ -298,6 +298,8 @@ proc_body (void)
 {
   current_type = body_type;
   current_regex = &body_regex;
+  if (reset_numbers)
+    line_no = starting_line_number;
   putchar ('\n');
 }
 
@@ -308,6 +310,8 @@ proc_footer (void)
 {
   current_type = footer_type;
   current_regex = &footer_regex;
+  if (reset_numbers)
+    line_no = starting_line_number;
   putchar ('\n');
 }
 
diff --git a/tests/misc/nl.sh b/tests/misc/nl.sh
index e111d7a..826894a 100755
--- a/tests/misc/nl.sh
+++ b/tests/misc/nl.sh
@@ -37,7 +37,22 @@ cat <<\EOF > exp
      1^Ia$
        $
 EOF
+compare exp out || fail=1
 
+# Ensure numbering reset at each delimiter.
+# coreutils <= v8.25 only reset at a page header.
+printf '%s\n' '\:\:\:' a '\:\:' b '\:' c > in.txt || framework_failure_
+nl -ha -fa in.txt > out.tmp || fail=1
+nl -p -ha -fa in.txt >> out.tmp || fail=1
+sed '/^$/d' < out.tmp > out || framework_failure_
+cat <<\EOF > exp
+     1	a
+     1	b
+     1	c
+     1	a
+     2	b
+     3	c
+EOF
 compare exp out || fail=1
 
 Exit $fail
-- 
2.5.5

Reply via email to