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
|
// 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.Mba.Core
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.InteropServices;
/// <summary>
/// Default implementation of <see cref="IBootstrapperCommand"/>.
/// </summary>
public sealed class BootstrapperCommand : IBootstrapperCommand
{
/// <summary>
/// See <see cref="IBootstrapperCommand"/>.
/// </summary>
public BootstrapperCommand(
LaunchAction action,
Display display,
string commandLine,
int cmdShow,
ResumeType resume,
IntPtr splashScreen,
RelationType relation,
bool passthrough,
string layoutDirectory,
string bootstrapperWorkingFolder,
string bootstrapperApplicationDataPath)
{
this.Action = action;
this.Display = display;
this.CommandLine = commandLine;
this.CmdShow = cmdShow;
this.Resume = resume;
this.SplashScreen = splashScreen;
this.Relation = relation;
this.Passthrough = passthrough;
this.LayoutDirectory = layoutDirectory;
this.BootstrapperWorkingFolder = bootstrapperWorkingFolder;
this.BootstrapperApplicationDataPath = bootstrapperApplicationDataPath;
}
/// <inheritdoc/>
public LaunchAction Action { get; }
/// <inheritdoc/>
public Display Display { get; }
/// <inheritdoc/>
public string CommandLine { get; }
/// <inheritdoc/>
public int CmdShow { get; }
/// <inheritdoc/>
public ResumeType Resume { get; }
/// <inheritdoc/>
public IntPtr SplashScreen { get; }
/// <inheritdoc/>
public RelationType Relation { get; }
/// <inheritdoc/>
public bool Passthrough { get; }
/// <inheritdoc/>
public string LayoutDirectory { get; }
/// <inheritdoc/>
public string BootstrapperWorkingFolder { get; }
/// <inheritdoc/>
public string BootstrapperApplicationDataPath { get; }
/// <inheritdoc/>
public IMbaCommand ParseCommandLine()
{
var args = ParseCommandLineToArgs(this.CommandLine);
var unknownArgs = new List<string>();
var variables = new List<KeyValuePair<string, string>>();
var restart = Restart.Unknown;
foreach (var arg in args)
{
var unknownArg = false;
if (arg[0] == '-' || arg[0] == '/')
{
var parameter = arg.Substring(1).ToLowerInvariant();
switch (parameter)
{
case "norestart":
if (restart == Restart.Unknown)
{
restart = Restart.Never;
}
break;
case "forcerestart":
if (restart == Restart.Unknown)
{
restart = Restart.Always;
}
break;
default:
unknownArg = true;
break;
}
}
else
{
var index = arg.IndexOf('=');
if (index == -1)
{
unknownArg = true;
}
else
{
var name = arg.Substring(0, index);
var value = arg.Substring(index + 1);
variables.Add(new KeyValuePair<string, string>(name, value));
}
}
if (unknownArg)
{
unknownArgs.Add(arg);
}
}
if (restart == Restart.Unknown)
{
restart = this.Display < Display.Full ? Restart.Automatic : Restart.Prompt;
}
return new MbaCommand
{
Restart = restart,
UnknownCommandLineArgs = unknownArgs.ToArray(),
Variables = variables.ToArray(),
};
}
/// <summary>
/// Gets the command line arguments as a string array.
/// </summary>
/// <returns>
/// Array of command line arguments.
/// </returns>
/// <exception type="Win32Exception">The command line could not be parsed into an array.</exception>
/// <remarks>
/// This method uses the same parsing as the operating system which handles quotes and spaces correctly.
/// </remarks>
public static string[] ParseCommandLineToArgs(string commandLine)
{
if (null == commandLine)
{
return new string[0];
}
// Parse the filtered command line arguments into a native array.
int argc = 0;
// CommandLineToArgvW tries to treat the first argument as the path to the process,
// which fails pretty miserably if your first argument is something like
// FOO="C:\Program Files\My Company". So give it something harmless to play with.
IntPtr argv = NativeMethods.CommandLineToArgvW("ignored " + commandLine, out argc);
if (IntPtr.Zero == argv)
{
// Throw an exception with the last error.
throw new Win32Exception();
}
// Marshal each native array pointer to a managed string.
try
{
// Skip "ignored" argument/hack.
string[] args = new string[argc - 1];
for (int i = 1; i < argc; ++i)
{
IntPtr argvi = Marshal.ReadIntPtr(argv, i * IntPtr.Size);
args[i - 1] = Marshal.PtrToStringUni(argvi);
}
return args;
}
finally
{
NativeMethods.LocalFree(argv);
}
}
}
}
|