Leaked source code of windows server 2003
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.

430 lines
13 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. if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
  11. goto endofperl
  12. @rem ';
  13. #!perl
  14. #line 15
  15. eval 'exec perl -x -S "$0" ${1+"$@"}'
  16. if 0; # In case running under some shell
  17. require 5;
  18. use Getopt::Std;
  19. use Config;
  20. $0 =~ s|.*[/\\]||;
  21. my $usage = <<EOT;
  22. Usage: $0 [-h]
  23. or: $0 [-w] [-u] [-a argstring] [-s stripsuffix] [files]
  24. or: $0 [-w] [-u] [-n ntargs] [-o otherargs] [-s stripsuffix] [files]
  25. -n ntargs arguments to invoke perl with in generated file
  26. when run from Windows NT. Defaults to
  27. '-x -S %0 %*'.
  28. -o otherargs arguments to invoke perl with in generated file
  29. other than when run from Windows NT. Defaults
  30. to '-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9'.
  31. -a argstring arguments to invoke perl with in generated file
  32. ignoring operating system (for compatibility
  33. with previous pl2bat versions).
  34. -u update files that may have already been processed
  35. by (some version of) pl2bat.
  36. -w include "-w" on the /^#!.*perl/ line (unless
  37. a /^#!.*perl/ line was already present).
  38. -s stripsuffix strip this suffix from file before appending ".bat"
  39. Not case-sensitive
  40. Can be a regex if it begins with `/'
  41. Defaults to "/\.plx?/"
  42. -h show this help
  43. EOT
  44. my %OPT = ();
  45. warn($usage), exit(0) if !getopts('whun:o:a:s:',\%OPT) or $OPT{'h'};
  46. # NOTE: %0 is already enclosed in doublequotes by cmd.exe, as appropriate
  47. $OPT{'n'} = '-x -S %0 %*' unless exists $OPT{'n'};
  48. $OPT{'o'} = '-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9' unless exists $OPT{'o'};
  49. $OPT{'s'} = '/\\.plx?/' unless exists $OPT{'s'};
  50. $OPT{'s'} = ($OPT{'s'} =~ m#^/([^/]*[^/\$]|)\$?/?$# ? $1 : "\Q$OPT{'s'}\E");
  51. my $head;
  52. if( defined( $OPT{'a'} ) ) {
  53. $head = <<EOT;
  54. \@rem = '--*-Perl-*--
  55. \@echo off
  56. perl $OPT{'a'}
  57. goto endofperl
  58. \@rem ';
  59. EOT
  60. } else {
  61. $head = <<EOT;
  62. \@rem = '--*-Perl-*--
  63. \@echo off
  64. if "%OS%" == "Windows_NT" goto WinNT
  65. perl $OPT{'o'}
  66. goto endofperl
  67. :WinNT
  68. perl $OPT{'n'}
  69. if NOT "%COMSPEC%" == "%SystemRoot%\\system32\\cmd.exe" goto endofperl
  70. if %errorlevel% == 9009 echo You do not have Perl in your PATH.
  71. if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
  72. goto endofperl
  73. \@rem ';
  74. EOT
  75. }
  76. $head =~ s/^\t//gm;
  77. my $headlines = 2 + ($head =~ tr/\n/\n/);
  78. my $tail = "\n__END__\n:endofperl\n";
  79. @ARGV = ('-') unless @ARGV;
  80. foreach ( @ARGV ) {
  81. process($_);
  82. }
  83. sub process {
  84. my( $file )= @_;
  85. my $myhead = $head;
  86. my $linedone = 0;
  87. my $taildone = 0;
  88. my $linenum = 0;
  89. my $skiplines = 0;
  90. my $line;
  91. my $start= $Config{startperl};
  92. $start= "#!perl" unless $start =~ /^#!.*perl/;
  93. open( FILE, $file ) or die "$0: Can't open $file: $!";
  94. @file = <FILE>;
  95. foreach $line ( @file ) {
  96. $linenum++;
  97. if ( $line =~ /^:endofperl\b/ ) {
  98. if( ! exists $OPT{'u'} ) {
  99. warn "$0: $file has already been converted to a batch file!\n";
  100. return;
  101. }
  102. $taildone++;
  103. }
  104. if ( not $linedone and $line =~ /^#!.*perl/ ) {
  105. if( exists $OPT{'u'} ) {
  106. $skiplines = $linenum - 1;
  107. $line .= "#line ".(1+$headlines)."\n";
  108. } else {
  109. $line .= "#line ".($linenum+$headlines)."\n";
  110. }
  111. $linedone++;
  112. }
  113. if ( $line =~ /^#\s*line\b/ and $linenum == 2 + $skiplines ) {
  114. $line = "";
  115. }
  116. }
  117. close( FILE );
  118. $file =~ s/$OPT{'s'}$//oi;
  119. $file .= '.bat' unless $file =~ /\.bat$/i or $file =~ /^-$/;
  120. open( FILE, ">$file" ) or die "Can't open $file: $!";
  121. print FILE $myhead;
  122. print FILE $start, ( $OPT{'w'} ? " -w" : "" ),
  123. "\n#line ", ($headlines+1), "\n" unless $linedone;
  124. print FILE @file[$skiplines..$#file];
  125. print FILE $tail unless $taildone;
  126. close( FILE );
  127. }
  128. __END__
  129. =head1 NAME
  130. pl2bat - wrap perl code into a batch file
  131. =head1 SYNOPSIS
  132. B<pl2bat> B<-h>
  133. B<pl2bat> [B<-w>] S<[B<-a> I<argstring>]> S<[B<-s> I<stripsuffix>]> [files]
  134. B<pl2bat> [B<-w>] S<[B<-n> I<ntargs>]> S<[B<-o> I<otherargs>]> S<[B<-s> I<stripsuffix>]> [files]
  135. =head1 DESCRIPTION
  136. This utility converts a perl script into a batch file that can be
  137. executed on DOS-like operating systems. This is intended to allow
  138. you to use a Perl script like regular programs and batch files where
  139. you just enter the name of the script [probably minus the extension]
  140. plus any command-line arguments and the script is found in your B<PATH>
  141. and run.
  142. =head2 ADVANTAGES
  143. There are several alternatives to this method of running a Perl script.
  144. They each have disadvantages that help you understand the motivation
  145. for using B<pl2bat>.
  146. =over
  147. =item 1
  148. C:> perl x:/path/to/script.pl [args]
  149. =item 2
  150. C:> perl -S script.pl [args]
  151. =item 3
  152. C:> perl -S script [args]
  153. =item 4
  154. C:> ftype Perl=perl.exe "%1" %*
  155. C:> assoc .pl=Perl
  156. then
  157. C:> script.pl [args]
  158. =item 5
  159. C:> ftype Perl=perl.exe "%1" %*
  160. C:> assoc .pl=Perl
  161. C:> set PathExt=%PathExt%;.PL
  162. then
  163. C:> script [args]
  164. =back
  165. B<1> and B<2> are the most basic invocation methods that should work on
  166. any system [DOS-like or not]. They require extra typing and require
  167. that the script user know that the script is written in Perl. This
  168. is a pain when you have lots of scripts, some written in Perl and some
  169. not. It can be quite difficult to keep track of which scripts need to
  170. be run through Perl and which do not. Even worse, scripts often get
  171. rewritten from simple batch files into more powerful Perl scripts in
  172. which case these methods would require all existing users of the scripts
  173. be updated.
  174. B<3> works on modern Win32 versions of Perl. It allows the user to
  175. omit the ".pl" or ".bat" file extension, which is a minor improvement.
  176. B<4> and B<5> work on some Win32 operating systems with some command
  177. shells. One major disadvantage with both is that you can't use them
  178. in pipelines nor with file redirection. For example, none of the
  179. following will work properly if you used method B<4> or B<5>:
  180. C:> script.pl <infile
  181. C:> script.pl >outfile
  182. C:> echo y | script.pl
  183. C:> script.pl | more
  184. This is due to a Win32 bug which Perl has no control over. This bug
  185. is the major motivation for B<pl2bat> [which was originally written
  186. for DOS] being used on Win32 systems.
  187. Note also that B<5> works on a smaller range of combinations of Win32
  188. systems and command shells while B<4> requires that the user know
  189. that the script is a Perl script [because the ".pl" extension must
  190. be entered]. This makes it hard to standardize on either of these
  191. methods.
  192. =head2 DISADVANTAGES
  193. There are several potential traps you should be aware of when you
  194. use B<pl2bat>.
  195. The generated batch file is initially processed as a batch file each
  196. time it is run. This means that, to use it from within another batch
  197. file you should preceed it with C<call> or else the calling batch
  198. file will not run any commands after the script:
  199. call script [args]
  200. Except under Windows NT, if you specify more than 9 arguments to
  201. the generated batch file then the 10th and subsequent arguments
  202. are silently ignored.
  203. Except when using F<CMD.EXE> under Windows NT, if F<perl.exe> is not
  204. in your B<PATH>, then trying to run the script will give you a generic
  205. "Command not found"-type of error message that will probably make you
  206. think that the script itself is not in your B<PATH>. When using
  207. F<CMD.EXE> under Windows NT, the generic error message is followed by
  208. "You do not have Perl in your PATH", to make this clearer.
  209. On most DOS-like operating systems, the only way to exit a batch file
  210. is to "fall off the end" of the file. B<pl2bat> implements this by
  211. doing C<goto :endofperl> and adding C<__END__> and C<:endofperl> as
  212. the last two lines of the generated batch file. This means:
  213. =over
  214. =item No line of your script should start with a colon.
  215. In particular, for this version of B<pl2bat>, C<:endofperl>,
  216. C<:WinNT>, and C<:script_failed_so_exit_with_non_zero_val> should not
  217. be used.
  218. =item Care must be taken when using C<__END__> and the C<DATA> file handle.
  219. One approach is:
  220. . #!perl
  221. . while( <DATA> ) {
  222. . last if /^__END__$/;
  223. . [...]
  224. . }
  225. . __END__
  226. . lines of data
  227. . to be processed
  228. . __END__
  229. . :endofperl
  230. The dots in the first column are only there to prevent F<cmd.exe> to interpret
  231. the C<:endofperl> line in this documentation. Otherwise F<pl2bat.bat> itself
  232. wouldn't work. See the previous item. :-)
  233. =item The batch file always "succeeds"
  234. The following commands illustrate the problem:
  235. C:> echo exit(99); >fail.pl
  236. C:> pl2bat fail.pl
  237. C:> perl -e "print system('perl fail.pl')"
  238. 99
  239. C:> perl -e "print system('fail.bat')"
  240. 0
  241. So F<fail.bat> always reports that it completed successfully. Actually,
  242. under Windows NT, we have:
  243. C:> perl -e "print system('fail.bat')"
  244. 1
  245. So, for Windows NT, F<fail.bat> fails when the Perl script fails, but
  246. the return code is always C<1>, not the return code from the Perl script.
  247. =back
  248. =head2 FUNCTION
  249. By default, the ".pl" suffix will be stripped before adding a ".bat" suffix
  250. to the supplied file names. This can be controlled with the C<-s> option.
  251. The default behavior is to have the batch file compare the C<OS>
  252. environment variable against C<"Windows_NT">. If they match, it
  253. uses the C<%*> construct to refer to all the command line arguments
  254. that were given to it, so you'll need to make sure that works on your
  255. variant of the command shell. It is known to work in the F<CMD.EXE> shell
  256. under Windows NT. 4DOS/NT users will want to put a C<ParameterChar = *>
  257. line in their initialization file, or execute C<setdos /p*> in
  258. the shell startup file.
  259. On Windows95 and other platforms a nine-argument limit is imposed
  260. on command-line arguments given to the generated batch file, since
  261. they may not support C<%*> in batch files.
  262. These can be overridden using the C<-n> and C<-o> options or the
  263. deprecated C<-a> option.
  264. =head1 OPTIONS
  265. =over 8
  266. =item B<-n> I<ntargs>
  267. Arguments to invoke perl with in generated batch file when run from
  268. Windows NT (or Windows 98, probably). Defaults to S<'-x -S %0 %*'>.
  269. =item B<-o> I<otherargs>
  270. Arguments to invoke perl with in generated batch file except when
  271. run from Windows NT (ie. when run from DOS, Windows 3.1, or Windows 95).
  272. Defaults to S<'-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9'>.
  273. =item B<-a> I<argstring>
  274. Arguments to invoke perl with in generated batch file. Specifying
  275. B<-a> prevents the batch file from checking the C<OS> environment
  276. variable to determine which operating system it is being run from.
  277. =item B<-s> I<stripsuffix>
  278. Strip a suffix string from file name before appending a ".bat"
  279. suffix. The suffix is not case-sensitive. It can be a regex if
  280. it begins with `/' (the trailing '/' is optional and a trailing
  281. C<$> is always assumed). Defaults to C</.plx?/>.
  282. =item B<-w>
  283. If no line matching C</^#!.*perl/> is found in the script, then such
  284. a line is inserted just after the new preamble. The exact line
  285. depends on C<$Config{startperl}> [see L<Config>]. With the B<-w>
  286. option, C<" -w"> is added after the value of C<$Config{startperl}>.
  287. If a line matching C</^#!.*perl/> already exists in the script,
  288. then it is not changed and the B<-w> option is ignored.
  289. =item B<-u>
  290. If the script appears to have already been processed by B<pl2bat>,
  291. then the script is skipped and not processed unless B<-u> was
  292. specified. If B<-u> is specified, the existing preamble is replaced.
  293. =item B<-h>
  294. Show command line usage.
  295. =back
  296. =head1 EXAMPLES
  297. C:\> pl2bat foo.pl bar.PM
  298. [..creates foo.bat, bar.PM.bat..]
  299. C:\> pl2bat -s "/\.pl|\.pm/" foo.pl bar.PM
  300. [..creates foo.bat, bar.bat..]
  301. C:\> pl2bat < somefile > another.bat
  302. C:\> pl2bat > another.bat
  303. print scalar reverse "rekcah lrep rehtona tsuj\n";
  304. ^Z
  305. [..another.bat is now a certified japh application..]
  306. C:\> ren *.bat *.pl
  307. C:\> pl2bat -u *.pl
  308. [..updates the wrapping of some previously wrapped scripts..]
  309. C:\> pl2bat -u -s .bat *.bat
  310. [..same as previous example except more dangerous..]
  311. =head1 BUGS
  312. C<$0> will contain the full name, including the ".bat" suffix
  313. when the generated batch file runs. If you don't like this,
  314. see runperl.bat for an alternative way to invoke perl scripts.
  315. Default behavior is to invoke Perl with the B<-S> flag, so Perl will
  316. search the B<PATH> to find the script. This may have undesirable
  317. effects.
  318. On really old versions of Win32 Perl, you can't run the script
  319. via
  320. C:> script.bat [args]
  321. and must use
  322. C:> script [args]
  323. A loop should be used to build up the argument list when not on
  324. Windows NT so more than 9 arguments can be processed.
  325. See also L</Disadvantages>.
  326. =head1 SEE ALSO
  327. perl, perlwin32, runperl.bat
  328. =cut
  329. __END__
  330. :endofperl