blob: f6c69e7f4339266b482ef5279523ca643c704fd0 [file] [log] [blame]
Elliott Hughesf7fb59f2010-03-16 16:43:17 -07001/*
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 Hughesf7fb59f2010-03-16 16:43:17 -070018#include "JNIHelp.h"
19
20#include <errno.h>
21#include <termios.h>
22#include <unistd.h>
23
Brian Carlstrom44e0e562010-05-06 23:44:16 -070024static jboolean java_io_Console_isatty(JNIEnv*, jclass, jint fd) {
Elliott Hughesf7fb59f2010-03-16 16:43:17 -070025 return TEMP_FAILURE_RETRY(isatty(fd));
26}
27
28static 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
47static JNINativeMethod gMethods[] = {
Elliott Hughesf7fb59f2010-03-16 16:43:17 -070048 { "isatty", "(I)Z", (void*) java_io_Console_isatty },
49 { "setEchoImpl", "(ZI)I", (void*) java_io_Console_setEcho },
50};
51int register_java_io_Console(JNIEnv* env) {
52 return jniRegisterNativeMethods(env, "java/io/Console", gMethods, NELEM(gMethods));
53}