Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/native/java/io/Console_md.c
32287 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include "jni.h"26#include "jni_util.h"27#include "jvm.h"28#include "java_io_Console.h"2930#include <stdlib.h>31#include <Wincon.h>3233static HANDLE hStdOut = INVALID_HANDLE_VALUE;34static HANDLE hStdIn = INVALID_HANDLE_VALUE;35JNIEXPORT jboolean JNICALL36Java_java_io_Console_istty(JNIEnv *env, jclass cls)37{38if (hStdIn == INVALID_HANDLE_VALUE &&39(hStdIn = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) {40return JNI_FALSE;41}42if (hStdOut == INVALID_HANDLE_VALUE &&43(hStdOut = GetStdHandle(STD_OUTPUT_HANDLE)) == INVALID_HANDLE_VALUE) {44return JNI_FALSE;45}46if (GetFileType(hStdIn) != FILE_TYPE_CHAR ||47GetFileType(hStdOut) != FILE_TYPE_CHAR)48return JNI_FALSE;49return JNI_TRUE;50}5152JNIEXPORT jstring JNICALL53Java_java_io_Console_encoding(JNIEnv *env, jclass cls)54{55char buf[64];56int cp = GetConsoleCP();57if (cp >= 874 && cp <= 950)58sprintf(buf, "ms%d", cp);59else60sprintf(buf, "cp%d", cp);61return JNU_NewStringPlatform(env, buf);62}6364JNIEXPORT jboolean JNICALL65Java_java_io_Console_echo(JNIEnv *env, jclass cls, jboolean on)66{67DWORD fdwMode;68jboolean old;69if (! GetConsoleMode(hStdIn, &fdwMode)) {70JNU_ThrowIOExceptionWithLastError(env, "GetConsoleMode failed");71return !on;72}73old = (fdwMode & ENABLE_ECHO_INPUT) != 0;74if (on) {75fdwMode |= ENABLE_ECHO_INPUT;76} else {77fdwMode &= ~ENABLE_ECHO_INPUT;78}79if (! SetConsoleMode(hStdIn, fdwMode)) {80JNU_ThrowIOExceptionWithLastError(env, "SetConsoleMode failed");81}82return old;83}848586