Debian Codegraph @

lib/ansible/module_utils/powershell/Ansible.ModuleUtils.ArgvParser.psm1

raw
1 # Copyright (c) 2017 Ansible Project
2 # Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)
3
4 # The rules used in these functions are derived from the below
5 # https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments
6 # https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
7
8 Function Escape-Argument($argument, $force_quote = $false) {
9 # this converts a single argument to an escaped version, use Join-Arguments
10 # instead of this function as this only escapes a single string.
11
12 # check if argument contains a space, \n, \t, \v or "
13 if ($force_quote -eq $false -and $argument.Length -gt 0 -and $argument -notmatch "[ \n\t\v`"]") {
14 # argument does not need escaping (and we don't want to force it),
15 # return as is
16 return $argument
17 }
18 else {
19 # we need to quote the arg so start with "
20 $new_argument = '"'
21
22 for ($i = 0; $i -lt $argument.Length; $i++) {
23 $num_backslashes = 0
24
25 # get the number of \ from current char until end or not a \
26 while ($i -ne ($argument.Length - 1) -and $argument[$i] -eq "\") {
27 $num_backslashes++
28 $i++
29 }
30
31 $current_char = $argument[$i]
32 if ($i -eq ($argument.Length - 1) -and $current_char -eq "\") {
33 # We are at the end of the string so we need to add the same \
34 # * 2 as the end char would be a "
35 $new_argument += ("\" * ($num_backslashes + 1) * 2)
36 }
37 elseif ($current_char -eq '"') {
38 # we have a inline ", we need to add the existing \ but * by 2
39 # plus another 1
40 $new_argument += ("\" * (($num_backslashes * 2) + 1))
41 $new_argument += $current_char
42 }
43 else {
44 # normal character so no need to escape the \ we have counted
45 $new_argument += ("\" * $num_backslashes)
46 $new_argument += $current_char
47 }
48 }
49
50 # we need to close the special arg with a "
51 $new_argument += '"'
52 return $new_argument
53 }
54 }
55
56 Function Argv-ToString($arguments, $force_quote = $false) {
57 # Takes in a list of un escaped arguments and convert it to a single string
58 # that can be used when starting a new process. It will escape the
59 # characters as necessary in the list.
60 # While there is a CommandLineToArgvW function there is a no
61 # ArgvToCommandLineW that we can call to convert a list to an escaped
62 # string.
63 # You can also pass in force_quote so that each argument is quoted even
64 # when not necessary, by default only arguments with certain characters are
65 # quoted.
66 # TODO: add in another switch which will escape the args for cmd.exe
67
68 $escaped_arguments = @()
69 foreach ($argument in $arguments) {
70 $escaped_argument = Escape-Argument -argument $argument -force_quote $force_quote
71 $escaped_arguments += $escaped_argument
72 }
73
74 return ($escaped_arguments -join ' ')
75 }
76
77 # this line must stay at the bottom to ensure all defined module parts are exported
78 Export-ModuleMember -Alias * -Function * -Cmdlet *