From 53bee9dfb0cdcb21f58087ac3b40690f2d72c58a Mon Sep 17 00:00:00 2001 From: iridiumR Date: Sun, 18 Sep 2022 22:12:57 +0800 Subject: [PATCH] feat(line): add history record When the program exits, the history will be saved in ~/.fish_history --- line.c | 9 +++++++-- main.c | 13 +++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/line.c b/line.c index 8df7b91..47a81c9 100644 --- a/line.c +++ b/line.c @@ -1,13 +1,16 @@ /* * @Author: 1ridic * @Date: 2022-09-18 14:14:05 - * @Last Modified by: 1ridic - * @Last Modified time: 2022-09-18 14:14:05 + * @Last Modified by: 1ridic + * @Last Modified time: 2022-09-18 22:08:27 */ #include "line.h" #include #include #include +#include + +FILE *hf = NULL; char *readLine(void) { int bufsize = LINE_BUF_SIZE; @@ -26,6 +29,8 @@ char *readLine(void) { // repleace EOF with a null character and return if (c == EOF || c == '\n') { buffer[position] = '\0'; + time_t tick=time(NULL); + fprintf(hf,"%d:%s\n",(int)tick,buffer); return buffer; } else { buffer[position] = c; diff --git a/main.c b/main.c index 2c6c1a2..4cfdeab 100644 --- a/main.c +++ b/main.c @@ -2,12 +2,13 @@ * @Author: 1ridic * @Date: 2022-09-18 14:13:59 * @Last Modified by: 1ridic - * @Last Modified time: 2022-09-18 20:43:07 + * @Last Modified time: 2022-09-18 22:01:12 */ #include #include #include #include +#include #include "loop.h" char volatile isWaiting = 0; @@ -18,7 +19,9 @@ void intHandler(int dummy) { if (isWaiting==1) { return; } - fprintf(stderr,"\nSIGINT exit.\n"); + extern FILE* hf; + fprintf(stderr,"\nSIGINT exit."); + fclose(hf); exit(EXIT_FAILURE); } @@ -28,11 +31,17 @@ int main(int argc, char *argv[]) { signal(SIGINT, intHandler); /* clear screen */ fprintf(stdout,"\033[H\033[J"); + /* open history file */ + extern FILE* hf; + hf = fopen(strcat(getenv("HOME"), "/.dish_history"), "w"); + #ifdef DEBUG fprintf(stdout,"DEBUG is defined\n"); #endif while (1) { loop(); } + + fclose(hf); return 0; } \ No newline at end of file