#!/hgdv/software/IRIX6/bin/perl -w # # Transform csh script into ksh script # # Synopsis: csh2ksh infile [outfile] # # Knows about and will translate: # setenv -> export # if, then, else, endif -> if,then,elif,fi # # Knows about and will kill: # alias, unalias, complete # # Not yet known, but trivial to implement, too: # if w/o then # # Everything else is unknown and will be retained as is. # # Warning: # the csh script is assumed to be "well-formed"! # (i.e., nicely written and formatted) # if ( @ARGV < 1 || @ARGV > 2 ) { die "csh2ksh: Usage: csh2ksh infile [outfile]\n"; } $infile = shift; $outfile = shift; open INF, "$infile" or die "csh2ksh: couldn't open infile $infile: $!\n"; if ( defined($outfile) ) { open OUTF, ">$outfile" or die "csh2ksh: couldn't open outfile $outfile: $!\n"; select OUTF; } # read infile @_ = ; $_ = join "", @_; # delete un-translatable stuff s/^\s*(alias|unalias|complete).*$//mgo; # translate known stuff s/^([ \t]*)setenv\b\s*(\w+)\s*(.*)$/$1export $2=$3/mgo; # setenv s/^([ \t]*)if\b\s*\(([^)]*)\)\s*\bthen\b(.*)$/$1if [[$2]]\n$1then/mgo; # if .. then s/^([ \t]*)endif\b(.*)$/$1fi$2/mgo; # endif s/^([ \t]*)set\b\s*(\w+)\s*=(.*)$/$1$2=$3/mgo; # set x = .. s/=\s*\(\s*(.*?)\s*\)/="$1"/mgo; # set .. = ( .. ) # can't use \s after the ^, b/c that would also match newlines # "small" translations s/==/=/go; # if ( .. == .. ) s/(\bif\b.*)=~/$1=/go; # if ( .. =~ .. ) s/!\s*\$\?(\w+)/"\$$1" = ""/go; # if ( ! $?VAR ) s/\$\?(\w+)/"\$$1" != ""/go; # if ( $?VAR ) # clean up s/\belse\b(\s*(\#.*)?\n)*\s*\bfi\b/fi/mgo; # kill empty else .. fi s/^([ \t]*)if\b.*\n(\s*(\#.*)?\n)*then(\s*(\#.*)?\n)*\s*\bfi\b//mgo; # kill empty then .. fi # output print; if ( defined(OUTF) ) { close OUTF; select STDOUT; }