From 439a0b0bb2c20f70fb407382ccff4b2355235990 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 8 Jul 2021 20:30:09 +0200 Subject: [PATCH] WIP: [BUGGY] cli history --- src/cli.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/src/cli.c b/src/cli.c index f489a5b..957ff51 100644 --- a/src/cli.c +++ b/src/cli.c @@ -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;