New pl2bat.pl
[p5sagit/p5-mst-13.2.git] / win32 / bin / pl2bat.pl
CommitLineData
c9c878ae 1 eval 'exec perl -x -S "$0" ${1+"$@"}'
2 if 0; # In case running under some shell
3
d444a431 4require 5;
5use Getopt::Std;
c9c878ae 6use Config;
d444a431 7
8$0 =~ s|.*[/\\]||;
9
10my $usage = <<EOT;
c9c878ae 11Usage: $0 [-h]
12 or: $0 [-w] [-u] [-a argstring] [-s stripsuffix] [files]
13 or: $0 [-w] [-u] [-n ntargs] [-o otherargs] [-s stripsuffix] [files]
14 -n ntargs arguments to invoke perl with in generated file
15 when run from Windows NT. Defaults to
16 '-x -S "%0" %*'.
17 -o otherargs arguments to invoke perl with in generated file
18 other than when run from Windows NT. Defaults
19 to '-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9'.
d444a431 20 -a argstring arguments to invoke perl with in generated file
c9c878ae 21 ignoring operating system (for compatibility
22 with previous pl2bat versions).
23 -u update files that may have already been processed
24 by (some version of) pl2bat.
25 -w include "-w" on the /^#!.*perl/ line (unless
26 a /^#!.*perl/ line was already present).
d444a431 27 -s stripsuffix strip this suffix from file before appending ".bat"
c9c878ae 28 Not case-sensitive
d444a431 29 Can be a regex if it begins with `/'
c9c878ae 30 Defaults to "/\.plx?/"
d444a431 31 -h show this help
32EOT
33
34my %OPT = ();
c9c878ae 35warn($usage), exit(0) if !getopts('whun:o:a:s:',\%OPT) or $OPT{'h'};
36$OPT{'n'} = '-x -S "%0" %*' unless exists $OPT{'n'};
37$OPT{'o'} = '-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9' unless exists $OPT{'o'};
38$OPT{'s'} = '/\\.plx?/' unless exists $OPT{'s'};
d444a431 39$OPT{'s'} = ($OPT{'s'} =~ m|^/([^/]*)| ? $1 : "\Q$OPT{'s'}\E");
40
c9c878ae 41my $head;
42if( defined( $OPT{'a'} ) ) {
43 $head = <<EOT;
d444a431 44 \@rem = '--*-Perl-*--
45 \@echo off
46 perl $OPT{'a'}
47 goto endofperl
48 \@rem ';
49EOT
c9c878ae 50} else {
51 $head = <<EOT;
52 \@rem = '--*-Perl-*--
53 \@echo off
54 if "%OS%" == "Windows_NT" goto WinNT
55 perl $OPT{'o'}
56 goto endofperl
57 :WinNT
58 perl $OPT{'n'}
59 if NOT "%COMSPEC%" == "%SystemRoot%\\system32\\cmd.exe" goto endofperl
60 if %errorlevel% == 9009 echo You do not have Perl in your PATH.
61 goto endofperl
62 \@rem ';
63EOT
64}
65$head =~ s/^\t//gm;
d444a431 66my $headlines = 2 + ($head =~ tr/\n/\n/);
67my $tail = "__END__\n:endofperl\n";
68
69@ARGV = ('-') unless @ARGV;
70
c9c878ae 71foreach ( @ARGV ) {
72 process($_);
73}
d444a431 74
75sub process {
c9c878ae 76 my( $file )= @_;
77 my $myhead = $head;
78 my $linedone = 0;
79 my $taildone = 0;
80 my $linenum = 0;
81 my $skiplines = 0;
82 my $line;
83 open( FILE, $file ) or die "$0: Can't open $file: $!";
84 @file = <FILE>;
85 foreach $line ( @file ) {
86 $linenum++;
87 if ( $line =~ /^:endofperl\b/ ) {
88 if( ! exists $OPT{'u'} ) {
89 warn "$0: $file has already been converted to a batch file!\n";
90 return;
d444a431 91 }
c9c878ae 92 $taildone++;
93 }
94 if ( not $linedone and $line =~ /^#!.*perl/ ) {
95 if( exists $OPT{'u'} ) {
96 $skiplines = $linenum - 1;
97 $line .= "#line ".(1+$headlines)."\n";
98 } else {
99 $line .= "#line ".($linenum+$headlines)."\n";
d444a431 100 }
c9c878ae 101 $linedone++;
102 }
103 if ( $line =~ /^#\s*line\b/ and $linenum == 2 + $skiplines ) {
104 $line = "";
105 }
d444a431 106 }
c9c878ae 107 close( FILE );
108 $file =~ s/$OPT{'s'}$//oi;
109 $file .= '.bat' unless $file =~ /\.bat$/i or $file =~ /^-$/;
110 open( FILE, ">$file" ) or die "Can't open $file: $!";
111 print FILE $myhead;
112 print FILE $Config{startperl}, ( $OPT{'w'} ? " -w" : "" ),
113 "\n#line ", ($headlines+1), "\n" unless $linedone;
114 print FILE @file[$skiplines..$#file];
115 print FILE $tail unless $taildone;
116 close( FILE );
d444a431 117}
118__END__
119
120=head1 NAME
121
122pl2bat - wrap perl code into a batch file
123
124=head1 SYNOPSIS
125
c9c878ae 126B<pl2bat> B<-h>
127
128B<pl2bat> [B<-w>] S<[B<-a> I<argstring>]> S<[B<-s> I<stripsuffix>]> [files]
129
130B<pl2bat> [B<-w>] S<[B<-n> I<ntargs>]> S<[B<-o> I<otherargs>]> S<[B<-s> I<stripsuffix>]> [files]
d444a431 131
132=head1 DESCRIPTION
133
134This utility converts a perl script into a batch file that can be
135executed on DOS-like operating systems.
136
137Note that by default, the ".pl" suffix will be stripped before adding
138a ".bat" suffix to the supplied file names. This can be controlled
139with the C<-s> option.
140
c9c878ae 141The default behavior is to have the batch file compare the C<OS>
142environment variable against C<"Windows_NT">. If they match, it
d444a431 143uses the C<%*> construct to refer to all the command line arguments
144that were given to it, so you'll need to make sure that works on your
145variant of the command shell. It is known to work in the cmd.exe shell
146under WindowsNT. 4DOS/NT users will want to put a C<ParameterChar = *>
147line in their initialization file, or execute C<setdos /p*> in
c9c878ae 148the shell startup file.
149
150On Windows95 and other platforms a nine-argument limit is imposed
151on command-line arguments given to the generated batch file, since
152they may not support C<%*> in batch files.
153
154These can be overridden using the C<-n> and C<-o> options or the
155deprecated C<-a> option.
d444a431 156
157=head1 OPTIONS
158
159=over 8
160
c9c878ae 161=item B<-n> I<ntargs>
162
163Arguments to invoke perl with in generated batch file when run from
164Windows NT (or Windows 98, probably). Defaults to S<'-x -S "%0" %*'>.
165
166=item B<-o> I<otherargs>
167
168Arguments to invoke perl with in generated batch file except when
169run from Windows NT (ie. when run from DOS, Windows 3.1, or Windows 95).
170Defaults to S<'-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9'>.
171
d444a431 172=item B<-a> I<argstring>
173
c9c878ae 174Arguments to invoke perl with in generated batch file. Specifying
175B<-a> prevents the batch file from checking the C<OS> environment
176variable to determine which operating system it is being run from.
d444a431 177
178=item B<-s> I<stripsuffix>
179
180Strip a suffix string from file name before appending a ".bat"
c9c878ae 181suffix. The suffix is not case-sensitive. It can be a regex if
182it begins with `/' (the trailing '/' is optional and a trailing
183C<$> is always assumed). Defaults to C</.plx?/>.
184
185=item B<-w>
186
187If no line matching C</^#!.*perl/> is found in the script, then such
188a line is inserted just after the new preamble. The exact line
189depends on C<$Config{startperl}> [see L<Config>]. With the B<-w>
190option, C<" -w"> is added after the value of C<$Config{startperl}>.
191If a line matching C</^#!.*perl/> already exists in the script,
192then it is not changed and the B<-w> option is ignored.
193
194=item B<-u>
195
196If the script appears to have already been processed by B<pl2bat>,
197then the script is skipped and not processed unless B<-u> was
198specified. If B<-u> is specified, the existing preamble is replaced.
d444a431 199
200=item B<-h>
201
202Show command line usage.
203
204=back
205
206=head1 EXAMPLES
207
208 C:\> pl2bat foo.pl bar.PM
209 [..creates foo.bat, bar.PM.bat..]
210
211 C:\> pl2bat -s "/\.pl|\.pm/" foo.pl bar.PM
212 [..creates foo.bat, bar.bat..]
213
214 C:\> pl2bat < somefile > another.bat
215
216 C:\> pl2bat > another.bat
217 print scalar reverse "rekcah lrep rehtona tsuj\n";
218 ^Z
219 [..another.bat is now a certified japh application..]
c9c878ae 220
221 C:\> ren *.bat *.pl
222 C:\> pl2bat -u *.pl
223 [..updates the wrapping of some previously wrapped scripts..]
224
225 C:\> pl2bat -u -s .bat *.bat
226 [..same as previous example except more dangerous..]
d444a431 227
228=head1 BUGS
229
230C<$0> will contain the full name, including the ".bat" suffix
231when the generated batch file runs. If you don't like this,
232see runperl.bat for an alternative way to invoke perl scripts.
233
c9c878ae 234Default behavior is to invoke Perl with the B<-S> flag, so Perl will
235search the PATH to find the script. This may have undesirable
d444a431 236effects.
237
238=head1 SEE ALSO
239
240perl, perlwin32, runperl.bat
241
242=cut
243