New pl2bat.pl
[p5sagit/p5-mst-13.2.git] / win32 / bin / pl2bat.pl
1     eval 'exec perl -x -S "$0" ${1+"$@"}'
2         if 0;   # In case running under some shell
3
4 require 5;
5 use Getopt::Std;
6 use Config;
7
8 $0 =~ s|.*[/\\]||;
9
10 my $usage = <<EOT;
11 Usage:  $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'.
20         -a argstring    arguments to invoke perl with in generated file
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).
27         -s stripsuffix  strip this suffix from file before appending ".bat"
28                             Not case-sensitive
29                             Can be a regex if it begins with `/'
30                             Defaults to "/\.plx?/"
31         -h              show this help
32 EOT
33
34 my %OPT = ();
35 warn($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'};
39 $OPT{'s'} = ($OPT{'s'} =~ m|^/([^/]*)| ? $1 : "\Q$OPT{'s'}\E");
40
41 my $head;
42 if(  defined( $OPT{'a'} )  ) {
43     $head = <<EOT;
44         \@rem = '--*-Perl-*--
45         \@echo off
46         perl $OPT{'a'}
47         goto endofperl
48         \@rem ';
49 EOT
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 ';
63 EOT
64 }
65 $head =~ s/^\t//gm;
66 my $headlines = 2 + ($head =~ tr/\n/\n/);
67 my $tail = "__END__\n:endofperl\n";
68
69 @ARGV = ('-') unless @ARGV;
70
71 foreach ( @ARGV ) {
72     process($_);
73 }
74
75 sub process {
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;
91             }
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";
100             }
101             $linedone++;
102         }
103         if ( $line =~ /^#\s*line\b/ and $linenum == 2 + $skiplines ) {
104             $line = "";
105         }
106     }
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 );
117 }
118 __END__
119
120 =head1 NAME
121
122 pl2bat - wrap perl code into a batch file
123
124 =head1 SYNOPSIS
125
126 B<pl2bat> B<-h>
127
128 B<pl2bat> [B<-w>] S<[B<-a> I<argstring>]> S<[B<-s> I<stripsuffix>]> [files]
129
130 B<pl2bat> [B<-w>] S<[B<-n> I<ntargs>]> S<[B<-o> I<otherargs>]> S<[B<-s> I<stripsuffix>]> [files]
131
132 =head1 DESCRIPTION
133
134 This utility converts a perl script into a batch file that can be
135 executed on DOS-like operating systems.
136
137 Note that by default, the ".pl" suffix will be stripped before adding
138 a ".bat" suffix to the supplied file names.  This can be controlled
139 with the C<-s> option.
140
141 The default behavior is to have the batch file compare the C<OS>
142 environment variable against C<"Windows_NT">.  If they match, it
143 uses the C<%*> construct to refer to all the command line arguments
144 that were given to it, so you'll need to make sure that works on your
145 variant of the command shell.  It is known to work in the cmd.exe shell
146 under WindowsNT.  4DOS/NT users will want to put a C<ParameterChar = *>
147 line in their initialization file, or execute C<setdos /p*> in
148 the shell startup file.
149
150 On Windows95 and other platforms a nine-argument limit is imposed
151 on command-line arguments given to the generated batch file, since
152 they may not support C<%*> in batch files.
153
154 These can be overridden using the C<-n> and C<-o> options or the
155 deprecated C<-a> option.
156
157 =head1 OPTIONS
158
159 =over 8
160
161 =item B<-n> I<ntargs>
162
163 Arguments to invoke perl with in generated batch file when run from
164 Windows NT (or Windows 98, probably).  Defaults to S<'-x -S "%0" %*'>.
165
166 =item B<-o> I<otherargs>
167
168 Arguments to invoke perl with in generated batch file except when
169 run from Windows NT (ie. when run from DOS, Windows 3.1, or Windows 95).
170 Defaults to S<'-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9'>.
171
172 =item B<-a> I<argstring>
173
174 Arguments to invoke perl with in generated batch file.  Specifying
175 B<-a> prevents the batch file from checking the C<OS> environment
176 variable to determine which operating system it is being run from.
177
178 =item B<-s> I<stripsuffix>
179
180 Strip a suffix string from file name before appending a ".bat"
181 suffix.  The suffix is not case-sensitive.  It can be a regex if
182 it begins with `/' (the trailing '/' is optional and a trailing
183 C<$> is always assumed).  Defaults to C</.plx?/>.
184
185 =item B<-w>
186
187 If no line matching C</^#!.*perl/> is found in the script, then such
188 a line is inserted just after the new preamble.  The exact line
189 depends on C<$Config{startperl}> [see L<Config>].  With the B<-w>
190 option, C<" -w"> is added after the value of C<$Config{startperl}>.
191 If a line matching C</^#!.*perl/> already exists in the script,
192 then it is not changed and the B<-w> option is ignored.
193
194 =item B<-u>
195
196 If the script appears to have already been processed by B<pl2bat>,
197 then the script is skipped and not processed unless B<-u> was
198 specified.  If B<-u> is specified, the existing preamble is replaced.
199
200 =item B<-h>
201
202 Show 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..]
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..]
227
228 =head1 BUGS
229
230 C<$0> will contain the full name, including the ".bat" suffix
231 when the generated batch file runs.  If you don't like this,
232 see runperl.bat for an alternative way to invoke perl scripts.
233
234 Default behavior is to invoke Perl with the B<-S> flag, so Perl will
235 search the PATH to find the script.   This may have undesirable
236 effects.
237
238 =head1 SEE ALSO
239
240 perl, perlwin32, runperl.bat
241
242 =cut
243