From 6be1d905dbc7a6e5f740e9ee304b2214e63a26a3 Mon Sep 17 00:00:00 2001 From: paladin-t Date: Tue, 26 Jan 2016 15:43:22 +0800 Subject: [PATCH] +added a SET_IMPORTING_DIRS statement to the shell; *fixed a memory corruption bug with importing directory setting. --- HISTORY | 2 ++ README.md | 2 ++ shell/main.c | 45 ++++++++++++++++++++++++++++++++------------- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/HISTORY b/HISTORY index 0249769..cd9a9cd 100755 --- a/HISTORY +++ b/HISTORY @@ -1,6 +1,8 @@ Jan. 26 2016 +Added a SET_IMPORTING_DIRS statement to the shell Added friendly error promption when memory overflow Added source file information to stepped handler +Fixed a memory corruption bug with importing directory setting Optimized cached list accessing Jan. 25 2016 diff --git a/README.md b/README.md index 3f0dfee..ebb98cc 100755 --- a/README.md +++ b/README.md @@ -127,6 +127,8 @@ Most of the fundamental topics are mentioned in the [MY-BASIC Quick Reference](M * [Use prototype-based class](https://github.com/paladin-t/my_basic/wiki/Use-prototype-based-class) * [Define a class in C](https://github.com/paladin-t/my_basic/wiki/Define-a-class-in-C) * [Meta methods](https://github.com/paladin-t/my_basic/wiki/Meta-methods) +* Standalone shell + * [Extra functions](https://github.com/paladin-t/my_basic/wiki/Extra-functions) * Integration * [Link with MY-BASIC](https://github.com/paladin-t/my_basic/wiki/Link-with-MY_BASIC) * [Write a debugger](https://github.com/paladin-t/my_basic/wiki/Write-a-debugger) diff --git a/shell/main.c b/shell/main.c index 5a14f6d..bd000ff 100755 --- a/shell/main.c +++ b/shell/main.c @@ -539,6 +539,18 @@ typedef struct _importing_dirs_t { static _importing_dirs_t* importing_dirs = 0; +static void _destroy_importing_directories(void) { + int i = 0; + + if(!importing_dirs) return; + + for(i = 0; i < importing_dirs->count; ++i) { + free(importing_dirs->dirs[i]); + } + free(importing_dirs->dirs); + free(importing_dirs); +} + static _importing_dirs_t* _set_importing_directories(char* dirs) { if(dirs) { char* end = dirs + strlen(dirs); @@ -560,7 +572,7 @@ static _importing_dirs_t* _set_importing_directories(char* dirs) { } l = (int)strlen(dirs); as = dirs[l - 1] != '/' && dirs[l - 1] != '\\'; - buf = (char*)malloc(l + as ? 2 : 1); + buf = (char*)malloc(l + (as ? 2 : 1)); memcpy(buf, dirs, l); if(as) { buf[l] = '/'; @@ -576,6 +588,7 @@ static _importing_dirs_t* _set_importing_directories(char* dirs) { dirs += l + 1; } + _destroy_importing_directories(); importing_dirs = result; return result; @@ -584,18 +597,6 @@ static _importing_dirs_t* _set_importing_directories(char* dirs) { return 0; } -static void _destroy_importing_directories(void) { - int i = 0; - - if(!importing_dirs) return; - - for(i = 0; i < importing_dirs->count; ++i) { - free(importing_dirs->dirs[i]); - } - free(importing_dirs->dirs); - free(importing_dirs); -} - static bool_t _try_import(struct mb_interpreter_t* s, const char* p) { bool_t result = false; int i = 0; @@ -1114,6 +1115,23 @@ static int now(struct mb_interpreter_t* s, void** l) { return result; } +static int set_importing_dirs(struct mb_interpreter_t* s, void** l) { + int result = MB_FUNC_OK; + char* arg = 0; + + mb_assert(s && l); + + mb_check(mb_attempt_open_bracket(s, l)); + + mb_check(mb_pop_string(s, l, &arg)); + if(arg) + _set_importing_directories(arg); + + mb_check(mb_attempt_close_bracket(s, l)); + + return result; +} + static int beep(struct mb_interpreter_t* s, void** l) { int result = MB_FUNC_OK; @@ -1197,6 +1215,7 @@ static void _on_startup(void) { mb_reg_fun(bas, ticks); #endif /* _HAS_TICKS */ mb_reg_fun(bas, now); + mb_reg_fun(bas, set_importing_dirs); mb_reg_fun(bas, beep); }