summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/perl/win32/bin/pl2bat.pl
blob: 73ae87164dac66fbc0c950d66fe7b018112f1223 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!perl -w
require 5;
use Getopt::Std;

$0 =~ s|.*[/\\]||;

my $usage = <<EOT;
Usage:  $0 [-h] [-a argstring] [-s stripsuffix] [files]
        -a argstring    arguments to invoke perl with in generated file
                            Defaults to "-x -S %0 %*" on WindowsNT,
			    "-x -S %0 %1 %2 %3 %4 %5 %6 %7 %8 %9" otherwise
        -s stripsuffix  strip this suffix from file before appending ".bat"
	                    Not case-sensitive
                            Can be a regex if it begins with `/'
	                    Defaults to "/\.pl/"
        -h              show this help
EOT

my %OPT = ();
warn($usage), exit(0) if !getopts('ha:s:',\%OPT) or $OPT{'h'};
$OPT{'a'} = ($^O eq 'MSWin32' and &Win32::IsWinNT
             ? '-x -S %0 %*'
	     : '-x -S %0 %1 %2 %3 %4 %5 %6 %7 %8 %9')
	  unless exists $OPT{'a'};
$OPT{'s'} = '.pl' unless exists $OPT{'s'};
$OPT{'s'} = ($OPT{'s'} =~ m|^/([^/]*)| ? $1 : "\Q$OPT{'s'}\E");

(my $head = <<EOT) =~ s/^\t//gm;
	\@rem = '--*-Perl-*--
	\@echo off
	perl $OPT{'a'}
	goto endofperl
	\@rem ';
EOT
my $headlines = 2 + ($head =~ tr/\n/\n/);
my $tail = "__END__\n:endofperl\n";

@ARGV = ('-') unless @ARGV;

process(@ARGV);

sub process {
   LOOP:
    foreach ( @_ ) {
    	my $myhead = $head;
    	my $linedone = 0;
	my $linenum = $headlines;
	my $line;
        open( FILE, $_ ) or die "$0: Can't open $_: $!";
        @file = <FILE>;
        foreach $line ( @file ) {
	    $linenum++;
            if ( $line =~ /^:endofperl/) {
                warn "$0: $_ has already been converted to a batch file!\n";
                next LOOP;
	    }
	    if ( not $linedone and $line =~ /^#!.*perl/ ) {
		$line .= "#line $linenum\n";
		$linedone++;
	    }
        }
        close( FILE );
        s/$OPT{'s'}$//oi;
        $_ .= '.bat' unless /\.bat$/i or /^-$/;
        open( FILE, ">$_" ) or die "Can't open $_: $!";
	print FILE $myhead;
	print FILE "#!perl\n#line " . ($headlines+1) . "\n" unless $linedone;
        print FILE @file, $tail;
        close( FILE );
    }
}
__END__

=head1 NAME

pl2bat - wrap perl code into a batch file

=head1 SYNOPSIS

B<pl2bat> [B<-h>] S<[B<-a> I<argstring>]> S<[B<-s> I<stripsuffix>]> [files]

=head1 DESCRIPTION

This utility converts a perl script into a batch file that can be
executed on DOS-like operating systems.

Note that by default, the ".pl" suffix will be stripped before adding
a ".bat" suffix to the supplied file names.  This can be controlled
with the C<-s> option.

The default behavior on WindowsNT is to generate a batch file that
uses the C<%*> construct to refer to all the command line arguments
that were given to it, so you'll need to make sure that works on your
variant of the command shell.  It is known to work in the cmd.exe shell
under WindowsNT.  4DOS/NT users will want to put a C<ParameterChar = *>
line in their initialization file, or execute C<setdos /p*> in
the shell startup file.  On Windows95 and other platforms a nine
argument limit is imposed on command-line arguments given to the
generated batch file, since they may not support C<%*> in batch files.
This can be overridden using the C<-a> option.

=head1 OPTIONS

=over 8

=item B<-a> I<argstring>

Arguments to invoke perl with in generated batch file.  Defaults to
S<"-x -S %0 %*"> on WindowsNT, S<"-x -S %0 %1 %2 %3 %4 %5 %6 %7 %8 %9">
on other platforms.

=item B<-s> I<stripsuffix>

Strip a suffix string from file name before appending a ".bat"
suffix.  The suffix is not case-sensitive.  It can be a regex if it
begins with `/' (the trailing '/' being optional.  Defaults to ".pl".

=item B<-h>

Show command line usage.

=back

=head1 EXAMPLES

	C:\> pl2bat foo.pl bar.PM 
	[..creates foo.bat, bar.PM.bat..]
	
	C:\> pl2bat -s "/\.pl|\.pm/" foo.pl bar.PM
	[..creates foo.bat, bar.bat..]
	
	C:\> pl2bat < somefile > another.bat
	
	C:\> pl2bat > another.bat
	print scalar reverse "rekcah lrep rehtona tsuj\n";
	^Z
	[..another.bat is now a certified japh application..]

=head1 BUGS

C<$0> will contain the full name, including the ".bat" suffix
when the generated batch file runs.  If you don't like this,
see runperl.bat for an alternative way to invoke perl scripts.

Default behavior is to invoke Perl with the -S flag, so Perl will
search the PATH to find the script.  This may have undesirable
effects.

=head1 SEE ALSO

perl, perlwin32, runperl.bat

=cut