Elliott Hughes | f7fb59f | 2010-03-16 16:43:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
Elliott Hughes | f7fb59f | 2010-03-16 16:43:17 -0700 | [diff] [blame] | 18 | #include "JNIHelp.h" |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <termios.h> |
| 22 | #include <unistd.h> |
| 23 | |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 24 | static jboolean java_io_Console_isatty(JNIEnv*, jclass, jint fd) { |
Elliott Hughes | f7fb59f | 2010-03-16 16:43:17 -0700 | [diff] [blame] | 25 | return TEMP_FAILURE_RETRY(isatty(fd)); |
| 26 | } |
| 27 | |
| 28 | static jint java_io_Console_setEcho(JNIEnv* env, jclass, jboolean on, jint previousState) { |
| 29 | termios state; |
| 30 | if (TEMP_FAILURE_RETRY(tcgetattr(STDIN_FILENO, &state)) == -1) { |
| 31 | jniThrowIOException(env, errno); |
| 32 | return 0; |
| 33 | } |
| 34 | if (on) { |
| 35 | state.c_lflag = previousState; |
| 36 | } else { |
| 37 | previousState = state.c_lflag; |
| 38 | state.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL); |
| 39 | } |
| 40 | if (TEMP_FAILURE_RETRY(tcsetattr(STDIN_FILENO, TCSAFLUSH, &state)) == -1){ |
| 41 | jniThrowIOException(env, errno); |
| 42 | return 0; |
| 43 | } |
| 44 | return previousState; |
| 45 | } |
| 46 | |
| 47 | static JNINativeMethod gMethods[] = { |
Elliott Hughes | f7fb59f | 2010-03-16 16:43:17 -0700 | [diff] [blame] | 48 | { "isatty", "(I)Z", (void*) java_io_Console_isatty }, |
| 49 | { "setEchoImpl", "(ZI)I", (void*) java_io_Console_setEcho }, |
| 50 | }; |
| 51 | int register_java_io_Console(JNIEnv* env) { |
| 52 | return jniRegisterNativeMethods(env, "java/io/Console", gMethods, NELEM(gMethods)); |
| 53 | } |