0f67fd40233038083ae40170776d27330c3e15a2
[p5sagit/p5-mst-13.2.git] / win32 / bin / pl2bat.pl
1 #!perl -w
2 require 5;
3 use Getopt::Std;
4
5 $0 =~ s|.*[/\\]||;
6
7 my $usage = <<EOT;
8 Usage:  $0 [-h] [-a argstring] [-s stripsuffix] [files]
9         -a argstring    arguments to invoke perl with in generated file
10                             Defaults to "-x -S %0 %*" on WindowsNT,
11                             "-x -S %0 %1 %2 %3 %4 %5 %6 %7 %8 %9" otherwise
12         -s stripsuffix  strip this suffix from file before appending ".bat"
13                             Not case-sensitive
14                             Can be a regex if it begins with `/'
15                             Defaults to "/\.pl/"
16         -h              show this help
17 EOT
18
19 my %OPT = ();
20 warn($usage), exit(0) if !getopts('ha:s:',\%OPT) or $OPT{'h'};
21 $OPT{'a'} = ($^O eq 'MSWin32' and &Win32::IsWinNT
22              ? '-x -S %0 %*'
23              : '-x -S %0 %1 %2 %3 %4 %5 %6 %7 %8 %9')
24           unless exists $OPT{'a'};
25 ($OPT{'a2'} = $OPT{'a'} ) =~ s/\%0/\%0.bat/g;
26 $OPT{'s'} = '.pl' unless exists $OPT{'s'};
27 $OPT{'s'} = ($OPT{'s'} =~ m|^/([^/]*)| ? $1 : "\Q$OPT{'s'}\E");
28
29 (my $head = <<EOT) =~ s/^\t//gm;
30         \@rem = '--*-Perl-*--
31         \@echo off
32         if not exist \%0.bat goto over
33         perl $OPT{'a2'}
34         goto endofperl
35         :over
36         perl $OPT{'a'}
37         goto endofperl
38         \@rem ';
39 EOT
40 my $headlines = 2 + ($head =~ tr/\n/\n/);
41 my $tail = "__END__\n:endofperl\n";
42
43 @ARGV = ('-') unless @ARGV;
44
45 process(@ARGV);
46
47 sub process {
48    LOOP:
49     foreach ( @_ ) {
50         my $myhead = $head;
51         my $linedone = 0;
52         my $linenum = $headlines;
53         my $line;
54         open( FILE, $_ ) or die "$0: Can't open $_: $!";
55         @file = <FILE>;
56         foreach $line ( @file ) {
57             $linenum++;
58             if ( $line =~ /^:endofperl/) {
59                 warn "$0: $_ has already been converted to a batch file!\n";
60                 next LOOP;
61             }
62             if ( not $linedone and $line =~ /^#!.*perl/ ) {
63                 $line .= "#line $linenum\n";
64                 $linedone++;
65             }
66         }
67         close( FILE );
68         s/$OPT{'s'}$//oi;
69         $_ .= '.bat' unless /\.bat$/i or /^-$/;
70         open( FILE, ">$_" ) or die "Can't open $_: $!";
71         print FILE $myhead;
72         print FILE "#!perl\n#line " . ($headlines+1) . "\n" unless $linedone;
73         print FILE @file, $tail;
74         close( FILE );
75     }
76 }
77 __END__
78
79 =head1 NAME
80
81 pl2bat - wrap perl code into a batch file
82
83 =head1 SYNOPSIS
84
85 B<pl2bat> [B<-h>] S<[B<-a> I<argstring>]> S<[B<-s> I<stripsuffix>]> [files]
86
87 =head1 DESCRIPTION
88
89 This utility converts a perl script into a batch file that can be
90 executed on DOS-like operating systems.
91
92 Note that by default, the ".pl" suffix will be stripped before adding
93 a ".bat" suffix to the supplied file names.  This can be controlled
94 with the C<-s> option.
95
96 The default behavior on WindowsNT is to generate a batch file that
97 uses the C<%*> construct to refer to all the command line arguments
98 that were given to it, so you'll need to make sure that works on your
99 variant of the command shell.  It is known to work in the cmd.exe shell
100 under WindowsNT.  4DOS/NT users will want to put a C<ParameterChar = *>
101 line in their initialization file, or execute C<setdos /p*> in
102 the shell startup file.  On Windows95 and other platforms a nine
103 argument limit is imposed on command-line arguments given to the
104 generated batch file, since they may not support C<%*> in batch files.
105 This can be overridden using the C<-a> option.
106
107 =head1 OPTIONS
108
109 =over 8
110
111 =item B<-a> I<argstring>
112
113 Arguments to invoke perl with in generated batch file.  Defaults to
114 S<"-x -S %0 %*"> on WindowsNT, S<"-x -S %0 %1 %2 %3 %4 %5 %6 %7 %8 %9">
115 on other platforms.
116
117 =item B<-s> I<stripsuffix>
118
119 Strip a suffix string from file name before appending a ".bat"
120 suffix.  The suffix is not case-sensitive.  It can be a regex if it
121 begins with `/' (the trailing '/' being optional.  Defaults to ".pl".
122
123 =item B<-h>
124
125 Show command line usage.
126
127 =back
128
129 =head1 EXAMPLES
130
131         C:\> pl2bat foo.pl bar.PM 
132         [..creates foo.bat, bar.PM.bat..]
133         
134         C:\> pl2bat -s "/\.pl|\.pm/" foo.pl bar.PM
135         [..creates foo.bat, bar.bat..]
136         
137         C:\> pl2bat < somefile > another.bat
138         
139         C:\> pl2bat > another.bat
140         print scalar reverse "rekcah lrep rehtona tsuj\n";
141         ^Z
142         [..another.bat is now a certified japh application..]
143
144 =head1 BUGS
145
146 C<$0> will contain the full name, including the ".bat" suffix
147 when the generated batch file runs.  If you don't like this,
148 see runperl.bat for an alternative way to invoke perl scripts.
149
150 Default behavior is to invoke Perl with the -S flag, so Perl will
151 search the PATH to find the script.  This may have undesirable
152 effects.
153
154 =head1 SEE ALSO
155
156 perl, perlwin32, runperl.bat
157
158 =cut
159