On 25/10/2020 14:16, Pádraig Brady wrote:
On 25/10/2020 08:39, KOBAYASHI Takashi wrote:
Hello,
I wrote a little patch for nl(1). Could you merge this mainstream?
It reduces the line number with the "--line-increment(-i)" option and
negative value. The "--starting-line-number(-v)" option has been allowed
negative value but "-i" has not until now. It should be unified.
This makes sense for consistency.
I'll also add another commit along the lines off the following,
to only fail when we need to output the overflowed number,
and add some tests around these limits.
I'll apply the attached two patches later.
thanks again,
Pádraig
>From 23926a5276df3e9345b6924fa29f185975bc01ac Mon Sep 17 00:00:00 2001
From: KOBAYASHI Takashi <a141...@aiit.ac.jp>
Date: Sun, 25 Oct 2020 17:09:04 +0000
Subject: [PATCH] nl: support a negative --line-increment
* src/nl.c (main): Allow -i to accept down to INTMAX_MIN.
* tests/misc/nl.sh: Add test cases.
* NEWS: Mention the new feature.
---
NEWS | 2 ++
src/nl.c | 2 +-
tests/misc/nl.sh | 12 ++++++++++++
3 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/NEWS b/NEWS
index 61b711611..7cf15498c 100644
--- a/NEWS
+++ b/NEWS
@@ -36,6 +36,8 @@ GNU coreutils NEWS -*- outline -*-
ls --classify now supports the "always", "auto", or "never" flags,
to support only outputting classifier characters if connected to a tty.
+ nl --line-increment can now take a negative number to decrement the count.
+
** Improvements
stat and tail now know about the "vboxsf" file system type.
diff --git a/src/nl.c b/src/nl.c
index 154131f36..959909f05 100644
--- a/src/nl.c
+++ b/src/nl.c
@@ -521,7 +521,7 @@ main (int argc, char **argv)
0);
break;
case 'i':
- page_incr = xdectoimax (optarg, 1, INTMAX_MAX, "",
+ page_incr = xdectoimax (optarg, INTMAX_MIN, INTMAX_MAX, "",
_("invalid line number increment"), 0);
break;
case 'p':
diff --git a/tests/misc/nl.sh b/tests/misc/nl.sh
index c134a9896..fd9c5326c 100755
--- a/tests/misc/nl.sh
+++ b/tests/misc/nl.sh
@@ -67,4 +67,16 @@ EOF
compare exp out || fail=1
returns_ 1 nl -p -v$INTMAX_MAX in.txt > out || fail=1
+# Test negative iteration
+returns_ 1 nl -i$INTMAX_UFLOW /dev/null || fail=1
+printf '%s\n' a b > in.txt || framework_failure_
+nl -v$INTMAX_MAX -i$INTMAX_MIN in.txt > out || fail=1
+cat <<EOF > exp
+$INTMAX_MAX a
+ -1 b
+EOF
+compare exp out || fail=1
+printf '%s\n' a b c > in.txt || framework_failure_
+returns_ 1 nl -v$INTMAX_MAX -i$INTMAX_MIN in.txt > out || fail=1
+
Exit $fail
--
2.26.2
>From fe41d6c651eb0044e1d152b13f413874ba6bd731 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <p...@draigbrady.com>
Date: Sun, 25 Oct 2020 16:40:35 +0000
Subject: [PATCH] nl: only fail if need to output overflowed numbers
Previously we would have failed immediately upon internal overflow,
which didn't output the full line being processed, and assumed
there would be another numbered line.
* src/nl.c (line_no_overflow): A new global to track overflow.
(print_lineno): Only fail if about to output an overflowed number.
(reset_lineno): A new function to refactor resetting of the number,
and which also clears line_no_overflow.
* tests/misc/nl.sh: Add a test case.
---
src/nl.c | 27 ++++++++++++++++++++-------
tests/misc/nl.sh | 14 +++++++++++++-
2 files changed, 33 insertions(+), 8 deletions(-)
diff --git a/src/nl.c b/src/nl.c
index 8fe91f773..154131f36 100644
--- a/src/nl.c
+++ b/src/nl.c
@@ -143,6 +143,9 @@ static char const *lineno_format = FORMAT_RIGHT_NOLZ;
/* Current print line number. */
static intmax_t line_no;
+/* Whether the current line number has incremented past limits. */
+static bool line_no_overflow;
+
/* True if we have ever read standard input. */
static bool have_read_stdin;
@@ -275,10 +278,23 @@ build_type_arg (char const **typep,
static void
print_lineno (void)
{
+ if (line_no_overflow)
+ die (EXIT_FAILURE, 0, _("line number overflow"));
+
printf (lineno_format, lineno_width, line_no, separator_str);
if (INT_ADD_WRAPV (line_no, page_incr, &line_no))
- die (EXIT_FAILURE, 0, _("line number overflow"));
+ line_no_overflow = true;
+}
+
+static void
+reset_lineno (void)
+{
+ if (reset_numbers)
+ {
+ line_no = starting_line_number;
+ line_no_overflow = false;
+ }
}
/* Switch to a header section. */
@@ -288,8 +304,7 @@ proc_header (void)
{
current_type = header_type;
current_regex = &header_regex;
- if (reset_numbers)
- line_no = starting_line_number;
+ reset_lineno ();
putchar ('\n');
}
@@ -300,8 +315,7 @@ proc_body (void)
{
current_type = body_type;
current_regex = &body_regex;
- if (reset_numbers)
- line_no = starting_line_number;
+ reset_lineno ();
putchar ('\n');
}
@@ -312,8 +326,7 @@ proc_footer (void)
{
current_type = footer_type;
current_regex = &footer_regex;
- if (reset_numbers)
- line_no = starting_line_number;
+ reset_lineno ();
putchar ('\n');
}
diff --git a/tests/misc/nl.sh b/tests/misc/nl.sh
index 3a9919243..c134a9896 100755
--- a/tests/misc/nl.sh
+++ b/tests/misc/nl.sh
@@ -18,7 +18,7 @@
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
print_ver_ nl
-
+getlimits_
echo a | nl > out || fail=1
echo b | nl -s%n >> out || fail=1
@@ -55,4 +55,16 @@ cat <<\EOF > exp
EOF
compare exp out || fail=1
+# Ensure we only indicate overflow when needing to output overflowed numbers
+returns_ 1 nl -v$INTMAX_OFLOW /dev/null || fail=1
+printf '%s\n' a \\:\\: b > in.txt || framework_failure_
+nl -v$INTMAX_MAX in.txt > out || fail=1
+cat <<EOF > exp
+$INTMAX_MAX a
+
+$INTMAX_MAX b
+EOF
+compare exp out || fail=1
+returns_ 1 nl -p -v$INTMAX_MAX in.txt > out || fail=1
+
Exit $fail
--
2.26.2