forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMshexpression.cs
More file actions
279 lines (238 loc) · 8.82 KB
/
Copy pathMshexpression.cs
File metadata and controls
279 lines (238 loc) · 8.82 KB
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
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.Management.Automation;
using System.Management.Automation.Internal;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Microsoft.PowerShell.Commands.Internal.Format
{
/// <summary>
/// class to hold results
/// NOTE: we should make it an PSObject eventually
/// </summary>
internal class MshExpressionResult
{
internal MshExpressionResult(object res, MshExpression re, Exception e)
{
Result = res;
ResolvedExpression = re;
Exception = e;
}
internal object Result { get; } = null;
internal MshExpression ResolvedExpression { get; } = null;
internal Exception Exception { get; } = null;
}
internal class MshExpression
{
/// <summary>
/// constructor
/// </summary>
/// <param name="s">expression</param>
/// <exception cref="ArgumentNullException"></exception>
internal MshExpression(string s)
: this(s, false)
{
}
/// <summary>
/// constructor
/// </summary>
/// <param name="s">expression</param>
/// <param name="isResolved"><c>true</c> if no further attempts should be made to resolve wildcards</param>
/// <exception cref="ArgumentNullException"></exception>
internal MshExpression(string s, bool isResolved)
{
if (string.IsNullOrEmpty(s))
{
throw PSTraceSource.NewArgumentNullException("s");
}
_stringValue = s;
_isResolved = isResolved;
}
/// <summary>
/// constructor
/// </summary>
/// <param name="scriptBlock"></param>
/// <exception cref="ArgumentNullException"></exception>
internal MshExpression(ScriptBlock scriptBlock)
{
if (scriptBlock == null)
{
throw PSTraceSource.NewArgumentNullException("scriptBlock");
}
Script = scriptBlock;
}
public ScriptBlock Script { get; } = null;
public override string ToString()
{
if (Script != null)
return Script.ToString();
return _stringValue;
}
internal List<MshExpression> ResolveNames(PSObject target)
{
return ResolveNames(target, true);
}
internal bool HasWildCardCharacters
{
get
{
if (Script != null)
return false;
return WildcardPattern.ContainsWildcardCharacters(_stringValue);
}
}
internal List<MshExpression> ResolveNames(PSObject target, bool expand)
{
List<MshExpression> retVal = new List<MshExpression>();
if (_isResolved)
{
retVal.Add(this);
return retVal;
}
if (Script != null)
{
// script block, just add it to the list and be done
MshExpression ex = new MshExpression(Script);
ex._isResolved = true;
retVal.Add(ex);
return retVal;
}
// we have a string value
IEnumerable<PSMemberInfo> members = null;
if (HasWildCardCharacters)
{
// get the members first: this will expand the globbing on each parameter
members = target.Members.Match(_stringValue,
PSMemberTypes.Properties | PSMemberTypes.PropertySet);
}
else
{
// we have no globbing: try an exact match, because this is quicker.
PSMemberInfo x = target.Members[_stringValue];
List<PSMemberInfo> temp = new List<PSMemberInfo>();
if (x != null)
{
temp.Add(x);
}
members = temp;
}
// we now have a list of members, we have to expand property sets
// and remove duplicates
List<PSMemberInfo> temporaryMemberList = new List<PSMemberInfo>();
foreach (PSMemberInfo member in members)
{
// it can be a property set
PSPropertySet propertySet = member as PSPropertySet;
if (propertySet != null)
{
if (expand)
{
// NOTE: we expand the property set under the
// assumption that it contains property names that
// do not require any further expansion
Collection<string> references = propertySet.ReferencedPropertyNames;
for (int j = 0; j < references.Count; j++)
{
ReadOnlyPSMemberInfoCollection<PSPropertyInfo> propertyMembers =
target.Properties.Match(references[j]);
for (int jj = 0; jj < propertyMembers.Count; jj++)
{
temporaryMemberList.Add(propertyMembers[jj]);
}
}
}
continue;
}
// it can be a property
if (member is PSPropertyInfo)
{
temporaryMemberList.Add(member);
}
}
Hashtable hash = new Hashtable();
// build the list of unique values: remove the possible duplicates
// from property set expansion
foreach (PSMemberInfo m in temporaryMemberList)
{
if (!hash.ContainsKey(m.Name))
{
MshExpression ex = new MshExpression(m.Name);
ex._isResolved = true;
retVal.Add(ex);
hash.Add(m.Name, null);
}
}
return retVal;
}
internal List<MshExpressionResult> GetValues(PSObject target)
{
return GetValues(target, true, true);
}
internal List<MshExpressionResult> GetValues(PSObject target, bool expand, bool eatExceptions)
{
List<MshExpressionResult> retVal = new List<MshExpressionResult>();
// process the script case
if (Script != null)
{
MshExpression scriptExpression = new MshExpression(Script);
MshExpressionResult r = scriptExpression.GetValue(target, eatExceptions);
retVal.Add(r);
return retVal;
}
// process the expression
List<MshExpression> resolvedExpressionList = this.ResolveNames(target, expand);
foreach (MshExpression re in resolvedExpressionList)
{
MshExpressionResult r = re.GetValue(target, eatExceptions);
retVal.Add(r);
}
return retVal;
}
#region Private Members
private MshExpressionResult GetValue(PSObject target, bool eatExceptions)
{
try
{
object result;
if (Script != null)
{
result = Script.DoInvokeReturnAsIs(
useLocalScope: true,
errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe,
dollarUnder: target,
input: AutomationNull.Value,
scriptThis: AutomationNull.Value,
args: Utils.EmptyArray<object>());
}
else
{
PSMemberInfo member = target.Properties[_stringValue];
if (member == null)
{
return new MshExpressionResult(null, this, null);
}
result = member.Value;
}
return new MshExpressionResult(result, this, null);
}
catch (RuntimeException e)
{
if (eatExceptions)
{
return new MshExpressionResult(null, this, e);
}
else
{
throw;
}
}
}
// private members
private string _stringValue;
private bool _isResolved = false;
#endregion Private Members
}
}