b645039873a3716565f1e7d1723d32fd63ea8301
4 #include <readline/readline.h>
5 #include <readline/history.h>
9 static char *cs_answer
= (char *)NULL
;
11 char *cleanStringToCString(CleanString s
){
12 unsigned long len
= CleanStringLength(s
);
13 char *cs
= (char *)malloc(len
+1);
15 printf("malloc failed...\n");
18 memcpy(cs
, CleanStringCharacters(s
), len
);
23 int cleanReadHistory(CleanString path
)
25 char *cs_path
= cleanStringToCString(path
);
26 int errno
= read_history(cs_path
);
31 int cleanReadHistoryRange(CleanString path
, int from
, int to
)
33 char *cs_path
= cleanStringToCString(path
);
34 int errno
= read_history_range(cs_path
, from
, to
);
39 int cleanWriteHistory(CleanString path
)
41 char *cs_path
= cleanStringToCString(path
);
42 int errno
= write_history(cs_path
);
47 int cleanAppendHistory(int n
, CleanString path
)
49 char *cs_path
= cleanStringToCString(path
);
50 int errno
= append_history(n
, cs_path
);
55 int cleanHistoryTruncateFile(CleanString path
, int nlines
)
57 char *cs_path
= cleanStringToCString(path
);
58 int errno
= history_truncate_file(cs_path
, nlines
);
63 void cleanReadLine(CleanString prompt
, int history
, CleanString
*result
)
65 char *cs_prompt
= cleanStringToCString(prompt
);
67 //Get the answer and add to history if not empty
70 cs_answer
= (char *)NULL
;
72 cs_answer
= readline(cs_prompt
);
74 if(cs_answer
&& *cs_answer
&& history
){
75 add_history(cs_answer
);
78 if(!cs_answer
){ //In case of an EOF
79 CleanStringVariable(answer
, 1);
80 *result
= (CleanString
) answer
;
81 memcpy(CleanStringCharacters(answer
), "", 1);
82 CleanStringLength(answer
) = 1;
83 } else { //In case of a proper response
84 CleanStringVariable(answer
, strlen(cs_answer
));
85 *result
= (CleanString
) answer
;
86 memcpy(CleanStringCharacters(answer
), cs_answer
, strlen(cs_answer
));
87 CleanStringLength(answer
) = strlen(cs_answer
);