Skip to content

Commit

Permalink
imp - Used Terminaux's console reader
Browse files Browse the repository at this point in the history
---

We've used Terminaux as a way to fix Console.ReadLine() related issues regarding Linux systems and Mono.

---

Type: imp
Breaking: False
Doc Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Oct 29, 2023
1 parent 6969342 commit 23d210b
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 64 deletions.
3 changes: 2 additions & 1 deletion Kernel Simulator/Arguments/ArgumentPrompt.vb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
' along with this program. If not, see <https://www.gnu.org/licenses/>.

Imports KS.Arguments.ArgumentBase
Imports Terminaux.Reader

Namespace Arguments
Public Module ArgumentPrompt
Expand All @@ -43,7 +44,7 @@ Namespace Arguments
'Prompts for the arguments
While Not AnswerArgs = "q"
TextWriterColor.Write(">> ", False, ColTypes.Input)
AnswerArgs = Console.ReadLine()
AnswerArgs = TermReader.Read()

'Add an argument to the entered arguments list
If AnswerArgs <> "q" Then
Expand Down
3 changes: 2 additions & 1 deletion Kernel Simulator/Arguments/KernelArguments/CmdInject.vb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
' along with this program. If not, see <https://www.gnu.org/licenses/>.

Imports KS.Arguments.ArgumentBase
Imports Terminaux.Reader

Namespace Arguments.KernelArguments
Class CmdInjectArgument
Expand All @@ -32,7 +33,7 @@ Namespace Arguments.KernelArguments
Else
TextWriterColor.Write(DoTranslation("Available commands: {0}"), True, ColTypes.Neutral, String.Join(", ", Shell.Shell.Commands.Keys))
TextWriterColor.Write(">> ", False, ColTypes.Input)
InjectedCommands.AddRange(Console.ReadLine().Split({" : "}, StringSplitOptions.RemoveEmptyEntries))
InjectedCommands.AddRange(TermReader.Read().Split({" : "}, StringSplitOptions.RemoveEmptyEntries))
If String.Join(", ", InjectedCommands) <> "q" Then
CommandFlag = True
Else
Expand Down
30 changes: 7 additions & 23 deletions Kernel Simulator/Console/Input.vb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
' along with this program. If not, see <https://www.gnu.org/licenses/>.

Imports System.IO
Imports Terminaux.Reader

Namespace ConsoleBase
Public Module Input
Expand All @@ -42,28 +43,11 @@ Namespace ConsoleBase
''' </summary>
''' <param name="MaskChar">Specifies the password mask character</param>
Public Function ReadLineNoInput(MaskChar As Char) As String
Dim Final As String = ""
While True
Dim KeyInfo As ConsoleKeyInfo = Console.ReadKey(True)
Dim KeyCharacter As Char = KeyInfo.KeyChar
If KeyCharacter = vbCr Or KeyCharacter = vbLf Then
Exit While
ElseIf KeyInfo.Key = ConsoleKey.Backspace Then
If Not Final.Length = 0 Then
Final = Final.Remove(Final.Length - 1)
If Not MaskChar = vbNullChar Then
Dim OldCursorLeft As Integer = Console.CursorLeft
Console.CursorLeft = OldCursorLeft - 1
Console.Write(" ")
Console.CursorLeft = OldCursorLeft - 1
End If
End If
Else
Final += KeyCharacter
If Not MaskChar = vbNullChar Then Console.Write(MaskChar)
End If
End While
Return Final
If MaskChar = "" Then
Return TermReader.ReadPassword()
Else
Return TermReader.ReadPassword(New TermReaderSettings() With {.PasswordMaskChar = MaskChar})
End If
End Function

''' <summary>
Expand Down Expand Up @@ -121,7 +105,7 @@ Namespace ConsoleBase
''' </summary>
Public Function ReadLineLong() As String
Console.SetIn(New StreamReader(Console.OpenStandardInput(65536), Console.InputEncoding, False, 65536))
Return Console.ReadLine()
Return TermReader.Read()
End Function

