Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2017-11-28 (0.12.10/11)
COMMON: owner strings

2017-11-28 (0.12.10)
COMMON: Fix some coverty warnings

Expand Down
17 changes: 13 additions & 4 deletions ide/android/assets/main.bas
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const setupId = "_setup"
const aboutId = "_about"
const backId = "_back"
const scratchId = "_scratch"
const scratch_file = HOME + "scratch.bas"

func mk_bn(value, lab, fg)
local bn
Expand All @@ -50,10 +51,9 @@ end

func mk_scratch()
local text
local file = "scratch.bas"
local result = false

if (not exist(file)) then
if (not exist(scratch_file)) then
dim text
text << "rem Welcome to SmallBASIC"
text << "rem"
Expand All @@ -66,7 +66,7 @@ func mk_scratch()
text << "rem Press the 3 vertical dots for menu options."
endif
try
tsave file, text
tsave scratch_file, text
result = true
catch e
local wnd = window()
Expand Down Expand Up @@ -232,6 +232,7 @@ end

sub loadFileList(path, byref basList)
erase basList
local emptyNode

func walker(node)
if (node.depth==0) then
Expand All @@ -246,6 +247,14 @@ sub loadFileList(path, byref basList)
return node.depth == 0
end
dirwalk path, "", use walker(x)

if (path = "/" && len(basList) == 0 && !is_sdl) then
emptyNode.name = "sdcard"
emptyNode.dir = true
emptyNode.size = ""
emptyNode.mtime = 0
basList << emptyNode
endif
end

sub listFiles(byref frm, path, sortDir, byref basList)
Expand Down Expand Up @@ -641,7 +650,7 @@ sub main
frm = makeUI(path, sortDir)
elif frm.value == scratchId then
if (mk_scratch())
frm.close("scratch.bas")
frm.close(scratch_file)
endif
elif frm.value == backId then
cls
Expand Down
1 change: 1 addition & 0 deletions samples/distro-examples/games/sokoban.bas
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ sub main
frm.inputs(0).value = game_names
frm.inputs(1).label = "Open"
frm.inputs(2).label = "Play"
frm.focus = 0
frm = form(frm)
end

