-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.c
168 lines (125 loc) · 2.08 KB
/
history.c
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include "main.h"
#include "util.h"
#include "array.h"
#include "history.h"
#define HISTORY_MAX_COUNT 500
static char str_current_line[STR_LINE_SIZE];
static array_t *array_history;
static offset;
static char* get_path_history_file()
{
static char path[STR_PATH_SIZE];
if( path[0] == '\0' )
{
sprintf(path, "%s/." SHELL_NAME "_history", get_home_dir() );
}
return path;
}
int history_init()
{
array_history = array_new();
str_current_line[0] = '\0';
offset = -1;
return 0;
}
int history_load()
{
char line[STR_LINE_SIZE];
FILE *file;
file = fopen(get_path_history_file(), "rt");
if( file == NULL )
{
return -1;
}
while( fgets(line, STR_LINE_SIZE-1, file) != NULL )
{
int len;
len = strlen(line);
if( line > 0 )
{
line[len-1] = '\0';
}
history_add(line);
}
fclose(file);
return 0;
}
int history_save()
{
FILE *file;
int i;
file = fopen(get_path_history_file(), "wt");
if( file == NULL )
{
return -1;
}
for(i = 0; i < array_history->count; i++)
{
char *s;
s = (char *) array_get(array_history, i);
fputs(s, file);
fputc('\n', file);
}
fclose(file);
return 0;
}
void history_backup_current_line(const char *str_command)
{
strcpy(str_current_line, str_command);
}
void history_add(const char *str_command)
{
array_add(array_history, strdup(str_command));
if( array_history->count > HISTORY_MAX_COUNT )
{
array_del_item(array_history, 0, free);
}
offset = array_history->count;
}
int history_up()
{
if( offset <= 0 )
{
return 0;
}
offset--;
return 1;
}
int history_down()
{
if( offset >= array_history->count )
{
return 0;
}
offset++;
return 1;
}
char* history_get_select()
{
if( offset == array_history->count )
{
return str_current_line;
}
if( offset < 0 || offset > array_history->count )
{
return NULL;
}
return array_get(array_history, offset);
}
int history_quit()
{
array_destroy_item(array_history, free);
return 0;
}
//#define TEST_HISTORY
#ifdef TEST_HISTORY
int main()
{
return 0;
}
#endif