End Module
Expand Down
3 changes: 2 additions & 1 deletion Kernel Simulator/Login/Login.vb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Imports KS.Misc.Encryption
Imports KS.Misc.Screensaver
Imports KS.Network.RSS
Imports Terminaux.Reader

Namespace Login
Public Module Login
Expand Down Expand Up @@ -135,7 +136,7 @@ Namespace Login
Else
TextWriterColor.Write(DoTranslation("Username: "), False, ColTypes.Input)
End If
Dim answeruser As String = Console.ReadLine()
Dim answeruser As String = TermReader.Read()

'Parse input
If answeruser.Contains(" ") Then
Expand Down
4 changes: 3 additions & 1 deletion Kernel Simulator/Misc/Games/Solver.vb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <https://www.gnu.org/licenses/>.

Imports Terminaux.Reader

Namespace Misc.Games
Public Module Solver

Expand Down Expand Up @@ -47,7 +49,7 @@ Namespace Misc.Games
TextWriterColor.Write(RandomExpression, True, ColTypes.Input)

'Wait for response
UserEvaluated = If(SolverShowInput, Console.ReadLine(), ReadLineNoInput(""))
UserEvaluated = If(SolverShowInput, TermReader.Read(), ReadLineNoInput(""))
Wdbg(DebugLevel.I, "Evaluated: {0}", UserEvaluated)

'Check to see if the user has entered the correct answer
Expand Down
3 changes: 2 additions & 1 deletion Kernel Simulator/Misc/Timers/TimerScreen.vb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Imports System.Threading
Imports System.Timers
Imports Figgle
Imports Timer = System.Timers.Timer
Imports Terminaux.Reader

Namespace Misc.Timers
Public Module TimerScreen
Expand Down Expand Up @@ -96,7 +97,7 @@ Namespace Misc.Timers
WriteWhere(DoTranslation("Specify the timeout in milliseconds") + " [{0}] {1}", 2, KeysTextTopPosition - 4, False, ColTypes.Question, TimerInterval, InputColor.VTSequenceForeground)

'Try to parse the interval
Dim UnparsedInterval As String = Console.ReadLine()
Dim UnparsedInterval As String = TermReader.Read()
If Not Double.TryParse(UnparsedInterval, TimerInterval) Then
'Not numeric.
WriteWhere(DoTranslation("Indicated timeout is not numeric."), 2, KeysTextTopPosition - 4, False, ColTypes.Error)
Expand Down
61 changes: 31 additions & 30 deletions Kernel Simulator/Network/FTP/FTPTools.vb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Imports System.Net.Security
Imports Newtonsoft.Json.Linq
Imports KS.Network.FTP.Transfer
Imports KS.Misc.Reflection
Imports Terminaux.Reader

