From 5764d02324862748aeb23ac6cab0cb97f72bc906 Mon Sep 17 00:00:00 2001 From: nishi Date: Sat, 27 Apr 2024 00:07:44 +0000 Subject: [PATCH] add readline git-svn-id: file:///raid/svn-main/nishi-serenade/trunk@57 0f02c867-ac3d-714e-8a88-971ba1f6efcf --- Serenade/main.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/Serenade/main.c b/Serenade/main.c index f3f8890..cd2e031 100644 --- a/Serenade/main.c +++ b/Serenade/main.c @@ -41,6 +41,11 @@ #include #include +#ifdef HAS_READLINE_SUPPORT +#include +#include +#endif + extern bool is_repl; int main(int argc, char** argv) { @@ -109,8 +114,44 @@ int main(int argc, char** argv) { char* str = malloc(1); str[0] = 0; int br = 0; +#ifdef HAS_READLINE_SUPPORT + char* line = NULL; +#endif while(1) { +#ifdef HAS_READLINE_SUPPORT + line = readline("> "); + if(line == NULL){ + free(line); + continue; + } + int i; + for(i = 0; line[i] != 0; i++){ + if(line[i] == '('){ + br++; + }else if(line[i] == ')'){ + br--; + } + } + if(strcmp(line, ":quit") == 0){ + free(line); + break; + } + char* tmp = str; + str = sn_strcat(tmp, line); + free(tmp); + if(br == 0){ + sn_eval(sn, str, strlen(str)); + add_history(str); + free(str); + str = malloc(1); + str[0] = 0; + } + free(line); +#else if(cbuf[0] == '\n') { + if(strcmp(str, ":quit") == 0){ + break; + } if(br == 0 && strlen(str) > 0) { sn_eval(sn, str, strlen(str)); free(str); @@ -137,6 +178,7 @@ int main(int argc, char** argv) { str = sn_strcat(tmp, cbuf); free(tmp); } +#endif } free(str); sn_interpreter_free(sn); -- 2.43.0