subst.in 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/perl -w
  2. # This script finishes the job started by config.status by replacing the variables
  3. # of the form ${...} which were inserted into the file(s) by config.status.
  4. # Read all files with a single read statement
  5. $/ = undef;
  6. # List of variables to replace
  7. my %configvars = (
  8. "prefix" => { "value" => '@prefix@'},
  9. "exec_prefix" => { "value" => '@exec_prefix@'},
  10. );
  11. sub replace_var {
  12. my $filep = shift;
  13. my $cvp = shift;
  14. my $varname = shift;
  15. return if( $cvp->{ $varname}->{ "replaced"});
  16. if( defined( $cvp->{ $varname}->{ "dependency"})) {
  17. if( !$cvp->{ $cvp->{ $varname}->{ "dependency"}}->{ "replaced"}) {
  18. # If a dependency exists and it is not already replaced, replace it
  19. replace_var( $filep, $cvp, $cvp->{ $varname}->{ "dependency"});
  20. }
  21. }
  22. my $replacement = $cvp->{ $varname}->{ "value"};
  23. $$filep =~ s/\${$varname}/$replacement/g;
  24. $cvp->{ $varname}->{ "replaced"} = 1;
  25. }
  26. # Figure out the dependencies.
  27. foreach my $cv ( keys %configvars ) {
  28. if( $configvars{ $cv}->{ "value"} =~ /\${([^}]+)}/) {
  29. my $dependency = $1;
  30. if( exists( $configvars{ $dependency})) {
  31. $configvars{ $dependency}->{ "dependency"} = $cv;
  32. }
  33. $configvars{ $cv}->{ "replaced"} = 0;
  34. }
  35. }
  36. # Process each file
  37. while ($f = shift @ARGV) {
  38. # Read in the file
  39. open( FILE, $f) || die "Unable to open $f for reading";
  40. my $file = <FILE>;
  41. close( FILE) || die "Unable to close $f after reading";
  42. # Replace each of the variables we know about
  43. foreach $cv ( keys %configvars ) {
  44. replace_var( \$file, \%configvars, $cv);
  45. }
  46. # Write out the replacements
  47. open( FILE, ">$f") || die "Unable to open $f for writing";
  48. print FILE $file;
  49. close( FILE) || die "Unable to close $f after writing";
  50. }