getch !
This commit is contained in:
parent
b5be741617
commit
e5ba2b9303
47
src/cli.c
47
src/cli.c
|
@ -25,6 +25,8 @@
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <termios.h>
|
||||||
|
|
||||||
#define SEND_BUFFER_SIZE 80
|
#define SEND_BUFFER_SIZE 80
|
||||||
#define RECEIVE_BUFFER_SIZE 80 * 24
|
#define RECEIVE_BUFFER_SIZE 80 * 24
|
||||||
|
@ -32,6 +34,45 @@
|
||||||
#define SERVER_IP_ADDR "127.0.0.1"
|
#define SERVER_IP_ADDR "127.0.0.1"
|
||||||
#define SERVER_PORT 9000
|
#define SERVER_PORT 9000
|
||||||
|
|
||||||
|
static char getch(void)
|
||||||
|
{
|
||||||
|
char buf = 0;
|
||||||
|
// old terminal
|
||||||
|
struct termios old = {0};
|
||||||
|
|
||||||
|
// force flush stdout
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
|
if(tcgetattr(0, &old) < 0) {
|
||||||
|
printLog("Error getting terminal settings!\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
old.c_lflag &= ~ICANON; // disable buffered I/O
|
||||||
|
old.c_lflag &= ~ECHO; // set no echo mode
|
||||||
|
old.c_cc[VMIN] = 1;
|
||||||
|
old.c_cc[VTIME] = 0;
|
||||||
|
|
||||||
|
if(tcsetattr(0, TCSANOW, &old) < 0) {
|
||||||
|
printLog("Error setting terminal settings!\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(read(0, &buf, 1) < 0) {
|
||||||
|
printLog("Error reading character!\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
old.c_lflag |= ICANON; // enable buffered I/O
|
||||||
|
old.c_lflag |= ECHO; // set echo mode
|
||||||
|
|
||||||
|
if(tcsetattr(0, TCSADRAIN, &old) < 0) {
|
||||||
|
printLog("Error resetting terminal settings!\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
void connectedCommunication(int sockfd)
|
void connectedCommunication(int sockfd)
|
||||||
{
|
{
|
||||||
char sendBuff[SEND_BUFFER_SIZE], receiveBuff[RECEIVE_BUFFER_SIZE], curChar;
|
char sendBuff[SEND_BUFFER_SIZE], receiveBuff[RECEIVE_BUFFER_SIZE], curChar;
|
||||||
|
@ -46,7 +87,7 @@ void connectedCommunication(int sockfd)
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
||||||
// Read command from terminal
|
// Read command from terminal
|
||||||
while (( curChar= getchar()) != '\n') {
|
while (( curChar= getch()) != '\n') {
|
||||||
// XXX ARROWS
|
// XXX ARROWS
|
||||||
sendBuff[i++] = curChar;
|
sendBuff[i++] = curChar;
|
||||||
}
|
}
|
||||||
|
@ -71,10 +112,10 @@ void connectedCommunication(int sockfd)
|
||||||
|
|
||||||
// Detect null-string returned
|
// Detect null-string returned
|
||||||
if (receiveBuff[0] == 0) {
|
if (receiveBuff[0] == 0) {
|
||||||
printLog("An error occured or invalid command!");
|
printLog("An error occured or invalid command!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%s\n\n", receiveBuff);
|
printf("%s\n", receiveBuff);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue