feat(line): add history record

When the program exits, the history will be saved in ~/.fish_history
This commit is contained in:
iridiumR 2022-09-18 22:12:57 +08:00
parent 72b7a424b7
commit 53bee9dfb0
No known key found for this signature in database
GPG Key ID: 5574BE4450D55618
2 changed files with 18 additions and 4 deletions

9
line.c
View File

@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
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;

13
main.c
View File

@ -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 <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <strings.h>
#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;
}