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

7
line.c
View file

@ -2,12 +2,15 @@
* @Author: 1ridic * @Author: 1ridic
* @Date: 2022-09-18 14:14:05 * @Date: 2022-09-18 14:14:05
* @Last Modified by: 1ridic * @Last Modified by: 1ridic
* @Last Modified time: 2022-09-18 14:14:05 * @Last Modified time: 2022-09-18 22:08:27
*/ */
#include "line.h" #include "line.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
FILE *hf = NULL;
char *readLine(void) { char *readLine(void) {
int bufsize = LINE_BUF_SIZE; int bufsize = LINE_BUF_SIZE;
@ -26,6 +29,8 @@ char *readLine(void) {
// repleace EOF with a null character and return // repleace EOF with a null character and return
if (c == EOF || c == '\n') { if (c == EOF || c == '\n') {
buffer[position] = '\0'; buffer[position] = '\0';
time_t tick=time(NULL);
fprintf(hf,"%d:%s\n",(int)tick,buffer);
return buffer; return buffer;
} else { } else {
buffer[position] = c; buffer[position] = c;

13
main.c
View file

@ -2,12 +2,13 @@
* @Author: 1ridic * @Author: 1ridic
* @Date: 2022-09-18 14:13:59 * @Date: 2022-09-18 14:13:59
* @Last Modified by: 1ridic * @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 <signal.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <strings.h>
#include "loop.h" #include "loop.h"
char volatile isWaiting = 0; char volatile isWaiting = 0;
@ -18,7 +19,9 @@ void intHandler(int dummy) {
if (isWaiting==1) { if (isWaiting==1) {
return; return;
} }
fprintf(stderr,"\nSIGINT exit.\n"); extern FILE* hf;
fprintf(stderr,"\nSIGINT exit.");
fclose(hf);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -28,11 +31,17 @@ int main(int argc, char *argv[]) {
signal(SIGINT, intHandler); signal(SIGINT, intHandler);
/* clear screen */ /* clear screen */
fprintf(stdout,"\033[H\033[J"); fprintf(stdout,"\033[H\033[J");
/* open history file */
extern FILE* hf;
hf = fopen(strcat(getenv("HOME"), "/.dish_history"), "w");
#ifdef DEBUG #ifdef DEBUG
fprintf(stdout,"DEBUG is defined\n"); fprintf(stdout,"DEBUG is defined\n");
#endif #endif
while (1) { while (1) {
loop(); loop();
} }
fclose(hf);
return 0; return 0;
} }