Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
7.7 KiB

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