]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
cut: improve error reporting
authorCojocaru Alexandru <xojoc@gmx.com>
Wed, 5 Dec 2012 13:13:51 +0000 (13:13 +0000)
committerPádraig Brady <P@draigBrady.com>
Thu, 6 Dec 2012 09:37:16 +0000 (09:37 +0000)
* src/cut.c (main): Treat a NUL delimiter (-d '') consistently
with non NUL delimiters, and disallow such a delimiter option,
unless a field is also specified.
(set_fields): Provide a more accurate error message when
a given list is invalid.
* tests/misc/cut.pl: Add a test case.

src/cut.c
tests/misc/cut.pl

index 4219d24e9a6cf42c54757167aa764333b0e93854..dc830721df9c2b39964a8610f573615ed5fc4c63 100644 (file)
--- a/src/cut.c
+++ b/src/cut.c
@@ -365,7 +365,7 @@ set_fields (const char *fieldstr)
           in_digits = false;
           /* Starting a range. */
           if (dash_found)
-            FATAL_ERROR (_("invalid byte or field list"));
+            FATAL_ERROR (_("invalid byte, character or field list"));
           dash_found = true;
           fieldstr++;
 
@@ -491,7 +491,7 @@ set_fields (const char *fieldstr)
           fieldstr++;
         }
       else
-        FATAL_ERROR (_("invalid byte or field list"));
+        FATAL_ERROR (_("invalid byte, character or field list"));
     }
 
   max_range_endpoint = 0;
@@ -846,7 +846,7 @@ main (int argc, char **argv)
   if (operating_mode == undefined_mode)
     FATAL_ERROR (_("you must specify a list of bytes, characters, or fields"));
 
-  if (delim != '\0' && operating_mode != field_mode)
+  if (delim_specified && operating_mode != field_mode)
     FATAL_ERROR (_("an input delimiter may be specified only\
  when operating on fields"));
 
index 27768ff291db6f1d995ba3c0216d7025d435c95a..aff0cbe10e585e2447a172fda54724d4f6070ceb 100755 (executable)
@@ -32,6 +32,8 @@ my $try = "Try '$prog --help' for more information.\n";
 my $from_1 = "$prog: fields and positions are numbered from 1\n$try";
 my $inval = "$prog: invalid byte or field list\n$try";
 my $no_endpoint = "$prog: invalid range with no endpoint: -\n$try";
+my $nofield = "$prog: an input delimiter may be specified only when " .
+              "operating on fields\n$try";
 
 my @Tests =
  (
@@ -118,6 +120,11 @@ my @Tests =
   ['multichar-od', qw(-d: --out=_._), '-f2,3', {IN=>"a:b:c\n"},
    {OUT=>"b_._c\n"}],
 
+  # Ensure delim is not allowed without a field
+  # Prior to 8.21, a NUL delim was allowed without a field
+  ['delim-no-field1', qw(-d ''), '-b1', {EXIT=>1}, {ERR=>$nofield}],
+  ['delim-no-field2', qw(-d:), '-b1', {EXIT=>1}, {ERR=>$nofield}],
+
   # Prior to 1.22i, you couldn't use a delimiter that would sign-extend.
   ['8bit-delim', '-d', "\255", '--out=_', '-f2,3', {IN=>"a\255b\255c\n"},
    {OUT=>"b_c\n"}],