3 # Copyright (C) 2015 Red Hat, Inc.
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library. If not, see
17 # <http://www.gnu.org/licenses/>.
19 # This script is intended to be passed a list of .args files, used
20 # to store command line ARGV for the test suites. It will reformat
21 # them such that there is at most one '-param value' on each line
22 # of the file. Parameter values that are longer than 80 chars will
25 # If --in-place is supplied as the first parameter of this script,
26 # the files will be changed in place.
27 # If --check is the first parameter, the script will return
28 # a non-zero value if a file is not wrapped correctly.
29 # Otherwise the rewrapped files are printed to the standard output.
34 if (@ARGV[0] eq "--in-place" or @ARGV[0] eq "-i") {
37 } elsif (@ARGV[0] eq "--check") {
43 foreach my $file (@ARGV) {
44 if (&rewrap
($file) < 0) {
54 # Read the original file
55 open FILE
, "<", $file or die "cannot read $file: $!";
56 my @orig_lines = <FILE
>;
58 my @lines = @orig_lines;
60 # If there is a trailing '\' then kill the new line
70 # Kill the last new line in the file
71 chomp @lines[$#lines];
73 # Reconstruct the master data by joining all lines
74 # and then split again based on the real desired
76 @lines = split /\n/, join('', @lines);
78 # Now each @lines represents a single command, we
80 @lines = map { &rewrap_line
($_) } @lines;
83 open FILE
, ">", $file or die "cannot write $file: $!";
84 foreach my $line (@lines) {
89 my $nl = join('', @lines);
90 my $ol = join('', @orig_lines);
92 open DIFF
, "| diff -u $file -" or die "cannot run diff: $!";
96 print STDERR
"Incorrect line wrapping in $file\n";
97 print STDERR
"Use test-wrap-argv.pl to wrap test data files\n";
101 foreach my $line (@lines) {
110 my @bits = split / /, join('', $line);
112 # @bits contains env vars, then the command line
113 # and then the arguments
118 if ($bits[0] !~ /=/) {
122 foreach my $bit (@bits) {
123 # If no command is defined yet, we must still
126 # Look for leading / to indicate command name
133 # If there's a leading '-' then this is a new
134 # parameter, otherwise its a value for the prev
139 $args[$#args] .= " " . $bit;
144 # We might have to split line argument values...
145 @args = map { &rewrap_arg
($_) } @args;
146 # Print env + command first
147 return join(" \\\n", @env, $cmd, @args), "\n";
155 while (length($arg) > $max_len) {
156 my $split = rindex $arg, ",", $max_len;
158 $split = rindex $arg, ":", $max_len;
161 $split = rindex $arg, " ", $max_len;
164 warn "cannot find nice place to split '$arg' below 80 chars\n";
165 $split = $max_len - 1;
169 push @ret, substr $arg, 0, $split;
170 $arg = substr $arg, $split;
173 return join("\\\n", @ret);