update
authorMart Lubbers <mart@martlubbers.net>
Mon, 25 Jan 2016 12:23:04 +0000 (13:23 +0100)
committerMart Lubbers <mart@martlubbers.net>
Mon, 25 Jan 2016 12:23:04 +0000 (13:23 +0100)
Clean System Files/readLine.c
Makefile
README.md
ReadLine.dcl
ReadLine.icl
test.icl

index b645039..b72d31a 100644 (file)
@@ -20,6 +20,73 @@ char *cleanStringToCString(CleanString s){
        return cs;
 }
 
+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){
+               free(cs_answer);
+               cs_answer = (char *)NULL;
+       }
+       cs_answer = readline(cs_prompt);
+       free(cs_prompt);
+       if(cs_answer && *cs_answer && history){
+               add_history(cs_answer);
+       }
+
+       if(!cs_answer){ //In case of an EOF
+               CleanStringVariable(answer, 1);
+               *result = (CleanString) answer;
+               memcpy(CleanStringCharacters(answer), "", 1);
+               CleanStringLength(answer) = 1;
+       } else { //In case of a proper response
+               CleanStringVariable(answer, strlen(cs_answer));
+               *result = (CleanString) answer;
+               memcpy(CleanStringCharacters(answer), cs_answer, strlen(cs_answer));
+               CleanStringLength(answer) = strlen(cs_answer);
+       }
+}
+
+void cleanSetReadLineName(CleanString name){
+       rl_readline_name = cleanStringToCString(name);
+}
+
+void cleanUsingHistory(){
+       using_history();
+}
+
+void cleanAddHistory(CleanString entry){
+       char *cs_entry = cleanStringToCString(entry);
+       add_history(cs_entry);
+       free(cs_entry);
+}
+
+void cleanClearHistory(){
+       clear_history();
+}
+
+int cleanHistorySearch(CleanString s, int dir){
+       char *cs_s = cleanStringToCString(s);
+       int ret = history_search(cs_s, dir);
+       free(cs_s);
+       return ret;
+}
+
+int cleanHistorySearchPrefix(CleanString s, int dir){
+       char *cs_s = cleanStringToCString(s);
+       int ret = history_search_prefix(cs_s, dir);
+       free(cs_s);
+       return ret;
+}
+
+int cleanHistorySearchPos(CleanString s, int dir, int pos){
+       char *cs_s = cleanStringToCString(s);
+       int ret = history_search_pos(cs_s, dir, pos);
+       free(cs_s);
+       return ret;
+}
+
 int cleanReadHistory(CleanString path)
 {
        char *cs_path = cleanStringToCString(path);
@@ -60,30 +127,3 @@ int cleanHistoryTruncateFile(CleanString path, int nlines)
        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){
-               free(cs_answer);
-               cs_answer = (char *)NULL;
-       }
-       cs_answer = readline(cs_prompt);
-       free(cs_prompt);
-       if(cs_answer && *cs_answer && history){
-               add_history(cs_answer);
-       }
-
-       if(!cs_answer){ //In case of an EOF
-               CleanStringVariable(answer, 1);
-               *result = (CleanString) answer;
-               memcpy(CleanStringCharacters(answer), "", 1);
-               CleanStringLength(answer) = 1;
-       } else { //In case of a proper response
-               CleanStringVariable(answer, strlen(cs_answer));
-               *result = (CleanString) answer;
-               memcpy(CleanStringCharacters(answer), cs_answer, strlen(cs_answer));
-               CleanStringLength(answer) = strlen(cs_answer);
-       }
-}
index 6dd1a70..ae01361 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -8,4 +8,4 @@ all: test
        $(CLM) $(CLMFLAGS) $(basename $<) -o $@
 
 clean:
-       $(RM) -v test Clean\ System\ Files/test.*
+       $(RM) -v test Clean\ System\ Files/{ReadLine,test}.*
index b2a9865..391e314 100644 (file)
--- a/README.md
+++ b/README.md
@@ -19,6 +19,11 @@ make
 When you want to use the library in your program you should compile with the
 `-l /usr/lib/libreadline.so` linker flag
 
