3 use File::Spec::Functions qw ( :ALL );
8 my $config_h = "config.h";
10 # Read in a whole file
15 open my $fh, "<$file" or die "Could not open file $file: $!\n";
22 # Write out a whole file
28 open my $fh, ">$file" or die "Could not write $file: $!\n";
38 unlink $file or die "Could not delete $file: $!\n";
41 # Read a directory listing (excluding the . and .. entries)
46 opendir my $dh, $dir or die "Could not open directory $dir: $!\n";
47 my @entries = grep { ! /^(\.)+$/ } readdir $dh;
52 # Get the current configuration by reading the configuration file
59 foreach my $file ( read_dir ( $dir ) ) {
60 $cfg->{$file} = read_file ( catfile ( $dir, $file ) );
65 # Calculate guard name for a header file
71 return "CONFIG_".( uc $name );
74 # Calculate preamble for a header file
80 my $guard = guard ( $name );
81 my $preamble = <<"EOF";
83 * This file is automatically generated from $master. Do not edit this
84 * file; edit $master instead.
94 # Calculate postamble for a header file
99 my $guard = guard ( $name );
100 return "\n#endif /* $guard */\n";
103 # Get the new configuration by splitting config.h file using the
112 open my $fh, "<$file" or die "Could not open $file: $!\n";
114 if ( ( my $newcursor, my $suffix ) = /\@BEGIN\s+(\w+\.h)(.*)$/ ) {
115 die "Missing \"\@END $cursor\" before \"\@BEGIN $1\""
116 ." at $file line $.\n" if $cursor;
117 $cursor = $newcursor;
118 $cfg->{$cursor} = preamble ( $cursor, $file )
119 unless exists $cfg->{$cursor};
120 $cfg->{$cursor} .= "\n/*".$suffix."\n";
121 } elsif ( ( my $prefix, my $oldcursor ) = /^(.*)\@END\s+(\w+\.h)/ ) {
122 die "Missing \"\@BEGIN $oldcursor\" before \"\@END $oldcursor\""
123 ." at $file line $.\n" unless $cursor eq $oldcursor;
124 $cfg->{$cursor} .= $prefix."*/\n";
127 $cfg->{$cursor} .= $_ if $cursor;
131 die "Missing \"\@END $cursor\" in $file\n" if $cursor;
133 foreach $cursor ( keys %$cfg ) {
134 $cfg->{$cursor} .= postamble ( $cursor );
140 #############################################################################
144 # Read in current config file fragments
146 my $current = current_config ( $cfgdir );
148 # Read in config.h and split it into fragments
150 my $new = new_config ( $config_h );
152 # Delete any no-longer-wanted config file fragments
154 foreach my $file ( keys %$current ) {
155 unlink catfile ( $cfgdir, $file ) unless exists $new->{$file};
158 # Write out any modified fragments
160 foreach my $file ( keys %$new ) {
161 write_file ( catfile ( $cfgdir, $file ), $new->{$file} )
162 unless $current->{$file} && $new->{$file} eq $current->{$file};