/* * convert's a user's string to variable * * its decides in what format to store the value * its used mostly by 'input' functions */ void v_input2var(const char *str, var_t *var) { v_free(var); if (strlen(str) == 0) { // no data v_setstr(var, str); } else { char *np, *sb; char buf[64]; int type; var_int_t lv; var_num_t dv; sb = strdup(str); np = get_numexpr(sb, buf, &type, &lv, &dv); if (type == 1 && *np == '\0') { v_setint(var, lv); } else if (type == 2 && *np == '\0') { v_setreal(var, dv); } else { v_setstr(var, str); } free(sb); } }
// assign error to variable or match with next expression int err_throw_catch(const char *err) { var_t *arg; var_t v_catch; int caught = 1; switch (code_peek()) { case kwTYPE_VAR: arg = code_getvarptr(); v_setstr(arg, err); break; case kwTYPE_STR: v_init(&v_catch); eval(&v_catch); // catch is conditional on matching error caught = (v_catch.type == V_STR && strstr(err, v_catch.v.p.ptr) != NULL); v_free(&v_catch); break; case kwTYPE_EOC: case kwTYPE_LINE: break; default: rt_raise(ERR_INVALID_CATCH); break; } return caught; }
/* * execute the 'index' procedure */ int sblib_proc_exec(int index, int param_count, slib_par_t *params, var_t *retval) { int success = 0; int handle; switch ( index ) { case 0: // GDBM_CLOSE handle success = mod_parint(0, params, param_count, &handle); if ( success ) { if ( is_valid_handle(handle) ) { gdbm_close(table[handle].dbf); table[handle].dbf = NULL; success = 1; } else { success = 0; v_setstr(retval, "GDBM_CLOSE: INVALID HANDLE"); } } else v_setstr(retval, "GDBM_CLOSE: argument error"); break; case 1: // GDBM_SYNC handle success = mod_parint(0, params, param_count, &handle); if ( success ) { if ( is_valid_handle(handle) ) { gdbm_sync(table[handle].dbf); success = 1; } else { success = 0; v_setstr(retval, "GDBM_SYNC: INVALID HANDLE"); } } else v_setstr(retval, "GDBM_SYNC: argument error"); break; default: v_setstr(retval, "GDBM: procedure does not exist!"); } return success; }
/* * set the value of 'var' to 'itable' string array */ void v_setstrarray(var_t *var, char **ctable, int count) { int i; var_t *elem_p; v_toarray1(var, count); for (i = 0; i < count; i++) { elem_p = (var_t *)(var->v.a.ptr + sizeof(var_t) * i); v_setstr(elem_p, ctable[i]); } }
/* *set the value of 'var' to string */ void v_setstrf(var_t *var, const char *fmt, ...) { char *buf; va_list ap; va_start(ap, fmt); buf = tmp_alloc(1024); vsnprintf(buf, 1024, fmt, ap); v_setstr(var, buf); tmp_free(buf); va_end(ap); }
/* * set the value of 'var' to string */ void v_setstrf(var_t *var, const char *fmt, ...) { char *buf; va_list ap; va_start(ap, fmt); #if defined(OS_LIMITED) buf = tmp_alloc(1024); #else buf = tmp_alloc(0x10000); #endif vsnprintf(buf, 1024, fmt, ap); v_setstr(var, buf); tmp_free(buf); va_end(ap); }
/* * execute the 'index' function */ int sblib_func_exec(int index, int param_count, slib_par_t *params, var_t *retval) { int success = 0; switch ( index ) { case 0: // handle <- GDBM_OPEN(file[, block_size, flags, mode]) { char *file; int bsize, flags, mode; success = mod_parstr_ptr(0, params, param_count, &file); success = mod_opt_parint(1, params, param_count, &bsize, 0); success = mod_opt_parint(2, params, param_count, &flags, GDBM_WRCREAT); success = mod_opt_parint(3, params, param_count, &mode, 0666); if ( success ) { int handle; handle = get_free_handle(); if ( handle >= 0 ) { table[handle].dbf = gdbm_open(file, bsize, flags, mode, NULL); success = (table[handle].dbf != NULL); if ( success ) v_setint(retval, handle); else v_setstr(retval, gdbm_strerror(gdbm_errno)); } else { success = 0; v_setstr(retval, "GDBM_OPEN: NO FREE HANDLES"); } } else v_setstr(retval, "GDBM_OPEN: argument error"); } break; case 1: // handle <- GDBM_STORE(handle, key, data [, flags]) { int handle, flags; char *key, *data; success = mod_parint (0, params, param_count, &handle); success = mod_parstr_ptr(1, params, param_count, &key); success = mod_parstr_ptr(2, params, param_count, &data); success = mod_opt_parint(3, params, param_count, &flags, GDBM_REPLACE); if ( success ) { if ( is_valid_handle(handle) ) { datum dt_key, dt_data; int r; dt_key.dptr = key; dt_key.dsize = strlen(key) + 1; dt_data.dptr = data; dt_data.dsize = strlen(data) + 1; r = gdbm_store(table[handle].dbf, dt_key, dt_data, flags); v_setint(retval, r); success = 1; } else { success = 0; v_setstr(retval, "GDBM_STORE: INVALID HANDLE"); } } else v_setstr(retval, "GDBM_STORE: argument error"); } break; case 2: // data <- GDBM_FETCH(handle, key) { int handle; char *key; success = mod_parint (0, params, param_count, &handle); success = mod_parstr_ptr(1, params, param_count, &key); if ( success ) { if ( is_valid_handle(handle) ) { datum dt_key, dt_data; dt_key.dptr = key; dt_key.dsize = strlen(key) + 1; dt_data = gdbm_fetch(table[handle].dbf, dt_key); v_setstr(retval, (char *) dt_data.dptr); success = 1; } else { success = 0; v_setstr(retval, "GDBM_FETCH: INVALID HANDLE"); } } else v_setstr(retval, "GDBM_FETCH: argument error"); } break; case 3: // status <- GDBM_DELETE(handle, key) { int handle; char *key; success = mod_parint (0, params, param_count, &handle); success = mod_parstr_ptr(1, params, param_count, &key); if ( success ) { if ( is_valid_handle(handle) ) { datum dt_key; int r; dt_key.dptr = key; dt_key.dsize = strlen(key) + 1; r = gdbm_delete(table[handle].dbf, dt_key); v_setint(retval, r); success = 1; } else { success = 0; v_setstr(retval, "GDBM_DELETE: INVALID HANDLE"); } } else v_setstr(retval, "GDBM_DELETE: argument error"); } break; case 4: // key <- GDBM_FIRSTKEY(handle) { int handle; success = mod_parint (0, params, param_count, &handle); if ( success ) { if ( is_valid_handle(handle) ) { datum dt_key; dt_key = gdbm_firstkey(table[handle].dbf); v_setstr(retval, (char *) dt_key.dptr); success = 1; } else { success = 0; v_setstr(retval, "GDBM_FIRSTKEY: INVALID HANDLE"); } } else v_setstr(retval, "GDBM_FIRSTKEY: argument error"); } break; case 5: // key <- GDBM_NEXTKEY(handle, key) { int handle; char *key; success = mod_parint (0, params, param_count, &handle); success = mod_parstr_ptr(1, params, param_count, &key); if ( success ) { if ( is_valid_handle(handle) ) { datum dt_key; dt_key.dptr = key; dt_key.dsize = strlen(key) + 1; dt_key = gdbm_nextkey(table[handle].dbf, dt_key); v_setstr(retval, (char *) dt_key.dptr); success = 1; } else { success = 0; v_setstr(retval, "GDBM_NEXTKEY: INVALID HANDLE"); } } else v_setstr(retval, "GDBM_NEXTKEY: argument error"); } break; case 6: // status <- GDBM_REORGANIZE(handle) { int handle; success = mod_parint (0, params, param_count, &handle); if ( success ) { if ( is_valid_handle(handle) ) { int r; r = gdbm_reorganize(table[handle].dbf); v_setint(retval, r); success = 1; } else { success = 0; v_setstr(retval, "GDBM_REORGANIZE: INVALID HANDLE"); } } else v_setstr(retval, "GDBM_REORGANIZE: argument error"); } break; case 7: // status <- GDBM_EXISTS(handle, key) { int handle; char *key; success = mod_parint (0, params, param_count, &handle); success = mod_parstr_ptr(1, params, param_count, &key); if ( success ) { if ( is_valid_handle(handle) ) { datum dt_key; int r; dt_key.dptr = key; dt_key.dsize = strlen(key) + 1; r = gdbm_exists(table[handle].dbf, dt_key); v_setint(retval, r); success = 1; } else { success = 0; v_setstr(retval, "GDBM_EXISTS: INVALID HANDLE"); } } else v_setstr(retval, "GDBM_EXISTS: argument error"); } break; case 8: // str <- GDBM_STRERROR() v_setstr(retval, gdbm_strerror(gdbm_errno)); break; case 9: // status <- GDBM_SETOPT(handle, option, value, size) { int handle, option, value, size; success = mod_parint (0, params, param_count, &handle); success = mod_parint (1, params, param_count, &option); success = mod_parint (2, params, param_count, &value); success = mod_parint (3, params, param_count, &size); if ( success ) { if ( is_valid_handle(handle) ) { int r; r = gdbm_setopt(table[handle].dbf, option, &value, size); v_setint(retval, r); success = 1; } else { success = 0; v_setstr(retval, "GDBM_SETOPT: INVALID HANDLE"); } } else v_setstr(retval, "GDBM_SETOPT: argument error"); } break; case 10: // status <- GDBM_FDESC(handle) { int handle; success = mod_parint (0, params, param_count, &handle); if ( success ) { if ( is_valid_handle(handle) ) { int r; r = gdbm_fdesc(table[handle].dbf); v_setint(retval, r); success = 1; } else { success = 0; v_setstr(retval, "GDBM_FDESC: INVALID HANDLE"); } } else v_setstr(retval, "GDBM_FDESC: argument error"); } break; default: v_setstr(retval, "GDBM: function does not exist!"); } return success; }