Importing NetBSD "Kyua" test framework
To do so, a few dependencies have been imported: * external/bsd/lutok * external/mit/lua * external/public-domain/sqlite * external/public-domain/xz The Kyua framework is the new generation of ATF (Automated Test Framework), it is composed of: * external/bsd/atf * external/bsd/kyua-atf-compat * external/bsd/kyua-cli * external/bsd/kyua-tester * tests Kyua/ATF being written in C++, it depends on libstdc++ which is provided by GCC. As this is not part of the sources, Kyua is only compiled when the native GCC utils are installed. To install Kyua do the following: * In a cross-build enviromnent, add the following to the build.sh commandline: -V MKBINUTILS=yes -V MKGCCCMDS=yes WARNING: At this point the import is still experimental, and not supported on native builds (a.k.a make build). Change-Id: I26aee23c5bbd2d64adcb7c1beb98fe0d479d7ada
This commit is contained in:
10
lib/lua/Makefile
Normal file
10
lib/lua/Makefile
Normal file
@@ -0,0 +1,10 @@
|
||||
# $NetBSD: Makefile,v 1.5 2011/11/08 01:52:05 joerg Exp $
|
||||
|
||||
.include <bsd.own.mk>
|
||||
|
||||
.if ${MKPIC} != "no"
|
||||
# No support for shared libraries and pic code.
|
||||
SUBDIR+= gpio sqlite
|
||||
.endif
|
||||
|
||||
.include <bsd.subdir.mk>
|
||||
3
lib/lua/Makefile.inc
Normal file
3
lib/lua/Makefile.inc
Normal file
@@ -0,0 +1,3 @@
|
||||
# $NetBSD: Makefile.inc,v 1.1 2011/10/11 07:10:15 plunky Exp $
|
||||
|
||||
WARNS?= 4
|
||||
6
lib/lua/gpio/Makefile
Normal file
6
lib/lua/gpio/Makefile
Normal file
@@ -0,0 +1,6 @@
|
||||
# $NetBSD: Makefile,v 1.3 2011/10/11 07:10:15 plunky Exp $
|
||||
|
||||
LUA_MODULES= gpio
|
||||
LUA_SRCS.gpio= gpio.c
|
||||
|
||||
.include <bsd.lua.mk>
|
||||
316
lib/lua/gpio/gpio.c
Normal file
316
lib/lua/gpio/gpio.c
Normal file
@@ -0,0 +1,316 @@
|
||||
/* $NetBSD: gpio.c,v 1.7 2012/03/15 02:02:21 joerg Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2011 Marc Balmer <marc@msys.ch>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* GPIO interface for Lua */
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
|
||||
#include <sys/gpio.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#define GPIO_METATABLE "GPIO object methods"
|
||||
|
||||
static __printflike(2, 3) void
|
||||
gpio_error(lua_State *L, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int len;
|
||||
char *msg;
|
||||
|
||||
va_start(ap, fmt);
|
||||
len = vasprintf(&msg, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
if (len != -1) {
|
||||
lua_pushstring(L, msg);
|
||||
free(msg);
|
||||
} else
|
||||
lua_pushstring(L, "vasprintf failed");
|
||||
lua_error(L);
|
||||
}
|
||||
|
||||
static int
|
||||
gpio_open(lua_State *L)
|
||||
{
|
||||
int *fd;
|
||||
|
||||
fd = lua_newuserdata(L, sizeof(int));
|
||||
*fd = open(luaL_checkstring(L, -2), O_RDWR);
|
||||
if (*fd == -1) {
|
||||
gpio_error(L, "%s", strerror(errno));
|
||||
/* NOTREACHED */
|
||||
return 0;
|
||||
}
|
||||
luaL_getmetatable(L, GPIO_METATABLE);
|
||||
lua_setmetatable(L, -2);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
gpio_close(lua_State *L)
|
||||
{
|
||||
int *fd;
|
||||
|
||||
fd = luaL_checkudata(L, 1, GPIO_METATABLE);
|
||||
if (*fd != -1) {
|
||||
close(*fd);
|
||||
*fd = -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
gpio_info(lua_State *L)
|
||||
{
|
||||
struct gpio_info info;
|
||||
int *fd;
|
||||
|
||||
fd = luaL_checkudata(L, 1, GPIO_METATABLE);
|
||||
if (ioctl(*fd, GPIOINFO, &info) == -1)
|
||||
gpio_error(L, "GPIOINFO");
|
||||
lua_pushinteger(L, info.gpio_npins);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
gpio_get_pin(lua_State *L, int n, struct gpio_req *req)
|
||||
{
|
||||
switch (lua_type(L, n)) {
|
||||
case LUA_TNUMBER:
|
||||
req->gp_pin = (int)lua_tointeger(L, n); /* not 1 based! */
|
||||
break;
|
||||
case LUA_TSTRING:
|
||||
strlcpy(req->gp_name, lua_tostring(L, n), sizeof(req->gp_name));
|
||||
break;
|
||||
default:
|
||||
luaL_argerror(L, n, "expected string or integer");
|
||||
/* NOTREACHED */
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
gpio_set(lua_State *L)
|
||||
{
|
||||
struct gpio_set set;
|
||||
int *fd;
|
||||
|
||||
fd = luaL_checkudata(L, 1, GPIO_METATABLE);
|
||||
memset(&set, 0, sizeof(set));
|
||||
gpio_get_pin(L, 2, (void *)&set);
|
||||
set.gp_flags = (int)luaL_checkinteger(L, 3);
|
||||
if (ioctl(*fd, GPIOSET, &set) == -1)
|
||||
gpio_error(L, "GPIOSET");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
gpio_unset(lua_State *L)
|
||||
{
|
||||
struct gpio_set set;
|
||||
int *fd;
|
||||
|
||||
fd = luaL_checkudata(L, 1, GPIO_METATABLE);
|
||||
memset(&set, 0, sizeof(set));
|
||||
gpio_get_pin(L, 2, (void *)&set);
|
||||
if (ioctl(*fd, GPIOUNSET, &set) == -1)
|
||||
gpio_error(L, "GPIOUNSET");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
gpio_read(lua_State *L)
|
||||
{
|
||||
struct gpio_req req;
|
||||
int *fd;
|
||||
|
||||
fd = luaL_checkudata(L, 1, GPIO_METATABLE);
|
||||
memset(&req, 0, sizeof(req));
|
||||
gpio_get_pin(L, 2, &req);
|
||||
if (ioctl(*fd, GPIOREAD, &req) == -1)
|
||||
gpio_error(L, "GPIOREAD");
|
||||
lua_pushinteger(L, req.gp_value);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
gpio_write(lua_State *L)
|
||||
{
|
||||
struct gpio_req req;
|
||||
int *fd, val;
|
||||
|
||||
fd = luaL_checkudata(L, 1, GPIO_METATABLE);
|
||||
val = (int)luaL_checkinteger(L, 3);
|
||||
if (val != GPIO_PIN_HIGH && val != GPIO_PIN_LOW)
|
||||
gpio_error(L, "%d: invalid value", val);
|
||||
memset(&req, 0, sizeof(req));
|
||||
gpio_get_pin(L, 2, &req);
|
||||
req.gp_value = val;
|
||||
if (ioctl(*fd, GPIOWRITE, &req) == -1)
|
||||
gpio_error(L, "GPIOWRITE");
|
||||
lua_pushinteger(L, req.gp_value);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
gpio_toggle(lua_State *L)
|
||||
{
|
||||
struct gpio_req req;
|
||||
int *fd;
|
||||
|
||||
fd = luaL_checkudata(L, 1, GPIO_METATABLE);
|
||||
memset(&req, 0, sizeof(req));
|
||||
gpio_get_pin(L, 2, &req);
|
||||
if (ioctl(*fd, GPIOTOGGLE, &req) == -1)
|
||||
gpio_error(L, "GPIOTOGGLE");
|
||||
lua_pushinteger(L, req.gp_value);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
gpio_attach(lua_State *L)
|
||||
{
|
||||
struct gpio_attach attach;
|
||||
int *fd;
|
||||
|
||||
fd = luaL_checkudata(L, 1, GPIO_METATABLE);
|
||||
memset(&attach, 0, sizeof(attach));
|
||||
strlcpy(attach.ga_dvname, luaL_checkstring(L, 2),
|
||||
sizeof(attach.ga_dvname));
|
||||
attach.ga_offset = (int)luaL_checkinteger(L, 3);
|
||||
attach.ga_mask = (int)luaL_checkinteger(L, 4);
|
||||
if (lua_gettop(L) > 4)
|
||||
attach.ga_flags = (int)luaL_checkinteger(L, 5);
|
||||
else
|
||||
attach.ga_flags = 0;
|
||||
|
||||
if (ioctl(*fd, GPIOATTACH, &attach) == -1)
|
||||
gpio_error(L, "GPIOATTACH");
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct constant {
|
||||
const char *name;
|
||||
int value;
|
||||
};
|
||||
|
||||
static const struct constant gpio_constant[] = {
|
||||
/* GPIO pin states */
|
||||
{ "PIN_LOW", GPIO_PIN_LOW },
|
||||
{ "PIN_HIGH", GPIO_PIN_HIGH },
|
||||
|
||||
/* GPIO pin configuration flags */
|
||||
{ "PIN_INPUT", GPIO_PIN_INPUT },
|
||||
{ "PIN_OUTPUT", GPIO_PIN_OUTPUT },
|
||||
{ "PIN_INOUT", GPIO_PIN_INOUT },
|
||||
{ "PIN_OPENDRAIN", GPIO_PIN_OPENDRAIN },
|
||||
{ "PIN_PUSHPULL", GPIO_PIN_PUSHPULL },
|
||||
{ "PIN_TRISTATE", GPIO_PIN_TRISTATE },
|
||||
{ "PIN_PULLUP", GPIO_PIN_PULLUP },
|
||||
{ "PIN_PULLDOWN", GPIO_PIN_PULLDOWN },
|
||||
{ "PIN_INVIN", GPIO_PIN_INVIN },
|
||||
{ "PIN_INVOUT", GPIO_PIN_INVOUT },
|
||||
{ "PIN_USER", GPIO_PIN_USER },
|
||||
{ "PIN_PULSATE", GPIO_PIN_PULSATE },
|
||||
{ "PIN_SET", GPIO_PIN_SET },
|
||||
{ NULL, 0 }
|
||||
};
|
||||
|
||||
static void
|
||||
gpio_set_info(lua_State *L)
|
||||
{
|
||||
lua_pushliteral(L, "_COPYRIGHT");
|
||||
lua_pushliteral(L, "Copyright (C) 2011 Marc Balmer <marc@msys.ch>");
|
||||
lua_settable(L, -3);
|
||||
lua_pushliteral(L, "_DESCRIPTION");
|
||||
lua_pushliteral(L, "GPIO interface for Lua");
|
||||
lua_settable(L, -3);
|
||||
lua_pushliteral(L, "_VERSION");
|
||||
lua_pushliteral(L, "gpio 1.0.1");
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
|
||||
int luaopen_gpio(lua_State*);
|
||||
|
||||
int
|
||||
luaopen_gpio(lua_State* L)
|
||||
{
|
||||
static const struct luaL_Reg methods[] = {
|
||||
{ "open", gpio_open },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
static const struct luaL_Reg gpio_methods[] = {
|
||||
{ "info", gpio_info },
|
||||
{ "close", gpio_close },
|
||||
{ "set", gpio_set },
|
||||
{ "unset", gpio_unset },
|
||||
{ "read", gpio_read },
|
||||
{ "write", gpio_write },
|
||||
{ "toggle", gpio_toggle },
|
||||
{ "attach", gpio_attach },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
int n;
|
||||
|
||||
luaL_register(L, "gpio", methods);
|
||||
gpio_set_info(L);
|
||||
|
||||
/* The gpio metatable */
|
||||
if (luaL_newmetatable(L, GPIO_METATABLE)) {
|
||||
luaL_register(L, NULL, gpio_methods);
|
||||
|
||||
lua_pushliteral(L, "__gc");
|
||||
lua_pushcfunction(L, gpio_close);
|
||||
lua_settable(L, -3);
|
||||
|
||||
lua_pushliteral(L, "__index");
|
||||
lua_pushvalue(L, -2);
|
||||
lua_settable(L, -3);
|
||||
|
||||
lua_pushliteral(L, "__metatable");
|
||||
lua_pushliteral(L, "must not access this metatable");
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
|
||||
for (n = 0; gpio_constant[n].name != NULL; n++) {
|
||||
lua_pushinteger(L, gpio_constant[n].value);
|
||||
lua_setfield(L, -2, gpio_constant[n].name);
|
||||
};
|
||||
return 1;
|
||||
}
|
||||
9
lib/lua/sqlite/Makefile
Normal file
9
lib/lua/sqlite/Makefile
Normal file
@@ -0,0 +1,9 @@
|
||||
# $NetBSD: Makefile,v 1.3 2012/03/03 11:32:14 mbalmer Exp $
|
||||
|
||||
LUA_MODULES= sqlite
|
||||
LUA_SRCS.sqlite= sqlite.c
|
||||
|
||||
LUA_DPLIBS+= sqlite3 \
|
||||
${.CURDIR}/../../../external/public-domain/sqlite/lib
|
||||
|
||||
.include <bsd.lua.mk>
|
||||
489
lib/lua/sqlite/sqlite.c
Normal file
489
lib/lua/sqlite/sqlite.c
Normal file
@@ -0,0 +1,489 @@
|
||||
/* $NetBSD: sqlite.c,v 1.5 2012/11/02 12:24:52 mbalmer Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2011 Marc Balmer <marc@msys.ch>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* SQLite interface for Lua */
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sqlite3.h>
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
|
||||
#define SQLITE_DB_METATABLE "SQLite database connection methods"
|
||||
#define SQLITE_STMT_METATABLE "SQLite statement methods"
|
||||
|
||||
int luaopen_sqlite(lua_State*);
|
||||
|
||||
static __printflike(2, 3) void
|
||||
sqlite_error(lua_State *L, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int len;
|
||||
char *msg;
|
||||
|
||||
va_start(ap, fmt);
|
||||
len = vasprintf(&msg, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
if (len != -1) {
|
||||
lua_pushstring(L, msg);
|
||||
free(msg);
|
||||
} else
|
||||
lua_pushstring(L, "vasprintf failed");
|
||||
lua_error(L);
|
||||
}
|
||||
|
||||
static int
|
||||
sqlite_initialize(lua_State *L)
|
||||
{
|
||||
lua_pushinteger(L, sqlite3_initialize());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
sqlite_shutdown(lua_State *L)
|
||||
{
|
||||
lua_pushinteger(L, sqlite3_shutdown());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
sqlite_open(lua_State *L)
|
||||
{
|
||||
sqlite3 **db;
|
||||
|
||||
db = lua_newuserdata(L, sizeof(sqlite3 *));
|
||||
lua_pushinteger(L, sqlite3_open_v2(luaL_checkstring(L, -3), db,
|
||||
(int)luaL_checkinteger(L, -2), NULL));
|
||||
|
||||
luaL_getmetatable(L, SQLITE_DB_METATABLE);
|
||||
lua_setmetatable(L, -3);
|
||||
return 2;
|
||||
|
||||
}
|
||||
|
||||
static int
|
||||
sqlite_libversion(lua_State *L)
|
||||
{
|
||||
lua_pushstring(L, sqlite3_libversion());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
sqlite_libversion_number(lua_State *L)
|
||||
{
|
||||
lua_pushinteger(L, sqlite3_libversion_number());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
sqlite_sourceid(lua_State *L)
|
||||
{
|
||||
lua_pushstring(L, sqlite3_sourceid());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
db_close(lua_State *L)
|
||||
{
|
||||
sqlite3 **db;
|
||||
|
||||
db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
|
||||
lua_pushinteger(L, sqlite3_close(*db));
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
static int
|
||||
db_prepare(lua_State *L)
|
||||
{
|
||||
sqlite3 **db;
|
||||
sqlite3_stmt **stmt;
|
||||
const char *sql;
|
||||
|
||||
db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
|
||||
stmt = lua_newuserdata(L, sizeof(sqlite3_stmt *));
|
||||
sql = luaL_checkstring(L, 2);
|
||||
lua_pushinteger(L, sqlite3_prepare_v2(*db, sql,
|
||||
(int)strlen(sql) + 1, stmt, NULL));
|
||||
luaL_getmetatable(L, SQLITE_STMT_METATABLE);
|
||||
lua_setmetatable(L, -3);
|
||||
return 2;
|
||||
|
||||
}
|
||||
|
||||
static int
|
||||
db_exec(lua_State *L)
|
||||
{
|
||||
sqlite3 **db;
|
||||
|
||||
db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
|
||||
lua_pushinteger(L, sqlite3_exec(*db, lua_tostring(L, 2), NULL,
|
||||
NULL, NULL));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
db_errcode(lua_State *L)
|
||||
{
|
||||
sqlite3 **db;
|
||||
|
||||
db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
|
||||
lua_pushinteger(L, sqlite3_errcode(*db));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
db_errmsg(lua_State *L)
|
||||
{
|
||||
sqlite3 **db;
|
||||
|
||||
db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
|
||||
lua_pushstring(L, sqlite3_errmsg(*db));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
db_get_autocommit(lua_State *L)
|
||||
{
|
||||
sqlite3 **db;
|
||||
|
||||
db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
|
||||
lua_pushboolean(L, sqlite3_get_autocommit(*db));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
db_changes(lua_State *L)
|
||||
{
|
||||
sqlite3 **db;
|
||||
|
||||
db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
|
||||
lua_pushinteger(L, sqlite3_changes(*db));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
stmt_bind(lua_State *L)
|
||||
{
|
||||
sqlite3_stmt **stmt;
|
||||
int pidx;
|
||||
|
||||
stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
|
||||
pidx = (int)luaL_checkinteger(L, 2);
|
||||
|
||||
switch (lua_type(L, 3)) {
|
||||
case LUA_TNUMBER:
|
||||
lua_pushinteger(L, sqlite3_bind_double(*stmt, pidx,
|
||||
lua_tonumber(L, 3)));
|
||||
break;
|
||||
case LUA_TSTRING:
|
||||
lua_pushinteger(L, sqlite3_bind_text(*stmt, pidx,
|
||||
lua_tostring(L, 3), -1, SQLITE_TRANSIENT));
|
||||
break;
|
||||
case LUA_TNIL:
|
||||
lua_pushinteger(L, sqlite3_bind_null(*stmt, pidx));
|
||||
break;
|
||||
default:
|
||||
sqlite_error(L, "unsupported data type %s",
|
||||
luaL_typename(L, 3));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
stmt_bind_parameter_count(lua_State *L)
|
||||
{
|
||||
sqlite3_stmt **stmt;
|
||||
|
||||
stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
|
||||
lua_pushinteger(L, sqlite3_bind_parameter_count(*stmt));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
stmt_bind_parameter_index(lua_State *L)
|
||||
{
|
||||
sqlite3_stmt **stmt;
|
||||
|
||||
stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
|
||||
lua_pushinteger(L, sqlite3_bind_parameter_index(*stmt,
|
||||
lua_tostring(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
stmt_bind_parameter_name(lua_State *L)
|
||||
{
|
||||
sqlite3_stmt **stmt;
|
||||
int pidx;
|
||||
|
||||
stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
|
||||
pidx = (int)luaL_checkinteger(L, 2);
|
||||
lua_pushstring(L, sqlite3_bind_parameter_name(*stmt, pidx));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
stmt_step(lua_State *L)
|
||||
{
|
||||
sqlite3_stmt **stmt;
|
||||
|
||||
stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
|
||||
lua_pushinteger(L, sqlite3_step(*stmt));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
stmt_column_name(lua_State *L)
|
||||
{
|
||||
sqlite3_stmt **stmt;
|
||||
int cidx;
|
||||
|
||||
stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
|
||||
cidx = (int)luaL_checkinteger(L, 2) - 1;
|
||||
|
||||
lua_pushstring(L, sqlite3_column_name(*stmt, cidx));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
stmt_column_count(lua_State *L)
|
||||
{
|
||||
sqlite3_stmt **stmt;
|
||||
|
||||
stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
|
||||
lua_pushinteger(L, sqlite3_column_count(*stmt));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
stmt_column(lua_State *L)
|
||||
{
|
||||
sqlite3_stmt **stmt;
|
||||
int cidx;
|
||||
|
||||
stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
|
||||
cidx = (int)luaL_checkinteger(L, 2) - 1;
|
||||
|
||||
switch (sqlite3_column_type(*stmt, cidx)) {
|
||||
case SQLITE_INTEGER:
|
||||
lua_pushinteger(L, sqlite3_column_int(*stmt, cidx));
|
||||
break;
|
||||
case SQLITE_FLOAT:
|
||||
lua_pushnumber(L, sqlite3_column_double(*stmt, cidx));
|
||||
break;
|
||||
case SQLITE_TEXT:
|
||||
lua_pushstring(L, (const char *)sqlite3_column_text(*stmt,
|
||||
cidx));
|
||||
break;
|
||||
case SQLITE_BLOB:
|
||||
case SQLITE_NULL:
|
||||
lua_pushnil(L);
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
stmt_reset(lua_State *L)
|
||||
{
|
||||
sqlite3_stmt **stmt;
|
||||
|
||||
stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
|
||||
sqlite3_reset(*stmt);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
stmt_clear_bindings(lua_State *L)
|
||||
{
|
||||
sqlite3_stmt **stmt;
|
||||
|
||||
stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
|
||||
sqlite3_clear_bindings(*stmt);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
stmt_finalize(lua_State *L)
|
||||
{
|
||||
sqlite3_stmt **stmt;
|
||||
|
||||
stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
|
||||
sqlite3_finalize(*stmt);
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct constant {
|
||||
const char *name;
|
||||
int value;
|
||||
};
|
||||
|
||||
static const struct constant sqlite_constant[] = {
|
||||
/* SQLite return codes */
|
||||
{ "OK", SQLITE_OK },
|
||||
{ "ERROR", SQLITE_ERROR },
|
||||
{ "INTERNAL", SQLITE_INTERNAL },
|
||||
{ "PERM", SQLITE_PERM },
|
||||
{ "ABORT", SQLITE_ABORT },
|
||||
{ "BUSY", SQLITE_BUSY },
|
||||
{ "LOCKED", SQLITE_LOCKED },
|
||||
{ "NOMEM", SQLITE_NOMEM },
|
||||
{ "READONLY", SQLITE_READONLY },
|
||||
{ "INTERRUPT", SQLITE_INTERRUPT },
|
||||
{ "IOERR", SQLITE_IOERR },
|
||||
{ "CORRUPT", SQLITE_CORRUPT },
|
||||
{ "FULL", SQLITE_FULL },
|
||||
{ "CANTOPEN", SQLITE_CANTOPEN },
|
||||
{ "EMPTY", SQLITE_EMPTY },
|
||||
{ "SCHEMA", SQLITE_SCHEMA },
|
||||
{ "TOOBIG", SQLITE_TOOBIG },
|
||||
{ "CONSTRAINT", SQLITE_CONSTRAINT },
|
||||
{ "MISMATCH", SQLITE_MISMATCH },
|
||||
{ "MISUSE", SQLITE_MISUSE },
|
||||
{ "NOLFS", SQLITE_NOLFS },
|
||||
{ "AUTH", SQLITE_AUTH },
|
||||
{ "FORMAT", SQLITE_FORMAT },
|
||||
{ "RANGE", SQLITE_RANGE },
|
||||
{ "NOTADB", SQLITE_NOTADB },
|
||||
|
||||
{ "ROW", SQLITE_ROW },
|
||||
{ "DONE", SQLITE_DONE },
|
||||
|
||||
/* File modes */
|
||||
{ "OPEN_READONLY", SQLITE_OPEN_READONLY },
|
||||
{ "OPEN_READWRITE", SQLITE_OPEN_READWRITE },
|
||||
{ "OPEN_CREATE", SQLITE_OPEN_CREATE },
|
||||
|
||||
{ NULL, 0 }
|
||||
};
|
||||
|
||||
static void
|
||||
gpio_set_info(lua_State *L)
|
||||
{
|
||||
lua_pushliteral(L, "_COPYRIGHT");
|
||||
lua_pushliteral(L, "Copyright (C) 2011, 2012 by "
|
||||
"Marc Balmer <marc@msys.ch>");
|
||||
lua_settable(L, -3);
|
||||
lua_pushliteral(L, "_DESCRIPTION");
|
||||
lua_pushliteral(L, "SQLite interface for Lua");
|
||||
lua_settable(L, -3);
|
||||
lua_pushliteral(L, "_VERSION");
|
||||
lua_pushliteral(L, "sqlite 1.0.2");
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
|
||||
int
|
||||
luaopen_sqlite(lua_State* L)
|
||||
{
|
||||
static const struct luaL_Reg sqlite_methods[] = {
|
||||
{ "initialize", sqlite_initialize },
|
||||
{ "shutdown", sqlite_shutdown },
|
||||
{ "open", sqlite_open },
|
||||
{ "libversion", sqlite_libversion },
|
||||
{ "libversion_number", sqlite_libversion_number },
|
||||
{ "sourceid", sqlite_sourceid },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
static const struct luaL_Reg db_methods[] = {
|
||||
{ "close", db_close },
|
||||
{ "prepare", db_prepare },
|
||||
{ "exec", db_exec },
|
||||
{ "errcode", db_errcode },
|
||||
{ "errmsg", db_errmsg },
|
||||
{ "get_autocommit", db_get_autocommit },
|
||||
{ "changes", db_changes },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
static const struct luaL_Reg stmt_methods[] = {
|
||||
{ "bind", stmt_bind },
|
||||
{ "bind_parameter_count", stmt_bind_parameter_count },
|
||||
{ "bind_parameter_index", stmt_bind_parameter_index },
|
||||
{ "bind_parameter_name", stmt_bind_parameter_name },
|
||||
{ "step", stmt_step },
|
||||
{ "column", stmt_column },
|
||||
{ "reset", stmt_reset },
|
||||
{ "clear_bindings", stmt_clear_bindings },
|
||||
{ "finalize", stmt_finalize },
|
||||
{ "column_name", stmt_column_name },
|
||||
{ "column_count", stmt_column_count },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
int n;
|
||||
|
||||
sqlite3_initialize();
|
||||
|
||||
luaL_register(L, "sqlite", sqlite_methods);
|
||||
gpio_set_info(L);
|
||||
|
||||
/* The database connection metatable */
|
||||
if (luaL_newmetatable(L, SQLITE_DB_METATABLE)) {
|
||||
luaL_register(L, NULL, db_methods);
|
||||
|
||||
lua_pushliteral(L, "__gc");
|
||||
lua_pushcfunction(L, db_close);
|
||||
lua_settable(L, -3);
|
||||
|
||||
lua_pushliteral(L, "__index");
|
||||
lua_pushvalue(L, -2);
|
||||
lua_settable(L, -3);
|
||||
|
||||
lua_pushliteral(L, "__metatable");
|
||||
lua_pushliteral(L, "must not access this metatable");
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
|
||||
/* The statement metatable */
|
||||
if (luaL_newmetatable(L, SQLITE_STMT_METATABLE)) {
|
||||
luaL_register(L, NULL, stmt_methods);
|
||||
|
||||
lua_pushliteral(L, "__gc");
|
||||
lua_pushcfunction(L, stmt_finalize);
|
||||
lua_settable(L, -3);
|
||||
|
||||
lua_pushliteral(L, "__index");
|
||||
lua_pushvalue(L, -2);
|
||||
lua_settable(L, -3);
|
||||
|
||||
lua_pushliteral(L, "__metatable");
|
||||
lua_pushliteral(L, "must not access this metatable");
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
|
||||
for (n = 0; sqlite_constant[n].name != NULL; n++) {
|
||||
lua_pushinteger(L, sqlite_constant[n].value);
|
||||
lua_setfield(L, -2, sqlite_constant[n].name);
|
||||
};
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user