#!/usr/bin/perl -w # # dbcol # Copyright (C) 1991-2002 by John Heidemann # $Id: dbcol,v 1.25 2003/07/09 23:38:49 johnh Exp $ # # This program is distributed under terms of the GNU general # public license, version 2. See the file COPYING # in $dblibdir for details. # sub usage { print <getopt) { $ch = $dbopts->opt; if ($ch eq 'e') { $null_value = $dbopts->optarg; } elsif ($ch eq 'd') { $debug = 1; } elsif ($ch eq 'r') { $relaxed_errors = 1; } elsif ($ch eq 'v') { $invert_match = 1; } else { &usage; }; }; &readprocess_header; if ($invert_match) { my(@old_av) = @ARGV; my($badcol); my(@candidate_colnames) = @colnames; # ick... O(n^2)... but n is small. foreach $badcol (@old_av) { my($badf) = $colnametonum{$badcol}; if (!defined($badf)) { die "$prog: unknown column ``$badcol'' for ommision.\n" if (!$relaxed_errors); # skip it if relaxed next; }; # remove this column from the acceptable list my(@new_colnames) = (); foreach (@candidate_colnames) { push (@new_colnames, $_) if ($badf != $colnametonum{$_}); }; die "$prog: multiply omitted column ``$badcol''.\n" if ($#candidate_colnames != $#new_colnames + 1); @candidate_colnames = @new_colnames; }; # Pretend like the user actually asked for the longer version. @ARGV = @candidate_colnames; }; my($code) = ""; for $i (0..$#ARGV) { if (defined($colnametonum{$ARGV[$i]})) { $code .= '$nf['.$i.'] = $f['.$colnametonum{$ARGV[$i]}.'];' . "\n"; } elsif (!defined($null_value)) { die ("$prog: creating new column ``$ARGV[$i]'' without specifying null value.\n"); } else { $code .= '$nf['.$i."] = '" . $null_value . "';\n"; }; }; @outcolnames = (); foreach (@ARGV) { # convert to colnames (to map 0 back to the first column name) if (defined($colnametonum{$_})) { # existing column push(@outcolnames, $colnames[$colnametonum{$_}]); } else { push(@outcolnames, $_); }; }; &write_header(@outcolnames); # since perl5 doesn't cache eval, eval the whole loop; my(@nf) = (); # for -w my($loop) = q[ while () { &pass_comments && next; &split_cols; ] . $code . q[ &write_these_cols(@nf); }; ]; eval $loop; $@ && die "$prog: interal eval error: $@.\n"; print "# $prog\'s code: " . code_prettify($code) . "\n" if ($debug); print "# | $prog ", join(" ", @orig_argv), "\n"; exit 0;