fixed issue 1, changed function type of readline
[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 //Helper functions
13 char *cleanStringToCString(CleanString s)
14 {
15 unsigned long len = CleanStringLength(s);
16 char *cs = (char *)malloc(len+1);
17 if(cs == NULL){
18 printf("malloc failed...\n");
19 exit(1);
20 }
21 memcpy(cs, CleanStringCharacters(s), len);
22 cs[len] = '\0';
23 return cs;
24 }
25
26 void cleanSetReadLineName(CleanString name)
27 {
28 rl_readline_name = cleanStringToCString(name);
29 }
30
31 //Readline functions
32 void cleanReadLine(CleanString prompt, int history, CleanString *result, int *eof)
33 {
34 char *cs_prompt = cleanStringToCString(prompt);
35
36 //Get the answer and add to history if not empty
37 if(cs_answer){
38 free(cs_answer);
39 cs_answer = (char *)NULL;
40 }
41 cs_answer = readline(cs_prompt);
42 free(cs_prompt);
43 if(cs_answer && *cs_answer && history){
44 add_history(cs_answer);
45 }
46
47 *eof = 0;
48 if(!cs_answer){ //In case of an EOF
49 cs_answer = (char *)malloc(1);
50 cs_answer[0] = '\0';
51 *eof = 1;
52 }
53 CleanStringVariable(answer, strlen(cs_answer));
54 *result = (CleanString) answer;
55 memcpy(CleanStringCharacters(answer), cs_answer, strlen(cs_answer));
56 CleanStringLength(answer) = strlen(cs_answer);
57 }
58
59 //History Functions
60 //Initializing History and State Management
61 void cleanUsingHistory()
62 {
63 using_history();
64 }
65
66 void cleanGetState(int *offset, int *num, int *flags)
67 {
68 *offset = history_get_history_state()->offset;
69 *num = history_get_history_state()->length;
70 *flags = history_get_history_state()->flags;
71 }
72
73 void cleanGetHistoryItem(int num, CleanString *line, CleanString *timestamp)
74 {
75 char *cs_line = history_get_history_state()->entries[num]->line;
76 char *cs_stamp = history_get_history_state()->entries[num]->timestamp;
77
78 CleanStringVariable(cleanLine, strlen(cs_line));
79 *line = (CleanString) cleanLine;
80 memcpy(CleanStringCharacters(cleanLine), cs_line, strlen(cs_line));
81 CleanStringLength(cleanLine) = strlen(cs_line);
82
83 CleanStringVariable(cleanTimestamp, strlen(cs_stamp));
84 *timestamp = (CleanString) cleanTimestamp;
85 memcpy(CleanStringCharacters(cleanTimestamp), cs_stamp, strlen(cs_stamp));
86 CleanStringLength(cleanTimestamp) = strlen(cs_stamp);
87 }
88
89 void cleanInitNewHistoryState(int offset, int flags, int num)
90 {
91 if(history_state != NULL){
92 //we should test if we can recursively free the object
93 free(history_state);
94 }
95 history_state = (HISTORY_STATE *)malloc(sizeof(HISTORY_STATE));
96 if(history_state == NULL){
97 printf("Not enough memory...\n");
98 exit(1);
99 }
100 HIST_ENTRY **entries = (HIST_ENTRY **)malloc(sizeof(HIST_ENTRY*)*num);
101 if(entries == NULL){
102 printf("Not enough memory...\n");
103 exit(1);
104 }
105 for(int i = 0; i<num; i++){
106 entries[i] = (HIST_ENTRY *)malloc(sizeof(HIST_ENTRY));
107 }
108 history_state->offset = offset;
109 history_state->entries = entries;
110 history_state->length = num;
111 history_state->size = num;
112 history_state->flags = 0;
113 }
114
115 void cleanSetNewHistoryEntry(int i, CleanString line, CleanString timestamp)
116 {
117 char *cs_line = cleanStringToCString(line);
118 char *cs_timestamp = cleanStringToCString(timestamp);
119 history_state->entries[i]->line = cs_line;
120 history_state->entries[i]->timestamp = cs_timestamp;
121 }
122
123 void cleanCommitSetHistory(void)
124 {
125 history_set_history_state(history_state);
126 }
127
128 //History List Management
129 void cleanAddHistoryTime(CleanString timestamp)
130 {
131 char *cs_timestamp = cleanStringToCString(timestamp);
132 add_history_time(cs_timestamp);
133 free(cs_timestamp);
134 }
135
136 void cleanAddHistory(CleanString entry)
137 {
138 char *cs_entry = cleanStringToCString(entry);
139 add_history(cs_entry);
140 free(cs_entry);
141 }
142
143 void cleanRemoveHistory(int which, CleanString *line, CleanString *timestamp)
144 {
145 HIST_ENTRY *entry = remove_history(which);
146 char *cs_line = entry->line;
147 char *cs_stamp = entry->timestamp;
148
149 CleanStringVariable(cleanLine, strlen(cs_line));
150 *line = (CleanString) cleanLine;
151 memcpy(CleanStringCharacters(cleanLine), cs_line, strlen(cs_line));
152 CleanStringLength(cleanLine) = strlen(cs_line);
153
154 CleanStringVariable(cleanTimestamp, strlen(cs_stamp));
155 *timestamp = (CleanString) cleanTimestamp;
156 memcpy(CleanStringCharacters(cleanTimestamp), cs_stamp, strlen(cs_stamp));
157 CleanStringLength(cleanTimestamp) = strlen(cs_stamp);
158
159 histdata_t tofree = free_history_entry(entry);
160 free(tofree);
161 }
162
163 void cleanReplaceHistoryEntry(
164 int which, CleanString l, CleanString *line, CleanString *timestamp)
165 {
166 char *cs_l = cleanStringToCString(l);
167 HIST_ENTRY *entry = replace_history_entry(which, cs_l, NULL);
168 free(cs_l);
169 if(entry == NULL){
170 printf("invalid which\n");
171 }
172
173 char *cs_line = entry->line;
174 char *cs_stamp = entry->timestamp;
175
176 CleanStringVariable(cleanLine, strlen(cs_line));
177 *line = (CleanString) cleanLine;
178 memcpy(CleanStringCharacters(cleanLine), cs_line, strlen(cs_line));
179 CleanStringLength(cleanLine) = strlen(cs_line);
180
181 CleanStringVariable(cleanTimestamp, strlen(cs_stamp));
182 *timestamp = (CleanString) cleanTimestamp;
183 memcpy(CleanStringCharacters(cleanTimestamp), cs_stamp, strlen(cs_stamp));
184 CleanStringLength(cleanTimestamp) = strlen(cs_stamp);
185
186 histdata_t tofree = free_history_entry(entry);
187 free(tofree);
188 }
189
190
191 void cleanClearHistory()
192 {
193 clear_history();
194 }
195
196 void cleanStifleHistory(int max)
197 {
198 stifle_history(max);
199 }
200
201 int cleanUnstifleHistory()
202 {
203 return unstifle_history();
204 }
205
206 int cleanHistoryIsStifled()
207 {
208 return history_is_stifled();
209 }
210
211 //Information About the History List
212 int cleanHistoryTotalBytes()
213 {
214 return history_total_bytes();
215 }
216
217 //Moving Around the History List
218
219 //Searching the History List
220 int cleanHistorySearch(CleanString s, int dir)
221 {
222 char *cs_s = cleanStringToCString(s);
223 int ret = history_search(cs_s, dir);
224 free(cs_s);
225 return ret;
226 }
227
228 int cleanHistorySearchPrefix(CleanString s, int dir)
229 {
230 char *cs_s = cleanStringToCString(s);
231 int ret = history_search_prefix(cs_s, dir);
232 free(cs_s);
233 return ret;
234 }
235
236 int cleanHistorySearchPos(CleanString s, int dir, int pos)
237 {
238 char *cs_s = cleanStringToCString(s);
239 int ret = history_search_pos(cs_s, dir, pos);
240 free(cs_s);
241 return ret;
242 }
243
244 //Managing the History File
245 int cleanReadHistory(CleanString path)
246 {
247 char *cs_path = cleanStringToCString(path);
248 int errno = read_history(cs_path);
249 free(cs_path);
250 return errno;
251 }
252
253 int cleanReadHistoryRange(CleanString path, int from, int to)
254 {
255 char *cs_path = cleanStringToCString(path);
256 int errno = read_history_range(cs_path, from, to);
257 free(cs_path);
258 return errno;
259 }
260
261 int cleanWriteHistory(CleanString path)
262 {
263 char *cs_path = cleanStringToCString(path);
264 int errno = write_history(cs_path);
265 free(cs_path);
266 return errno;
267 }
268
269 int cleanAppendHistory(int n, CleanString path)
270 {
271 char *cs_path = cleanStringToCString(path);
272 int errno = append_history(n, cs_path);
273 free(cs_path);
274 return errno;
275 }
276
277 int cleanHistoryTruncateFile(CleanString path, int nlines)
278 {
279 char *cs_path = cleanStringToCString(path);
280 int errno = history_truncate_file(cs_path, nlines);
281 free(cs_path);
282 return errno;
283 }