13 getopts('vnsj:f:o:i:', \%opts);
16 if (defined $opts{o}) {
18 # output is a directory
21 # output is a single file, imply -i <ext> unless -i has also been specified
22 if ($opts{o} =~ /\.(\w+)$/) {
23 $opts{i} = $1 unless exists $opts{i};
25 print "Specified -o <file>, but '$opts{o}' has no extension.\n";
31 print "Using $opts{j} processors\n" if $opts{v};
33 my $var_re = qr/[A-Za-z_][A-Za-z0-9_]*/;
36 my ($base, $iext, $oext) = @_;
37 my $cmd = $rules{$oext}{$iext};
38 my $infile = "$base.$iext";
40 $cmd =~ s!\$\<!"$infile"!g;
44 # Strip leading path from base so we can stick the file in
46 $base = basename($base);
47 $outfile = "$opts{o}/$base.$oext";
52 $outfile = "$base.$oext";
54 $outfile =~ s/"/\\"/g;
55 $cmd =~ s!\$\@!"$outfile"!g;
67 s/\$($var_re)/(exists $vars{$1} ? $vars{$1} : '$' . $1)/eg;
71 open CONF, ($opts{f} || 'Convfile') or die "Could not open Convfile";
78 if (/^([\w.]+)\s*=>\s*([\w.]+)\s*:\s*(.*)$/) {
80 } elsif (/^($var_re)\s*=\s*(.*)$/) {
81 $vars{$1} = expand($2);
83 warn "Error in Convfile line $.: $_\n";
91 # If we specify reading from stdin, do this.
92 if (exists $opts{s}) {
103 # Reversed semantics. Files are input to be converted to the specified extension
105 unless (exists $rules{$opts{i}}) {
107 You have specified -i <ext>, but the Convfile has no rule to convert to
114 print "$_ does not exist.\n";
118 if (/^(.+)\.(\w+)$/) {
121 print "Considering $base.$iext\n" if $opts{v};
122 if (exists $rules{$opts{i}}{$iext}) {
123 print "Using $base.$iext\n" if $opts{v};
124 push @queue, [$base, $iext, $opts{i}];
127 print "I don't see an extension on $_, so I don't know what to do with it.\n";
132 # Make semantics, files specified are desired output files
134 # Do something fun (convert *.foo into a list of files that would match if they existed)
140 map { s/\.(\w+)$/\.$extsub/; $_ }
149 # I like to call this the TOWER OF POWER
150 for my $ofile (@files) {
152 for my $oext (keys %rules) {
154 $oext_re =~ s/\./\\./g;
155 if ($ofile =~ /^(.*)\.$oext_re$/) {
158 for my $iext (keys %{$rules{$oext}}) {
159 print "Considering $base.$iext\n" if $opts{v};
160 if (-e "$base.$iext") {
161 print "Using $base.$iext\n" if $opts{v};
162 push @queue, [$base, $iext, $oext];
168 print "No suitable input file found for $ofile\n";
175 print "No rule to make $ofile\n";
180 print scalar @queue, " items to process\n" if $opts{v};
183 foreach my $chunk (@queue) {
186 if ($procs == $opts{j}) {
193 # vim:ts=4 sts=4 sw=4 expandtab