linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Cruz Julian Bishop <cruzjbishop@gmail.com>
To: mcgrof@gmail.com
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/5] scripts/checkincludes.pl: Simplify and shorten argument logic
Date: Tue, 04 Sep 2012 08:29:02 +1000	[thread overview]
Message-ID: <1346711342.2312.1.camel@Cruz-PC> (raw)
In-Reply-To: <1346710853-19166-3-git-send-email-cruzjbishop@gmail.com>

In my last submission, Joe Perches (is that right?)
suggested using a function that is built in to Perl.

I haven't had a chance to test it yet, but if it turns
out to be okay, I'll make a separate merge request at
a later date.

On Tue, 2012-09-04 at 08:20 +1000, Cruz Julian Bishop wrote:
> This patch allows for much easier implementation of arguments
> when modifying checkincludes.pl
> 
> The variable $file is also initially named $arg - I wasn't sure
> if memory would be a problem (I know it would be in Java/C#) if
> another $file variable was initialized with the value of $arg,
> so I just left it.
> 
> Overall, this still works nicely. There are some more
> potential simplifications, but I will cover those in another
> patch if I decide to implement them tonight.
> 
> Signed-off-by: Cruz Julian Bishop <cruzjbishop@gmail.com>
> ---
>  scripts/checkincludes.pl | 85 ++++++++++++++++++++++--------------------------
>  1 file changed, 39 insertions(+), 46 deletions(-)
> 
> diff --git a/scripts/checkincludes.pl b/scripts/checkincludes.pl
> index 801ed5f..7d713c2 100755
> --- a/scripts/checkincludes.pl
> +++ b/scripts/checkincludes.pl
> @@ -30,64 +30,57 @@ if ($#ARGV < 0) {
>  	usage();
>  }
>  
> -if ($#ARGV >= 1) {
> -	if ($ARGV[0] =~ /^-/) {
> -		if ($ARGV[0] eq "-r") {
> -			$remove = 1;
> -			shift;
> -		} else {
> -			usage();
> -		}
> -	}
> -}
> +foreach my $arg (@ARGV) {
> +	if ($arg eq "-r") {
> +		$remove = 1;
> +	} else {
> +		open(my $f, '<', $arg)
> +		    or die "Cannot open $arg: $!.\n";
>  
> -foreach my $file (@ARGV) {
> -	open(my $f, '<', $file)
> -	    or die "Cannot open $file: $!.\n";
> +		my %includedfiles = ();
> +		my @file_lines = ();
>  
> -	my %includedfiles = ();
> -	my @file_lines = ();
> -
> -	while (<$f>) {
> -		if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
> -			++$includedfiles{$1};
> +		while (<$f>) {
> +			if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
> +				++$includedfiles{$1};
> +			}
> +			push(@file_lines, $_);
>  		}
> -		push(@file_lines, $_);
> -	}
>  
> -	close($f);
> +		close($f);
>  
> -	if (!$remove) {
> -		foreach my $filename (keys %includedfiles) {
> -			if ($includedfiles{$filename} > 1) {
> -				print "$file: $filename is included more than once.\n";
> +		if (!$remove) {
> +			foreach my $filename (keys %includedfiles) {
> +				if ($includedfiles{$filename} > 1) {
> +					print "$arg: $filename is included more than once.\n";
> +				}
>  			}
> +			next;
>  		}
> -		next;
> -	}
>  
> -	open($f, '>', $file)
> -	    or die("Cannot write to $file: $!");
> +		open($f, '>', $arg)
> +			or die("Cannot write to $arg: $!");
>  
> -	my $dups = 0;
> -	foreach (@file_lines) {
> -		if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
> -			foreach my $filename (keys %includedfiles) {
> -				if ($1 eq $filename) {
> -					if ($includedfiles{$filename} > 1) {
> -						$includedfiles{$filename}--;
> -						$dups++;
> -					} else {
> -						print {$f} $_;
> +		my $dups = 0;
> +		foreach(@file_lines) {
> +			if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
> +				foreach my $filename (keys %includedfiles) {
> +					if ($1 eq $filename) {
> +						if ($includedfiles{$filename} > 1) {
> +							$includedfiles{$filename}--;
> +							$dups++;
> +						} else {
> +							print {$f} $_;
> +						}
>  					}
>  				}
> +			} else {
> +				print {$f} $_;
>  			}
> -		} else {
> -			print {$f} $_;
>  		}
> +		if ($dups > 0) {
> +			print "$arg: removed $dups duplicate includes \n";
> +		}
> +		close($f);
>  	}
> -	if ($dups > 0) {
> -		print "$file: removed $dups duplicate includes\n";
> -	}
> -	close($f);
>  }



       reply	other threads:[~2012-09-03 22:29 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1346710853-19166-1-git-send-email-cruzjbishop@gmail.com>
     [not found] ` <1346710853-19166-3-git-send-email-cruzjbishop@gmail.com>
2012-09-03 22:29   ` Cruz Julian Bishop [this message]
     [not found] ` <1346710853-19166-6-git-send-email-cruzjbishop@gmail.com>
2012-09-03 23:12   ` [PATCH 5/5] scripts/checkincludes.pl: Replace tabs with spaces Cruz Julian Bishop

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1346711342.2312.1.camel@Cruz-PC \
    --to=cruzjbishop@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mcgrof@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).