+All functions try to resemble the original
+[readline](http://cnswww.cns.cwru.edu/php/chet/readline/readline.html) and 
+[history](http://cnswww.cns.cwru.edu/php/chet/readline/history.html) api
+functions as close as possible
+
 ###Changelog
 
 - 0.2 (2016-01-25)
@@ -30,11 +35,11 @@ When you want to use the library in your program you should compile with the
 
 ###Todo
 
+- Access to the history datastructure
+- Access to the current location
+- Complete history api implementation
 - Embed the readline library in the object files in such a way that no special
   compiler flag is needed.
-- Control over history, right now entries are added automatically to the
-  history when the boolean flag is on. It would be nice to be able to add
-  custom entries.
 - Control over tabcompletion, right now it completes on filenames.
 - Check possibilities for Windows/Mac
 
index 540886a..ecc58bf 100644 (file)
@@ -2,16 +2,31 @@ definition module ReadLine
 
 //Readline
 readLine :: !String !Bool !*env -> (!String, !*env)
+setReadLineName :: !String !*env -> !*env
 
 //Initializing History and State Management
+//Note that this HAS to be executed when you want to add entries when the
+//history has not been used
+usingHistory :: !*env -> !*env
 
 //History List Management
+addHistory :: !String !*env -> !*env
+clearHistory :: !*env -> !*env
+//TODO some more functions
 
 //Information About the History List
+//TODO
 
 //Moving Around the History List
+//TODO
 
 //Searching the History List
+//Note that the return integers are just success flags. The actual found item
+//will be the current history position and the int is the offset of the search
+//within that item...
+historySearch :: !String !Int !*env-> (!Int, !*env)
+historySearchPrefix :: !String !Int !*env-> (!Int, !*env)
+historySearchPos :: !String !Int !Int !*env-> (!Int, !*env)
 
 //Managing the History File
 readHistory :: !String !*env -> (!Bool, !*env)
index 8807f4f..4bb84d1 100644 (file)
@@ -4,11 +4,56 @@ import StdEnv
 
 import code from "readLine.o"
 
+//Readline
 readLine :: !String !Bool !*env -> (!String, !*env)
 readLine s h e = code {
                        ccall cleanReadLine "SI:S:A"
        }
 
+setReadLineName :: !String !*env -> !*env
+setReadLineName s e = code inline {
+                       ccall cleanSetReadLineName "S-"
+       }
+
+//Initializing History and State Management
+usingHistory :: !*env -> !*env
+usingHistory e = code inline {
+                       ccall cleanUsingHistory "-"
+       }
+
+//History List Management
+addHistory :: !String !*env -> !*env
+addHistory s e = code inline {
+                       ccall cleanAddHistory "S-"
+       }
+
+clearHistory :: !*env -> !*env
+clearHistory e = code inline{
+                       ccall cleanClearHistory "-"
+       }
+
+//Information About the History List
+
+//Moving Around the History List
+
+//Searching the History List
+historySearch :: !String !Int !*env-> (!Int, !*env)
+historySearch s i e = code {
+                       ccall cleanHistorySearch "SI:I:A"
+       }
+
+historySearchPrefix :: !String !Int !*env-> (!Int, !*env)
+historySearchPrefix s i e = code {
+                       ccall cleanHistorySearchPrefix "SI:I:A"
+       }
+       
+historySearchPos :: !String !Int !Int !*env-> (!Int, !*env)
+historySearchPos s i1 i2 e = code {
+                       ccall cleanHistorySearchPos "SI:I:A"
+       }
+       
+
+//Managing the History File
 readHistory :: !String !*env -> (!Bool, !*env)
 readHistory s e = code {
                        ccall cleanReadHistory "S:I:A"
index 26ced3c..729c126 100644 (file)
--- a/test.icl
+++ b/test.icl
@@ -3,11 +3,16 @@ module test
 import StdEnv
 import ReadLine
 
-Start :: *World -> (String, *World)
+Start :: *World -> (Int, String, *World)
 Start w
 # (f, w) = stdio w
+#! w = setReadLineName "Test program" w
+#! w = usingHistory w
 #! (_, w) = readHistory "readline.history" w
+#! w = addHistory "testentry" w
 #! (s, w) = readLine "first prompt: " True w
 #! (s, w) = readLine "uparrow should word with history: " False w
 #! (_, w) = writeHistory "readline.history" w
-= (s, w)
+#! (i, w) = historySearch "testentry" -1 w
+//#! w = clearHistory w
+= (i, s, w)