Fixed code style.

This commit is contained in:
Oliver Schmidt 2017-11-15 19:45:08 +01:00
parent 9f652e7c21
commit d21fc5bf40
1 changed files with 85 additions and 65 deletions

View File

@ -1,11 +1,14 @@
#include <stdio.h>
#ifdef _WIN32
#include <conio.h>
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#else
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
@ -25,7 +28,6 @@
#define ioctlsocket ioctl
#define SOCKADDR struct sockaddr
#define SOCKADDR_IN struct sockaddr_in
#define getch cgetc
static void Sleep(u_int msec)
{
@ -35,113 +37,131 @@ static void Sleep(u_int msec)
nanosleep(&ts, NULL);
}
/* kbhit() and cgetc() for Unix: */
/* kbhit() and getch() for Unix: */
static int __conio_initialized;
static struct termios tty,otty;
static __inline__ void makecooked(void)
static void makecooked(void)
{
tcsetattr(0, TCSANOW, &otty);
tcsetattr(0, TCSANOW, &otty);
}
static void sighand( int num )
{
signal(SIGINT, SIG_IGN);
signal(SIGTERM, SIG_IGN);
signal(SIGHUP, SIG_IGN);
exit(0);
signal(SIGINT, SIG_IGN);
signal(SIGTERM, SIG_IGN);
signal(SIGHUP, SIG_IGN);
exit(0);
}
static void exitfn(void)
{
if (__conio_initialized) makecooked();
printf("\n");
if (__conio_initialized)
{
makecooked();
}
printf("\n");
}
static void makeraw(void)
{
static int first_call = 1;
static int first_call = 1;
if (first_call) {
first_call = 0;
if (tcgetattr(0, &tty)) { /* input terminal */
fprintf(stderr, "cannot get terminal attributes: %s\n", strerror(errno));
exit(1);
}
otty=tty; /* save it */
#ifdef NO_MAKERAW /* makeraw code from NetBSD 1.6.2 (/usr/src/lib/libc/termios/cfmakeraw.c) */
tty.c_iflag &= ~(IMAXBEL|IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
tty.c_oflag &= ~OPOST;
tty.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
tty.c_cflag &= ~(CSIZE|PARENB);
tty.c_cflag |= CS8;
if (first_call)
{
first_call = 0;
if (tcgetattr(0, &tty))
{ /* input terminal */
fprintf(stderr, "cannot get terminal attributes: %s\n", strerror(errno));
exit(1);
}
otty=tty; /* save it */
#ifdef NO_MAKERAW /* makeraw code from NetBSD 1.6.2 (/usr/src/lib/libc/termios/cfmakeraw.c) */
tty.c_iflag &= ~(IMAXBEL|IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
tty.c_oflag &= ~OPOST;
tty.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
tty.c_cflag &= ~(CSIZE|PARENB);
tty.c_cflag |= CS8;
#else
cfmakeraw(&tty);
cfmakeraw(&tty);
#endif
tty.c_lflag |= ISIG; /* enable signals from ^C etc. */
tty.c_oflag |= ONLCR | OPOST; /* translate '\n' to '\r\n' on output */
}
tcsetattr(0, TCSANOW, &tty);
tty.c_lflag |= ISIG; /* enable signals from ^C etc. */
tty.c_oflag |= ONLCR | OPOST; /* translate '\n' to '\r\n' on output */
}
tcsetattr(0, TCSANOW, &tty);
}
static void __init_conio(void)
{
if (! __conio_initialized) {
signal(SIGINT, sighand);
signal(SIGTERM, sighand);
signal(SIGHUP, sighand);
atexit(exitfn);
makeraw();
__conio_initialized = 1;
}
if (! __conio_initialized)
{
signal(SIGINT, sighand);
signal(SIGTERM, sighand);
signal(SIGHUP, sighand);
atexit(exitfn);
makeraw();
__conio_initialized = 1;
}
}
static void __makeraw(void)
{
if (! __conio_initialized) __init_conio();
else makeraw();
if (! __conio_initialized)
{
__init_conio();
}
else
{
makeraw();
}
}
static void __makecooked(void)
{
if (__conio_initialized) {
makecooked();
}
if (__conio_initialized)
{
makecooked();
}
}
static char cgetc (void)
static char getch (void)
{
char c;
char c;
__makeraw();
read(0, &c, 1);
return c;
__makeraw();
read(0, &c, 1);
return c;
}
static unsigned char kbhit (void)
{
int retval;
fd_set fds;
struct timeval tv;
int retval;
fd_set fds;
struct timeval tv;
__makeraw();
__makeraw();
again:
FD_ZERO(&fds);
FD_SET(0, &fds);
tv.tv_sec = tv.tv_usec = 0;
retval = select(1, &fds, NULL, NULL, &tv);
if (retval == -1) {
if (errno == EINTR) goto again;
__makecooked();
exit(1);
}
if (FD_ISSET(0, &fds)) {
return 1;
}
return 0;
again:
FD_ZERO(&fds);
FD_SET(0, &fds);
tv.tv_sec = tv.tv_usec = 0;
retval = select(1, &fds, NULL, NULL, &tv);
if (retval == -1)
{
if (errno == EINTR)
{
goto again;
}
__makecooked();
exit(1);
}
if (FD_ISSET(0, &fds))
{
return 1;
}
return 0;
}
#endif