/
NativeCommandParameterBinder.cs
481 lines (422 loc) · 20 KB
/
NativeCommandParameterBinder.cs
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Management.Automation.Internal;
using System.Text;
using Microsoft.PowerShell.Commands;
namespace System.Management.Automation
{
using Language;
/// <summary>
/// The parameter binder for native commands.
/// </summary>
internal class NativeCommandParameterBinder : ParameterBinderBase
{
#region ctor
/// <summary>
/// Constructs a NativeCommandParameterBinder.
/// </summary>
/// <param name="command">
/// The NativeCommand to bind to.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="command"/>.Context is null
/// </exception>
internal NativeCommandParameterBinder(
NativeCommand command) : base(command.MyInvocation, command.Context, command)
{
_nativeCommand = command;
}
#endregion ctor
#region internal members
#region Parameter binding
/// <summary>
/// Binds a parameter for a native command (application).
/// </summary>
/// <param name="name">
/// The name of the parameter to bind the value to. For applications
/// this just becomes another parameter...
/// </param>
/// <param name="value">
/// The value to bind to the parameter. It should be assumed by
/// derived classes that the proper type coercion has already taken
/// place and that any prerequisite metadata has been satisfied.
/// </param>
/// <param name="parameterMetadata"></param>
internal override void BindParameter(string name, object value, CompiledCommandParameter parameterMetadata)
{
Diagnostics.Assert(false, "Unreachable code");
throw new NotSupportedException();
}
internal override object GetDefaultParameterValue(string name)
{
return null;
}
internal void BindParameters(Collection<CommandParameterInternal> parameters)
{
bool sawVerbatimArgumentMarker = false;
bool first = true;
foreach (CommandParameterInternal parameter in parameters)
{
if (!first)
{
_arguments.Append(' ');
}
first = false;
if (parameter.ParameterNameSpecified)
{
Diagnostics.Assert(!parameter.ParameterText.Contains(' '), "Parameters cannot have whitespace");
PossiblyGlobArg(parameter.ParameterText, StringConstantType.BareWord);
if (parameter.SpaceAfterParameter)
{
_arguments.Append(' ');
}
}
if (parameter.ArgumentSpecified)
{
// If this is the verbatim argument marker, we don't pass it on to the native command.
// We do need to remember it though - we'll expand environment variables in subsequent args.
object argValue = parameter.ArgumentValue;
if (string.Equals("--%", argValue as string, StringComparison.OrdinalIgnoreCase))
{
sawVerbatimArgumentMarker = true;
continue;
}
if (argValue != AutomationNull.Value && argValue != UnboundParameter.Value)
{
// ArrayLiteralAst is used to reconstruct the correct argument, e.g.
// windbg -k com:port=\\devbox\pipe\debug,pipe,resets=0,reconnect
// The parser produced an array of strings but marked the parameter so we
// can properly reconstruct the correct command line.
StringConstantType stringConstantType = StringConstantType.BareWord;
ArrayLiteralAst arrayLiteralAst = null;
switch (parameter?.ArgumentAst)
{
case StringConstantExpressionAst sce:
stringConstantType = sce.StringConstantType;
break;
case ExpandableStringExpressionAst ese:
stringConstantType = ese.StringConstantType;
break;
case ArrayLiteralAst ala:
arrayLiteralAst = ala;
break;
}
// Prior to PSNativePSPathResolution experimental feature, a single quote worked the same as a double quote
// so if the feature is not enabled, we treat any quotes as double quotes. When this feature is no longer
// experimental, this code here needs to be removed.
if (!ExperimentalFeature.IsEnabled("PSNativePSPathResolution") && stringConstantType == StringConstantType.SingleQuoted)
{
stringConstantType = StringConstantType.DoubleQuoted;
}
AppendOneNativeArgument(Context, argValue, arrayLiteralAst, sawVerbatimArgumentMarker, stringConstantType);
}
}
}
}
#endregion Parameter binding
/// <summary>
/// Gets the command arguments in string form.
/// </summary>
internal string Arguments
{
get
{
return _arguments.ToString();
}
}
private readonly StringBuilder _arguments = new StringBuilder();
#endregion internal members
#region private members
/// <summary>
/// Stringize a non-IEnum argument to a native command, adding quotes
/// and trailing spaces as appropriate. An array gets added as multiple arguments
/// each of which will be stringized.
/// </summary>
/// <param name="context">Execution context instance.</param>
/// <param name="obj">The object to append.</param>
/// <param name="argArrayAst">If the argument was an array literal, the Ast, otherwise null.</param>
/// <param name="sawVerbatimArgumentMarker">True if the argument occurs after --%.</param>
/// <param name="stringConstantType">Bare, SingleQuoted, or DoubleQuoted.</param>
private void AppendOneNativeArgument(ExecutionContext context, object obj, ArrayLiteralAst argArrayAst, bool sawVerbatimArgumentMarker, StringConstantType stringConstantType)
{
IEnumerator list = LanguagePrimitives.GetEnumerator(obj);
Diagnostics.Assert((argArrayAst == null) || obj is object[] && ((object[])obj).Length == argArrayAst.Elements.Count, "array argument and ArrayLiteralAst differ in number of elements");
int currentElement = -1;
string separator = string.Empty;
do
{
string arg;
if (list == null)
{
arg = PSObject.ToStringParser(context, obj);
}
else
{
if (!ParserOps.MoveNext(context, null, list))
{
break;
}
arg = PSObject.ToStringParser(context, ParserOps.Current(null, list));
currentElement += 1;
if (currentElement != 0)
{
separator = GetEnumerableArgSeparator(argArrayAst, currentElement);
}
}
if (!string.IsNullOrEmpty(arg))
{
_arguments.Append(separator);
if (sawVerbatimArgumentMarker)
{
arg = Environment.ExpandEnvironmentVariables(arg);
_arguments.Append(arg);
}
else
{
// We need to add quotes if the argument has unquoted spaces. The
// quotes could appear anywhere inside the string, not just at the start,
// e.g.
// $a = 'a"b c"d'
// echoargs $a 'a"b c"d' a"b c"d
//
// The above should see 3 identical arguments in argv (the command line will
// actually have quotes in different places, but the Win32 command line=>argv parser
// erases those differences.
//
// We need to check quotes that the win32 argument parser checks which is currently
// just the normal double quotes, no other special quotes. Also note that mismatched
// quotes are supported
if (NeedQuotes(arg))
{
_arguments.Append('"');
if (stringConstantType == StringConstantType.DoubleQuoted)
{
_arguments.Append(ResolvePath(arg, Context));
}
else
{
_arguments.Append(arg);
}
// need to escape all trailing backslashes so the native command receives it correctly
// according to http://www.daviddeley.com/autohotkey/parameters/parameters.htm#WINCRULESDOC
for (int i = arg.Length - 1; i >= 0 && arg[i] == '\\'; i--)
{
_arguments.Append('\\');
}
_arguments.Append('"');
}
else
{
PossiblyGlobArg(arg, stringConstantType);
}
}
}
}
while (list != null);
}
/// <summary>
/// On Windows, just append <paramref name="arg"/>.
/// On Unix, do globbing as appropriate, otherwise just append <paramref name="arg"/>.
/// </summary>
/// <param name="arg">The argument that possibly needs expansion.</param>
/// <param name="stringConstantType">Bare, SingleQuoted, or DoubleQuoted.</param>
private void PossiblyGlobArg(string arg, StringConstantType stringConstantType)
{
var argExpanded = false;
#if UNIX
// On UNIX systems, we expand arguments containing wildcard expressions against
// the file system just like bash, etc.
if (stringConstantType == StringConstantType.BareWord)
{
if (WildcardPattern.ContainsWildcardCharacters(arg))
{
// See if the current working directory is a filesystem provider location
// We won't do the expansion if it isn't since native commands can only access the file system.
var cwdinfo = Context.EngineSessionState.CurrentLocation;
// If it's a filesystem location then expand the wildcards
if (cwdinfo.Provider.Name.Equals(FileSystemProvider.ProviderName, StringComparison.OrdinalIgnoreCase))
{
// On UNIX, paths starting with ~ or absolute paths are not normalized
bool normalizePath = arg.Length == 0 || !(arg[0] == '~' || arg[0] == '/');
// See if there are any matching paths otherwise just add the pattern as the argument
Collection<PSObject> paths = null;
try
{
paths = Context.EngineSessionState.InvokeProvider.ChildItem.Get(arg, false);
}
catch
{
// Fallthrough will append the pattern unchanged.
}
// Expand paths, but only from the file system.
if (paths?.Count > 0 && paths.All(p => p.BaseObject is FileSystemInfo))
{
var sep = string.Empty;
foreach (var path in paths)
{
_arguments.Append(sep);
sep = " ";
var expandedPath = (path.BaseObject as FileSystemInfo).FullName;
if (normalizePath)
{
expandedPath =
Context.SessionState.Path.NormalizeRelativePath(expandedPath, cwdinfo.ProviderPath);
}
// If the path contains spaces, then add quotes around it.
if (NeedQuotes(expandedPath))
{
_arguments.Append("\"");
_arguments.Append(expandedPath);
_arguments.Append("\"");
}
else
{
_arguments.Append(expandedPath);
}
argExpanded = true;
}
}
}
}
else
{
// Even if there are no wildcards, we still need to possibly
// expand ~ into the filesystem provider home directory path
ProviderInfo fileSystemProvider = Context.EngineSessionState.GetSingleProvider(FileSystemProvider.ProviderName);
string home = fileSystemProvider.Home;
if (string.Equals(arg, "~"))
{
_arguments.Append(home);
argExpanded = true;
}
else if (arg.StartsWith("~/", StringComparison.OrdinalIgnoreCase))
{
var replacementString = home + arg.Substring(1);
_arguments.Append(replacementString);
argExpanded = true;
}
}
}
#endif // UNIX
if (stringConstantType != StringConstantType.SingleQuoted)
{
arg = ResolvePath(arg, Context);
}
if (!argExpanded)
{
_arguments.Append(arg);
}
}
/// <summary>
/// Check if string is prefixed by psdrive, if so, expand it if filesystem path.
/// </summary>
/// <param name="path">The potential PSPath to resolve.</param>
/// <param name="context">The current ExecutionContext.</param>
/// <returns>Resolved PSPath if applicable otherwise the original path</returns>
internal static string ResolvePath(string path, ExecutionContext context)
{
if (ExperimentalFeature.IsEnabled("PSNativePSPathResolution"))
{
#if !UNIX
// on Windows, we need to expand ~ to point to user's home path
if (string.Equals(path, "~", StringComparison.Ordinal) || path.StartsWith(TildeDirectorySeparator, StringComparison.Ordinal) || path.StartsWith(TildeAltDirectorySeparator, StringComparison.Ordinal))
{
try
{
ProviderInfo fileSystemProvider = context.EngineSessionState.GetSingleProvider(FileSystemProvider.ProviderName);
return new StringBuilder(fileSystemProvider.Home)
.Append(path.Substring(1))
.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)
.ToString();
}
catch
{
return path;
}
}
// check if the driveName is an actual disk drive on Windows, if so, no expansion
if (path.Length >= 2 && path[1] == ':')
{
foreach (var drive in DriveInfo.GetDrives())
{
if (drive.Name.StartsWith(new string(path[0], 1), StringComparison.OrdinalIgnoreCase))
{
return path;
}
}
}
#endif
if (path.Contains(':'))
{
LocationGlobber globber = new LocationGlobber(context.SessionState);
try
{
ProviderInfo providerInfo;
// replace the argument with resolved path if it's a filesystem path
string pspath = globber.GetProviderPath(path, out providerInfo);
if (string.Equals(providerInfo.Name, FileSystemProvider.ProviderName, StringComparison.OrdinalIgnoreCase))
{
path = pspath;
}
}
catch
{
// if it's not a provider path, do nothing
}
}
}
return path;
}
/// <summary>
/// Check to see if the string contains spaces and therefore must be quoted.
/// </summary>
/// <param name="stringToCheck">The string to check for spaces.</param>
internal static bool NeedQuotes(string stringToCheck)
{
bool needQuotes = false, followingBackslash = false;
int quoteCount = 0;
for (int i = 0; i < stringToCheck.Length; i++)
{
if (stringToCheck[i] == '"' && !followingBackslash)
{
quoteCount += 1;
}
else if (char.IsWhiteSpace(stringToCheck[i]) && (quoteCount % 2 == 0))
{
needQuotes = true;
}
followingBackslash = stringToCheck[i] == '\\';
}
return needQuotes;
}
private static string GetEnumerableArgSeparator(ArrayLiteralAst arrayLiteralAst, int index)
{
if (arrayLiteralAst == null) return " ";
// index points to the *next* element, so we're looking for space between
// it and the previous element.
var next = arrayLiteralAst.Elements[index];
var prev = arrayLiteralAst.Elements[index - 1];
var arrayExtent = arrayLiteralAst.Extent;
var afterPrev = prev.Extent.EndOffset;
var beforeNext = next.Extent.StartOffset - 1;
if (afterPrev == beforeNext) return ",";
var arrayText = arrayExtent.Text;
afterPrev -= arrayExtent.StartOffset;
beforeNext -= arrayExtent.StartOffset;
if (arrayText[afterPrev] == ',') return ", ";
if (arrayText[beforeNext] == ',') return " ,";
return " , ";
}
/// <summary>
/// The native command to bind to.
/// </summary>
private NativeCommand _nativeCommand;
private static readonly string TildeDirectorySeparator = $"~{Path.DirectorySeparatorChar}";
private static readonly string TildeAltDirectorySeparator = $"~{Path.AltDirectorySeparatorChar}";
#endregion private members
}
}