bump release number
[pingofdeath.git] / shell_include
1 #!/usr/bin/perl
2
3 use strict;
4 use Getopt::Long;
5
6 my $source = "";
7 my $destination = "";
8 my $make;
9 my $help;
10
11 my @lines;
12 my %included;
13
14 ########################################################################
15 #
16 # usage
17 #
18
19 sub usage()
20 {
21     print "Usage: shell_include --source <file> --destination <file> [--make] [--help]\n";
22     print "\n";
23     print "--make will output makefile dependencies to stdout and exit\n";
24     print "--help will output this text and exit\n";
25     print "\n";
26 }
27 ########################################################################
28 #
29 # check_options
30 #
31
32 sub check_options()
33 {
34     if ( !GetOptions( 
35                      's|source=s' => \$source,
36                      'd|destination=s' => \$destination,
37                      'm|make' => \$make,
38                      'h|help' => \$help ) ) 
39     {
40         &usage;
41         exit 1;
42     }
43
44     if ( $help ) {
45         &usage;
46         exit 0;
47     };
48
49     die( "Missing input file - use --source <file>" ) if ( $source eq "" );
50     die( "Missing output file - use --destination <file>") if ( $destination eq "" );
51 }
52
53 ########################################################################
54 #
55 # read_input : Read in the initial input file into @lines
56 #
57
58 sub read_input()
59 {
60     open( SOURCE, $source ) || die ( "Cannot open input file : $source" );
61     while ( <SOURCE> ) {
62         @lines = ( @lines, $_ );
63     };
64     close( SOURCE );
65 }
66
67 ########################################################################
68 #
69 # Process #include directives
70 #
71 sub process_includes()
72 {
73     my $changed = 0;
74     my @newlines;
75
76     foreach my $line ( @lines ) {
77
78         if ( $line =~ /^(\s*)\#include(\s+)(\S+)/ ) {
79             $changed = 1;
80             my $filename = $3;
81             if ( ! defined $included{ $filename } ) {
82                 open( INC, $filename ) || die ( "Cannot open include file : $filename" );
83                 @newlines = ( @newlines, "########################################################################\n" );
84                 @newlines = ( @newlines, "#\n" );
85                 @newlines = ( @newlines, "# Included file : $filename\n" );
86                 @newlines = ( @newlines, "#\n" );
87                 while( <INC> ) {
88                     @newlines = ( @newlines, $_ );
89                 }
90                 close( INC );
91                 @newlines = ( @newlines, "#\n" );
92                 @newlines = ( @newlines, "# End of included file : $filename\n" );
93                 @newlines = ( @newlines, "#\n" );
94                 @newlines = ( @newlines, "########################################################################\n" );
95                 $included{ $filename } = "";
96             }
97         } else {
98             @newlines = ( @newlines, $line );
99         }
100     }
101     @lines = @newlines;
102     return $changed;
103 }
104
105 ########################################################################
106 #
107 # write_output : write the final set of lines to the output file
108 #
109 sub write_output() 
110 {
111     open( DEST, ">$destination" ) || die( "Cannot open output file : $destination" );
112     foreach my $line ( @lines ) {
113         print DEST $line;
114     }
115     close( DEST );
116 }
117
118 &check_options;
119 &read_input;
120 while ( &process_includes ) { };
121 if ( $make ) {
122     print "$destination: $source ";
123     foreach my $inc ( sort( keys( %included ) ) ) {
124         print "$inc ";
125     };
126     print "\n";
127 } else {
128     &write_output;
129 }