Beispiel #1
0
JNIEXPORT void JNICALL Java_com_yifanlu_Josh_Josh_SETCONSOLEHISTORYINFO
  (JNIEnv *env, jclass jcls, jint bufferSize, jint numberOfBuffers, jint flags)
{
	CONSOLE_HISTORY_INFO info;
	info.cbSize = sizeof(CONSOLE_HISTORY_INFO);
	info.HistoryBufferSize = bufferSize;
	info.NumberOfHistoryBuffers = numberOfBuffers;
	info.dwFlags = flags;

	SetConsoleHistoryInfo(&info);
}
Beispiel #2
0
char *x_readline(const char *prompt) {
	static char buffer[1024];
	DWORD count = 0;
	BOOL ok;


	HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);

	/* MS CRT uses -2 to indicate there is no stdin/stdout/stderr fileno */

/*
	if (h == INVALID_HANDLE_VALUE) {
		//* cygwin? * /
		fputs("GetStdHandle\n", stderr);
		fflush(stderr);
		return NULL;

		char *cp;
		fputs(prompt, stdout);
		fflush(stdout);
		return fgets(buffer, sizeof(buffer), stdin);
	}
*/

	if (!readline_init) {

		//struct stat st;


		CONSOLE_HISTORY_INFO chi;
		DWORD mode;

		memset(&chi, 0, sizeof(chi));
		chi.cbSize = sizeof(CONSOLE_HISTORY_INFO);
		chi.HistoryBufferSize = HISTORY_SIZE;
		chi.NumberOfHistoryBuffers = 1; /* ???? */
		chi.dwFlags = HISTORY_NO_DUP_FLAG;
		ok = SetConsoleHistoryInfo(&chi);

		if (!ok) {
			fprintf(stderr, "SetConsoleHistoryInfo: %lx\n", GetLastError());
			fflush(stderr);			
		}


		mode = ENABLE_ECHO_INPUT | ENABLE_EXTENDED_FLAGS | ENABLE_INSERT_MODE | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_QUICK_EDIT_MODE;
		ok = SetConsoleMode(hIn, mode);

		if (!ok) {
			fprintf(stderr, "SetConsoleMode: %lx (h = %p)\n", GetLastError(), hIn);
			fflush(stderr);			
		}

		readline_init = 1;
	}

	fflush(stderr);
	fflush(stdout);

	ok = WriteConsole(hOut, prompt, strlen(prompt), NULL, NULL);
	if (!ok) {
		/* msys/cygwin? */

		// fprintf(stderr, "WriteConsole: %lx (h = %p)\n", GetLastError(), hOut);
		fflush(stderr);
		if (prompt) fputs(prompt, stdout);
		fflush(stdout);
		char *cp = fgets(buffer, sizeof(buffer), stdin);
		if (!cp) {
			if (prompt) fputc('\n', stdout);
			return NULL;
		}
		cleanup_buffer(buffer, strlen(buffer));
		return buffer;		
	}
	ok = ReadConsole(hIn, buffer, sizeof(buffer), &count, NULL);
	if (!ok) {
		if (prompt) fputc('\n', stdout);
		// fflush(stdout);
		// fprintf(stderr, "Error: %lx (h = %p)\n", GetLastError(), hIn);
		// fprintf(stderr, "Type: %08x\n", GetFileType(hIn));
		// fflush(stderr);
		return NULL;
	}
	cleanup_buffer(buffer, count);
	return buffer;
}