-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.cpp
77 lines (71 loc) · 1.57 KB
/
history.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "history.h"
#include <readline/readline.h>
shell_history::shell_history(){
strcat(strcpy(history_file, getenv("HOME")), "/.myshell_history");
latest_command[0] = '\0';
FILE *fp = fopen(history_file,"r");
if(!fp){
fclose(fopen(history_file,"w"));
history_cnt=0;
history_idx=0;
dq.clear();
}
else{
char buff[4096];
dq.clear();
history_cnt=0;
while(fgets(buff,sizeof(buff),fp)){
buff[strlen(buff)-1] = '\0';
dq.push_back(strdup(buff));
history_cnt++;
}
fclose(fp);
int fl=0;
if(dq.size()>MAX_COMMANDS) fl=1;
while(dq.size()>MAX_COMMANDS){
if(dq[0]!=NULL && dq[0][0]!=EOF)free(dq[0]);
dq.pop_front();
history_cnt--;
}
history_idx = history_cnt;
}
}
shell_history::~shell_history(){
FILE *fp = fopen(history_file,"w");
for(auto &it:dq){
if(it!=NULL && strlen(it)>0 && it[0]!=EOF){
fprintf(fp,"%s\n",it);
}
}
fclose(fp);
for(int i=0;i<history_cnt;i++){
if(dq[i]!=NULL && strlen(dq[i])>0 && dq[i][0]!=EOF)free(dq[i]);
}
}
void shell_history::manage_history(){
if(line==NULL || strlen(line)==0)return;
if(history_cnt==MAX_COMMANDS){
if(dq[0]!=NULL && dq[0][0]!=EOF)free(dq[0]);
dq.pop_front();
history_cnt--;
}
if(dq.size()>0 && strcmp(dq[dq.size()-1],line)==0){
history_idx = history_cnt;
return;
}
dq.push_back(line);
history_cnt++;
history_idx = history_cnt;
return;
}
void shell_history :: get_history(){
char prompt[1024]="",temp[1024];
if(getcwd(temp , 1024) == NULL){
perror("getcwd");
exit(0);
}
strcat(prompt,temp);
strcat(prompt,"$ ");
line = readline(prompt);
return;
}