#include "less.h"
#if OS2
#include "cmd.h"
#include "pckeys.h"
#endif
#if MSDOS_COMPILER==WIN32C
#include "windows.h"
extern char WIN32getch();
static DWORD console_mode;
#endif
public int tty;
extern int sigs;
extern int utf_mode;
extern char * active_dashp_command;
extern int add_newline;
public void
open_getchr()
{
#if MSDOS_COMPILER==WIN32C
SECURITY_ATTRIBUTES sa;
memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES));
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
tty = (int) CreateFile("CONIN$", GENERIC_READ,
FILE_SHARE_READ, &sa,
OPEN_EXISTING, 0L, NULL);
GetConsoleMode((HANDLE)tty, &console_mode);
SetConsoleMode((HANDLE)tty, ENABLE_PROCESSED_INPUT);
#else
#if MSDOS_COMPILER
extern int fd0;
fd0 = dup(0);
close(0);
tty = open("CON", OPEN_READ);
#if MSDOS_COMPILER==DJGPPC
(void) __djgpp_set_ctrl_c(1);
#endif
#else
#if OS2
tty = __open("/dev/tty", OPEN_READ);
#else
tty = open("/dev/tty", OPEN_READ);
#endif
if (tty < 0)
tty = 2;
#endif
#endif
}
public void
close_getchr()
{
#if MSDOS_COMPILER==WIN32C
SetConsoleMode((HANDLE)tty, console_mode);
CloseHandle((HANDLE)tty);
#endif
}
public int
getchr()
{
char c;
int result;
if (active_dashp_command) {
c = *active_dashp_command++;
if (c =='\0') {
active_dashp_command = NULL;
if (add_newline) {
c = '\n';
add_newline = 0;
return (c & 0377);
}
} else
return (c & 0377);
}
do
{
#if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
flush();
#if MSDOS_COMPILER==WIN32C
if (ABORT_SIGS())
return (READ_INTR);
c = WIN32getch(tty);
#else
c = getch();
#endif
result = 1;
if (c == '\003')
return (READ_INTR);
#else
result = iread(tty, &c, sizeof(char));
if (result == READ_INTR)
return (READ_INTR);
if (result < 0)
{
quit(QUIT_ERROR);
}
#endif
#if 0
{
int hex_in = 0;
int hex_value = 0;
if (c == CONTROL('A'))
{
hex_in = 2;
result = 0;
continue;
}
if (hex_in > 0)
{
int v;
if (c >= '0' && c <= '9')
v = c - '0';
else if (c >= 'a' && c <= 'f')
v = c - 'a' + 10;
else if (c >= 'A' && c <= 'F')
v = c - 'A' + 10;
else
hex_in = 0;
hex_value = (hex_value << 4) | v;
if (--hex_in > 0)
{
result = 0;
continue;
}
c = hex_value;
}
}
#endif
if (c == '\0')
c = '\340';
} while (result != 1);
return (c & 0xFF);
}