File src/hstr_history.c changed (mode: 100644) (index 1d57aa3..c5b9d02) |
11 |
11 |
#include "include/hashset.h" |
#include "include/hashset.h" |
12 |
12 |
#include "include/hashmap.h" |
#include "include/hashmap.h" |
13 |
13 |
#include "include/radixsort.h" |
#include "include/radixsort.h" |
|
14 |
|
#include <stdbool.h> |
|
15 |
|
|
|
16 |
|
#include "include/hstr_utils.h" |
14 |
17 |
|
|
15 |
18 |
#define NDEBUG |
#define NDEBUG |
16 |
19 |
#include <assert.h> |
#include <assert.h> |
|
... |
... |
typedef struct { |
22 |
25 |
|
|
23 |
26 |
static HistoryItems *history; |
static HistoryItems *history; |
24 |
27 |
static HistoryItems *prioritizedHistory; |
static HistoryItems *prioritizedHistory; |
|
28 |
|
static bool dirty; |
25 |
29 |
|
|
26 |
30 |
unsigned history_ranking_function(unsigned rank, unsigned newOccurenceOrder, unsigned lng) { |
unsigned history_ranking_function(unsigned rank, unsigned newOccurenceOrder, unsigned lng) { |
27 |
31 |
rank+=newOccurenceOrder/10 + lng; |
rank+=newOccurenceOrder/10 + lng; |
28 |
32 |
return rank; |
return rank; |
29 |
33 |
} |
} |
30 |
34 |
|
|
31 |
|
char *get_history_file() { |
|
32 |
|
char *home = getenv(ENV_VAR_HOME); |
|
33 |
|
char *fileName = (char*) malloc( |
|
34 |
|
strlen(home) + 1 + strlen(FILE_HISTORY) + 1); |
|
35 |
|
strcat(strcat(strcpy(fileName, home), "/"), FILE_HISTORY); |
|
36 |
|
return fileName; |
|
|
35 |
|
char *get_history_file_name() { |
|
36 |
|
char *historyFile=getenv(ENV_VAR_HISTFILE); |
|
37 |
|
if(!historyFile || strlen(historyFile)==0) { |
|
38 |
|
char *home = getenv(ENV_VAR_HOME); |
|
39 |
|
historyFile = (char*) malloc(strlen(home) + 1 + strlen(FILE_HISTORY) + 1); |
|
40 |
|
strcat(strcat(strcpy(historyFile, home), "/"), FILE_HISTORY); |
|
41 |
|
} |
|
42 |
|
return historyFile; |
37 |
43 |
} |
} |
38 |
44 |
|
|
39 |
45 |
void dump_prioritized_history(HistoryItems *ph) { |
void dump_prioritized_history(HistoryItems *ph) { |
|
... |
... |
HistoryItems *get_history_items() { |
120 |
126 |
|
|
121 |
127 |
using_history(); |
using_history(); |
122 |
128 |
|
|
123 |
|
char *historyFile = getenv(ENV_VAR_HISTFILE); |
|
124 |
|
if(!historyFile || strlen(historyFile)==0) { |
|
125 |
|
historyFile=get_history_file(); |
|
126 |
|
} |
|
127 |
|
|
|
|
129 |
|
char *historyFile = get_history_file_name(); |
128 |
130 |
if(read_history(historyFile)!=0) { |
if(read_history(historyFile)!=0) { |
129 |
131 |
fprintf(stderr, "\nUnable to read history file from '%s'!\n",historyFile); |
fprintf(stderr, "\nUnable to read history file from '%s'!\n",historyFile); |
130 |
132 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
|
... |
... |
HistoryItems *get_prioritized_history() { |
167 |
169 |
return prioritize_history(fileItems); |
return prioritize_history(fileItems); |
168 |
170 |
} |
} |
169 |
171 |
|
|
|
172 |
|
void history_mgmt_open() { |
|
173 |
|
dirty=false; |
|
174 |
|
} |
|
175 |
|
|
|
176 |
|
void history_mgmt_remove(char *cmd) { |
|
177 |
|
get_history_items(); |
|
178 |
|
|
|
179 |
|
int offset=history_search_pos(cmd, 0, 0); |
|
180 |
|
while(offset>=0) { |
|
181 |
|
free_history_entry(remove_history(offset)); |
|
182 |
|
offset=history_search_pos(cmd, 0, ++offset); |
|
183 |
|
} |
170 |
184 |
|
|
|
185 |
|
write_history(get_history_file_name()); |
|
186 |
|
dirty=true; |
|
187 |
|
} |
|
188 |
|
|
|
189 |
|
void history_mgmt_close() { |
|
190 |
|
if(dirty) { |
|
191 |
|
fill_terminal_input("history -r\n"); |
|
192 |
|
} |
|
193 |
|
} |