X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=shell_include;fp=shell_include;h=b404f85747b4d04e02bb8863fbf6b36603b49514;hb=93f0412887322c8f7f38ec71d6a57d5b3ddd9260;hp=0000000000000000000000000000000000000000;hpb=2df2fbe518d5a221ce6e3ee88a3fb23fb1b94b27;p=pingofdeath.git diff --git a/shell_include b/shell_include new file mode 100755 index 0000000..b404f85 --- /dev/null +++ b/shell_include @@ -0,0 +1,129 @@ +#!/usr/bin/perl + +use strict; +use Getopt::Long; + +my $source = ""; +my $destination = ""; +my $make; +my $help; + +my @lines; +my %included; + +######################################################################## +# +# usage +# + +sub usage() +{ + print "Usage: shell_include --source --destination [--make] [--help]\n"; + print "\n"; + print "--make will output makefile dependencies to stdout and exit\n"; + print "--help will output this text and exit\n"; + print "\n"; +} +######################################################################## +# +# check_options +# + +sub check_options() +{ + if ( !GetOptions( + 's|source=s' => \$source, + 'd|destination=s' => \$destination, + 'm|make' => \$make, + 'h|help' => \$help ) ) + { + &usage; + exit 1; + } + + if ( $help ) { + &usage; + exit 0; + }; + + die( "Missing input file - use --source " ) if ( $source eq "" ); + die( "Missing output file - use --destination ") if ( $destination eq "" ); +} + +######################################################################## +# +# read_input : Read in the initial input file into @lines +# + +sub read_input() +{ + open( SOURCE, $source ) || die ( "Cannot open input file : $source" ); + while ( ) { + @lines = ( @lines, $_ ); + }; + close( SOURCE ); +} + +######################################################################## +# +# Process #include directives +# +sub process_includes() +{ + my $changed = 0; + my @newlines; + + foreach my $line ( @lines ) { + + if ( $line =~ /^(\s*)\#include(\s+)(\S+)/ ) { + $changed = 1; + my $filename = $3; + if ( ! defined $included{ $filename } ) { + open( INC, $filename ) || die ( "Cannot open include file : $filename" ); + @newlines = ( @newlines, "########################################################################\n" ); + @newlines = ( @newlines, "#\n" ); + @newlines = ( @newlines, "# Included file : $filename\n" ); + @newlines = ( @newlines, "#\n" ); + while( ) { + @newlines = ( @newlines, $_ ); + } + close( INC ); + @newlines = ( @newlines, "#\n" ); + @newlines = ( @newlines, "# End of included file : $filename\n" ); + @newlines = ( @newlines, "#\n" ); + @newlines = ( @newlines, "########################################################################\n" ); + $included{ $filename } = ""; + } + } else { + @newlines = ( @newlines, $line ); + } + } + @lines = @newlines; + return $changed; +} + +######################################################################## +# +# write_output : write the final set of lines to the output file +# +sub write_output() +{ + open( DEST, ">$destination" ) || die( "Cannot open output file : $destination" ); + foreach my $line ( @lines ) { + print DEST $line; + } + close( DEST ); +} + +&check_options; +&read_input; +while ( &process_includes ) { }; +if ( $make ) { + print "$destination: $source "; + foreach my $inc ( sort( keys( %included ) ) ) { + print "$inc "; + }; + print "\n"; +} else { + &write_output; +}