blob: 647adf59b6a3cba7fccfe88bfcf32071ad96e788 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26#include "jni.h"
27#include "jni_util.h"
28#include "jvm.h"
29#include "java_io_Console.h"
30
31#include <stdlib.h>
32#include <Wincon.h>
33
34static HANDLE hStdOut = INVALID_HANDLE_VALUE;
35static HANDLE hStdIn = INVALID_HANDLE_VALUE;
36JNIEXPORT jboolean JNICALL
37Java_java_io_Console_istty(JNIEnv *env, jclass cls)
38{
39 if (hStdIn == INVALID_HANDLE_VALUE &&
40 (hStdIn = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
41 JNU_ThrowIOExceptionWithLastError(env, "Open Console input failed");
42 return JNI_FALSE;
43 }
44 if (hStdOut == INVALID_HANDLE_VALUE &&
45 (hStdOut = GetStdHandle(STD_OUTPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
46 JNU_ThrowIOExceptionWithLastError(env, "Open Console output failed");
47 return JNI_FALSE;
48 }
49 if (GetFileType(hStdIn) != FILE_TYPE_CHAR ||
50 GetFileType(hStdOut) != FILE_TYPE_CHAR)
51 return JNI_FALSE;
52 return JNI_TRUE;
53}
54
55JNIEXPORT jstring JNICALL
56Java_java_io_Console_encoding(JNIEnv *env, jclass cls)
57{
58 char buf[64];
59 int cp = GetConsoleCP();
60 if (cp >= 874 && cp <= 950)
61 sprintf(buf, "ms%d", cp);
62 else
63 sprintf(buf, "cp%d", cp);
64 return JNU_NewStringPlatform(env, buf);
65}
66
67JNIEXPORT jboolean JNICALL
68Java_java_io_Console_echo(JNIEnv *env, jclass cls, jboolean on)
69{
70 DWORD fdwMode;
71 jboolean old;
72 if (! GetConsoleMode(hStdIn, &fdwMode)) {
73 JNU_ThrowIOExceptionWithLastError(env, "GetConsoleMode failed");
74 return !on;
75 }
76 old = (fdwMode & ENABLE_ECHO_INPUT) != 0;
77 if (on) {
78 fdwMode |= ENABLE_ECHO_INPUT;
79 } else {
80 fdwMode &= ~ENABLE_ECHO_INPUT;
81 }
82 if (! SetConsoleMode(hStdIn, fdwMode)) {
83 JNU_ThrowIOExceptionWithLastError(env, "SetConsoleMode failed");
84 }
85 return old;
86}