ensure __END__ appears on a line by itself in wrapped
[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/);
517db077 67my $tail = "\n__END__\n:endofperl\n";
d444a431 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;
a8ac1e79 83 my $start= $Config{startperl};
84 $start= "#!perl" unless $start =~ /^#!.*perl/;
c9c878ae 85 open( FILE, $file ) or die "$0: Can't open $file: $!";
86 @file = <FILE>;
87 foreach $line ( @file ) {
88 $linenum++;
89 if ( $line =~ /^:endofperl\b/ ) {
90 if( ! exists $OPT{'u'} ) {
91 warn "$0: $file has already been converted to a batch file!\n";
92 return;
d444a431 93 }
c9c878ae 94 $taildone++;
95 }
96 if ( not $linedone and $line =~ /^#!.*perl/ ) {
97 if( exists $OPT{'u'} ) {
98 $skiplines = $linenum - 1;
99 $line .= "#line ".(1+$headlines)."\n";
100 } else {
101 $line .= "#line ".($linenum+$headlines)."\n";
d444a431 102 }
c9c878ae 103 $linedone++;
104 }
105 if ( $line =~ /^#\s*line\b/ and $linenum == 2 + $skiplines ) {
106 $line = "";
107 }
d444a431 108 }
c9c878ae 109 close( FILE );
110 $file =~ s/$OPT{'s'}$//oi;
111 $file .= '.bat' unless $file =~ /\.bat$/i or $file =~ /^-$/;
112 open( FILE, ">$file" ) or die "Can't open $file: $!";
113 print FILE $myhead;
a8ac1e79 114 print FILE $start, ( $OPT{'w'} ? " -w" : "" ),
c9c878ae 115 "\n#line ", ($headlines+1), "\n" unless $linedone;
116 print FILE @file[$skiplines..$#file];
117 print FILE $tail unless $taildone;
118 close( FILE );
d444a431 119}
120__END__
121
122=head1 NAME
123
124pl2bat - wrap perl code into a batch file
125
126=head1 SYNOPSIS
127
c9c878ae 128B<pl2bat> B<-h>
129
130B<pl2bat> [B<-w>] S<[B<-a> I<argstring>]> S<[B<-s> I<stripsuffix>]> [files]
131
132B<pl2bat> [B<-w>] S<[B<-n> I<ntargs>]> S<[B<-o> I<otherargs>]> S<[B<-s> I<stripsuffix>]> [files]
d444a431 133
134=head1 DESCRIPTION
135
136This utility converts a perl script into a batch file that can be
137executed on DOS-like operating systems.
138
139Note that by default, the ".pl" suffix will be stripped before adding
140a ".bat" suffix to the supplied file names. This can be controlled
141with the C<-s> option.
142
c9c878ae 143The default behavior is to have the batch file compare the C<OS>
144environment variable against C<"Windows_NT">. If they match, it
d444a431 145uses the C<%*> construct to refer to all the command line arguments
146that were given to it, so you'll need to make sure that works on your
147variant of the command shell. It is known to work in the cmd.exe shell
148under WindowsNT. 4DOS/NT users will want to put a C<ParameterChar = *>
149line in their initialization file, or execute C<setdos /p*> in
c9c878ae 150the shell startup file.
151
152On Windows95 and other platforms a nine-argument limit is imposed
153on command-line arguments given to the generated batch file, since
154they may not support C<%*> in batch files.
155
156These can be overridden using the C<-n> and C<-o> options or the
157deprecated C<-a> option.
d444a431 158
159=head1 OPTIONS
160
161=over 8
162
c9c878ae 163=item B<-n> I<ntargs>
164
165Arguments to invoke perl with in generated batch file when run from
166Windows NT (or Windows 98, probably). Defaults to S<'-x -S "%0" %*'>.
167
168=item B<-o> I<otherargs>
169
170Arguments to invoke perl with in generated batch file except when
171run from Windows NT (ie. when run from DOS, Windows 3.1, or Windows 95).
172Defaults to S<'-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9'>.
173
d444a431 174=item B<-a> I<argstring>
175
c9c878ae 176Arguments to invoke perl with in generated batch file. Specifying
177B<-a> prevents the batch file from checking the C<OS> environment
178variable to determine which operating system it is being run from.
d444a431 179
180=item B<-s> I<stripsuffix>
181
182Strip a suffix string from file name before appending a ".bat"
c9c878ae 183suffix. The suffix is not case-sensitive. It can be a regex if
184it begins with `/' (the trailing '/' is optional and a trailing
185C<$> is always assumed). Defaults to C</.plx?/>.
186
187=item B<-w>
188
189If no line matching C</^#!.*perl/> is found in the script, then such
190a line is inserted just after the new preamble. The exact line
191depends on C<$Config{startperl}> [see L<Config>]. With the B<-w>
192option, C<" -w"> is added after the value of C<$Config{startperl}>.
193If a line matching C</^#!.*perl/> already exists in the script,
194then it is not changed and the B<-w> option is ignored.
195
196=item B<-u>
197
198If the script appears to have already been processed by B<pl2bat>,
199then the script is skipped and not processed unless B<-u> was
200specified. If B<-u> is specified, the existing preamble is replaced.
d444a431 201
202=item B<-h>
203
204Show command line usage.
205
206=back
207
208=head1 EXAMPLES
209
210 C:\> pl2bat foo.pl bar.PM
211 [..creates foo.bat, bar.PM.bat..]
212
213 C:\> pl2bat -s "/\.pl|\.pm/" foo.pl bar.PM
214 [..creates foo.bat, bar.bat..]
215
216 C:\> pl2bat < somefile > another.bat
217
218 C:\> pl2bat > another.bat
219 print scalar reverse "rekcah lrep rehtona tsuj\n";
220 ^Z
221 [..another.bat is now a certified japh application..]
c9c878ae 222
223 C:\> ren *.bat *.pl
224 C:\> pl2bat -u *.pl
225 [..updates the wrapping of some previously wrapped scripts..]
226
227 C:\> pl2bat -u -s .bat *.bat
228 [..same as previous example except more dangerous..]
d444a431 229
230=head1 BUGS
231
232C<$0> will contain the full name, including the ".bat" suffix
233when the generated batch file runs. If you don't like this,
234see runperl.bat for an alternative way to invoke perl scripts.
235
c9c878ae 236Default behavior is to invoke Perl with the B<-S> flag, so Perl will
237search the PATH to find the script. This may have undesirable
d444a431 238effects.
239
240=head1 SEE ALSO
241
242perl, perlwin32, runperl.bat
243
244=cut
245