WIP: [BUGGY] cli history

This commit is contained in:
Adrien Bourmault 2021-07-08 20:30:09 +02:00
parent 9566a3c74e
commit 439a0b0bb2
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
1 changed files with 47 additions and 9 deletions

View File

@ -30,6 +30,7 @@
#define SEND_BUFFER_SIZE 80
#define RECEIVE_BUFFER_SIZE 80 * 24
#define NHISTORY 25
#define SERVER_IP_ADDR "127.0.0.1"
#define SERVER_PORT 9000
@ -73,24 +74,56 @@ static char getch(void)
return buf;
}
char sendBuff[SEND_BUFFER_SIZE] = {0};
char receiveBuff[RECEIVE_BUFFER_SIZE] = {0};
char historyBuff[SEND_BUFFER_SIZE * NHISTORY] = {0};
char curChar;
char *exitCommand = "exit";
volatile int curLineLength, readLine, historyIndex, historyModifier = 0;
void connectedCommunication(int sockfd)
{
char sendBuff[SEND_BUFFER_SIZE], receiveBuff[RECEIVE_BUFFER_SIZE], curChar;
char *exitCommand = "exit";
int i;
for (;;) {
// Zeroing buffer
bzero(sendBuff, sizeof(sendBuff));
printf("gem-graph console> ");
i = 0;
// Read command from terminal
while (( curChar= getch()) != '\n') {
// XXX ARROWS
sendBuff[i++] = curChar;
printf("%c", curChar);
curLineLength = 0;
readLine = 1;
historyModifier = 1;
while (readLine && (curChar = getch())) {
switch (curChar) {
case '\n':
printf("%c", curChar);
readLine = 0;
break;
case 27:
if (getch() == 91)
if (getch() == 65) {
historyModifier--;
printf("%s", &historyBuff[
(historyIndex - historyModifier - 1)
* SEND_BUFFER_SIZE
]);
memcpy(sendBuff, &historyBuff[
(historyIndex - historyModifier - 1)
* SEND_BUFFER_SIZE
], SEND_BUFFER_SIZE);
curLineLength = curLineLength + strlen(sendBuff);
}
break;
default:
sendBuff[curLineLength++] = curChar;
printf("%c", curChar);
break;
}
fflush(stdout);
}
@ -98,6 +131,11 @@ void connectedCommunication(int sockfd)
if (sendBuff[0] == 0)
continue;
// Update history
memcpy(&historyBuff[historyIndex * SEND_BUFFER_SIZE], sendBuff,
SEND_BUFFER_SIZE);
historyIndex = (historyIndex + 1) % NHISTORY;
// Quit if asked
if (strcmp(exitCommand, sendBuff) == 0) {
return;