setting history now works
[CleanReadLine.git] / Clean System Files / readLine.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include <readline/readline.h>
5 #include <readline/history.h>
6
7 #include "Clean.h"
8
9 static char *cs_answer = (char *)NULL;
10 HISTORY_STATE *history_state = NULL;
11
12 char *cleanStringToCString(CleanString s){
13 unsigned long len = CleanStringLength(s);
14 char *cs = (char *)malloc(len+1);
15 if(cs == NULL){
16 printf("malloc failed...\n");
17 exit(1);
18 }
19 memcpy(cs, CleanStringCharacters(s), len);
20 cs[len] = '\0';
21 return cs;
22 }
23
24 void cleanReadLine(CleanString prompt, int history, CleanString *result)
25 {
26 char *cs_prompt = cleanStringToCString(prompt);
27
28 //Get the answer and add to history if not empty
29 if(cs_answer){
30 free(cs_answer);
31 cs_answer = (char *)NULL;
32 }
33 cs_answer = readline(cs_prompt);
34 free(cs_prompt);
35 if(cs_answer && *cs_answer && history){
36 add_history(cs_answer);
37 }
38
39 if(!cs_answer){ //In case of an EOF
40 CleanStringVariable(answer, 1);
41 *result = (CleanString) answer;
42 memcpy(CleanStringCharacters(answer), "", 1);
43 CleanStringLength(answer) = 1;
44 } else { //In case of a proper response
45 CleanStringVariable(answer, strlen(cs_answer));
46 *result = (CleanString) answer;
47 memcpy(CleanStringCharacters(answer), cs_answer, strlen(cs_answer));
48 CleanStringLength(answer) = strlen(cs_answer);
49 }
50 }
51
52 void cleanSetReadLineName(CleanString name){
53 rl_readline_name = cleanStringToCString(name);
54 }
55
56 void cleanUsingHistory(){
57 using_history();
58 }
59
60 void cleanGetState(int *offset, int *num, int *flags){
61 *offset = history_get_history_state()->offset;
62 *num = history_get_history_state()->length;
63 *flags = history_get_history_state()->flags;
64 }
65
66 void cleanGetHistoryItem(int num, CleanString *line, CleanString *timestamp){
67 char *cs_line = history_get_history_state()->entries[num]->line;
68 char *cs_stamp = history_get_history_state()->entries[num]->timestamp;
69
70 CleanStringVariable(cleanLine, strlen(cs_line));
71 *line = (CleanString) cleanLine;
72 memcpy(CleanStringCharacters(cleanLine), cs_line, strlen(cs_line));
73 CleanStringLength(cleanLine) = strlen(cs_line);
74
75 CleanStringVariable(cleanTimestamp, strlen(cs_stamp));
76 *timestamp = (CleanString) cleanTimestamp;
77 memcpy(CleanStringCharacters(cleanTimestamp), cs_stamp, strlen(cs_stamp));
78 CleanStringLength(cleanTimestamp) = strlen(cs_stamp);
79 }
80
81 void cleanInitNewHistoryState(int offset, int flags, int num){
82 printf("Initializing: %d, %d, %d\n", offset, flags, num);
83 if(history_state != NULL){
84 free(history_state);
85 }
86 history_state = (HISTORY_STATE *)malloc(sizeof(HISTORY_STATE));
87 if(history_state == NULL){
88 printf("Not enough memory...\n");
89 exit(1);
90 }
91 HIST_ENTRY **entries = (HIST_ENTRY **)malloc(sizeof(HIST_ENTRY*)*num);
92 if(entries == NULL){
93 printf("Not enough memory...\n");
94 exit(1);
95 }
96 for(int i = 0; i<num; i++){
97 entries[i] = (HIST_ENTRY *)malloc(sizeof(HIST_ENTRY));
98 }
99 history_state->offset = offset;
100 history_state->entries = entries;
101 history_state->length = num;
102 history_state->size = num;
103 history_state->flags = 0;
104 printf("Initialization done\n");
105 }
106
107 void cleanSetNewHistoryEntry(int i, CleanString line, CleanString timestamp){
108 char *cs_line = cleanStringToCString(line);
109 char *cs_timestamp = cleanStringToCString(timestamp);
110 printf("adding: %d, %s, %s\n", i, cs_line, cs_timestamp);
111 history_state->entries[i]->line = cs_line;
112 history_state->entries[i]->timestamp = cs_timestamp;
113 printf("done adding: %d, %s, %s\n", i, cs_line, cs_timestamp);
114 }
115
116 void cleanCommitSetHistory(void){
117 printf("commit\n");
118 history_set_history_state(history_state);
119 printf("committed\n");
120 }
121
122 void cleanAddHistory(CleanString entry){
123 char *cs_entry = cleanStringToCString(entry);
124 add_history(cs_entry);
125 free(cs_entry);
126 }
127
128 void cleanClearHistory(){
129 clear_history();
130 }
131
132 int cleanHistorySearch(CleanString s, int dir){
133 char *cs_s = cleanStringToCString(s);
134 int ret = history_search(cs_s, dir);
135 free(cs_s);
136 return ret;
137 }
138
139 int cleanHistorySearchPrefix(CleanString s, int dir){
140 char *cs_s = cleanStringToCString(s);
141 int ret = history_search_prefix(cs_s, dir);
142 free(cs_s);
143 return ret;
144 }
145
146 int cleanHistorySearchPos(CleanString s, int dir, int pos){
147 char *cs_s = cleanStringToCString(s);
148 int ret = history_search_pos(cs_s, dir, pos);
149 free(cs_s);
150 return ret;
151 }
152
153 int cleanReadHistory(CleanString path)
154 {
155 char *cs_path = cleanStringToCString(path);
156 int errno = read_history(cs_path);
157 free(cs_path);
158 return errno;
159 }
160
161 int cleanReadHistoryRange(CleanString path, int from, int to)
162 {
163 char *cs_path = cleanStringToCString(path);
164 int errno = read_history_range(cs_path, from, to);
165 free(cs_path);
166 return errno;
167 }
168
169 int cleanWriteHistory(CleanString path)
170 {
171 char *cs_path = cleanStringToCString(path);
172 int errno = write_history(cs_path);
173 free(cs_path);
174 return errno;
175 }
176
177 int cleanAppendHistory(int n, CleanString path)
178 {
179 char *cs_path = cleanStringToCString(path);
180 int errno = append_history(n, cs_path);
181 free(cs_path);
182 return errno;
183 }
184
185 int cleanHistoryTruncateFile(CleanString path, int nlines)
186 {
187 char *cs_path = cleanStringToCString(path);
188 int errno = history_truncate_file(cs_path, nlines);
189 free(cs_path);
190 return errno;
191 }
192