8 Usage: $0 [options] source-filename
13 -h, -?, --help print this message
14 -c, --conf FILENAME use FILENAME as configuration file, default
16 -d, --directory DIR change to directory DIR
17 -s, --suffix SUFFIX use SUFFIX for all output filenames, default null
19 The configuration file is of the following form:
21 # Comments are preceded by hashes and extend to the end of the line.
22 # List of names of parameters to be substituted (as $param1$, $param2$,
26 # Multiple records, one per line.
27 # Each record consists of an output filename (automatically used as the value
28 # of the name parameter; suffix will be added if present) followed by a list of
29 # parameter values. Fields are space-separated. Double quotes (") may be used
30 # to delimit fields containing spaces, and double quotes themselves may be
31 # escaped with backslashes.
37 my %opts = (conf => '.headers', directory => '', suffix => '');
38 GetOptions(\%opts, 'help|h|?', 'conf|c=s', 'directory|d=s', 'suffix|s=s');
40 usage unless defined $source;
42 usage if $opts{'help'};
43 chdir $opts{'directory'} if $opts{'directory'};
44 open CONF, $opts{'conf'} or
45 die "Can't open configuration file " . $opts{'conf'} . ": $!";
46 -r $source or die "Can't read source file $source: $!";
51 next if /^#/ || /^\s+/;
58 # Eek. This matches the next (optionally double-quoted) string on the line.
59 my $stringre = qr/\s*("(?:[^\\]|\\.)*?"|(?:[^\\]|\\.)+?)(?:\s|$)/;
62 next if /^#/ || /^\s+/;
64 next unless $confline =~ /$stringre/g;
66 my %values = (name => $target);
67 my @conffields = ($confline =~ /\G$stringre/g);
69 for (my $i = 0; $i + $offset <= $#params && $i <= $#conffields; $i++)
71 $offset++ while $params[$i + $offset] eq 'name';
72 last if $i + $offset > $#params;
73 my $value = $conffields[$i];
74 $value =~ s/^"(.*)"$/$1/;
75 $value =~ s/\\(.)/$1/g;
76 $values{$params[$i + $offset]} = $value;
79 # Do the substitution in the source file.
80 open SOURCE, $source or die "Can't open source file $source: $!";
81 open TARGET, ">$target" . $opts{'suffix'} or
82 die "Can't open target file $target" . $opts{'suffix'} . ": $!";
86 foreach my $param (keys %values)
88 my $value = $values{$param};
89 s/\$\Q$param\E\$/$value/g;