Fixed code style.

This commit is contained in:
Oliver Schmidt 2017-11-15 19:45:08 +01:00
parent 9f652e7c21
commit d21fc5bf40

View File

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