#!/usr/local/bin/perl # prevent matching perl -pe "s///" mbin/* my $unixperl = "\/usr\/local\/bin\/perl5"; my $dosperl = 'perl'; my $perlflags = ''; my $mswin = $ENV{'windir'} || $ENV{'WINDIR'}; # TRY: perl -i~ -pe 's,\A#!/usr/local/bin/perl,#!/sas/bin/perl,' mbin/* $USAGE = ' SYNOPSIS: Convert perl scripts to run on windows and unix. AUTHOR: GPL(C) Mohsin Ahmed, http://www.cs.albany.edu/~mosh USAGE: n2l OUTDIR FILES This perl is used on NT/95. cmd> n2l perl/ mbin/* Here mbin is used on Unix. ksh$ n2l -unixperl=/usr/local/bin/perl5 u:/mohsin/mbin perl/* OPTIONS: -unixperl=perlpath -v verbose NOTES: 1. Filename decide conversion direction, eg *.bat means "to unix". 2. Skips non perl files (bash/bat). 3. chmod 0755 mbin/* ... do this yourself on unix. 4. CR/LF Problems in scripts a. Use ascii mode in ftp to transfer bat files. b. CR/LF mismatches work on NT perl scripts, but SUN/Linux/95 perl script says "file not found". Fix: bash$ perl -i~ -pe "s,\\r\\n,\\n," * cmd> perl -i~ -pe "s,(^\cM)\n,$1\cM\n," *.bat '; while( $_ = $ARGV[0], /^-/ ){ shift; if( m/^--$/ ){ last; }elsif( m/^-unixperl=(.+)$/ ){ $unixperl= $1; }elsif( m/^-v/ ){ $verbose++; print STDERR "$0 @ARGV\n"; } } undef( $/ ); # slurp mode. $perp = "\@rem='\n" . "\@goto endofperl\n" . "';\n"; $post = "__END__\n" . ":endofperl\n" . '@' . "$dosperl $perlflags" . '%perlflags% -S %0.bat %1 %2 %3 %4 %5 %6 %7 %8 %9' . "\n"; unless( $outdir = shift ){ print $USAGE; die "Need options and files\n"; } unless( -d $outdir ){ die "Output dir '$outdir' doesnot exists.\n" } foreach $infile (@ARGV){ unless( -r $infile ){ warn "Cannot read $infile, skipping.\n"; next; } # step 1, get filenames. $outfile = $infile; $outfile =~ s,.*[/\\],,; # remove dir/ from dir/infiles. if( ( $outfile =~ m/\.pl/ ) || # skip .pl files. ( $outfile =~ m/\.cgi/) ) # skip .cgi files. { warn "Skipping infile=$infile\n"; next; } $nt2unix = ($infile =~ m/\.(bat|cmd)/i); if( $nt2unix ){ # convert bat to unix. $outfile =~ s/\.(bat|cmd)$//i # $outfile = "p-$outfile" # unless $outfile =~ m/^p-/; }else{ # convert unix to bat $outfile =~ s/^p-//; # remove prefix p- if any. $outfile .= '.bat'; } $outfile = "$outdir/$outfile"; if( -f $outfile && ! -w $outfile ){ warn "Cannot write outfile=$outfile\n"; next; } # step 2, read / write files. open( INFILE, "< $infile" ) or die "Cannot read infile=$infile\n"; $_ = ; close INFILE; if( $nt2unix ){ warn "Converting $infile to run on unix outfile=$outfile.\n" if $verbose; unless( m,\A\@rem, ){ # skip non perl bat files. warn "skipping non perl file infile=$infile\n"; next; } s/\A.*\@rem[^;]+;\n//; # zap beginning to @rem to ; s/\n__END__[^\0]*/\n/; # zap __END__ onwards. s|\A(\#![^\n]*\n)?|\#\!$unixperl\n|; # add \#!perl s/\r\n/\n/g; }else{ warn "Converting $infile to run on windows outfile=$outfile.\n" if $verbose; unless( m,\A\#\!\S+perl, ){ # Skip non perl files. warn "skipping non perl file infile=$infile\n"; next; } s/\A(\#!.*\n)?/$perp/; # add header. s/[\s\n]*\Z/\n$post/; # add trailer. # 95's command.com can't see \n alone. s/\n/\r\n/g unless $mswin; } open( OUTFILE, "> $outfile" ) or die "Cannot write outfile=$outfile.\n"; # binmode( OUTFILE ) if $mswin; .. doesn't work. print OUTFILE $_; close OUTFILE; print STDERR "infile=$infile => outfile=$outfile\n"; } # EOF