summaryrefslogtreecommitdiff
path: root/lib/chef/win32/api/command_line_helper.rb
blob: 6ddc74ae0f15bc32802aa802cddd0094a31bdbcb (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
#
# Author:: Kapil Chouhan <kapil.chouhan@msystechnologies.com>
# Copyright:: Copyright 2013-2020, Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require_relative "../api"

class Chef
  module ReservedNames::Win32
    module API
      module CommandLineHelper
        # extend Chef::ReservedNames::Win32
        extend Chef::ReservedNames::Win32::API

        ###############################################
        # Win32 API Bindings
        ###############################################

        ffi_lib "Shell32"

=begin
LPWSTR * CommandLineToArgvW(
  LPCWSTR lpCmdLine,
  int     *pNumArgs
);
=end

        safe_attach_function :command_line_to_argv_w, :CommandLineToArgvW, %i{pointer pointer}, :pointer

        ffi_lib "Kernel32"

=begin
LPSTR GetCommandLineA();
=end

        safe_attach_function :get_command_line, :GetCommandLineA, [], :pointer

=begin
HLOCAL LocalFree(
  _Frees_ptr_opt_ HLOCAL hMem
);
=end

        safe_attach_function :local_free, :LocalFree, [:pointer], :pointer

        ###############################################
        # Helpers
        ###############################################

        # It takes the supplied string and splits it into an array.
        def command_line_to_argv_w_helper(args)
          arguments_list = []
          argv = args.to_wstring
          result = get_command_line
          argc = FFI::MemoryPointer.new(:int)

          # Parses a Unicode command line string
          # It is return an array of pointers to the command line arguments.
          # Along with a count of such arguments
          result = command_line_to_argv_w(argv, argc)
          str_ptr = result.read_pointer
          offset = 0
          number_of_agrs = argc.read_int
          number_of_agrs.times do
            new_str_pointer = str_ptr.+(offset)
            argument = new_str_pointer.read_wstring
            arguments_list << argument
            offset = offset + argument.length * 2 + 2
          end
          local_free(result)
          arguments_list
        end
      end
    end
  end
end