]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
seq: give better diagnostics for invalid formats.
authorSteven Schubiger <schubiger@gmail.com>
Mon, 18 Feb 2008 21:39:22 +0000 (22:39 +0100)
committerJim Meyering <meyering@redhat.com>
Tue, 19 Feb 2008 14:41:16 +0000 (15:41 +0100)
* src/seq.c: (validate_format): New function.
(main): Use it.
* tests/misc/seq (fmt-d, fmt-e): Test for expected diagnostics with
invalid formats.
* NEWS: Mention this change.
* TODO: Remove this item.
[jm: src/seq.c: make diagnostics more consistent
 tests/misc/seq (fmt-eos1): adjust the expected diagnostic ]

NEWS
TODO
src/seq.c
tests/misc/seq

diff --git a/NEWS b/NEWS
index daa5633b675a5a4b7d0097104907e52adc06f557..233e514d3e9799a0aab04e7cfe31b84b72eff9bc 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -25,6 +25,8 @@ GNU coreutils NEWS                                    -*- outline -*-
 
   ls --color no longer outputs unnecessary escape sequences
 
+  seq gives better diagnostics for invalid formats.
+
 ** Consistency
 
   mkdir and split now write --verbose output to stdout, not stderr.
diff --git a/TODO b/TODO
index 8c6b6fc1b377c3ea888a68393720a9dfb6e4bcaa..3f4d26c625a3ec2fcfb6ceb2fdece6f6798c7894 100644 (file)
--- a/TODO
+++ b/TODO
@@ -61,8 +61,6 @@ printf: consider adapting builtins/printf.def from bash
 
 df: add `--total' option, suggested here http://bugs.debian.org/186007
 
-seq: give better diagnostics for invalid formats:
-   e.g. no or too many % directives
 seq: consider allowing format string to contain no %-directives
 
 tail: don't use xlseek; it *exits*.
index b073fd1693af73cdb6820e66a85f22d1e7a5a9be..c7e8cb971d9a3328134397e856fa3d0fe34e78ac 100644 (file)
--- a/src/seq.c
+++ b/src/seq.c
@@ -177,6 +177,33 @@ scan_arg (const char *arg)
   return ret;
 }
 
+/* Validate the format, FMT.  Print a diagnostic and exit
+   if there is not exactly one %-directive.  */
+
+static void
+validate_format (char const *fmt)
+{
+  unsigned int n_directives = 0;
+  char const *p;
+
+  for (p = fmt; *p; p++)
+    {
+      if (p[0] == '%' && p[1] != '%' && p[1] != '\0')
+       {
+         ++n_directives;
+         ++p;
+       }
+    }
+  if (! n_directives)
+    {
+      error (0, 0, _("no %% directive in format string %s"), quote (fmt));
+      usage (EXIT_FAILURE);
+    }
+  else if (1 < n_directives)
+    error (EXIT_FAILURE, 0, _("too many %% directives in format string %s"),
+          quote (fmt));
+}
+
 /* If FORMAT is a valid printf format for a double argument, return
    its long double equivalent, possibly allocated from dynamic
    storage, and store into *LAYOUT a description of the output layout;
@@ -405,6 +432,7 @@ main (int argc, char **argv)
 
   if (format_str)
     {
+      validate_format (format_str);
       char const *f = long_double_format (format_str, &layout);
       if (! f)
        {
index 1a153a310a5035068adb3ec921dfc12c468c668d..f48289bdf0763fabe7bc49317f350e8531e749c3 100755 (executable)
@@ -87,10 +87,14 @@ my @Tests =
    # "seq: memory exhausted".  In coreutils-6.0..6.8, it would mistakenly
    # succeed and print a blank line.
    ['fmt-eos1', qw(-f % 1), {EXIT => 1},
-    {ERR => "seq: invalid format string: `%'\n" . $try_help }],
+    {ERR => "seq: no % directive in format string `%'\n" . $try_help }],
    ['fmt-eos2', qw(-f %g% 1), {EXIT => 1},
     {ERR => "seq: invalid format string: `%g%'\n" . $try_help }],
 
+   ['fmt-d',   qw(-f "" 1), {EXIT => 1},
+    {ERR => "seq: no % directive in format string `'\n" . $try_help }],
+   ['fmt-e',   qw(-f %g%g 1), {EXIT => 1},
+    {ERR => "seq: too many % directives in format string `%g%g'\n"}],
   );
 
 # Append a newline to each entry in the OUT array.