2016-07-08 21:47:04 +03:00
#!/usr/bin/env perl
2015-11-06 16:20:06 +03:00
#
# Copyright (C) 2015 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see
# <http://www.gnu.org/licenses/>.
#
# This script is intended to be passed a list of .args files, used
# to store command line ARGV for the test suites. It will reformat
# them such that there is at most one '-param value' on each line
# of the file. Parameter values that are longer than 80 chars will
# also be split.
#
2016-05-31 11:19:04 +03:00
# If --in-place is supplied as the first parameter of this script,
# the files will be changed in place.
2016-06-15 15:18:17 +03:00
# If --check is the first parameter, the script will return
# a non-zero value if a file is not wrapped correctly.
2016-05-31 11:19:04 +03:00
# Otherwise the rewrapped files are printed to the standard output.
2015-11-06 16:20:06 +03:00
2016-05-31 11:19:04 +03:00
$ in_place = 0 ;
2016-06-15 15:18:17 +03:00
$ check = 0 ;
2016-05-31 11:19:04 +03:00
if ( @ ARGV [ 0 ] eq "--in-place" ) {
$ in_place = 1 ;
shift @ ARGV ;
2016-06-15 15:18:17 +03:00
} elsif ( @ ARGV [ 0 ] eq "--check" ) {
$ check = 1 ;
shift @ ARGV ;
2016-05-31 11:19:04 +03:00
}
2015-11-06 16:20:06 +03:00
foreach my $ file ( @ ARGV ) {
2016-06-15 15:18:17 +03:00
$ ret = 0 ;
if ( & rewrap ( $ file ) < 0 ) {
$ ret = 1 ;
}
2015-11-06 16:20:06 +03:00
}
2016-06-15 15:18:17 +03:00
exit $ ret ;
2015-11-06 16:20:06 +03:00
sub rewrap {
my $ file = shift ;
# Read the original file
open FILE , "<" , $ file or die "cannot read $file: $!" ;
2016-06-15 14:04:52 +03:00
my @ orig_lines = <FILE> ;
close FILE ;
my @ lines = @ orig_lines ;
foreach ( @ lines ) {
2015-11-06 16:20:06 +03:00
# If there is a trailing '\' then kill the new line
if ( /\\$/ ) {
chomp ;
$ _ =~ s/\\$// ;
}
}
# Skip empty files
return unless @ lines ;
# Kill the last new line in the file
chomp @ lines [ $# lines ] ;
# Reconstruct the master data by joining all lines
# and then split again based on the real desired
# newlines
@ lines = split /\n/ , join ( '' , @ lines ) ;
# Now each @lines represents a single command, we
# can process them
2016-05-31 11:19:04 +03:00
@ lines = map { & rewrap_line ( $ _ ) } @ lines ;
2016-05-30 18:20:23 +03:00
2016-05-31 11:19:04 +03:00
if ( $ in_place ) {
open FILE , ">" , $ file or die "cannot write $file: $!" ;
foreach my $ line ( @ lines ) {
print FILE $ line ;
}
close FILE ;
2016-06-15 15:18:17 +03:00
} elsif ( $ check ) {
my $ nl = join ( '' , @ lines ) ;
my $ ol = join ( '' , @ orig_lines ) ;
unless ( $ nl eq $ ol ) {
2016-07-11 17:15:02 +03:00
open DIFF , "| diff -u $file -" or die "cannot run diff: $!" ;
print DIFF $ nl ;
close DIFF ;
2016-06-15 15:18:17 +03:00
print STDERR "Incorrect line wrapping in $file\n" ;
print STDERR "Use test-wrap-argv.pl to wrap test data files\n" ;
return - 1 ;
}
2016-05-31 11:19:04 +03:00
} else {
foreach my $ line ( @ lines ) {
print $ line ;
}
}
2016-06-15 15:18:17 +03:00
return 0 ;
2016-05-30 18:20:23 +03:00
}
2015-11-06 16:20:06 +03:00
2016-05-30 18:20:23 +03:00
sub rewrap_line {
my $ line = shift ;
my @ bits = split / / , join ( '' , $ line ) ;
2015-11-06 16:20:06 +03:00
2016-05-30 18:20:23 +03:00
# @bits contains env vars, then the command line
# and then the arguments
my @ env ;
my $ cmd ;
my @ args ;
2015-11-06 16:20:06 +03:00
2016-05-30 18:20:23 +03:00
if ( $ bits [ 0 ] !~ /=/ ) {
$ cmd = shift @ bits ;
}
foreach my $ bit ( @ bits ) {
# If no command is defined yet, we must still
# have env vars
if ( ! defined $ cmd ) {
# Look for leading / to indicate command name
if ( $ bit =~ m , ^ / , ) {
$ cmd = $ bit ;
} else {
push @ env , $ bit ;
}
} else {
# If there's a leading '-' then this is a new
# parameter, otherwise its a value for the prev
# parameter.
if ( $ bit =~ m , ^ - , ) {
push @ args , $ bit ;
2015-11-06 16:20:06 +03:00
} else {
2016-05-30 18:20:23 +03:00
$ args [ $# args ] . = " " . $ bit ;
2015-11-06 16:20:06 +03:00
}
}
2016-05-30 18:20:23 +03:00
}
2015-11-06 16:20:06 +03:00
2016-05-30 18:20:23 +03:00
# We might have to split line argument values...
2016-05-30 19:59:42 +03:00
@ args = map { & rewrap_arg ( $ _ ) } @ args ;
# Print env + command first
2016-05-30 20:04:32 +03:00
return join ( " \\\n" , @ env , $ cmd , @ args ) , "\n" ;
2015-11-06 16:20:06 +03:00
}
2016-05-30 18:44:58 +03:00
sub rewrap_arg {
my $ arg = shift ;
2016-05-30 18:58:27 +03:00
my @ ret ;
2016-07-11 17:27:53 +03:00
my $ max_len = 78 ;
2016-05-30 18:44:58 +03:00
2016-07-11 17:27:53 +03:00
while ( length ( $ arg ) > $ max_len ) {
my $ split = rindex $ arg , "," , $ max_len ;
2016-05-30 18:44:58 +03:00
if ( $ split == - 1 ) {
2016-07-11 17:27:53 +03:00
$ split = rindex $ arg , ":" , $ max_len ;
2016-05-30 18:44:58 +03:00
}
if ( $ split == - 1 ) {
2016-07-11 17:27:53 +03:00
$ split = rindex $ arg , " " , $ max_len ;
2016-05-30 18:44:58 +03:00
}
if ( $ split == - 1 ) {
warn "cannot find nice place to split '$arg' below 80 chars\n" ;
2016-07-11 17:27:53 +03:00
$ split = $ max_len - 1 ;
2016-05-30 18:44:58 +03:00
}
$ split + + ;
2016-05-30 18:58:27 +03:00
push @ ret , substr $ arg , 0 , $ split ;
2016-05-30 18:44:58 +03:00
$ arg = substr $ arg , $ split ;
}
2016-05-30 18:58:27 +03:00
push @ ret , $ arg ;
return join ( "\\\n" , @ ret ) ;
2016-05-30 18:44:58 +03:00
}