NEWS - check tests/fmt/g-option - check (I also moved two other fmt tests) doc/coreutils.texi - check Oh, and src/fmt.c, too.
BSD's implementation allows a goal specification and has a different default goal width computation anyway. This change adds a "-g" option and allows the first two numeric operands to represent the goal width and actual line width. * src/fmt.c (main): implement the new option (check_for_goals): new function to implement the operands Based on BSD's and Plan-9's fmt programs. --- NEWS | 4 +++ THANKS.in | 3 ++ doc/coreutils.texi | 12 ++++++-- src/fmt.c | 39 ++++++++++++++++++++++++--- tests/Makefile.am | 5 ++- tests/fmt/base | 64 ++++++++++++++++++++++++++++++++++++++++++++++ tests/fmt/g-option | 58 +++++++++++++++++++++++++++++++++++++++++ tests/fmt/long-line | 61 +++++++++++++++++++++++++++++++++++++++++++ tests/misc/fmt | 64 ---------------------------------------------- tests/misc/fmt-long-line | 61 ------------------------------------------- 10 files changed, 236 insertions(+), 135 deletions(-) create mode 100755 tests/fmt/base create mode 100755 tests/fmt/g-option create mode 100755 tests/fmt/long-line delete mode 100755 tests/misc/fmt delete mode 100755 tests/misc/fmt-long-line diff --git a/NEWS b/NEWS index c8d2bbc..0442ca8 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,10 @@ GNU coreutils NEWS -*- outline -*- * Noteworthy changes in release ?.? (????-??-??) [?] +** New features + + fmt now supports the -g option for specifying the "goal" width. + * Noteworthy changes in release 8.16 (2012-03-26) [stable] diff --git a/THANKS.in b/THANKS.in index d23f7b3..9a525c4 100644 --- a/THANKS.in +++ b/THANKS.in @@ -11,6 +11,8 @@ note to the bug-report mailing list (as seen at end of e.g., cp --help). ## is used to generate the THANKS file. Note that numerous people listed ## here would have been listed as commit authors if we had been using git ## for version control when they contributed. +## +## Well, not completely true. It misses sometimes. ??? [email protected] A Costa [email protected] @@ -97,6 +99,7 @@ Brian M. Carlson [email protected] Brian Silverman [email protected] Brian Youmans [email protected] Britton Leo Kerin [email protected] +Bruce Korb [email protected] Bruce Robertson [email protected] Carl Johnson [email protected] Carl Lowenstein [email protected] diff --git a/doc/coreutils.texi b/doc/coreutils.texi index 510abb9..bcc054a 100644 --- a/doc/coreutils.texi +++ b/doc/coreutils.texi @@ -2203,9 +2203,15 @@ between sentences to two spaces. @opindex -@var{width} @opindex -w @opindex --width -Fill output lines up to @var{width} characters (default 75). @command{fmt} -initially tries to make lines about 7% shorter than this, to give it -room to balance line lengths. +Fill output lines up to @var{width} characters (default 75 or @var{goal} plus 10, +if @var{goal} is provided). + +@itemx -g @var{goal} +@itemx --goal=@var{goal} +@opindex -g +@opindex --goal +@command{fmt} initially tries to make lines @var{goal} characters wide. +By default, this is 7% shorter than @var{width}. @item -p @var{prefix} @itemx --prefix=@var{prefix} diff --git a/src/fmt.c b/src/fmt.c index 89d13a6..fc2a7b5 100644 --- a/src/fmt.c +++ b/src/fmt.c @@ -68,7 +68,7 @@ typedef long int COST; #define SQR(n) ((n) * (n)) #define EQUIV(n) SQR ((COST) (n)) -/* Cost of a filled line n chars longer or shorter than best_width. */ +/* Cost of a filled line n chars longer or shorter than goal_width. */ #define SHORT_COST(n) EQUIV ((n) * 10) /* Cost of the difference between adjacent filled lines. */ @@ -201,7 +201,7 @@ static int prefix_lead_space; static int prefix_length; /* The preferred width of text lines, set to LEEWAY % less than max_width. */ -static int best_width; +static int goal_width; /* Dynamic variables. */ @@ -286,6 +286,7 @@ Mandatory arguments to long options are mandatory for short options too.\n\ -t, --tagged-paragraph indentation of first line different from second\n\ -u, --uniform-spacing one space between words, two after sentences\n\ -w, --width=WIDTH maximum line width (default of 75 columns)\n\ + -g, --goal=WIDTH goal width (default of 93% of width)\n\ "), stdout); fputs (HELP_OPTION_DESCRIPTION, stdout); fputs (VERSION_OPTION_DESCRIPTION, stdout); @@ -308,6 +309,7 @@ static struct option const long_options[] = {"tagged-paragraph", no_argument, NULL, 't'}, {"uniform-spacing", no_argument, NULL, 'u'}, {"width", required_argument, NULL, 'w'}, + {"goal", required_argument, NULL, 'g'}, {GETOPT_HELP_OPTION_DECL}, {GETOPT_VERSION_OPTION_DECL}, {NULL, 0, NULL, 0}, @@ -319,6 +321,7 @@ main (int argc, char **argv) int optchar; bool ok = true; char const *max_width_option = NULL; + char const *goal_width_option = NULL; initialize_main (&argc, &argv); set_program_name (argv[0]); @@ -344,7 +347,7 @@ main (int argc, char **argv) argc--; } - while ((optchar = getopt_long (argc, argv, "0123456789cstuw:p:", + while ((optchar = getopt_long (argc, argv, "0123456789cstuw:p:g:", long_options, NULL)) != -1) switch (optchar) @@ -376,6 +379,10 @@ main (int argc, char **argv) max_width_option = optarg; break; + case 'g': + goal_width_option = optarg; + break; + case 'p': set_prefix (optarg); break; @@ -398,7 +405,22 @@ main (int argc, char **argv) max_width = tmp; } - best_width = max_width * (2 * (100 - LEEWAY) + 1) / 200; + if (goal_width_option) + { + /* Limit goal_width to max_width. */ + unsigned long int tmp; + if (! (xstrtoul (goal_width_option, NULL, 10, &tmp, "") == LONGINT_OK + && tmp <= max_width)) + error (EXIT_FAILURE, 0, _("invalid width: %s"), + quote (goal_width_option)); + goal_width = tmp; + if (max_width_option == NULL) + max_width = goal_width + 10; + } + else + { + goal_width = max_width * (2 * (100 - LEEWAY) + 1) / 200; + } if (optind == argc) fmt (stdin); @@ -924,7 +946,7 @@ line_cost (WORD *next, int len) if (next == word_limit) return 0; - n = best_width - len; + n = goal_width - len; cost = SHORT_COST (n); if (next->next_break != word_limit) { @@ -1010,3 +1032,10 @@ put_space (int space) out_column++; } } +/* + * Local Variables: + * mode: C + * c-file-style: "gnu" + * indent-tabs-mode: nil + * End: + * end of fmt.c */ diff --git a/tests/Makefile.am b/tests/Makefile.am index 011051a..47c29bf 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -140,6 +140,9 @@ TESTS = \ chgrp/no-x \ chgrp/posix-H \ chgrp/recurse \ + fmt/base \ + fmt/long-line \ + fmt/g-option \ misc/env \ misc/ptx \ misc/test \ @@ -188,8 +191,6 @@ TESTS = \ misc/expr \ misc/factor \ misc/false-status \ - misc/fmt \ - misc/fmt-long-line \ misc/fold \ misc/groups-dash \ misc/groups-version \ diff --git a/tests/fmt/base b/tests/fmt/base new file mode 100755 index 0000000..e94d613 --- /dev/null +++ b/tests/fmt/base @@ -0,0 +1,64 @@ +#!/usr/bin/perl +# Basic tests for "fmt". + +# Copyright (C) 2001-2012 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +use strict; + +(my $program_name = $0) =~ s|.*/||; + +my @Tests = + ( + ['8-bit-pfx', qw (-p 'ç'), + {IN=> "ça\nçb\n"}, + {OUT=>"ça b\n"}], + ['wide-1', '-w 32768', + {ERR => "fmt: invalid width: '32768'\n"}, {EXIT => 1}], + ['wide-2', '-w 2147483647', + {ERR => "fmt: invalid width: '2147483647'\n"}, {EXIT => 1}], + ['bad-suffix', '-72x', {IN=> ''}, + {ERR => "fmt: invalid width: '72x'\n"}, {EXIT => 1}], + ['no-file', 'no-such-file', + {ERR => "fmt: cannot open 'no-such-file' for reading:" + . " No such file or directory\n"}, {EXIT => 1}], + ['obs-1', '-c -72', + {ERR => "fmt: invalid option -- 7; -WIDTH is recognized only when it" + . " 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 space on lines without the prefix. + ['pfx-1', qw (-p '>'), + {IN=> " 1\n 2\n\t3\n\t\t4\n> quoted\n> text\n"}, + {OUT=> " 1\n 2\n\t3\n\t\t4\n> quoted text\n"}], + + # Don't remove prefix from a prefix-only line. + ['pfx-only', qw (-p '>'), + {IN=> ">\n"}, + {OUT=> ">\n"}], + + # With a multi-byte prefix, say, "foo", don't empty a line that + # starts with a strict prefix (e.g. "fo") of that prefix. + # With fmt from coreutils-6.7, it would mistakenly output an empty line. + ['pfx-of-pfx', qw (-p 'foo'), + {IN=> "fo\n"}, + {OUT=> "fo\n"}], +); + +my $save_temps = $ENV{DEBUG}; +my $verbose = $ENV{VERBOSE}; +my $prog = 'fmt'; +my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose); +exit $fail; diff --git a/tests/fmt/g-option b/tests/fmt/g-option new file mode 100644 index 0000000..e9a9a33 --- /dev/null +++ b/tests/fmt/g-option @@ -0,0 +1,58 @@ +#!/bin/sh +# Exercise the fmt -g option. + +# Copyright (C) 2012 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +. "${srcdir=.}/init.sh"; path_prepend_ ../src +print_ver_ fmt + +fmt -g 60 -w 72 <<\_EOF_ > base + +@command{fmt} prefers breaking lines at the end of a sentence, and tries to +avoid line breaks after the first word of a sentence or before the last word +of a sentence. A @dfn{sentence break} is defined as either the end of a +paragraph or a word ending in any of @samp{.?!}, followed by two spaces or end +of line, ignoring any intervening parentheses or quotes. Like @TeX{}, +@command{fmt} reads entire ``paragraphs'' before choosing line breaks; the +algorithm is a variant of that given by +Donald E. Knuth and Michael F. Plass +in ``Breaking Paragraphs Into Lines'', +@cite{Software---Practice & Experience} +@b{11}, 11 (November 1981), 1119--1184. +_EOF_ + +fmt -g 60 -w 72 base > out + +cat <<\_EOF_ > exp + +@command{fmt} prefers breaking lines at the end of a sentence, +and tries to avoid line breaks after the first word of a sentence +or before the last word of a sentence. A @dfn{sentence break} +is defined as either the end of a paragraph or a word ending +in any of @samp{.?!}, followed by two spaces or end of line, +ignoring any intervening parentheses or quotes. Like @TeX{}, +@command{fmt} reads entire ``paragraphs'' before choosing line +breaks; the algorithm is a variant of that given by Donald +E. Knuth and Michael F. Plass in ``Breaking Paragraphs Into +Lines'', @cite{Software---Practice & Experience} @b{11}, 11 +(November 1981), 1119--1184. +_EOF_ + +touch exp + +compare exp out || fail=1 + +Exit $fail diff --git a/tests/fmt/long-line b/tests/fmt/long-line new file mode 100755 index 0000000..04674e2 --- /dev/null +++ b/tests/fmt/long-line @@ -0,0 +1,61 @@ +#!/bin/sh +# make sure fmt -s works even on long lines + +# Copyright (C) 2002-2012 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +. "${srcdir=.}/init.sh"; path_prepend_ ../src +print_ver_ fmt + +printf '%2030s\n' ' '|sed 's/../ y/g' > in || framework_failure_ + +cat <<\EOF > exp || framework_failure_ + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y +EOF + + +fmt -s in > out || fail=1 + +compare exp out || fail=1 + +Exit $fail diff --git a/tests/misc/fmt b/tests/misc/fmt deleted file mode 100755 index e94d613..0000000 --- a/tests/misc/fmt +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/perl -# Basic tests for "fmt". - -# Copyright (C) 2001-2012 Free Software Foundation, Inc. - -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. - -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - -use strict; - -(my $program_name = $0) =~ s|.*/||; - -my @Tests = - ( - ['8-bit-pfx', qw (-p 'ç'), - {IN=> "ça\nçb\n"}, - {OUT=>"ça b\n"}], - ['wide-1', '-w 32768', - {ERR => "fmt: invalid width: '32768'\n"}, {EXIT => 1}], - ['wide-2', '-w 2147483647', - {ERR => "fmt: invalid width: '2147483647'\n"}, {EXIT => 1}], - ['bad-suffix', '-72x', {IN=> ''}, - {ERR => "fmt: invalid width: '72x'\n"}, {EXIT => 1}], - ['no-file', 'no-such-file', - {ERR => "fmt: cannot open 'no-such-file' for reading:" - . " No such file or directory\n"}, {EXIT => 1}], - ['obs-1', '-c -72', - {ERR => "fmt: invalid option -- 7; -WIDTH is recognized only when it" - . " 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 space on lines without the prefix. - ['pfx-1', qw (-p '>'), - {IN=> " 1\n 2\n\t3\n\t\t4\n> quoted\n> text\n"}, - {OUT=> " 1\n 2\n\t3\n\t\t4\n> quoted text\n"}], - - # Don't remove prefix from a prefix-only line. - ['pfx-only', qw (-p '>'), - {IN=> ">\n"}, - {OUT=> ">\n"}], - - # With a multi-byte prefix, say, "foo", don't empty a line that - # starts with a strict prefix (e.g. "fo") of that prefix. - # With fmt from coreutils-6.7, it would mistakenly output an empty line. - ['pfx-of-pfx', qw (-p 'foo'), - {IN=> "fo\n"}, - {OUT=> "fo\n"}], -); - -my $save_temps = $ENV{DEBUG}; -my $verbose = $ENV{VERBOSE}; -my $prog = 'fmt'; -my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose); -exit $fail; diff --git a/tests/misc/fmt-long-line b/tests/misc/fmt-long-line deleted file mode 100755 index 04674e2..0000000 --- a/tests/misc/fmt-long-line +++ /dev/null @@ -1,61 +0,0 @@ -#!/bin/sh -# make sure fmt -s works even on long lines - -# Copyright (C) 2002-2012 Free Software Foundation, Inc. - -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. - -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - -. "${srcdir=.}/init.sh"; path_prepend_ ../src -print_ver_ fmt - -printf '%2030s\n' ' '|sed 's/../ y/g' > in || framework_failure_ - -cat <<\EOF > exp || framework_failure_ - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y -EOF - - -fmt -s in > out || fail=1 - -compare exp out || fail=1 - -Exit $fail -- 1.7.7