Namespace Network.FTP
Public Module FTPTools
Expand Down Expand Up @@ -90,7 +91,7 @@ Namespace Network.FTP
Else
TextWriterColor.Write(DoTranslation("Username for {0}: "), False, ColTypes.Input, address)
End If
FtpUser = Console.ReadLine()
FtpUser = TermReader.Read()
If FtpUser = "" Then
Wdbg(DebugLevel.W, "User is not provided. Fallback to ""anonymous""")
FtpUser = "anonymous"
Expand Down Expand Up @@ -200,39 +201,39 @@ Namespace Network.FTP
''' Tries to validate certificate
''' </summary>
Public Sub TryToValidate(control As FtpClient, e As FtpSslValidationEventArgs)
Wdbg(DebugLevel.I, "Certificate checks")
If e.PolicyErrors = SslPolicyErrors.None Then
Wdbg(DebugLevel.I, "Certificate accepted.")
Wdbg(DebugLevel.I, e.Certificate.GetRawCertDataString)
e.Accept = True
Else
Wdbg(DebugLevel.W, $"Certificate error is {e.PolicyErrors}")
TextWriterColor.Write(DoTranslation("During certificate validation, there are certificate errors. It might be the first time you've connected to the server or the certificate might have been expired. Here's an error:"), True, ColTypes.Error)
TextWriterColor.Write("- {0}", True, ColTypes.Error, e.PolicyErrors.ToString)
If FtpAlwaysAcceptInvalidCerts Then
Wdbg(DebugLevel.W, "Certificate accepted, although there are errors.")
Wdbg(DebugLevel.I, "Certificate checks")
If e.PolicyErrors = SslPolicyErrors.None Then
Wdbg(DebugLevel.I, "Certificate accepted.")
Wdbg(DebugLevel.I, e.Certificate.GetRawCertDataString)
e.Accept = True
Else
Dim Answer As String = ""
Do Until Answer.ToLower = "y" Or Answer.ToLower = "n"
TextWriterColor.Write(DoTranslation("Are you sure that you want to connect?") + " (y/n) ", False, ColTypes.Question)
SetConsoleColor(InputColor)
Answer = Console.ReadKey.KeyChar
Console.WriteLine()
Wdbg(DebugLevel.I, $"Answer is {Answer}")
If Answer.ToLower = "y" Then
Wdbg(DebugLevel.W, "Certificate accepted, although there are errors.")
Wdbg(DebugLevel.I, e.Certificate.GetRawCertDataString)
e.Accept = True
ElseIf Answer.ToLower <> "n" Then
Wdbg(DebugLevel.W, "Invalid answer.")
TextWriterColor.Write(DoTranslation("Invalid answer. Please try again."), True, ColTypes.Error)
End If
Loop
Wdbg(DebugLevel.W, $"Certificate error is {e.PolicyErrors}")
TextWriterColor.Write(DoTranslation("During certificate validation, there are certificate errors. It might be the first time you've connected to the server or the certificate might have been expired. Here's an error:"), True, ColTypes.Error)
TextWriterColor.Write("- {0}", True, ColTypes.Error, e.PolicyErrors.ToString)
If FtpAlwaysAcceptInvalidCerts Then
Wdbg(DebugLevel.W, "Certificate accepted, although there are errors.")
Wdbg(DebugLevel.I, e.Certificate.GetRawCertDataString)
e.Accept = True
Else
Dim Answer As String = ""
Do Until Answer.ToLower = "y" Or Answer.ToLower = "n"
TextWriterColor.Write(DoTranslation("Are you sure that you want to connect?") + " (y/n) ", False, ColTypes.Question)
SetConsoleColor(InputColor)
Answer = Console.ReadKey.KeyChar
Console.WriteLine()
Wdbg(DebugLevel.I, $"Answer is {Answer}")
If Answer.ToLower = "y" Then
Wdbg(DebugLevel.W, "Certificate accepted, although there are errors.")
Wdbg(DebugLevel.I, e.Certificate.GetRawCertDataString)
e.Accept = True
ElseIf Answer.ToLower <> "n" Then
Wdbg(DebugLevel.W, "Invalid answer.")
TextWriterColor.Write(DoTranslation("Invalid answer. Please try again."), True, ColTypes.Error)
End If
Loop
End If
End If
End If
End Sub
End Sub

''' <summary>
''' Opens speed dial prompt
Expand Down
3 changes: 2 additions & 1 deletion Kernel Simulator/Network/SFTP/SFTPTools.vb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Imports Newtonsoft.Json.Linq
Imports KS.Network.SSH
Imports KS.Misc.Reflection
Imports Terminaux.Reader

