From b55c263bb820f2667cab744d4e5a5be945d67d02 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Thu, 28 Jan 2016 18:10:14 +0100 Subject: [PATCH] expanding implemented --- Clean System Files/readLine.c | 90 +++++++++++++++++++++++++++++++++++ README.md | 1 + ReadLine.dcl | 4 +- ReadLine.icl | 47 +++++++++++++++++- test.icl | 9 +++- 5 files changed, 145 insertions(+), 6 deletions(-) diff --git a/Clean System Files/readLine.c b/Clean System Files/readLine.c index 814541c..eb5c76a 100644 --- a/Clean System Files/readLine.c +++ b/Clean System Files/readLine.c @@ -7,6 +7,8 @@ #include "Clean.h" static char *cs_answer = (char *)NULL; +static char **cs_tokens = (char **)NULL; +static int num_tokens = 0; HISTORY_STATE *history_state = NULL; //Helper functions @@ -337,3 +339,91 @@ int cleanHistoryTruncateFile(CleanString path, int nlines) free(cs_path); return errno == 0; } + +//History Expansion +void cleanHistoryExpand(CleanString string, CleanString *output, int *ret) +{ + char *cs_output; + char *cs_string = cleanStringToCString(string); + *ret = history_expand(cs_string, &cs_output); + + CleanStringVariable(cleanOutput, strlen(cs_output)); + *output = (CleanString) cleanOutput; + memcpy(CleanStringCharacters(cleanOutput), cs_output, strlen(cs_output)); + CleanStringLength(cleanOutput) = strlen(cs_output); + free(cs_output); + free(cs_string); +} + +void cleanGetHistoryEvent( + CleanString string, int qchar, int cindex, CleanString *output, + int *retcindex, int *null) +{ + char *cs_string = cleanStringToCString(string); + char *cs_output = get_history_event(cs_string, &cindex, qchar); + + if(cs_output == NULL){ + *null = 1; + cs_output = ""; + } else { + *null = 0; + } + + CleanStringVariable(cleanOutput, strlen(cs_output)); + *output = (CleanString) cleanOutput; + memcpy(CleanStringCharacters(cleanOutput), cs_output, strlen(cs_output)); + CleanStringLength(cleanOutput) = strlen(cs_output); + + if(*null == 0){ + free(cs_output); + } + *retcindex = cindex; +} + +int cleanHistoryTokenizeInit(CleanString string) +{ + if(cs_tokens){ + for(int i = 0; i (!Bool, !*env) //History Expansion historyExpand :: !String !*env -> (!String, !Int, !*env) -getHistoryEvent :: !String !Int !Int !*env -> (!String, !*env) +getHistoryEvent :: !String !Int !Int !*env -> (!Maybe String, !Int, !*env) historyTokenize :: !String !*env -> ([String], !*env) -historyArgExtract :: !Int !Int !String !*env -> (!String, !*env) +historyArgExtract :: !Int !Int !String !*env -> (!Maybe String, !*env) diff --git a/ReadLine.icl b/ReadLine.icl index 71d64df..fa23473 100644 --- a/ReadLine.icl +++ b/ReadLine.icl @@ -248,6 +248,49 @@ historyTruncateFile i s e = code { } historyExpand :: !String !*env -> (!String, !Int, !*env) -getHistoryEvent :: !String !Int !Int !*env -> (!String, !*env) +historyExpand s e = code { + ccall cleanHistoryExpand "S:VSI:A" + } + +getHistoryEvent :: !String !Int !Int !*env -> (!Maybe String, !Int, !*env) +getHistoryEvent s i1 i2 e +# (string, cindex, null, e) = getHistoryEventItem s i1 i2 e += (if null Nothing (Just string), cindex, e) + where + getHistoryEventItem :: !String !Int !Int !*env -> (!String, !Int, !Bool, !*env) + getHistoryEventItem s i1 i2 e = code { + ccall cleanGetHistoryEvent "SII:VSII:A" + } + historyTokenize :: !String !*env -> ([String], !*env) -historyArgExtract :: !Int !Int !String !*env -> (!String, !*env) +historyTokenize s e +# (i, e) = historyTokenizeInit s e += historyTokenizeItems i e +//= ([], e) + where + historyTokenizeInit :: !String !*env -> (!Int, !*env) + historyTokenizeInit s e = code { + ccall cleanHistoryTokenizeInit "S:I:A" + } + + historyTokenizeItems :: !Int !*env -> (![String], !*env) + historyTokenizeItems 0 e = ([], e) + historyTokenizeItems i e + # (token, e) = historyTokenizeItem (i-1) e + # (rest, e) = historyTokenizeItems (i-1) e + = ([token:rest], e) + + historyTokenizeItem :: !Int !*env -> (!String, !*env) + historyTokenizeItem i e = code { + ccall cleanHistoryTokenizeItem "I:S:A" + } + +historyArgExtract :: !Int !Int !String !*env -> (!Maybe String, !*env) +historyArgExtract i1 i2 s e +# (string, null, e) = historyArgExtractItem i1 i2 s e += (if null Nothing (Just string), e) + where + historyArgExtractItem :: !Int !Int !String !*env -> (!String, !Bool, !*env) + historyArgExtractItem i1 i2 s e = code { + ccall cleanHistoryArgExtract "IIS:VSI:A" + } diff --git a/test.icl b/test.icl index 66a4996..5ed1daa 100644 --- a/test.icl +++ b/test.icl @@ -8,7 +8,7 @@ testHistory = {HistoryState | entries=[{HistoryItem | line="custom", timestamp=""}], offset=1, flags=0} -Start :: *World -> (String, *World) +Start :: *World -> (String, [String], *World) Start w #! w = setReadLineName "Test program" w #! w = usingHistory w @@ -35,8 +35,13 @@ Start w #! (i, w) = historySetPos 1 w #! (h, w) = previousHistory w #! (h, w) = nextHistory w +#! (s, i, w) = historyExpand "hoi" w +#! (s, i, w) = getHistoryEvent "hoi" 1 0 w +#! (s, w) = historyArgExtract 1 2 "hoi1 hoi2 hoi3" w +#! (s, w) = historyArgExtract 98 99 "hoi1 hoi2 hoi3" w +#! (ss, w) = historyTokenize "hoi1 hoi2 hoi3" w #! (oh, w) = historyGetHistoryState w #! w = historySetHistoryState testHistory w #! (h, w) = historyGetHistoryState w #! w = clearHistory w -= (toString h, w) += (toString h, ss, w) -- 2.20.1