1 /*
   2  * Copyright (c) 2005, 2018, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include "jni.h"
  27 #include "jni_util.h"
  28 #include "jvm.h"
  29 #include "java_io_Console.h"
  30 #include <stdlib.h>
  31 #include <Wincon.h>
  32 
  33 static HANDLE hStdOut = INVALID_HANDLE_VALUE;
  34 static HANDLE hStdIn = INVALID_HANDLE_VALUE;
  35 
  36 JNIEXPORT jint JNICALL
  37 Java_java_io_Console_width(JNIEnv *env, jobject this)
  38 {
  39     CONSOLE_SCREEN_BUFFER_INFO csbi;
  40 
  41     if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
  42         if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi)) {
  43             return -1;
  44         }
  45     }
  46 
  47     return csbi.dwSize.X;
  48 }
  49 
  50 JNIEXPORT jint JNICALL
  51 Java_java_io_Console_height(JNIEnv *env, jobject this)
  52 {
  53     CONSOLE_SCREEN_BUFFER_INFO csbi;
  54 
  55     if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
  56         if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi)) {
  57             return -1;
  58         }
  59     }
  60 
  61     return csbi.dwSize.Y;
  62 }
  63 
  64 JNIEXPORT jboolean JNICALL
  65 Java_java_io_Console_istty(JNIEnv *env, jclass cls)
  66 {
  67     if (hStdIn == INVALID_HANDLE_VALUE &&
  68         (hStdIn = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
  69         return JNI_FALSE;
  70     }
  71     if (hStdOut == INVALID_HANDLE_VALUE &&
  72         (hStdOut = GetStdHandle(STD_OUTPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
  73         return JNI_FALSE;
  74     }
  75     if (GetFileType(hStdIn) != FILE_TYPE_CHAR ||
  76         GetFileType(hStdOut) != FILE_TYPE_CHAR)
  77         return JNI_FALSE;
  78     return JNI_TRUE;
  79 }
  80 
  81 JNIEXPORT jstring JNICALL
  82 Java_java_io_Console_encoding(JNIEnv *env, jclass cls)
  83 {
  84     char buf[64];
  85     int cp = GetConsoleCP();
  86     if (cp >= 874 && cp <= 950)
  87         sprintf(buf, "ms%d", cp);
  88     else
  89         sprintf(buf, "cp%d", cp);
  90     return JNU_NewStringPlatform(env, buf);
  91 }
  92 
  93 JNIEXPORT jboolean JNICALL
  94 Java_java_io_Console_echo(JNIEnv *env, jclass cls, jboolean on)
  95 {
  96     DWORD fdwMode;
  97     jboolean old;
  98     if (! GetConsoleMode(hStdIn, &fdwMode)) {
  99         JNU_ThrowIOExceptionWithLastError(env, "GetConsoleMode failed");
 100         return !on;
 101     }
 102     old = (fdwMode & ENABLE_ECHO_INPUT) != 0;
 103     if (on) {
 104         fdwMode |= ENABLE_ECHO_INPUT;
 105     } else {
 106         fdwMode &= ~ENABLE_ECHO_INPUT;
 107     }
 108     if (! SetConsoleMode(hStdIn, fdwMode)) {
 109         JNU_ThrowIOExceptionWithLastError(env, "SetConsoleMode failed");
 110     }
 111     return old;
 112 }