First cut at sql support via sqlite3. Including preliminary varargs support

for internal functions, and returning values from functions in the compiler.
This commit is contained in:
2007-12-24 11:18:15 +00:00
parent 8251351a2e
commit df2e38a3e9
13 changed files with 207 additions and 19 deletions

View File

@@ -3,9 +3,9 @@
PREFIX?= /usr/local
OBJS= main.o vm.o plugins.o dmx.o midi.o beatdetect.o fft.o map3d.o mouse.o \
cmdsocket.o
cmdsocket.o sql.o
SRCS= main.c vm.c plugins.c dmx.c midi.c beatdetect.c fft.c map3d.c mouse.c \
cmdsocket.c
cmdsocket.c sql.c
OBJS+= abi.o
SRCS+= abi.c
@@ -14,10 +14,13 @@ COMMONOBJS= mem.o hash.o
COMMONDIR= ../common
INCDIR= ../include
SQLITEINCDIR= /usr/pkg/include
SQLITELIBDIR= /usr/pkg/lib
CFLAGS+= -Wall -Werror
CPPFLAGS+= -I${INCDIR}
LDLIBS+= -lm
CPPFLAGS+= -I${INCDIR} -I${SQLITEINCDIR}
LDFLAGS+= -L${SQLITELIBDIR} -Wl,-R${SQLITELIBDIR}
LDLIBS+= -lm -lsqlite3
PROGOBJS= ${OBJS} ${COMMONOBJS:S/^/${COMMONDIR}\//}

View File

@@ -49,3 +49,5 @@ int vm_intfn_cmdsocket_accept(void);
int vm_intfn_cmdsocket_read(void);
int vm_intfn_cmdsocket_write(void);
int vm_intfn_cmdsocket_prefix(void);
int vm_intfn_sql_query(void);
int vm_intfn_sql_query_1s(void);

View File

@@ -38,6 +38,9 @@ function cmdsocket_accept
function cmdsocket_read
function cmdsocket_write
function cmdsocket_prefix
/* NOT_YET function_v sql_query */
function sql_query
function sql_query_1s
/*
* The ABI should be identified by a SHA1 hash of this file

View File

@@ -37,6 +37,13 @@ do_function()
FNCOUNT=$((${FNCOUNT}+1))
}
do_function_v()
{
echo " vm_intfn_${ARG1}," >>${ABI}
echo "fndefint_v ${ARG1} ${FNCOUNT};" >>${HEADER}
FNCOUNT=$((${FNCOUNT}+1))
}
cat <<EOF >${ABI}
/* abi.c */
/* autogenerated - do not edit */
@@ -69,6 +76,8 @@ do
case "$TYPE" in
function) do_function
;;
function_v) do_function_v
;;
esac
done <${INFILE}

View File

@@ -10,7 +10,7 @@
#include <stdio.h>
#include "vm.h"
#define MOUSEDEVICE "/dev/wsmouse"
#define MOUSEDEVICE "/dev/wsmouse1"
char *events[] = {
"undefined",

View File

@@ -12,6 +12,7 @@
#include "beatdetect.h"
#include "mouse.h"
#include "cmdsocket.h"
#include "sql.h"
struct plugin plugins_table[] = {
{"midi", midi_init, midi_close, 0},
@@ -19,6 +20,7 @@ struct plugin plugins_table[] = {
{"beatdetect", beatdetect_init, beatdetect_close, 0},
{"mouse", mouse_init, mouse_close, 0},
{"cmdsocket", cmdsocket_init, cmdsocket_close, 0},
{"sql", sql_init, sql_close, 0},
};
int nplugins = (sizeof(plugins_table) / sizeof(struct plugin));

View File

@@ -23,6 +23,7 @@
#include "map3d.h"
#include "mouse.h"
#include "cmdsocket.h"
#include "sql.h"
#define DEBUG 0
@@ -482,6 +483,121 @@ int vm_intfn_cmdsocket_write(void)
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;
}
#endif
int vm_intfn_beatdetect_read(void)
{
if (!beatdetect_read()) {