aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core/CommandLine/CommandLineArguments.cs
blob: 37adcfd32487dbc751bfbe4c1cbf3416e0590f92 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolset.Core.CommandLine
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Text.RegularExpressions;
    using WixToolset.Extensibility.Services;

    internal class CommandLineArguments : ICommandLineArguments
    {
        public string[] OriginalArguments { get; set; }

        public string[] Arguments { get; set; }

        public string[] Extensions { get; set; }

        public string ErrorArgument { get; set; }

        private IServiceProvider ServiceProvider { get; }

        public CommandLineArguments(IServiceProvider serviceProvider)
        {
            this.ServiceProvider = serviceProvider;
        }

        public void Populate(string commandLine)
        {
            var args = CommandLineArguments.ParseArgumentsToArray(commandLine);

            this.Populate(args.ToArray());
        }

        public void Populate(string[] args)
        {
            this.FlattenArgumentsWithResponseFilesIntoOriginalArguments(args);

            this.ProcessArgumentsAndParseExtensions(this.OriginalArguments);
        }

        public IParseCommandLine Parse()
        {
            var messaging = (IMessaging)this.ServiceProvider.GetService(typeof(IMessaging));

            return new ParseCommandLine(messaging, this.Arguments, this.ErrorArgument);
        }

        private void FlattenArgumentsWithResponseFilesIntoOriginalArguments(string[] commandLineArguments)
        {
            List<string> args = new List<string>();

            foreach (var arg in commandLineArguments)
            {
                if ('@' == arg[0])
                {
                    var responseFileArguments = CommandLineArguments.ParseResponseFile(arg.Substring(1));
                    args.AddRange(responseFileArguments);
                }
                else
                {
                    args.Add(arg);
                }
            }

            this.OriginalArguments = args.ToArray();
        }

        private void ProcessArgumentsAndParseExtensions(string[] args)
        {
            var arguments = new List<string>();
            var extensions = new List<string>();

            for (var i = 0; i < args.Length; ++i)
            {
                var arg = args[i];

                if ("-ext" == arg || "/ext" == arg)
                {
                    if (!CommandLineArguments.IsSwitchAt(args, ++i))
                    {
                        extensions.Add(args[i]);
                    }
                    else
                    {
                        this.ErrorArgument = arg;
                        break;
                    }
                }
                else
                {
                    arguments.Add(arg);
                }
            }

            this.Arguments = arguments.ToArray();
            this.Extensions = extensions.ToArray();
        }

        private static List<string> ParseResponseFile(string responseFile)
        {
            string arguments;

            using (StreamReader reader = new StreamReader(responseFile))
            {
                arguments = reader.ReadToEnd();
            }

            return CommandLineArguments.ParseArgumentsToArray(arguments);
        }

        private static List<string> ParseArgumentsToArray(string arguments)
        {
            // Scan and parse the arguments string, dividing up the arguments based on whitespace.
            // Unescaped quotes cause whitespace to be ignored, while the quotes themselves are removed.
            // Quotes may begin and end inside arguments; they don't necessarily just surround whole arguments.
            // Escaped quotes and escaped backslashes also need to be unescaped by this process.

            // Collects the final list of arguments to be returned.
            var argsList = new List<string>();

            // True if we are inside an unescaped quote, meaning whitespace should be ignored.
            var insideQuote = false;

            // Index of the start of the current argument substring; either the start of the argument
            // or the start of a quoted or unquoted sequence within it.
            var partStart = 0;

            // The current argument string being built; when completed it will be added to the list.
            var arg = new StringBuilder();

            for (int i = 0; i <= arguments.Length; i++)
            {
                if (i == arguments.Length || (Char.IsWhiteSpace(arguments[i]) && !insideQuote))
                {
                    // Reached a whitespace separator or the end of the string.

                    // Finish building the current argument.
                    arg.Append(arguments.Substring(partStart, i - partStart));

                    // Skip over the whitespace character.
                    partStart = i + 1;

                    // Add the argument to the list if it's not empty.
                    if (arg.Length > 0)
                    {
                        argsList.Add(CommandLineArguments.ExpandEnvironmentVariables(arg.ToString()));
                        arg.Length = 0;
                    }
                }
                else if (i > partStart && arguments[i - 1] == '\\')
                {
                    // Check the character following an unprocessed backslash.
                    // Unescape quotes, and backslashes followed by a quote.
                    if (arguments[i] == '"' || (arguments[i] == '\\' && arguments.Length > i + 1 && arguments[i + 1] == '"'))
                    {
                        // Unescape the quote or backslash by skipping the preceeding backslash.
                        arg.Append(arguments.Substring(partStart, i - 1 - partStart));
                        arg.Append(arguments[i]);
                        partStart = i + 1;
                    }
                }
                else if (arguments[i] == '"')
                {
                    // Add the quoted or unquoted section to the argument string.
                    arg.Append(arguments.Substring(partStart, i - partStart));

                    // And skip over the quote character.
                    partStart = i + 1;

                    insideQuote = !insideQuote;
                }
            }

            return argsList;
        }

        private static string ExpandEnvironmentVariables(string arguments)
        {
            var id = Environment.GetEnvironmentVariables();

            var regex = new Regex("(?<=\\%)(?:[\\w\\.]+)(?=\\%)");
            MatchCollection matches = regex.Matches(arguments);

            string value = String.Empty;
            for (int i = 0; i <= (matches.Count - 1); i++)
            {
                try
                {
                    var key = matches[i].Value;
                    regex = new Regex(String.Concat("(?i)(?:\\%)(?:", key, ")(?:\\%)"));
                    value = id[key].ToString();
                    arguments = regex.Replace(arguments, value);
                }
                catch (NullReferenceException)
                {
                    // Collapse unresolved environment variables.
                    arguments = regex.Replace(arguments, value);
                }
            }

            return arguments;
        }

        private static bool IsSwitchAt(string[] args, int index)
        {
            return args.Length > index && !String.IsNullOrEmpty(args[index]) && ('/' == args[index][0] || '-' == args[index][0]);
        }
    }
}