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 <sys/ioctl.h> 32 #include <sys/resource.h> 33 #include <unistd.h> 34 #include <termios.h> 35 36 JNIEXPORT jint JNICALL 37 Java_java_io_Console_width(JNIEnv *env, jobject this) 38 { 39 struct winsize w; 40 41 if (ioctl(0, TIOCGWINSZ, &w) == 0 || ioctl(1, TIOCGWINSZ, &w) == 0 || ioctl(2, TIOCGWINSZ, &w) == 0) { 42 return w.ws_col; 43 } 44 return -1; 45 } 46 47 JNIEXPORT jint JNICALL 48 Java_java_io_Console_height(JNIEnv *env, jobject this) 49 { 50 struct winsize w; 51 52 if (ioctl(0, TIOCGWINSZ, &w) == 0 || ioctl(1, TIOCGWINSZ, &w) == 0 || ioctl(2, TIOCGWINSZ, &w) == 0) { 53 return w.ws_row; 54 } 55 return -1; 56 } 57 58 JNIEXPORT jboolean JNICALL 59 Java_java_io_Console_istty(JNIEnv *env, jclass cls) 60 { 61 return isatty(fileno(stdin)) && isatty(fileno(stdout)); 62 } 63 64 JNIEXPORT jstring JNICALL 65 Java_java_io_Console_encoding(JNIEnv *env, jclass cls) 66 { 67 return NULL; 68 } 69 70 JNIEXPORT jboolean JNICALL 71 Java_java_io_Console_echo(JNIEnv *env, 72 jclass cls, 73 jboolean on) 74 { 75 struct termios tio; 76 jboolean old; 77 int tty = fileno(stdin); 78 if (tcgetattr(tty, &tio) == -1) { 79 JNU_ThrowIOExceptionWithLastError(env, "tcgetattr failed"); 80 return !on; 81 } 82 old = (tio.c_lflag & ECHO); 83 if (on) { 84 tio.c_lflag |= ECHO; 85 } else { 86 tio.c_lflag &= ~ECHO; 87 } 88 if (tcsetattr(tty, TCSANOW, &tio) == -1) { 89 JNU_ThrowIOExceptionWithLastError(env, "tcsetattr failed"); 90 } 91 return old; 92 }