static char *cs_answer = (char *)NULL;
-void cleanReadLine(CleanString prompt, int history, CleanString *result)
-{
- //Initialize
- unsigned long promptlen = CleanStringLength(prompt);
- char *cs_prompt = (char *)malloc(promptlen+1);
- if(cs_prompt == NULL){
+char *cleanStringToCString(CleanString s){
+ unsigned long len = CleanStringLength(s);
+ char *cs = (char *)malloc(len+1);
+ if(cs == NULL){
printf("malloc failed...\n");
exit(1);
}
- memcpy(cs_prompt, CleanStringCharacters(prompt), promptlen);
- cs_prompt[promptlen] = '\0';
+ memcpy(cs, CleanStringCharacters(s), len);
+ cs[len] = '\0';
+ return cs;
+}
+
+int cleanReadHistory(CleanString path)
+{
+ char *cs_path = cleanStringToCString(path);
+ int errno = read_history(cs_path);
+ free(cs_path);
+ return errno;
+}
+
+int cleanReadHistoryRange(CleanString path, int from, int to)
+{
+ char *cs_path = cleanStringToCString(path);
+ int errno = read_history_range(cs_path, from, to);
+ free(cs_path);
+ return errno;
+}
+
+int cleanWriteHistory(CleanString path)
+{
+ char *cs_path = cleanStringToCString(path);
+ int errno = write_history(cs_path);
+ free(cs_path);
+ return errno;
+}
+
+int cleanAppendHistory(int n, CleanString path)
+{
+ char *cs_path = cleanStringToCString(path);
+ int errno = append_history(n, cs_path);
+ free(cs_path);
+ return errno;
+}
+
+int cleanHistoryTruncateFile(CleanString path, int nlines)
+{
+ char *cs_path = cleanStringToCString(path);
+ int errno = history_truncate_file(cs_path, nlines);
+ free(cs_path);
+ return errno;
+}
+
+void cleanReadLine(CleanString prompt, int history, CleanString *result)
+{
+ char *cs_prompt = cleanStringToCString(prompt);
//Get the answer and add to history if not empty
if(cs_answer){
add_history(cs_answer);
}
- //Transfor answer into cstring
- CleanStringVariable(answer, strlen(cs_answer));
- *result = (CleanString) answer;
- memcpy(CleanStringCharacters(answer), cs_answer, strlen(cs_answer));
- CleanStringLength(answer) = strlen(cs_answer);
+ if(!cs_answer){
+ //Transfor answer into cstring
+ CleanStringVariable(answer, 1);
+ *result = (CleanString) answer;
+ memcpy(CleanStringCharacters(answer), "", 1);
+ CleanStringLength(answer) = 1;
+ } else {
+ //Transfor answer into cstring
+ CleanStringVariable(answer, strlen(cs_answer));
+ *result = (CleanString) answer;
+ memcpy(CleanStringCharacters(answer), cs_answer, strlen(cs_answer));
+ CleanStringLength(answer) = strlen(cs_answer);
+ }
}
definition module ReadLine
-/*
- Reads a line from stdin with the readline library
- The value in the String argument will be used as a prompt
- To enable history the Boolean variable has to be set to True
-*/
+//Readline
readLine :: !String !Bool !*env -> (!String, !*env)
+
+//Initializing History and State Management
+
+//History List Management
+
+//Information About the History List
+
+//Moving Around the History List
+
+//Searching the History List
+
+//Managing the History File
+readHistory :: !String !*env -> (!Bool, !*env)
+readHistoryRange :: !String !Int !Int !*env -> (!Bool, !*env)
+writeHistory :: !String !*env -> (!Bool, !*env)
+appendHistory :: !Int !String !*env -> (!Bool, !*env)
+historyTruncateFile :: !String !Int !*env -> (!Bool, !*env)
ccall cleanReadLine "SI:S:A"
}
+readHistory :: !String !*env -> (!Bool, !*env)
+readHistory s e = code {
+ ccall cleanReadHistory "S:I:A"
+ }
+
+readHistoryRange :: !String !Int !Int !*env -> (!Bool, !*env)
+readHistoryRange s i1 i2 e = code {
+ ccall cleanReadHistoryRange "SII:I:A"
+ }
+
+writeHistory :: !String !*env -> (!Bool, !*env)
+writeHistory s e = code {
+ ccall cleanWriteHistory "S:I:A"
+ }
+
+appendHistory :: !Int !String !*env -> (!Bool, !*env)
+appendHistory i s e = code {
+ ccall cleanWriteHistory "IS:I:A"
+ }
+
+historyTruncateFile :: !String !Int !*env -> (!Bool, !*env)
+historyTruncateFile i s e = code {
+ ccall cleanWriteHistory "SI:I:A"
+ }