bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: fmt gobbles leading whitespace in prefix mode


From: Jim Meyering
Subject: Re: fmt gobbles leading whitespace in prefix mode
Date: Sat, 06 Jan 2007 09:57:10 +0100

"G.P. Halkes" <address@hidden> wrote:
> it appears there is another bug in fmt regarding prefixes: lines that contain
> only a prefix of the prefix and whitespace will be removed. This problem does
> not occur after applying the second of the two patches I sent in my previous
> post. However, that particular patch will introduce an extra newline at the
> end of the file, if the last line contains a prefix of the prefix followed by
> the end of file.
>
> Therefore, attached to this post are revised patches (assuming you haven't
> yet applied the patches for the previous post) that will solve these issues.
>
> Example:
> --------------------------
> $ cat test.txt
> foo bar
> bl
>         blah comment here
> $ fmt -p blah test.txt
> foo bar
>
>         blah comment here
> --------------------------

Thank you for the fine patch!
I've applied it locally.  It will propagate to the
savannah cvs and git mirrors later today or tomorrow.

Your change is just small enough not to require copyright paperwork.


 ChangeLog       |    8 ++++++++
 src/fmt.c       |   11 ++++++-----
 tests/fmt/basic |   20 ++++----------------
 3 files changed, 18 insertions(+), 21 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 9c4eb97..7fcccc1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2007-01-06  G.P. Halkes  <address@hidden>
+
+       * src/fmt.c (copy_rest): Correct prefix handling.
+       Don't elide a line with the prefix followed by only white space.
+       (get_line): Move EOF-check to loop-termination condition.
+       * tests/fmt/basic (pfx-1): Adjust test to expect desired result.
+       (pfx-2): Remove test; its premise was contrary to the documentation.
+
 2007-01-05  Jim Meyering  <address@hidden>

        Avoid a used-uninitialized bug for invalid input, i.e., when the size
diff --git a/src/fmt.c b/src/fmt.c
index 9a6c1c0..5ccc8c4 100644
--- a/src/fmt.c
+++ b/src/fmt.c
@@ -607,12 +607,15 @@ copy_rest (FILE *f, int c)
   const char *s;

   out_column = 0;
-  if (in_column > next_prefix_indent && c != '\n' && c != EOF)
+  if (in_column > next_prefix_indent || (c != '\n' && c != EOF))
     {
       put_space (next_prefix_indent);
       for (s = prefix; out_column != in_column && *s; out_column++)
        putchar (*s++);
-      put_space (in_column - out_column);
+      if (c != EOF && c != '\n')
+       put_space (in_column - out_column);
+      if (c == EOF && in_column >= next_prefix_indent + prefix_length)
+       putchar ('\n');
     }
   while (c != '\n' && c != EOF)
     {
@@ -688,10 +691,8 @@ get_line (FILE *f, int c)
          flush_paragraph ();
        }
       word_limit++;
-      if (c == EOF)
-       return EOF;
     }
-  while (c != '\n');
+  while (c != '\n' && c != EOF);
   return get_prefix (f);
 }

diff --git a/tests/fmt/basic b/tests/fmt/basic
index 77682f1..04938b8 100755
--- a/tests/fmt/basic
+++ b/tests/fmt/basic
@@ -2,7 +2,7 @@
 # -*- perl -*-
 # Basic tests for "fmt".

-# Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Free Software
+# Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
 # Foundation, Inc.

 # This program is free software; you can redistribute it and/or modify
@@ -60,23 +60,11 @@ my @Tests =
        . " is the first\noption; use -w N instead\n"
        . "Try `fmt --help' for more information.\n" }, {EXIT => 1}],

-     # With --prefix=P, Do not remove leading on lines without the prefix.
+     # With --prefix=P, do not remove leading space on lines without the 
prefix.
      ['pfx-1', qw (-p '>'),
       {IN=>  " 1\n  2\n\t3\n\t\t4\n> quoted\n> text\n"},
-      # This is the buggy output (leading white space removed),
-      # from coreutils-5.93.
-      {OUT=> "1\n2\n3\n4\n> quoted text\n"}],
-      # FIXME: this is the desired output
-      # {OUT=> " 1\n  2\n\t3\n\t\t4\n> quoted text\n"}],
-
-     # Like the above, but when two adjacent, non-prefixed lines have
-     # the same indentation, ensure that they are formatted.
-     ['pfx-2', qw (-p '>'),
-      {IN=>  " 1\n 2\n\t3\n\t4\n"},
-      {OUT=> "1\n2\n3\n4\n"}],
-      # FIXME: this is the desired output
-      # {OUT=> " 1 2\n\t3 4\n"}],
-    );
+      {OUT=> " 1\n  2\n\t3\n\t\t4\n> quoted text\n"}],
+);

 my $save_temps = $ENV{DEBUG};
 my $verbose = $ENV{VERBOSE};
--
1.4.4.3.g5485




reply via email to

[Prev in Thread] Current Thread [Next in Thread]