The unified diff between revisions [43bb367e..] and [4029749b..] is displayed below. It can also be downloaded as a raw diff.
This diff has been restricted to the following files: 'src/lsi/vm.c'
# # old_revision [43bb367e69d2a944206fd7f641ce73111e4bb780] # new_revision [4029749b9ead646e788e96bfd20e494a8bf99f61] # # patch "src/lsi/vm.c" # from [f74e79e92252eafbcbc81d3ee4ca5e2e3c79f4a9] # to [6f62d2db74a2a4932bd876c11abeab092af42a19] # ============================================================ --- src/lsi/vm.c f74e79e92252eafbcbc81d3ee4ca5e2e3c79f4a9 +++ src/lsi/vm.c 6f62d2db74a2a4932bd876c11abeab092af42a19 @@ -23,6 +23,7 @@ #include "map3d.h" #include "mouse.h" #include "cmdsocket.h" +#include "sql.h" #define DEBUG 0 @@ -464,6 +465,241 @@ int vm_intfn_cmdsocket_read(void) return 1; } +int vm_intfn_cmdsocket_write(void) +{ + int off = stack_get(vm_current, 1); + int len = stack_get(vm_current, 2); + char *buffer = stack_getstr(vm_current, len, 2); + int newoff; + + newoff = cmdsocket_write(buffer, len, off); + stack_poke(vm_current, 1, newoff); + + if (newoff != len) { + vm_queue(vm_current, VM_CMDWRITEQ); + vm_current = NULL; + return 0; + } + return 1; +} + +#ifdef NOT_YET + +#define MIN(a, b) ((a < b) ? a : b) + +#define MORE_QUERY_N(x, n) do { \ + off = strncpy(buf+off, (x), MIN(VM_STRING_MAX-off, (n)) - buf;\ + if (off >= VM_STRING_MAX) { \ + printf("Excessive string length - can't perform query\n"); \ + return 1; \ + } \ + } while (0) + +#define MORE_QUERY(x) MORE_QUERY_N((x), VM_STRING_MAX) + +int vm_intfn_sql_query(void) +{ + int nargs = stack_get(vm_current, 1); + int len = stack_get(vm_current, nargs+1); + char *fmt = stack_getstr(vm_current, len, nargs+1); + char buf1[VM_STRING_MAX]; + char buf2[VM_STRING_MAX]; + char buf[VM_STRING_MAX]; + int off = 0; + int result; + int next; + char *p, *p1; + + if (len > VM_STRING_MAX) { + printf("Excessive string length - can't perform query\n"); + return 1; + } + strncpy(buf1, fmt, len); + buf1[len] = '\0'; + + p = buf1; + while (p1 = strchr(p, '%') { + if (p1) { + *p1 = 0; + } + if (p != buf1) { + switch(*p) { + case '%': + MORE_QUERY("%"); + break; + case 's': + next = (len + sizeof(stkentry)-1) / sizeof(stkentry) + 1; + + MORE_QUERY_N(stack_getstr(vm_current, slen, nargs)); + } + } + MORE_QUERY(p); + if (p1) + p = p1; + else + break; + } + + sql_query(buffer, len, &result); + /* XXX what to do with an error here? */ + stack_poke(vm_current, 0, result); /* return value */ + return 1; +} +#else +int vm_intfn_sql_query(void) +{ + int len = stack_get(vm_current, 1); + char *query = stack_getstr(vm_current, len, 1); + int result; + + sql_query(query, len, &result); + /* XXX what to do with an error here? */ + stack_poke(vm_current, 0, result); /* return value */ + return 1; +} + +int vm_intfn_sql_query_1s(void) +{ + int len1, len2; + char buf1[VM_STRING_MAX]; + char buf2[VM_STRING_MAX]; + char query[VM_STRING_MAX]; + char *arg1; + char *arg2; + int result; + int next; + + next = 1; + + len2 = stack_get(vm_current, next); + arg2 = stack_getstr(vm_current, len2, next); + + next += (len2 + sizeof(stkentry)-1) / sizeof(stkentry) + 1; + + len1 = stack_get(vm_current, next); + arg1 = stack_getstr(vm_current, len1, next); + + if ((len1 > VM_STRING_MAX) || (len2 > VM_STRING_MAX)) { + printf("Excessive string length - can't perform query\n"); + return 1; + } + strncpy(buf1, arg1, len1); + buf1[len1] = '\0'; + strncpy(buf2, arg2, len2); + buf2[len2] = '\0'; + + snprintf(query, VM_STRING_MAX, buf1, buf2); + + sql_query(query, strlen(query), &result); + + /* XXX what to do with an error here? */ + stack_poke(vm_current, 0, result); /* return value */ + return 1; +} + +int vm_intfn_sql_getvar(void) +{ + int len; + char buf[VM_STRING_MAX]; + char query[VM_STRING_MAX]; + int result; + + len = stack_get(vm_current, 1); + + if (len >= VM_STRING_MAX) { + printf("Excessive string length - can't perform query\n"); + return 1; + } + + snprintf(buf, len+1, stack_getstr(vm_current, len, 1)); + snprintf(query, VM_STRING_MAX, "SELECT value FROM vars WHERE name=\"%s\"", buf); + + if (sql_query(query, strlen(query), &result) != 2) + result = -1; + + /* XXX what to do with an error here? */ + stack_poke(vm_current, 0, result); /* return value */ + return 1; +} + +int vm_intfn_sql_getvar_array(void) +{ + int len, off; + char buf[VM_STRING_MAX]; + char query[VM_STRING_MAX]; + int result; + + off = stack_get(vm_current, 1); + len = stack_get(vm_current, 2); + + if (len >= VM_STRING_MAX) { + printf("Excessive string length - can't perform query\n"); + return 1; + } + + snprintf(buf, len+1, stack_getstr(vm_current, len, 2)); + snprintf(query, VM_STRING_MAX, "SELECT value FROM vars WHERE name=\"%s[%d]\"", buf, off); + + if (sql_query(query, strlen(query), &result) != 2) + result = -1; + + /* XXX what to do with an error here? */ + stack_poke(vm_current, 0, result); /* return value */ + return 1; +} + +int vm_intfn_sql_setvar(void) +{ + int len, val; + char buf[VM_STRING_MAX]; + char query[VM_STRING_MAX]; + int result; + + val = stack_get(vm_current, 1); + len = stack_get(vm_current, 2); + + if (len >= VM_STRING_MAX) { + printf("Excessive string length - can't perform query\n"); + return 1; + } + + snprintf(buf, len+1, stack_getstr(vm_current, len, 2)); + snprintf(query, VM_STRING_MAX, "INSERT OR REPLACE INTO vars VALUES(\"%s\", %d)", buf, val); + + sql_query(query, strlen(query), &result); + + /* XXX what to do with an error here? */ + stack_poke(vm_current, 0, result); /* return value */ + return 1; +} + +int vm_intfn_sql_setvar_array(void) +{ + int len, off, val; + char buf[VM_STRING_MAX]; + char query[VM_STRING_MAX]; + int result; + + val = stack_get(vm_current, 1); + off = stack_get(vm_current, 2); + len = stack_get(vm_current, 3); + + if (len >= VM_STRING_MAX) { + printf("Excessive string length - can't perform query\n"); + return 1; + } + + snprintf(buf, len+1, stack_getstr(vm_current, len, 3)); + snprintf(query, VM_STRING_MAX, "INSERT OR REPLACE INTO vars VALUES(\"%s[%d]\", %d)", buf, off, val); + + sql_query(query, strlen(query), &result); + + /* XXX what to do with an error here? */ + stack_poke(vm_current, 0, result); /* return value */ + return 1; +} +#endif + int vm_intfn_beatdetect_read(void) { if (!beatdetect_read()) { @@ -625,7 +861,9 @@ void vm_load_file(char *filename) char *ptr; fh = fopen(filename, "r"); - assert(fh != NULL); + if (fh == NULL) { + err(1, "Failed to open '%s'", filename); + } for (ptr = vm_codearea; ptr < vm_codearea+vm_codesize; ptr++) { int c; @@ -651,16 +889,19 @@ void vm_init_functions(void) struct hashentry *ptr; /* First, let's check the magic */ - assert(vm_codearea[0] == MAGIC1); - assert(vm_codearea[1] == MAGIC2); - assert(vm_codearea[2] == VERSION1); - assert(vm_codearea[3] == VERSION2); + if ((vm_codearea[0] != MAGIC1) || + (vm_codearea[1] != MAGIC2)) + errx(1, "Bad magic - not a lightscript binary"); + if ((vm_codearea[2] != VERSION1) || + (vm_codearea[3] != VERSION2)) + errx(1, "Bad version - recompile"); - assert(GETINT(vm_codearea, 8) == vm_abiversion1); - assert(GETINT(vm_codearea, 12) == vm_abiversion2); - assert(GETINT(vm_codearea, 16) == vm_abiversion3); - assert(GETINT(vm_codearea, 20) == vm_abiversion4); - assert(GETINT(vm_codearea, 24) == vm_abiversion5); + if ((GETINT(vm_codearea, 8) != vm_abiversion1) || + (GETINT(vm_codearea, 12) == vm_abiversion2) || + (GETINT(vm_codearea, 16) == vm_abiversion3) || + (GETINT(vm_codearea, 20) == vm_abiversion4) || + (GETINT(vm_codearea, 24) == vm_abiversion5)) + errx(1, "Incompatible ABI version - recompile"); /* Now, get the function table pointer */ t = GETINT(vm_codearea, 4); @@ -750,16 +991,20 @@ int vm_spawn_args(char *fn, int n, ...) return 0; } newt->sp = newt->stackbase; - /* Push return address here, to point to some special thread exit - routine */ + stack_push(newt, 0); /* Return value */ + /* Push optional arguments */ va_start(ap, n); - while (n--) - stack_push(newt, va_arg(ap, int)); + while (n--) { + int a = va_arg(ap, int); +// printf("arg is %d\n", a); + stack_push(newt, a); + } va_end(ap); - stack_push(newt, 0); /* Return value */ + /* Push return address here, to point to some special thread exit + routine */ stack_push(newt, 0); /* Return address */ /* Insert into head of run queue */