| Filename | /usr/share/perl5/String/ShellQuote.pm |
| Statements | Executed 660 statements in 2.81ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 33 | 1 | 1 | 1.09ms | 1.28ms | String::ShellQuote::_shell_quote_backend |
| 33 | 2 | 1 | 440µs | 1.72ms | String::ShellQuote::shell_quote |
| 62 | 2 | 1 | 102µs | 102µs | String::ShellQuote::CORE:match (opcode) |
| 83 | 5 | 1 | 87µs | 87µs | String::ShellQuote::CORE:subst (opcode) |
| 1 | 1 | 1 | 14µs | 20µs | String::ShellQuote::BEGIN@28 |
| 1 | 1 | 1 | 9µs | 51µs | String::ShellQuote::BEGIN@29 |
| 0 | 0 | 0 | 0s | 0s | String::ShellQuote::croak |
| 0 | 0 | 0 | 0s | 0s | String::ShellQuote::shell_comment_quote |
| 0 | 0 | 0 | 0s | 0s | String::ShellQuote::shell_quote_best_effort |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | # $Id: ShellQuote.pm,v 1.11 2010-06-11 20:08:57 roderick Exp $ | ||||
| 2 | # | ||||
| 3 | # Copyright (c) 1997 Roderick Schertler. All rights reserved. This | ||||
| 4 | # program is free software; you can redistribute it and/or modify it | ||||
| 5 | # under the same terms as Perl itself. | ||||
| 6 | |||||
| 7 | =head1 NAME | ||||
| 8 | |||||
| 9 | String::ShellQuote - quote strings for passing through the shell | ||||
| 10 | |||||
| 11 | =head1 SYNOPSIS | ||||
| 12 | |||||
| 13 | $string = shell_quote @list; | ||||
| 14 | $string = shell_quote_best_effort @list; | ||||
| 15 | $string = shell_comment_quote $string; | ||||
| 16 | |||||
| 17 | =head1 DESCRIPTION | ||||
| 18 | |||||
| 19 | This module contains some functions which are useful for quoting strings | ||||
| 20 | which are going to pass through the shell or a shell-like object. | ||||
| 21 | |||||
| 22 | =over | ||||
| 23 | |||||
| 24 | =cut | ||||
| 25 | |||||
| 26 | package String::ShellQuote; | ||||
| 27 | |||||
| 28 | 2 | 28µs | 2 | 26µs | # spent 20µs (14+6) within String::ShellQuote::BEGIN@28 which was called:
# once (14µs+6µs) by RBM::BEGIN@18 at line 28 # spent 20µs making 1 call to String::ShellQuote::BEGIN@28
# spent 6µs making 1 call to strict::import |
| 29 | 2 | 694µs | 2 | 94µs | # spent 51µs (9+42) within String::ShellQuote::BEGIN@29 which was called:
# once (9µs+42µs) by RBM::BEGIN@18 at line 29 # spent 51µs making 1 call to String::ShellQuote::BEGIN@29
# spent 42µs making 1 call to vars::import |
| 30 | |||||
| 31 | 1 | 900ns | require Exporter; | ||
| 32 | |||||
| 33 | 1 | 300ns | $VERSION = '1.04'; | ||
| 34 | 1 | 12µs | @ISA = qw(Exporter); | ||
| 35 | 1 | 600ns | @EXPORT = qw(shell_quote shell_quote_best_effort shell_comment_quote); | ||
| 36 | |||||
| 37 | sub croak { | ||||
| 38 | require Carp; | ||||
| 39 | goto &Carp::croak; | ||||
| 40 | } | ||||
| 41 | |||||
| 42 | # spent 1.28ms (1.09+189µs) within String::ShellQuote::_shell_quote_backend which was called 33 times, avg 39µs/call:
# 33 times (1.09ms+189µs) by String::ShellQuote::shell_quote at line 119, avg 39µs/call | ||||
| 43 | 33 | 40µs | my @in = @_; | ||
| 44 | 33 | 32µs | my @err = (); | ||
| 45 | |||||
| 46 | if (0) { | ||||
| 47 | require RS::Handy; | ||||
| 48 | print RS::Handy::data_dump(\@in); | ||||
| 49 | } | ||||
| 50 | |||||
| 51 | 33 | 24µs | return \@err, '' unless @in; | ||
| 52 | |||||
| 53 | 33 | 31µs | my $ret = ''; | ||
| 54 | 33 | 16µs | my $saw_non_equal = 0; | ||
| 55 | 33 | 79µs | foreach (@in) { | ||
| 56 | 33 | 32µs | if (!defined $_ or $_ eq '') { | ||
| 57 | 2 | 1µs | $_ = "''"; | ||
| 58 | 2 | 3µs | next; | ||
| 59 | } | ||||
| 60 | |||||
| 61 | 31 | 175µs | 31 | 47µs | if (s/\x00//g) { # spent 47µs making 31 calls to String::ShellQuote::CORE:subst, avg 2µs/call |
| 62 | push @err, "No way to quote string containing null (\\000) bytes"; | ||||
| 63 | } | ||||
| 64 | |||||
| 65 | 31 | 14µs | my $escape = 0; | ||
| 66 | |||||
| 67 | # = needs quoting when it's the first element (or part of a | ||||
| 68 | # series of such elements), as in command position it's a | ||||
| 69 | # program-local environment setting | ||||
| 70 | |||||
| 71 | 31 | 270µs | 31 | 32µs | if (/=/) { # spent 32µs making 31 calls to String::ShellQuote::CORE:match, avg 1µs/call |
| 72 | if (!$saw_non_equal) { | ||||
| 73 | $escape = 1; | ||||
| 74 | } | ||||
| 75 | } | ||||
| 76 | else { | ||||
| 77 | 31 | 9µs | $saw_non_equal = 1; | ||
| 78 | } | ||||
| 79 | |||||
| 80 | 31 | 157µs | 31 | 70µs | if (m|[^\w!%+,\-./:=@^]|) { # spent 70µs making 31 calls to String::ShellQuote::CORE:match, avg 2µs/call |
| 81 | $escape = 1; | ||||
| 82 | } | ||||
| 83 | |||||
| 84 | 31 | 33µs | if ($escape | ||
| 85 | || (!$saw_non_equal && /=/)) { | ||||
| 86 | |||||
| 87 | # ' -> '\'' | ||||
| 88 | 13 | 45µs | 13 | 16µs | s/'/'\\''/g; # spent 16µs making 13 calls to String::ShellQuote::CORE:subst, avg 1µs/call |
| 89 | |||||
| 90 | # make multiple ' in a row look simpler | ||||
| 91 | # '\'''\'''\'' -> '"'''"' | ||||
| 92 | 13 | 43µs | 13 | 12µs | s|((?:'\\''){2,})|q{'"} . (q{'} x (length($1) / 4)) . q{"'}|ge; # spent 12µs making 13 calls to String::ShellQuote::CORE:subst, avg 900ns/call |
| 93 | |||||
| 94 | 13 | 17µs | $_ = "'$_'"; | ||
| 95 | 13 | 31µs | 13 | 5µs | s/^''//; # spent 5µs making 13 calls to String::ShellQuote::CORE:subst, avg 362ns/call |
| 96 | 13 | 38µs | 13 | 7µs | s/''$//; # spent 7µs making 13 calls to String::ShellQuote::CORE:subst, avg 538ns/call |
| 97 | } | ||||
| 98 | } | ||||
| 99 | continue { | ||||
| 100 | $ret .= "$_ "; | ||||
| 101 | } | ||||
| 102 | |||||
| 103 | 33 | 56µs | chop $ret; | ||
| 104 | 33 | 250µs | return \@err, $ret; | ||
| 105 | } | ||||
| 106 | |||||
| 107 | =item B<shell_quote> [I<string>]... | ||||
| 108 | |||||
| 109 | B<shell_quote> quotes strings so they can be passed through the shell. | ||||
| 110 | Each I<string> is quoted so that the shell will pass it along as a | ||||
| 111 | single argument and without further interpretation. If no I<string>s | ||||
| 112 | are given an empty string is returned. | ||||
| 113 | |||||
| 114 | If any I<string> can't be safely quoted B<shell_quote> will B<croak>. | ||||
| 115 | |||||
| 116 | =cut | ||||
| 117 | |||||
| 118 | # spent 1.72ms (440µs+1.28) within String::ShellQuote::shell_quote which was called 33 times, avg 52µs/call:
# 18 times (247µs+677µs) by Template::Stash::XS::get at line 4 of /root/tor-browser-build/input text, avg 51µs/call
# 15 times (193µs+603µs) by Template::Stash::XS::get at line 8 of /root/tor-browser-build/input text, avg 53µs/call | ||||
| 119 | 33 | 165µs | 33 | 1.28ms | my ($rerr, $s) = _shell_quote_backend @_; # spent 1.28ms making 33 calls to String::ShellQuote::_shell_quote_backend, avg 39µs/call |
| 120 | |||||
| 121 | 33 | 28µs | if (@$rerr) { | ||
| 122 | my %seen; | ||||
| 123 | @$rerr = grep { !$seen{$_}++ } @$rerr; | ||||
| 124 | my $s = join '', map { "shell_quote(): $_\n" } @$rerr; | ||||
| 125 | chomp $s; | ||||
| 126 | croak $s; | ||||
| 127 | } | ||||
| 128 | 33 | 479µs | return $s; | ||
| 129 | } | ||||
| 130 | |||||
| 131 | =item B<shell_quote_best_effort> [I<string>]... | ||||
| 132 | |||||
| 133 | This is like B<shell_quote>, excpet if the string can't be safely quoted | ||||
| 134 | it does the best it can and returns the result, instead of dying. | ||||
| 135 | |||||
| 136 | =cut | ||||
| 137 | |||||
| 138 | sub shell_quote_best_effort { | ||||
| 139 | my ($rerr, $s) = _shell_quote_backend @_; | ||||
| 140 | |||||
| 141 | return $s; | ||||
| 142 | } | ||||
| 143 | |||||
| 144 | =item B<shell_comment_quote> [I<string>] | ||||
| 145 | |||||
| 146 | B<shell_comment_quote> quotes the I<string> so that it can safely be | ||||
| 147 | included in a shell-style comment (the current algorithm is that a sharp | ||||
| 148 | character is placed after any newlines in the string). | ||||
| 149 | |||||
| 150 | This routine might be changed to accept multiple I<string> arguments | ||||
| 151 | in the future. I haven't done this yet because I'm not sure if the | ||||
| 152 | I<string>s should be joined with blanks ($") or nothing ($,). Cast | ||||
| 153 | your vote today! Be sure to justify your answer. | ||||
| 154 | |||||
| 155 | =cut | ||||
| 156 | |||||
| 157 | sub shell_comment_quote { | ||||
| 158 | return '' unless @_; | ||||
| 159 | unless (@_ == 1) { | ||||
| 160 | croak "Too many arguments to shell_comment_quote " | ||||
| 161 | . "(got " . @_ . " expected 1)"; | ||||
| 162 | } | ||||
| 163 | local $_ = shift; | ||||
| 164 | s/\n/\n#/g; | ||||
| 165 | return $_; | ||||
| 166 | } | ||||
| 167 | |||||
| 168 | 1 | 5µs | 1; | ||
| 169 | |||||
| 170 | __END__ | ||||
sub String::ShellQuote::CORE:match; # opcode | |||||
# spent 87µs within String::ShellQuote::CORE:subst which was called 83 times, avg 1µs/call:
# 31 times (47µs+0s) by String::ShellQuote::_shell_quote_backend at line 61, avg 2µs/call
# 13 times (16µs+0s) by String::ShellQuote::_shell_quote_backend at line 88, avg 1µs/call
# 13 times (12µs+0s) by String::ShellQuote::_shell_quote_backend at line 92, avg 900ns/call
# 13 times (7µs+0s) by String::ShellQuote::_shell_quote_backend at line 96, avg 538ns/call
# 13 times (5µs+0s) by String::ShellQuote::_shell_quote_backend at line 95, avg 362ns/call |