From 91476b920db00e59f4f85ac14e4f39203d78b52f Mon Sep 17 00:00:00 2001 From: s492459 Date: Sat, 4 Jan 2025 16:27:36 +0100 Subject: [PATCH] logging_implementation --- a.out | Bin 0 -> 20624 bytes main_logging.c | 145 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100755 a.out create mode 100644 main_logging.c diff --git a/a.out b/a.out new file mode 100755 index 0000000000000000000000000000000000000000..3f606b495102296361e04ffee0453468008cc83c GIT binary patch literal 20624 zcmeHPdvsLCd7rzhS*_4xS4aYhmlhjrh|!9NECWU$5DSZW*%)G|gO}C5w6V0S?85@r z1lckPdlbds*0FO+WAZqObK*KAZBpvEwuBu6shf%5JEtZXeK>{6vpX_XA;W?2DfWh+@Z z&eySp%m%zj&~$#Q4#3sR%a(j-mvk#2>D5wY5;#XEGX+%(36fr+VlUA-3NojTlOCt4 z#LcjBnl7iH;^+LibUrh|MY4Yi8uchPdmD7Uy)F6hjM9_+S9(Myy$-3@A@vj-qXwZU zsNzX6p<|`AXNF5j2}MD(UWe2(!;QKcQ;>X6x&3k~_&@P?tJG_92{X(LJ9RQsQ2DzJ zdK8yGw5Va;CH)l|hbQDXC@90r8at!y%}tG+!G_LgEPb%y;L7HP<|cPC?p`jMK<#qj zpgFa9#~ubVgG5uqY3bE*Wsi8(l27ur(_VY7==1B3%@WF>2qSXh9*rU3m}&|Nsph1xZleVWdb z(Phoa|2S_oI z@^$&6F<1#4^!vimn7=c6Z-{j#qOnw%r2^3Kr&0;jlCnlNdV<1xSGUyjr$Ps#DMm)= zLTF~2w{6+b=3DM=%GFj(RF}JNWWFuCxBG&jL?{wXrb3C`+uJ(hvCwXRduIqXBVF;B zY@JURom3D(u;9;%IZv=ioT;Yc3hKerAopjes_VZx8ZDN4D>QZ z1ROKaRiB7GZlD|IQ@5lSLx!$JRSp{H#`{UHfiBiZsc_6d*Y7hrQn@# zA}|$!sR&F(;QvPie&v|=8*kqmc5grb*G9&?M+Z}u?69}*1^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34Uc}zUuVV5GJ&$VOdx@$r#SNkaFm6kfD z-diX}57DW|QVH3n%OwA(ls7+3o)q+a+k}0y z0R6nQV}AJhX#x3nL8tzj$DKBpx~_K1W`RE0xHo`)%_Q<(lDFU?()9jlhp^+!y`oXi zM(p-LBDKsN$2MMnd(@XgDgi<=l@5ooW=w4P^`*Le0owGNgk&)8i*&}@{hhub7U`tV zpFYU2uy%Kb@Z%Eq(#a)gyK>ZrHP(N~7mB5j!h{q4u8=R7?&_j_(FP1FVlKN$EnQUAgQt&ungQdmeR?#82NOuTPFVS)UIZ z*yY?2!!_x4b@UQ@@=c1yZTI;snzE)GkGYeFx{%rerV={rP&K4+p+q-x$B^l|k!HD( z^CII;9Ws#G(^352ifB-h)^FH?L^;BQcn1<;<_;c;!G#X7?O$;22_=$9jq?y6c!^M_ zpA=-RyEDbyqS0Ti&8!{i7>LG^u(^eJX$ z{m+9&F;Vt&`GCv|)OSdTh+>{U^j*iO?~-~7UeeWaXO(U!_!W~rk~zIdsN|FZg)4a4 zr0-OE5)Lax36-9Le`nHHd7*;WsDPRDmD%S^`s#bGg6cc3k~hciyHa0`KXXY*3F$e} zSj_tW2qwi}={LD_X$9%Ezp +#include +#include +#include +#include + +#define MAX_SIZE 1024 +#define HISTORY_SIZE 10 + +// Global variable for termios +struct termios orig_termios; + +// Terminal input helpers +void EnableRawMode(struct termios* orig_termios) { + struct termios raw = *orig_termios; + raw.c_lflag &= ~(ECHO | ICANON); // Disable echo and canonical mode + tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw); +} + +void DisableRawMode(struct termios* orig_termios) { + tcsetattr(STDIN_FILENO, TCSAFLUSH, orig_termios); +} + +// Wrapper function to pass DisableRawMode with argument +void DisableRawModeWrapper() { + DisableRawMode(&orig_termios); +} + +int LogCommandHistory(char* buf, char** command_log, int* n) { + int index = *n % HISTORY_SIZE; + int prev_index = (*n == 0) ? HISTORY_SIZE - 1 : (*n - 1) % HISTORY_SIZE; + + if (command_log[prev_index] != NULL && strcmp(buf, command_log[prev_index]) == 0) { + return 0; + } + if (command_log[index] != NULL) { + free(command_log[index]); + } + + command_log[index] = strdup(buf); + (*n)++; + + return 0; +} + + +void FreeCommandHistory(char** command_log, int n) { + for (int i = 0; i < n; i++) { + free(command_log[i]); + } +} + +// Get input with arrow key navigation +void GetInputWithHistory(char* buf, char** command_log, int n) { + int index = n % HISTORY_SIZE; + int history_counter = 0; + int pos = 0; + char c; + + while (1) { + c = getchar(); + + + if (c == '\n') { // Enter key + buf[pos] = '\0'; + printf("\n"); + return; + } else if (c == 127 || c == '\b') { // Backspace + if (pos > 0) { + pos--; + buf[pos] = '\0'; + printf("\b \b"); + } + } else if (c == '\033') { // Inputting arrow keys + getchar(); // Skipping the first '[' character + char arrow = getchar(); + if (arrow == 'A' && history_counter < HISTORY_SIZE) { // Up arrow + int prev_index = (index == 0) ? HISTORY_SIZE - 1 : (index - 1) % HISTORY_SIZE; + if (command_log[prev_index] != NULL) { + index = (index == 0) ? HISTORY_SIZE - 1 : index - 1; // Decrease the index + history_counter++; + + strcpy(buf, command_log[index]); + printf("\33[2K\r"); // Clear line + printf("[%s] $ %s", getcwd(NULL, 0), buf); + pos = strlen(buf); + } + } else if (arrow == 'B') { // Down arrow + if (history_counter > 1) { + index = (index + 1) % HISTORY_SIZE; + history_counter--; + + strcpy(buf, command_log[index]); + printf("\33[2K\r"); // Clear line + printf("[%s] $ %s", getcwd(NULL, 0), buf); + pos = strlen(buf); + } else if (history_counter == 1) { + history_counter--; + index++; + + buf[0] = '\0'; // Clear buffer + printf("\33[2K\r"); // Clear line + printf("[%s] $ ", getcwd(NULL, 0)); + pos = 0; + } + } + } else { // Regular character + if (pos < MAX_SIZE - 1) { + buf[pos++] = c; + buf[pos] = '\0'; + printf("%c", c); + } + } + } +} + +int main() { + char buf[MAX_SIZE]; + char* command_log[HISTORY_SIZE] = {NULL}; + int n = 0; + + tcgetattr(STDIN_FILENO, &orig_termios); + + // Register atexit to call DisableRawModeWrapper when the program exits + atexit(DisableRawModeWrapper); + + while (1) { + printf("[%s] $ ", getcwd(NULL, 0)); + + // Enable raw mode for manual input handling + EnableRawMode(&orig_termios); + GetInputWithHistory(buf, command_log, n); + DisableRawMode(&orig_termios); + + if (strcmp(buf, "exit") == 0) { + break; + } + + LogCommandHistory(buf, command_log, &n); + printf("Executed: %s\n", buf); // For testing, replace with actual command handling + } + + FreeCommandHistory(command_log, n); + return 0; +}