Namespace Network.SFTP
Public Module SFTPTools
Expand Down Expand Up @@ -47,7 +48,7 @@ Namespace Network.SFTP
Else
TextWriterColor.Write(DoTranslation("Username for {0}: "), False, ColTypes.Input, address)
End If
SFTPUser = Console.ReadLine()
SFTPUser = TermReader.Read()
If SFTPUser = "" Then
Wdbg(DebugLevel.W, "User is not provided. Fallback to ""anonymous""")
SFTPUser = "anonymous"
Expand Down
3 changes: 2 additions & 1 deletion Kernel Simulator/Network/SSH/SSH.vb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

Imports System.IO
Imports Renci.SshNet.Common
Imports Terminaux.Reader

Namespace Network.SSH
Public Module SSH
Expand Down Expand Up @@ -92,7 +93,7 @@ Namespace Network.SSH

'Ask for location
TextWriterColor.Write(DoTranslation("Enter the location of the private key for {0}. Write ""q"" to finish adding keys: "), False, ColTypes.Input, Username)
PrivateKeyFile = Console.ReadLine()
PrivateKeyFile = TermReader.Read()
PrivateKeyFile = NeutralizePath(PrivateKeyFile)
If FileExists(PrivateKeyFile) Then
'Ask for passphrase
Expand Down
3 changes: 2 additions & 1 deletion Kernel Simulator/Shell/Shells/FTPShell.vb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
' along with this program. If not, see <https://www.gnu.org/licenses/>.

Imports KS.Network.FTP
Imports Terminaux.Reader

Namespace Shell.Shells
Public Class FTPShell
Expand Down Expand Up @@ -94,7 +95,7 @@ Namespace Shell.Shells
Connects = False
Else
Wdbg(DebugLevel.I, "Normal shell")
FtpCommand = Console.ReadLine()
FtpCommand = TermReader.Read()
End If
KernelEventManager.RaiseFTPPreExecuteCommand(FtpCommand)

Expand Down
3 changes: 2 additions & 1 deletion Kernel Simulator/Shell/Shells/HTTPShell.vb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
' along with this program. If not, see <https://www.gnu.org/licenses/>.

Imports KS.Network.HTTP
Imports Terminaux.Reader

Namespace Shell.Shells
Public Class HTTPShell
Expand Down Expand Up @@ -54,7 +55,7 @@ Namespace Shell.Shells

'Prompt for command
Wdbg(DebugLevel.I, "Normal shell")
Dim HttpCommand As String = Console.ReadLine()
Dim HttpCommand As String = TermReader.Read()
KernelEventManager.RaiseHTTPPreExecuteCommand(HttpCommand)

'Parse command
Expand Down
3 changes: 2 additions & 1 deletion Kernel Simulator/Shell/Shells/SFTPShell.vb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
' along with this program. If not, see <https://www.gnu.org/licenses/>.

Imports KS.Network.SFTP
Imports Terminaux.Reader

Namespace Shell.Shells
Public Class SFTPShell
Expand Down Expand Up @@ -93,7 +94,7 @@ Namespace Shell.Shells
Connects = False
Else
Wdbg(DebugLevel.I, "Normal shell")
SFTPStrCmd = Console.ReadLine()
SFTPStrCmd = TermReader.Read()
End If
KernelEventManager.RaiseSFTPPreExecuteCommand(SFTPStrCmd)

Expand Down
3 changes: 2 additions & 1 deletion Kernel Simulator/Shell/Shells/UESHShell.vb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

Imports KS.Misc.Screensaver
Imports KS.Modifications
Imports Terminaux.Reader

Namespace Shell.Shells
Public Class UESHShell
Expand Down Expand Up @@ -68,7 +69,7 @@ Namespace Shell.Shells
'Wait for command
Wdbg(DebugLevel.I, "Waiting for command")
KernelEventManager.RaiseShellInitialized()
Dim strcommand As String = Console.ReadLine()
Dim strcommand As String = TermReader.Read()

If Not InSaver Then
'Fire event of PreRaiseCommand
Expand Down

0 comments on commit 23d210b

Please sign in to comment.