rev 165 - trunk/src
SVN User <[email protected]>
| Newsgroups | gmane.comp.lang.prothon.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: bcollins
Date: 2004-03-27 18:47:00 -0500 (Sat, 27 Mar 2004)
New Revision: 165
Modified:
trunk/src/interp.c
Log:
Cleaned up usages of unbound str functions (like sprintf, strcat, strcpy)
using apr_* functions. Use apr_stat() instead of opening files to test for
existence.
Modified: trunk/src/interp.c
===================================================================
--- trunk/src/interp.c 2004-03-27 22:36:25 UTC (rev 164)
+++ trunk/src/interp.c 2004-03-27 23:47:00 UTC (rev 165)
@@ -58,6 +58,7 @@
#include "interp.h"
#include <prothon/prothon_dll.h>
+#include <apr.h>
#include <apr_thread_proc.h>
#include <apr_strings.h>
#include <apr_dso.h>
@@ -70,7 +71,7 @@
static FILE* tfout=NULL;
-int prt_code_line(exec_frame_t* frame, code* code, int pc, FILE* fout);
+static int prt_code_line(exec_frame_t* frame, code* code, int pc, FILE* fout);
isp get_ist() {
isp ist = pr_malloc(sizeof(interp_state_t));
@@ -307,6 +308,11 @@
ist->exception_obj = orig_excobj;
}
+#ifdef WIN32
+#define MODULE_EXTENSION ""
+#else
+#define MODULE_EXTENSION ".so"
+#endif
//******************************** load_dll_module *****************************
int load_dll_module(isp ist, char* full_path, char* module_name, obj_p dest_module, obj_p alias){
@@ -314,20 +320,21 @@
apr_dso_handle_t *handle;
apr_dso_handle_sym_t dll_entry;
apr_status_t aprerr;
+ char dll_path[APR_PATH_MAX];
- strcat(full_path, module_name);
-#ifndef WIN32
- strcat(full_path, ".so");
-#endif
- aprerr = apr_dso_load(&handle, full_path, get_pr_head_pool());
+ apr_snprintf(dll_path, sizeof(dll_path), "%s/%s%s", full_path, module_name,
+ MODULE_EXTENSION);
+
+ aprerr = apr_dso_load(&handle, dll_path, get_pr_head_pool());
if (aprerr != APR_SUCCESS)
return PR_FALSE;
aprerr = apr_dso_sym(&dll_entry, handle, "dll_entry");
if (aprerr != APR_SUCCESS) {
- sprintf(full_path, "Dll module %s initialization failed", module_name);
+ apr_snprintf(dll_path, sizeof(dll_path),
+ "Loadable module `%s': initialization failed", module_name);
apr_dso_unload(handle);
- raise_exception(ist, OBJ(INTERNAL_EXC), full_path);
+ raise_exception(ist, OBJ(INTERNAL_EXC), dll_path);
return PR_FALSE;
}
@@ -338,21 +345,17 @@
return PR_TRUE;
}
-void add_sep_if_needed(char* path) {
- char* p;
- int ch=0;
- for(p=path; *p; ch=*p, p++);
- if (ch != '/') strcat(path, "/");
-}
//******************************** import_module **********************************
obj_p import_module( isp ist, exec_frame_t* frame, int param,
int store_local, obj_p alias, int return_mod ){
int i, llen, pkg_depth = 1;
obj_p dest_module, module_symbol, dmod;
- char ch, full_path[1024], pkg_path[1024], temp_str[1024], *module_name = NULL;
- FILE* pkg_stream;
+ char ch, full_path[APR_PATH_MAX], pkg_path[APR_PATH_MAX], temp_str[APR_PATH_MAX],
+ *module_name = NULL;
obj_p path_list = get_sys_path_list();
+ apr_finfo_t finfo;
+
module_symbol = fr_data(param-1);
module_name = symch(ist, module_symbol);
if (alias) ch = *symch(ist, alias);
@@ -363,41 +366,55 @@
else dest_module = frame->locals;
} else dest_module = NULL;
llen = list_len(ist, path_list);
- *pkg_path = 0;
+ pkg_path[0] = 0;
for(i=0; i < llen; i++) {
- strcpy(full_path, strch(list_item(ist, path_list, i)));
- add_sep_if_needed(full_path);
- strcat(full_path, symch(ist, fr_data(pkg_depth)));
- add_sep_if_needed(full_path);
- strcpy(temp_str, full_path);
- strcat(full_path, "__init__.pr");
- pkg_stream = fopen(full_path, "rb");
- if (pkg_stream) {
- fclose(pkg_stream);
- strcpy(pkg_path, temp_str);
- if (alias && pkg_depth < param-1) dmod = NULL;
- else dmod = dest_module;
- load_module( ist, fr_data(pkg_depth), frame->locals,
- dmod, alias, full_path, ": a package", NULL, NULL); if_exc_return 0;
+ apr_snprintf(temp_str, sizeof(temp_str), "%s/%s",
+ strch(list_item(ist, path_list, i)),
+ symch(ist, fr_data(pkg_depth)));
+ apr_snprintf(full_path, sizeof(full_path), "%s/__init__.pr",
+ temp_str);
+
+ if (apr_stat(&finfo, full_path, APR_FINFO_TYPE, get_pr_head_pool()) ==
+ APR_SUCCESS && finfo.filetype == APR_REG) {
+ apr_cpystrn(pkg_path, temp_str, sizeof(pkg_path));
+
+ if (alias && pkg_depth < param-1)
+ dmod = NULL;
+ else
+ dmod = dest_module;
+
+ load_module(ist, fr_data(pkg_depth), frame->locals,
+ dmod, alias, full_path, ": a package", NULL, NULL);
+ if_exc_return 0;
+
if (!alias && dest_module)
dest_module = get_attr(ist, OBJ(MODULES), fr_data(pkg_depth));
+
pkg_depth++;
while (pkg_depth < param) {
- strcpy(full_path, pkg_path);
- strcat(full_path, symch(ist, fr_data(pkg_depth)));
- add_sep_if_needed(full_path);
- strcpy(temp_str, full_path);
- strcat(full_path, "__init__.pr");
- pkg_stream = fopen(full_path, "rb");
- if (pkg_stream) {
- fclose(pkg_stream);
- strcpy(pkg_path, temp_str);
- if (alias && pkg_depth < param-1) dmod = NULL;
- else dmod = dest_module;
- load_module( ist, fr_data(pkg_depth), frame->locals,
- dmod, alias, full_path, ": a package", NULL, NULL ); if_exc_return 0;
+ apr_snprintf(temp_str, sizeof(temp_str), "%s/%s",
+ pkg_path, symch(ist, fr_data(pkg_depth)));
+ apr_snprintf(full_path, sizeof(full_path), "%s/__init__.pr",
+ temp_str);
+
+ if (apr_stat(&finfo, full_path, APR_FINFO_TYPE,
+ get_pr_head_pool()) == APR_SUCCESS &&
+ finfo.filetype == APR_REG) {
+ apr_cpystrn(pkg_path, temp_str, sizeof(pkg_path));
+
+ if (alias && pkg_depth < param-1)
+ dmod = NULL;
+ else
+ dmod = dest_module;
+
+ load_module(ist, fr_data(pkg_depth), frame->locals,
+ dmod, alias, full_path, ": a package",
+ NULL, NULL );
+ if_exc_return 0;
+
if (!alias && dest_module)
- dest_module = get_attr(ist, OBJ(MODULES), fr_data(pkg_depth));
+ dest_module = get_attr(ist, OBJ(MODULES),
+ fr_data(pkg_depth));
pkg_depth++;
} else
break;
@@ -405,50 +422,63 @@
break;
}
}
+
if (pkg_depth < param-1) {
- sprintf(full_path, "Package %s not found", symch(ist, fr_data(pkg_depth)));
+ apr_snprintf(full_path, sizeof(full_path), "Package %s not found",
+ symch(ist, fr_data(pkg_depth)));
raise_exception(ist, OBJ(INTERPRETER_EXC), full_path);
return NULL;
}
- if (pkg_depth == param) goto success;
- if (*pkg_path) {
- strcpy(full_path, pkg_path);
- if (!alias) alias = module_symbol;
- if(load_dll_module(ist, full_path, module_name, dest_module, alias)) goto success;
+
+ if (pkg_depth == param)
+ goto success;
+
+ if (pkg_path[0]) {
+ apr_cpystrn(full_path, pkg_path, sizeof(full_path));
+ if (!alias)
+ alias = module_symbol;
+ if (load_dll_module(ist, full_path, module_name, dest_module, alias))
+ goto success;
if_exc_return NULL;
} else {
- for(i=0; i < llen; i++) {
+ for(i = 0; i < llen; i++) {
apr_snprintf(full_path, sizeof(full_path), "%s/",
strch(list_item(ist, path_list, i)));
- if (!alias) alias = module_symbol;
- if(load_dll_module(ist, full_path, module_name, dest_module, alias)) goto success;
+ if (!alias)
+ alias = module_symbol;
+ if (load_dll_module(ist, full_path, module_name, dest_module, alias))
+ goto success;
if_exc_return NULL;
}
}
- if (*pkg_path) {
- strcpy(full_path, pkg_path);
- strcat(full_path, module_name);
- strcat(full_path, ".pr");
- if ( load_module( ist, module_symbol, frame->locals,
- dest_module, alias, full_path, ": a module", NULL, NULL ) ) goto success;
+
+ if (pkg_path[0]) {
+ apr_snprintf(full_path, sizeof(full_path), "%s/%s.pr", pkg_path, module_name);
+ if (load_module(ist, module_symbol, frame->locals, dest_module,
+ alias, full_path, ": a module", NULL, NULL ))
+ goto success;
if_exc_return NULL;
} else {
- for(i=0; i < llen; i++) {
+ for(i = 0; i < llen; i++) {
apr_snprintf(full_path, sizeof(full_path), "%s/%s.pr",
strch(list_item(ist, path_list, i)),
module_name);
- if ( load_module( ist, module_symbol, frame->locals,
- dest_module, alias, full_path, ": a module", NULL, NULL ) ) goto success;
+ if (load_module(ist, module_symbol, frame->locals, dest_module,
+ alias, full_path, ": a module", NULL, NULL ))
+ goto success;
if_exc_return NULL;
}
}
- sprintf(full_path, "Import module %s not found", module_name);
+ apr_snprintf(full_path, sizeof(full_path), "Import module %s not found", module_name);
raise_exception(ist, OBJ(INTERPRETER_EXC), full_path);
return NULL;
+
success:
if_exc_return NULL;
- if (return_mod) return get_attr(ist, OBJ(MODULES), module_symbol);
- else return NULL;
+ if (return_mod)
+ return get_attr(ist, OBJ(MODULES), module_symbol);
+ else
+ return NULL;
}
//******************************** do_return **********************************
@@ -681,10 +711,13 @@
set_attr(ist, func, SYM(__GLOBALS__), frame->globals);
set_attr(ist, func, SYM(__SYN_LOCALS__), frame->locals);
#ifdef DUMP_MODULE_CODE
- { char dump_name[256];
- strcpy(dump_name, symch(ist, fr_stack[fr_sp+1]));
- strcat(dump_name, "_def_dump.txt");
- dump_code(func->data.ptr, dump_name); }
+ {
+ char dump_name[256];
+ apr_snprintf(dump_name, sizeof(dump_name),
+ "%s_def_dump.txt",
+ symch(ist, fr_stack[fr_sp+1]));
+ dump_code(func->data.ptr, dump_name);
+ }
#endif
if (op == OP_GEN) {
exec_frame_t* new_frame = new_exec_frame( ist,
@@ -867,7 +900,8 @@
if (intrp_exobj) break;
if (!func_obj) {
char str[1024];
- sprintf(str, "Function %s not found", as_str(ist, fr_stack[fr_sp+1]));
+ apr_snprintf(str, sizeof(str), "Function %s not found",
+ as_str(ist, fr_stack[fr_sp+1]));
raise_exception(ist, OBJ(FUNCNOTFOUND_EXC), str);
break;
}
@@ -910,7 +944,7 @@
fr_push(func_obj);
} else {
obj_p value;
- char str[80];
+ char str[128];
if (fr_stack[fr_sp+1] != SYM(__INIT__)) {
value = get_proto_attr(ist, fr_stack[fr_sp], fr_stack[fr_sp+1], NULL); if_exc_return NULL;
if (value) {
@@ -919,7 +953,8 @@
goto call_get_func;
}
}
- sprintf(str, "Function not found: \"%s\"", as_str(ist, fr_stack[fr_sp+1]));
+ apr_snprintf(str, sizeof(str), "Function not found: \"%s\"",
+ as_str(ist, fr_stack[fr_sp+1]));
raise_exception(ist, OBJ(FUNCNOTFOUND_EXC), str);
}
} break;
@@ -961,7 +996,7 @@
obj = get_attr(ist, fr_tos, fr_data(1));
if (!obj) {
char msg[1024];
- sprintf(msg, "Attribute not found in module %s",
+ apr_snprintf(msg, sizeof(msg), "Attribute not found in module %s",
symch(ist, fr_data(1)));
raise_exception(ist, OBJ(INTERPRETER_EXC), msg);
break;
@@ -974,8 +1009,9 @@
obj = get_attr(ist, obj, fr_data(i));
if (!obj) {
char msg[1024];
- sprintf(msg, "Attribute not found in module %s",
- symch(ist, fr_data(i)));
+ apr_snprintf(msg, sizeof(msg),
+ "Attribute not found in module %s",
+ symch(ist, fr_data(i)));
raise_exception(ist, OBJ(INTERPRETER_EXC), msg);
break;
}
@@ -995,7 +1031,7 @@
} break;
default: {
char str[80];
- sprintf(str, "Bad opcode: %d", op);
+ apr_snprintf(str, sizeof(str), "Bad opcode: %d", op);
raise_exception(ist, OBJ(INTERNAL_EXC), str);
}
}
@@ -1087,7 +1123,8 @@
func_obj = get_proto_attr(ist, self, func_sym, NULL); if_exc_return NULL;
if (!func_obj) {
char err_str[1024];
- sprintf(err_str, "Function not found: \"%s\"", as_str(ist, func_sym));
+ apr_snprintf(err_str, sizeof(err_str), "Function not found: \"%s\"",
+ as_str(ist, func_sym));
if (!ist) {
printf("Internal error with exceptions disabled: %s", err_str);
pr_exit(1);
@@ -1190,17 +1227,23 @@
obj_p new_locals;
exec_frame_t *frame, *new_frame;
parse_state* state = parse_file_or_string(ist, NULL, str);
- if (check_exceptions(ist)) return NULL;
- if (!state || !(code = (state->parse_results))) return NULL;
+
+ if (check_exceptions(ist))
+ return NULL;
+ if (!state || !(code = (state->parse_results)))
+ return NULL;
+
#ifdef DUMP_MODULE_CODE
- { char name[128], *p, *d;
- for (p=str, d=name; *p && (strlen(d) < 64); p++) {
+ {
+ char name[256], *p, *d;
+ for (p = str, d = name; *p && (strlen(d) < 64); p++) {
if (*p >= 'A' && *p <= 'Z') *(d++) = *p;
if (*p >= 'a' && *p <= 'z') *(d++) = *p;
}
- *d=0;
+ *d = 0;
strcat(name, "_execstr_code.txt");
- dump_code(code, dump_name); }
+ dump_code(code, dump_name);
+ }
#endif
if (frame_p && *frame_p) {
frame = *frame_p;
@@ -1235,26 +1278,36 @@
obj_p new_locals;
exec_frame_t *frame, *new_frame;
parse_state* state;
- char name[1024];
+ char full_doc[1024], *name;
code* code;
+
if (!module)
module = get_attr(ist, OBJ(MODULES), module_name);
if (!module)
module = new_object(NULL);
+
set_attr(ist, OBJ(MODULES), module_name, module);
- if (exec_string) state = parse_file_or_string(ist, NULL, exec_string);
- else state = parse_file_or_string(ist, filename, NULL);
+ if (exec_string)
+ state = parse_file_or_string(ist, NULL, exec_string);
+ else
+ state = parse_file_or_string(ist, filename, NULL);
+
catch_exception(ist, OBJ(FILENOTFOUND_EXC), NULL); if_exc_return PR_FALSE;
- if (!state || !(code = (state->parse_results))) return PR_FALSE;
- strcpy(name, symch(ist, module_name));
+
+ if (!state || !(code = (state->parse_results)))
+ return PR_FALSE;
+
+ name = symch(ist, module_name);
+
#ifdef DUMP_MODULE_CODE
- { char dump_name[256];
- strcpy(dump_name, name);
- strcat(dump_name, "_module_dump.txt");
- dump_code(code, dump_name); }
+ {
+ char dump_name[256];
+ apr_snprintf(dump_name, sizeof(dump_name), "%s_module_dump.txt", name);
+ dump_code(code, dump_name);
+ }
#endif
- strcat(name, comment);
- add_doc_to_obj(ist, module, name);
+ apr_snprintf(full_doc, sizeof(full_doc), "%s%s", name, comment);
+ add_doc_to_obj(ist, module, full_doc);
frame = ist->frame;
new_locals = new_object(NULL);
set_attr(ist, module, SYM(__LOCALS__), new_locals);
@@ -1291,13 +1344,12 @@
main_threads_started++;
main_threads_running++;
- while(PR_TRUE){
- char name[16], s[8];
- strcpy(name, "Main");
- sprintf(s,"%d",main_index);
- strcat(name,s);
+ while (PR_TRUE) {
+ char name[16];
+
+ apr_snprintf(name, sizeof(name), "Main%d", main_index);
main_sym = sym(ist, name);
- if(!get_attr(ist, OBJ(MODULES), main_sym)) {
+ if (!get_attr(ist, OBJ(MODULES), main_sym)) {
module = new_object(OBJ(ROOT_GLOBALS));
set_attr(ist, OBJ(MODULES), main_sym, module);
set_attr(ist, thread_obj, sym(ist, "name"), new_string_obj(name));
@@ -1335,30 +1387,42 @@
return NULL;
}
+#define CODE_DATA_STR_SIZE 1024
+
//******************************** codedata ***********************************
-char* codedata(char* str, code* code, int op, int param, int pc){
+static char* codedata(char* str, code* code, int op, int param, int pc)
+{
int i;
- str[0] = 0;
- if (op < OP_PARAM_WORDS_BOUNDARY)
- for (i=0; i < param-1; i++) {
- char str_tmp[32];
- apr_snprintf(str_tmp, sizeof(str_tmp), " %x", (unsigned)(uintptr_t)code->code_data[pc+1+i].data);
- strcat(str, str_tmp);
- }
- else if (op < OP_TWO_WORDS_BOUNDARY)
- sprintf(str, " %x", (unsigned)(uintptr_t)code->code_data[pc+1].data);
+
+ *str = 0;
+
+ if (op < OP_PARAM_WORDS_BOUNDARY) {
+ for (i = 0; i < param - 1; i++)
+ apr_snprintf(str, CODE_DATA_STR_SIZE, "%s %x", str,
+ (unsigned)(uintptr_t)code->code_data[pc+1+i].data);
+ } else if (op < OP_TWO_WORDS_BOUNDARY) {
+ apr_snprintf(str, CODE_DATA_STR_SIZE, " %x",
+ (unsigned)(uintptr_t)code->code_data[pc+1].data);
+ }
+
return str;
}
//******************************** prt_code_line ******************************
-int prt_code_line(exec_frame_t* frame, code* code, int pc, FILE* fout) {
- char str[1024];
+static int prt_code_line(exec_frame_t* frame, code* code, int pc, FILE* fout)
+{
+ char str[CODE_DATA_STR_SIZE];
opcode_t op;
int param;
+
op = code->code_data[pc].bytecode.opcode;
param = code->code_data[pc].bytecode.param;
- if (param < -1000000) param = 0;
- if (frame) fprintf(fout, "%x ", (unsigned)(uintptr_t)frame);
+
+ if (param < -1000000)
+ param = 0;
+ if (frame)
+ fprintf(fout, "%x ", (unsigned)(uintptr_t)frame);
+
switch (op) {
case OP_NOP: fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_NOP", param, codedata(str, code, op, param, pc));
break;