This commit was generated by cvs2svn to compensate for changes in r45,
[pingofdeath.git] / shell_include
diff --git a/shell_include b/shell_include
new file mode 100755 (executable)
index 0000000..b404f85
--- /dev/null
@@ -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 <file> --destination <file> [--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 <file>" ) if ( $source eq "" );
+    die( "Missing output file - use --destination <file>") 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 ( <SOURCE> ) {
+        @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( <INC> ) {
+                    @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;
+}