| 1 | package ExtUtils::Helpers::Windows; | ||
| 2 | $ExtUtils::Helpers::Windows::VERSION = '0.028'; | ||
| 3 | use strict; | ||
| 4 | use warnings FATAL => 'all'; | ||
| 5 | |||
| 6 | use Exporter 5.57 'import'; | ||
| 7 | our @EXPORT = qw/make_executable split_like_shell detildefy/; | ||
| 8 | |||
| 9 | use Config; | ||
| 10 | use Carp qw/carp croak/; | ||
| 11 | use ExtUtils::PL2Bat 'pl2bat'; | ||
| 12 | |||
| 13 | sub make_executable { | ||
| 14 | my $script = shift; | ||
| 15 | if (-T $script && $script !~ / \. (?:bat|cmd) $ /x) { | ||
| 16 | pl2bat(in => $script, update => 1); | ||
| 17 | } | ||
| 18 | return; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub split_like_shell { | ||
| 22 | # As it turns out, Windows command-parsing is very different from | ||
| 23 | # Unix command-parsing. Double-quotes mean different things, | ||
| 24 | # backslashes don't necessarily mean escapes, and so on. So we | ||
| 25 | # can't use Text::ParseWords::shellwords() to break a command string | ||
| 26 | # into words. The algorithm below was bashed out by Randy and Ken | ||
| 27 | # (mostly Randy), and there are a lot of regression tests, so we | ||
| 28 | # should feel free to adjust if desired. | ||
| 29 | |||
| 30 | local ($_) = @_; | ||
| 31 | |||
| 32 | my @argv; | ||
| 33 | return @argv unless defined && length; | ||
| 34 | |||
| 35 | my $arg = ''; | ||
| 36 | my ($i, $quote_mode ) = ( 0, 0 ); | ||
| 37 | |||
| 38 | while ( $i < length ) { | ||
| 39 | |||
| 40 | my $ch = substr $_, $i, 1; | ||
| 41 | my $next_ch = substr $_, $i+1, 1; | ||
| 42 | |||
| 43 | if ( $ch eq '\\' && $next_ch eq '"' ) { | ||
| 44 | $arg .= '"'; | ||
| 45 | $i++; | ||
| 46 | } elsif ( $ch eq '\\' && $next_ch eq '\\' ) { | ||
| 47 | $arg .= '\\'; | ||
| 48 | $i++; | ||
| 49 | } elsif ( $ch eq '"' && $next_ch eq '"' && $quote_mode ) { | ||
| 50 | $quote_mode = !$quote_mode; | ||
| 51 | $arg .= '"'; | ||
| 52 | $i++; | ||
| 53 | } elsif ( $ch eq '"' && $next_ch eq '"' && !$quote_mode && | ||
| 54 | ( $i + 2 == length() || substr( $_, $i + 2, 1 ) eq ' ' ) | ||
| 55 | ) { # for cases like: a"" => [ 'a' ] | ||
| 56 | push @argv, $arg; | ||
| 57 | $arg = ''; | ||
| 58 | $i += 2; | ||
| 59 | } elsif ( $ch eq '"' ) { | ||
| 60 | $quote_mode = !$quote_mode; | ||
| 61 | } elsif ( $ch =~ /\s/ && !$quote_mode ) { | ||
| 62 | push @argv, $arg if $arg; | ||
| 63 | $arg = ''; | ||
| 64 | ++$i while substr( $_, $i + 1, 1 ) =~ /\s/; | ||
| 65 | } else { | ||
| 66 | $arg .= $ch; | ||
| 67 | } | ||
| 68 | |||
| 69 | $i++; | ||
| 70 | } | ||
| 71 | |||
| 72 | push @argv, $arg if defined $arg && length $arg; | ||
| 73 | return @argv; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub detildefy { | ||
| 77 | my $value = shift; | ||
| 78 | $value =~ s{ ^ ~ (?= [/\\] | $ ) }[$ENV{USERPROFILE}]x if $ENV{USERPROFILE}; | ||
| 79 | return $value; | ||
| 80 | } | ||
| 81 | |||
| 82 | 1; | ||
| 83 | |||
| 84 | # ABSTRACT: Windows specific helper bits | ||
| 85 | |||
| 86 | __END__ |