2015-04-01 09:34:31 +02:00
|
|
|
# readlineSync
|
|
|
|
# https://github.com/anseki/readline-sync
|
|
|
|
#
|
|
|
|
# Copyright (c) 2015 anseki
|
|
|
|
# Licensed under the MIT license.
|
2015-03-25 13:22:44 +01:00
|
|
|
|
|
|
|
Param(
|
|
|
|
[string] $display,
|
2015-03-31 13:27:02 +02:00
|
|
|
[switch] $keyIn,
|
2015-03-25 13:22:44 +01:00
|
|
|
[switch] $noEchoBack,
|
2015-03-27 12:25:59 +01:00
|
|
|
[string] $mask,
|
2015-04-03 18:14:55 +02:00
|
|
|
[string] $limit,
|
2015-03-31 13:27:02 +02:00
|
|
|
[switch] $cs,
|
2015-03-25 13:22:44 +01:00
|
|
|
[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 = @{}
|
2015-04-03 18:14:55 +02:00
|
|
|
foreach ($arg in @('display', 'keyIn', 'noEchoBack', 'mask', 'limit', 'cs', 'encoded')) {
|
2015-03-27 12:25:59 +01:00
|
|
|
$options.Add($arg, (Get-Variable $arg -ValueOnly))
|
|
|
|
}
|
|
|
|
if ($options.encoded) {
|
|
|
|
$argList = New-Object string[] $options.Keys.Count
|
2015-04-03 18:14:55 +02:00
|
|
|
$options.Keys.CopyTo($argList, 0)
|
2015-03-27 12:25:59 +01:00
|
|
|
foreach ($arg in $argList) {
|
2015-04-03 18:14:55 +02:00
|
|
|
if ($options[$arg] -is [string] -and $options[$arg])
|
2015-03-27 12:25:59 +01:00
|
|
|
{ $options[$arg] = decodeDOS $options[$arg] }
|
|
|
|
}
|
2015-03-25 13:22:44 +01:00
|
|
|
}
|
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
[string] $inputTTY = ''
|
2015-04-03 18:14:55 +02:00
|
|
|
[bool] $silent = -not $options.display -and
|
|
|
|
$options.keyIn -and $options.noEchoBack -and -not $options.mask
|
|
|
|
[bool] $isCooked = -not $options.noEchoBack -and -not $options.keyIn
|
2015-03-27 12:25:59 +01:00
|
|
|
|
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-04-01 09:34:31 +02:00
|
|
|
# [string] $cmdPath = $Env:ComSpec
|
|
|
|
# [string] $psPath = 'powershell.exe'
|
2015-03-25 13:22:44 +01:00
|
|
|
function execWithTTY ($command, $getRes = $False) {
|
|
|
|
if ($getRes) {
|
|
|
|
$res = (cmd.exe /C "<CON powershell.exe -Command $command")
|
2015-04-01 12:36:00 +02:00
|
|
|
if ($LastExitCode -ne 0) { exit $LastExitCode }
|
2015-04-01 09:34:31 +02:00
|
|
|
return $res
|
2015-03-25 13:22:44 +01:00
|
|
|
} else {
|
|
|
|
$command | cmd.exe /C ">CON powershell.exe -Command -"
|
2015-04-01 12:36:00 +02:00
|
|
|
if ($LastExitCode -ne 0) { exit $LastExitCode }
|
2015-03-25 13:22:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 12:36:00 +02:00
|
|
|
function writeTTY ($text) {
|
|
|
|
execWithTTY ('Write-Host ''' + ($text -replace '''', '''''') + ''' -NoNewline')
|
|
|
|
}
|
|
|
|
|
2015-04-03 18:14:55 +02:00
|
|
|
if ($options.display) {
|
2015-03-27 12:25:59 +01:00
|
|
|
writeTTY $options.display
|
2015-03-25 13:22:44 +01:00
|
|
|
}
|
|
|
|
|
2015-04-03 18:14:55 +02:00
|
|
|
if (-not $options.keyIn -and $options.noEchoBack -and $options.mask -eq '*') {
|
|
|
|
$inputTTY = execWithTTY ('$text = Read-Host -AsSecureString;' +
|
|
|
|
'$bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($text);' +
|
|
|
|
'[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
|
|
|
|
2015-04-03 18:14:55 +02:00
|
|
|
if ($options.keyIn -and $options.limit) {
|
|
|
|
$limitPtn = '[^' + $options.limit + ']'
|
|
|
|
}
|
|
|
|
|
2015-03-25 13:22:44 +01:00
|
|
|
while ($True) {
|
2015-03-30 12:36:31 +02:00
|
|
|
if (-not $isCooked) {
|
2015-04-03 18:14:55 +02:00
|
|
|
$chunk = [char][int] (execWithTTY '[int] [Console]::ReadKey($True).KeyChar' $True)
|
2015-03-30 12:36:31 +02:00
|
|
|
} else {
|
|
|
|
$chunk = execWithTTY 'Read-Host' $True
|
2015-04-03 18:14:55 +02:00
|
|
|
$chunk += "`n"
|
2015-03-25 13:22:44 +01:00
|
|
|
}
|
|
|
|
|
2015-04-03 18:14:55 +02:00
|
|
|
if ($chunk -and $chunk -match '^(.*?)[\r\n]') {
|
|
|
|
$chunk = $Matches[1]
|
|
|
|
$atEol = $True
|
|
|
|
} else { $atEol = $False }
|
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
# other ctrl-chars
|
2015-04-03 18:14:55 +02:00
|
|
|
if ($chunk) { $chunk = $chunk -replace '[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]', '' }
|
|
|
|
if ($chunk -and $limitPtn) {
|
|
|
|
if ($options.cs) { $chunk = $chunk -creplace $limitPtn, '' }
|
|
|
|
else { $chunk = $chunk -ireplace $limitPtn, '' }
|
|
|
|
}
|
2015-03-25 13:22:44 +01:00
|
|
|
|
2015-04-03 18:14:55 +02:00
|
|
|
if ($chunk) {
|
|
|
|
if (-not $isCooked) {
|
|
|
|
if (-not $options.noEchoBack) {
|
|
|
|
writeTTY $chunk
|
|
|
|
} elseif ($options.mask) {
|
|
|
|
writeTTY ($options.mask * $chunk.Length)
|
|
|
|
}
|
2015-03-25 13:22:44 +01:00
|
|
|
}
|
2015-04-03 18:14:55 +02:00
|
|
|
$inputTTY += $chunk
|
2015-03-25 13:22:44 +01:00
|
|
|
}
|
|
|
|
|
2015-04-03 18:14:55 +02:00
|
|
|
if ((-not $options.keyIn -and $atEol) -or
|
|
|
|
($options.keyIn -and $inputTTY.Length -ge $reqSize)) { break }
|
2015-03-25 13:22:44 +01:00
|
|
|
}
|
|
|
|
|
2015-04-03 18:14:55 +02:00
|
|
|
if (-not $isCooked -and -not $silent) { execWithTTY 'Write-Host ''''' } # new line
|
2015-03-25 13:22:44 +01:00
|
|
|
|
2015-04-03 18:14:55 +02:00
|
|
|
return "'$inputTTY'"
|