IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
Console.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2007-2014 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24using System;
25using System.Runtime.InteropServices;
26
28{
29
30 static class Console
31 {
32
33 public static string encoding()
34 {
35 int cp = 437;
36 try
37 {
38 cp = System.Console.InputEncoding.CodePage;
39 }
40 catch
41 {
42
43 }
44
45 if (cp >= 874 && cp <= 950)
46 return "ms" + cp;
47
48 return "cp" + cp;
49 }
50
51 private const int STD_INPUT_HANDLE = -10;
52 private const int ENABLE_ECHO_INPUT = 0x0004;
53
54 [DllImport("kernel32")]
55 private static extern IntPtr GetStdHandle(int nStdHandle);
56
57 [DllImport("kernel32")]
58 private static extern int GetConsoleMode(IntPtr hConsoleHandle, out int lpMode);
59
60 [DllImport("kernel32")]
61 private static extern int SetConsoleMode(IntPtr hConsoleHandle, int dwMode);
62
63 public static bool echo(bool on)
64 {
65#if !FIRST_PASS
66 // HACK the only way to get this to work is by p/invoking the Win32 APIs
67 if (Environment.OSVersion.Platform == PlatformID.Win32NT)
68 {
69 IntPtr hStdIn = GetStdHandle(STD_INPUT_HANDLE);
70 if (hStdIn.ToInt64() == 0 || hStdIn.ToInt64() == -1)
71 {
72 throw new global::java.io.IOException("The handle is invalid");
73 }
74 int fdwMode;
75 if (GetConsoleMode(hStdIn, out fdwMode) == 0)
76 {
77 throw new global::java.io.IOException("GetConsoleMode failed");
78 }
79 bool old = (fdwMode & ENABLE_ECHO_INPUT) != 0;
80 if (on)
81 {
82 fdwMode |= ENABLE_ECHO_INPUT;
83 }
84 else
85 {
86 fdwMode &= ~ENABLE_ECHO_INPUT;
87 }
88 if (SetConsoleMode(hStdIn, fdwMode) == 0)
89 {
90 throw new global::java.io.IOException("SetConsoleMode failed");
91 }
92 return old;
93 }
94#endif
95 return true;
96 }
97
98 public static bool istty()
99 {
100 // The JDK returns false here if stdin or stdout (not stderr) is redirected to a file
101 // or if there is no console associated with the current process.
102 // The best we can do is to look at the KeyAvailable property, which
103 // will throw an InvalidOperationException if stdin is redirected or not available
104 try
105 {
106 return System.Console.KeyAvailable || true;
107 }
108 catch (InvalidOperationException)
109 {
110 return false;
111 }
112 }
113
114 }
115
116}
static bool echo(bool on)
Definition Console.cs:63