+added a SET_IMPORTING_DIRS statement to the shell; *fixed a memory corruption bug with importing directory setting.

This commit is contained in:
paladin-t 2016-01-26 15:43:22 +08:00
parent 25d76bb579
commit 6be1d905db
3 changed files with 36 additions and 13 deletions

View File

@ -1,6 +1,8 @@
Jan. 26 2016 Jan. 26 2016
Added a SET_IMPORTING_DIRS statement to the shell
Added friendly error promption when memory overflow Added friendly error promption when memory overflow
Added source file information to stepped handler Added source file information to stepped handler
Fixed a memory corruption bug with importing directory setting
Optimized cached list accessing Optimized cached list accessing
Jan. 25 2016 Jan. 25 2016

View File

@ -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) * [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) * [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) * [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 * Integration
* [Link with MY-BASIC](https://github.com/paladin-t/my_basic/wiki/Link-with-MY_BASIC) * [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) * [Write a debugger](https://github.com/paladin-t/my_basic/wiki/Write-a-debugger)

View File

@ -539,6 +539,18 @@ typedef struct _importing_dirs_t {
static _importing_dirs_t* importing_dirs = 0; 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) { static _importing_dirs_t* _set_importing_directories(char* dirs) {
if(dirs) { if(dirs) {
char* end = dirs + strlen(dirs); char* end = dirs + strlen(dirs);
@ -560,7 +572,7 @@ static _importing_dirs_t* _set_importing_directories(char* dirs) {
} }
l = (int)strlen(dirs); l = (int)strlen(dirs);
as = dirs[l - 1] != '/' && dirs[l - 1] != '\\'; 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); memcpy(buf, dirs, l);
if(as) { if(as) {
buf[l] = '/'; buf[l] = '/';
@ -576,6 +588,7 @@ static _importing_dirs_t* _set_importing_directories(char* dirs) {
dirs += l + 1; dirs += l + 1;
} }
_destroy_importing_directories();
importing_dirs = result; importing_dirs = result;
return result; return result;
@ -584,18 +597,6 @@ static _importing_dirs_t* _set_importing_directories(char* dirs) {
return 0; 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) { static bool_t _try_import(struct mb_interpreter_t* s, const char* p) {
bool_t result = false; bool_t result = false;
int i = 0; int i = 0;
@ -1114,6 +1115,23 @@ static int now(struct mb_interpreter_t* s, void** l) {
return result; 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) { static int beep(struct mb_interpreter_t* s, void** l) {
int result = MB_FUNC_OK; int result = MB_FUNC_OK;
@ -1197,6 +1215,7 @@ static void _on_startup(void) {
mb_reg_fun(bas, ticks); mb_reg_fun(bas, ticks);
#endif /* _HAS_TICKS */ #endif /* _HAS_TICKS */
mb_reg_fun(bas, now); mb_reg_fun(bas, now);
mb_reg_fun(bas, set_importing_dirs);
mb_reg_fun(bas, beep); mb_reg_fun(bas, beep);
} }