Expand Down
2 changes: 1 addition & 1 deletion samples/distro-examples/tests/output/array.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
TEST: Arrays, unound, lbound
array: {"cat":{"name":"lots"},"zz":"thing","zz":"memleak","other":"thing"}
array: {"cat":{"name":"lots"},"zz":"memleak","other":"thing"}
3 changes: 2 additions & 1 deletion src/common/bc.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,13 @@ void bc_add_creal(bc_t *bc, var_num_t v) {
*/
void bc_add_strn(bc_t *bc, const char *str, int len) {
bc_add_code(bc, kwTYPE_STR);
bc_add_dword(bc, len);
bc_add_dword(bc, len + 1);
if (bc->count + len >= bc->size) {
bc_resize(bc, bc->size + BC_ALLOC_INCR + len);
}
memcpy(bc->ptr + bc->count, str, len);
bc->count += len;
bc_add1(bc, 0);
}

/*
Expand Down
3 changes: 3 additions & 0 deletions src/common/blib.c
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ void cmd_print(int output) {
vuser_p->type = V_STR;
vuser_p->v.p.ptr = NULL;
vuser_p->v.p.length = 0;
vuser_p->v.p.owner = 1;
handle = (intptr_t)vuser_p;
}

Expand Down Expand Up @@ -1964,6 +1965,7 @@ void cmd_read() {
prog_dp += OS_STRLEN;

vp->v.p.ptr = malloc(len + 1);
vp->v.p.owner = 1;
memcpy(vp->v.p.ptr, prog_source + prog_dp, len);
*((char *) (vp->v.p.ptr + len)) = '\0';
vp->v.p.length = len;
Expand Down Expand Up @@ -2288,6 +2290,7 @@ void cmd_wjoin() {
v_free(str);
str->type = V_STR;
str->v.p.ptr = malloc(size);
str->v.p.owner = 1;
str->v.p.ptr[0] = '\0';

for (i = 0; i < v_asize(var_p); i++) {
Expand Down
7 changes: 2 additions & 5 deletions src/common/blib_db.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,7 @@ void cmd_floadln() {
// build var for line
var_p = v_elem(array_p, index);
int size = GROW_SIZE;
var_p->type = V_STR;
var_p->v.p.ptr = malloc(size);
v_init_str(var_p, size);
index++;

// process the next line
Expand Down Expand Up @@ -654,9 +653,7 @@ void cmd_floadln() {
} else { // if type=1
// build string
v_free(var_p);
var_p->type = V_STR;
var_p->v.p.length = dev_flength(handle) + 1;
var_p->v.p.ptr = malloc(var_p->v.p.length);
v_init_str(var_p, dev_flength(handle));
if (var_p->v.p.length > 1) {
dev_fread(handle, (byte *)var_p->v.p.ptr, var_p->v.p.length - 1);
}
Expand Down
30 changes: 6 additions & 24 deletions src/common/blib_func.c
Original file line number Diff line number Diff line change
Expand Up @@ -933,8 +933,6 @@ void cmd_str1(long funcCode, var_t *arg, var_t *r) {
// str <- CBS$(str)
// convert C-Style string to BASIC-style string
//
v_tostr(arg);
IF_ERR_RETURN;
r->v.p.ptr = cstrdup(arg->v.p.ptr);
r->v.p.length = strlen(r->v.p.ptr) + 1;
break;
Expand All @@ -943,9 +941,6 @@ void cmd_str1(long funcCode, var_t *arg, var_t *r) {
// str <- BCS$(str)
// convert BASIC-Style string to C-style string
//
v_tostr(arg);
IF_ERR_RETURN;

r->v.p.ptr = bstrdup(arg->v.p.ptr);
r->v.p.length = strlen(r->v.p.ptr) + 1;
break;
Expand Down Expand Up @@ -988,8 +983,6 @@ void cmd_str1(long funcCode, var_t *arg, var_t *r) {
//
// str <- LCASE$(s)
//
v_tostr(arg);
IF_ERR_RETURN;
r->v.p.ptr = (char *)malloc(strlen(arg->v.p.ptr) + 1);
strcpy(r->v.p.ptr, arg->v.p.ptr);
p = r->v.p.ptr;
Expand All @@ -1003,8 +996,6 @@ void cmd_str1(long funcCode, var_t *arg, var_t *r) {
//
// str <- UCASE$(s)
//
v_tostr(arg);
IF_ERR_RETURN;
r->v.p.ptr = (char *)malloc(strlen(arg->v.p.ptr) + 1);
strcpy(r->v.p.ptr, arg->v.p.ptr);
p = r->v.p.ptr;
Expand All @@ -1018,8 +1009,6 @@ void cmd_str1(long funcCode, var_t *arg, var_t *r) {
//
// str <- LTRIM$(s)
//
v_tostr(arg);
IF_ERR_RETURN;
p = arg->v.p.ptr;
while (is_wspace(*p)) {
p++;
Expand All @@ -1036,8 +1025,6 @@ void cmd_str1(long funcCode, var_t *arg, var_t *r) {
//
// str <- RTRIM$(s)
//
v_tostr(arg);
IF_ERR_RETURN;
p = arg->v.p.ptr;
if (*p != '\0') {
while (*p) {
Expand Down Expand Up @@ -1144,8 +1131,6 @@ void cmd_str1(long funcCode, var_t *arg, var_t *r) {
//
// str <- ENVIRON$(str)
//
v_tostr(arg);
IF_ERR_RETURN;
if (*arg->v.p.ptr != '\0') {
// return the variable
const char *v = dev_getenv(arg->v.p.ptr);
Expand Down Expand Up @@ -1293,6 +1278,7 @@ void cmd_str0(long funcCode, var_t *r) {
tms = *localtime(&now);
r->type = V_STR;
r->v.p.ptr = malloc(32);
r->v.p.owner = 1;
sprintf(r->v.p.ptr, "%02d/%02d/%04d", tms.tm_mday, tms.tm_mon + 1, tms.tm_year + 1900);
r->v.p.length = strlen(r->v.p.ptr) + 1;
break;
Expand All @@ -1304,6 +1290,7 @@ void cmd_str0(long funcCode, var_t *r) {
tms = *localtime(&now);
r->type = V_STR;
r->v.p.ptr = malloc(32);
r->v.p.owner = 1;
sprintf(r->v.p.ptr, "%02d:%02d:%02d", tms.tm_hour, tms.tm_min, tms.tm_sec);
r->v.p.length = strlen(r->v.p.ptr) + 1;
break;
Expand Down Expand Up @@ -2216,9 +2203,7 @@ void cmd_genfunc(long funcCode, var_t *r) {

if (funcCode == kwDATEFMT) {
// format
r->type = V_STR;
r->v.p.ptr = date_fmt(arg.v.p.ptr, d, m, y);
r->v.p.length = strlen(r->v.p.ptr) + 1;
v_move_str(r, date_fmt(arg.v.p.ptr, d, m, y));
v_free(&arg);
} else {
// weekday
Expand Down Expand Up @@ -2247,6 +2232,7 @@ void cmd_genfunc(long funcCode, var_t *r) {
r->type = V_STR;
r->v.p.ptr = malloc((count << 1) + 1);
r->v.p.ptr[0] = '\0';
r->v.p.owner = 1;
len = 0;
char tmp[3];
for (int i = 0; i < count; i++) {
Expand Down Expand Up @@ -2279,9 +2265,7 @@ void cmd_genfunc(long funcCode, var_t *r) {
r->v.p.ptr[len] = '\0';
} else {
// file
r->type = V_STR;
r->v.p.ptr = malloc(count + 1);
r->v.p.length = count + 1;
v_init_str(r, count);
dev_fread(handle, (byte *)r->v.p.ptr, count);
r->v.p.ptr[count] = '\0';
}
Expand Down Expand Up @@ -2837,9 +2821,7 @@ void cmd_genfunc(long funcCode, var_t *r) {
// add the entries
for (int i = 0; i < count; i++) {
var_t *elem_p = v_elem(r, i);
elem_p->type = V_STR;
elem_p->v.p.length = strlen(list[i]) + 1;
elem_p->v.p.ptr = malloc(elem_p->v.p.length);
v_init_str(elem_p, strlen(list[i]));
strcpy(elem_p->v.p.ptr, list[i]);
}
} else {
Expand Down
44 changes: 23 additions & 21 deletions src/common/brun.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,37 +269,38 @@ void exec_setup_predefined_variables() {
setsysvar_var(SYSVAR_NONE, 0, V_NIL);
setsysvar_num(SYSVAR_MAXINT, VAR_MAX_INT);

#if defined(_UnixOS)
#if defined(_ANDROID)
strcpy(homedir, "/sdcard/");
#else
#if defined(_Win32)
if (getenv("HOMEPATH")) {
strlcpy(homedir, getenv("HOMEPATH"), sizeof(homedir));
}
else {
GetModuleFileName(NULL, homedir, sizeof(homedir) - 1);
char *p = strrchr(homedir, '\\');
if (p) {
*p = '\0';
}
}
for (char *p = homedir; *p; p++) {
if (*p == '\\') {
*p = '/';
}
}
#elif defined(_UnixOS)
if (getenv("HOME")) {
strlcpy(homedir, getenv("HOME"), sizeof(homedir));
}
else {
strcpy(homedir, "/tmp/");
}
#endif
int l = strlen(homedir);
if (homedir[l - 1] != OS_DIRSEP) {
homedir[l] = OS_DIRSEP;
homedir[l + 1] = '\0';
}
#elif defined(_Win32)
if (getenv("HOME")) {
// this works on cygwin
strlcpy(homedir, getenv("HOME"), sizeof(homedir));
}
else {
GetModuleFileName(NULL, homedir, sizeof(homedir) - 1);
char *p = strrchr(homedir, '\\');
*p = '\0';
strcat(homedir, "\\");
if (OS_DIRSEP == '/') {
p = homedir;
while (*p) {
if (*p == '\\')
*p = '/';
p++;
}
}
}
#endif
setsysvar_str(SYSVAR_HOME, homedir);
}
Expand Down Expand Up @@ -1643,7 +1644,8 @@ void sbasic_exec_prepare(const char *filename) {
* remember the directory location of the running program
*/
void sbasic_set_bas_dir(const char *bas_file) {
int path_len = strrchr(bas_file, OS_DIRSEP) - bas_file;
const char *sep = strrchr(bas_file, OS_DIRSEP);
int path_len = sep == NULL ? 0 : (sep - bas_file);
char cwd[OS_PATHNAME_SIZE + 1];

cwd[0] = '\0';
Expand Down
3 changes: 3 additions & 0 deletions src/common/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,7 @@ static inline void eval_push(var_t *r) {
len = r->v.p.length;
eval_stk[eval_sp].type = V_STR;
eval_stk[eval_sp].v.p.ptr = malloc(len + 1);
eval_stk[eval_sp].v.p.owner = 1;
strcpy(eval_stk[eval_sp].v.p.ptr, r->v.p.ptr);
eval_stk[eval_sp].v.p.length = len;
break;
Expand Down Expand Up @@ -852,6 +853,7 @@ static inline void eval_callf_str1(long fcode, var_t *r) {
IP++;
r->type = V_STR;
r->v.p.ptr = NULL;
r->v.p.owner = 1;
cmd_str1(fcode, &vtmp, r);
v_free(&vtmp);
}
Expand All @@ -865,6 +867,7 @@ static inline void eval_callf_strn(long fcode, var_t *r) {
err_missing_lp();
} else {
r->type = V_STR;
r->v.p.owner = 1;
r->v.p.ptr = NULL;
IP++; // '('
cmd_strN(fcode, r);
Expand Down
1 change: 1 addition & 0 deletions src/common/fs_socket_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ int http_read(dev_file_t *f, var_t *var_p) {
if (rxbuff[i + 2] == '\n') {
var_p->v.p.length = bytes - i - 3;
var_p->v.p.ptr = malloc(var_p->v.p.length + 1);
var_p->v.p.owner = 1;
memcpy(var_p->v.p.ptr, rxbuff + i + 3, var_p->v.p.length);
var_p->v.p.ptr[var_p->v.p.length] = 0;
inHeader = 0;
Expand Down
Loading