update
[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
11 char *cleanStringToCString(CleanString s){
12 unsigned long len = CleanStringLength(s);
13 char *cs = (char *)malloc(len+1);
14 if(cs == NULL){
15 printf("malloc failed...\n");
16 exit(1);
17 }
18 memcpy(cs, CleanStringCharacters(s), len);
19 cs[len] = '\0';
20 return cs;
21 }
22
23 void cleanReadLine(CleanString prompt, int history, CleanString *result)
24 {
25 char *cs_prompt = cleanStringToCString(prompt);
26
27 //Get the answer and add to history if not empty
28 if(cs_answer){
29 free(cs_answer);
30 cs_answer = (char *)NULL;
31 }
32 cs_answer = readline(cs_prompt);
33 free(cs_prompt);
34 if(cs_answer && *cs_answer && history){
35 add_history(cs_answer);
36 }
37
38 if(!cs_answer){ //In case of an EOF
39 CleanStringVariable(answer, 1);
40 *result = (CleanString) answer;
41 memcpy(CleanStringCharacters(answer), "", 1);
42 CleanStringLength(answer) = 1;
43 } else { //In case of a proper response
44 CleanStringVariable(answer, strlen(cs_answer));
45 *result = (CleanString) answer;
46 memcpy(CleanStringCharacters(answer), cs_answer, strlen(cs_answer));
47 CleanStringLength(answer) = strlen(cs_answer);
48 }
49 }
50
51 void cleanSetReadLineName(CleanString name){
52 rl_readline_name = cleanStringToCString(name);
53 }
54
55 void cleanUsingHistory(){
56 using_history();
57 }
58
59 void cleanAddHistory(CleanString entry){
60 char *cs_entry = cleanStringToCString(entry);
61 add_history(cs_entry);
62 free(cs_entry);
63 }
64
65 void cleanClearHistory(){
66 clear_history();
67 }
68
69 int cleanHistorySearch(CleanString s, int dir){
70 char *cs_s = cleanStringToCString(s);
71 int ret = history_search(cs_s, dir);
72 free(cs_s);
73 return ret;
74 }
75
76 int cleanHistorySearchPrefix(CleanString s, int dir){
77 char *cs_s = cleanStringToCString(s);
78 int ret = history_search_prefix(cs_s, dir);
79 free(cs_s);
80 return ret;
81 }
82
83 int cleanHistorySearchPos(CleanString s, int dir, int pos){
84 char *cs_s = cleanStringToCString(s);
85 int ret = history_search_pos(cs_s, dir, pos);
86 free(cs_s);
87 return ret;
88 }
89
90 int cleanReadHistory(CleanString path)
91 {
92 char *cs_path = cleanStringToCString(path);
93 int errno = read_history(cs_path);
94 free(cs_path);
95 return errno;
96 }
97
98 int cleanReadHistoryRange(CleanString path, int from, int to)
99 {
100 char *cs_path = cleanStringToCString(path);
101 int errno = read_history_range(cs_path, from, to);
102 free(cs_path);
103 return errno;
104 }
105
106 int cleanWriteHistory(CleanString path)
107 {
108 char *cs_path = cleanStringToCString(path);
109 int errno = write_history(cs_path);
110 free(cs_path);
111 return errno;
112 }
113
114 int cleanAppendHistory(int n, CleanString path)
115 {
116 char *cs_path = cleanStringToCString(path);
117 int errno = append_history(n, cs_path);
118 free(cs_path);
119 return errno;
120 }
121
122 int cleanHistoryTruncateFile(CleanString path, int nlines)
123 {
124 char *cs_path = cleanStringToCString(path);
125 int errno = history_truncate_file(cs_path, nlines);
126 free(cs_path);
127 return errno;
128 }
129