Fix some logic

This commit is contained in:
Reinier Criel 2026-01-12 15:12:19 -08:00
parent 19652c49c9
commit 9a902af917

View file

@ -108,6 +108,40 @@ function Invoke-RealCommand {
} }
} }
function Get-ReconstructedArguments {
param(
[string]$RawLine,
[int]$RawOffset
)
if (-not $RawLine) { return $null }
$tokens = [System.Management.Automation.PSParser]::Tokenize($RawLine, [ref]$null)
$newArgs = @()
$foundCommand = $false
foreach ($t in $tokens) {
if (-not $foundCommand) {
if ($t.Start -eq ($RawOffset - 1)) { $foundCommand = $true }
continue
}
if ($t.Type -eq 'Operator' -and $t.Content -match '[|;&]') { break }
# Stop if complex variable expansion is used
if ($t.Type -eq 'Variable' -or $t.Type -eq 'Group' -or $t.Type -eq 'SubExpression') {
return $null
}
$newArgs += $t.Content
}
if ($foundCommand) {
return ,$newArgs
}
return $null
}
function Invoke-WrappedCommand { function Invoke-WrappedCommand {
param( param(
[string]$OriginalCmd, [string]$OriginalCmd,
@ -118,29 +152,9 @@ function Invoke-WrappedCommand {
# Use raw line parsing to recover arguments like '--' that PowerShell consumes # Use raw line parsing to recover arguments like '--' that PowerShell consumes
if ($RawLine) { if ($RawLine) {
$tokens = [System.Management.Automation.PSParser]::Tokenize($RawLine, [ref]$null) $reconstructedArgs = Get-ReconstructedArguments $RawLine $RawOffset
$newArgs = @() if ($null -ne $reconstructedArgs) {
$foundCommand = $false $Arguments = $reconstructedArgs
$canUseRaw = $true
foreach ($t in $tokens) {
# Find the command token based on offset
if (-not $foundCommand) {
if ($t.Start -eq ($RawOffset - 1)) { $foundCommand = $true }
continue
}
# Stop at command separators
if ($t.Type -eq 'Operator' -and $t.Content -match '[|;&]') { break }
# Stop if complex variable expansion is used
if ($t.Type -eq 'Variable' -or $t.Type -eq 'Group' -or $t.Type -eq 'SubExpression') {
$canUseRaw = $false
break
}
$newArgs += $t.Content
}
if ($foundCommand -and $canUseRaw) {
$Arguments = $newArgs
Write-Host "Safe-chain Powershell Wrapper: Reconstructed args: $($Arguments -join ' ')" Write-Host "Safe-chain Powershell Wrapper: Reconstructed args: $($Arguments -join ' ')"
} }
} }