2015-03-25 13:22:44 +01:00
|
|
|
|
|
|
|
Param(
|
|
|
|
[string] $display,
|
|
|
|
[switch] $noEchoBack,
|
2015-03-27 12:25:59 +01:00
|
|
|
[string] $mask,
|
2015-03-25 13:22:44 +01:00
|
|
|
[switch] $keyIn,
|
|
|
|
[switch] $encoded
|
|
|
|
)
|
|
|
|
|
|
|
|
$ErrorActionPreference = 'Stop' # for cmdlet
|
|
|
|
trap {
|
|
|
|
# `throw $_` and `Write-Error $_` return exit-code 0
|
|
|
|
$Host.UI.WriteErrorLine($_)
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
function decodeDOS ($arg) {
|
2015-03-25 13:22:44 +01:00
|
|
|
[Regex]::Replace($arg, '#(\d+);', { [char][int] $args[0].Groups[1].Value })
|
|
|
|
}
|
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
$options = @{}
|
|
|
|
foreach ($arg in @('display', 'noEchoBack', 'mask', 'keyIn', 'encoded')) {
|
|
|
|
$options.Add($arg, (Get-Variable $arg -ValueOnly))
|
|
|
|
}
|
|
|
|
if ($options.encoded) {
|
|
|
|
$argList = New-Object string[] $options.Keys.Count
|
|
|
|
$options.Keys.CopyTo($argList, 0);
|
|
|
|
foreach ($arg in $argList) {
|
|
|
|
if (($options[$arg] -is [string]) -and ($options[$arg] -ne ''))
|
|
|
|
{ $options[$arg] = decodeDOS $options[$arg] }
|
|
|
|
}
|
2015-03-25 13:22:44 +01:00
|
|
|
}
|
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
[string] $inputTTY = ''
|
|
|
|
[bool] $isInputLine = $False
|
|
|
|
[bool] $isEditable = (-not $options.noEchoBack) -and (-not $options.keyIn)
|
|
|
|
|
|
|
|
function writeTTY ($text) {
|
|
|
|
execWithTTY ('Write-Host ''' + ($text -replace '''', '''''') + ''' -NoNewline')
|
|
|
|
$script:isInputLine = $True
|
|
|
|
}
|
2015-03-25 13:22:44 +01:00
|
|
|
|
|
|
|
# Instant method that opens TTY without CreateFile via P/Invoke in .NET Framework
|
2015-03-27 12:25:59 +01:00
|
|
|
# **NOTE** Don't include special characters of DOS in $command when $getRes is True.
|
2015-03-25 13:22:44 +01:00
|
|
|
function execWithTTY ($command, $getRes = $False) {
|
|
|
|
if ($getRes) {
|
|
|
|
$res = (cmd.exe /C "<CON powershell.exe -Command $command")
|
|
|
|
if ($LastExitCode -ne 0) { exit 1 }
|
|
|
|
} else {
|
|
|
|
$command | cmd.exe /C ">CON powershell.exe -Command -"
|
|
|
|
if ($LastExitCode -ne 0) { exit 1 }
|
|
|
|
$res = ''
|
|
|
|
}
|
|
|
|
return $res
|
|
|
|
}
|
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
if ($options.display -ne '') {
|
|
|
|
writeTTY $options.display
|
|
|
|
$options.display = ''
|
2015-03-25 13:22:44 +01:00
|
|
|
}
|
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
if ($options.noEchoBack -and (-not $options.keyIn) -and ($options.mask -eq '*')) {
|
2015-03-25 13:22:44 +01:00
|
|
|
$inputTTY = execWithTTY ('$inputTTY = Read-Host -AsSecureString;' +
|
|
|
|
'$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($inputTTY);' +
|
|
|
|
'[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)') $True
|
2015-03-27 12:25:59 +01:00
|
|
|
return '''' + $inputTTY + ''''
|
2015-03-25 13:22:44 +01:00
|
|
|
}
|
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
if ($options.keyIn) { $reqSize = 1 }
|
2015-03-25 13:22:44 +01:00
|
|
|
else { $reqSize = 1024 } # dummy
|
|
|
|
|
|
|
|
while ($True) {
|
|
|
|
if ($isEditable) {
|
|
|
|
$chunk = execWithTTY 'Read-Host' $True
|
|
|
|
$chunk += "`n"
|
|
|
|
} else { # raw
|
|
|
|
$chunk = execWithTTY '[System.Console]::ReadKey($True).KeyChar' $True
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($chunk -eq '') { break }
|
2015-03-27 12:25:59 +01:00
|
|
|
# other ctrl-chars
|
2015-03-25 13:22:44 +01:00
|
|
|
$chunk = $chunk -replace '[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]', ''
|
|
|
|
if ($chunk -eq '') { continue }
|
|
|
|
|
|
|
|
if (-not $isEditable) {
|
2015-03-27 12:25:59 +01:00
|
|
|
$displayTmp = $chunk -replace '[\r\n]', ''
|
|
|
|
if ($displayTmp -ne '') {
|
|
|
|
if ($options.noEchoBack) {
|
|
|
|
if ($options.mask -eq '') { $displayTmp = '' }
|
|
|
|
else { $displayTmp = $options.mask * $displayTmp.Length }
|
2015-03-25 13:22:44 +01:00
|
|
|
}
|
2015-03-27 12:25:59 +01:00
|
|
|
if ($displayTmp -ne '') { writeTTY $displayTmp }
|
2015-03-25 13:22:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$inputTTY += $chunk
|
2015-03-27 12:25:59 +01:00
|
|
|
if (($inputTTY -match '[\r\n]$') -or
|
|
|
|
($options.keyIn -and ($inputTTY.Length -ge $reqSize))) { break }
|
2015-03-25 13:22:44 +01:00
|
|
|
}
|
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
if ((-not $isEditable) -and (-not ($options.keyIn -and (-not $isInputLine))))
|
|
|
|
{ execWithTTY 'Write-Host ''''' } # new line
|
2015-03-25 13:22:44 +01:00
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
return '''' + $inputTTY + ''''
|