lua-apk.c 4.5 KB
Newer Older
Natanael Copa's avatar
Natanael Copa committed
1 2 3 4
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

Natanael Copa's avatar
Natanael Copa committed
5 6
#include "apk_blob.h"
#include "apk_database.h"
Natanael Copa's avatar
Natanael Copa committed
7 8 9 10
#include "apk_defines.h"
#include "apk_version.h"

#define LIBNAME "apk"
Natanael Copa's avatar
Natanael Copa committed
11
#define APKDB_META "apk_database"
Natanael Copa's avatar
Natanael Copa committed
12

Natanael Copa's avatar
Natanael Copa committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
struct flagmap {
       const char *name;
       int flag;
};

struct flagmap opendb_flagmap[] = {
       {"read",                APK_OPENF_READ},
       {"write",               APK_OPENF_WRITE},
       {"create",              APK_OPENF_CREATE},
       {"no_installed",        APK_OPENF_NO_INSTALLED},
       {"no_scripts",          APK_OPENF_NO_SCRIPTS},
       {"no_world",            APK_OPENF_NO_WORLD},
       {"no_sys_repos",        APK_OPENF_NO_SYS_REPOS},
       {"no_installed_repo",   APK_OPENF_NO_INSTALLED_REPO},
       {"no_repos",            APK_OPENF_NO_REPOS},
       {"no_state",            APK_OPENF_NO_STATE},
       {"no_scripts",          APK_OPENF_NO_SCRIPTS},
       {"no_world",            APK_OPENF_NO_WORLD},
       {NULL, 0}
};

Natanael Copa's avatar
Natanael Copa committed
34 35 36 37 38 39 40
static apk_blob_t check_blob(lua_State *L, int index)
{
	apk_blob_t blob;
	blob.ptr = (char *)luaL_checklstring(L, index, (size_t *)&blob.len);
	return blob;
}

Natanael Copa's avatar
Natanael Copa committed
41 42 43 44
/* version_validate(verstr) */
/* returns boolean */
static int Pversion_validate(lua_State *L)
{
Natanael Copa's avatar
Natanael Copa committed
45
	apk_blob_t ver = check_blob(L, 1);
Natanael Copa's avatar
Natanael Copa committed
46 47 48 49 50 51 52 53 54 55
	lua_pushboolean(L, apk_version_validate(ver));
	return 1;
}

/* version_compare(verstr1, verstr2)
   returns either '<', '=' or '>'
*/
static int Pversion_compare(lua_State *L)
{
	apk_blob_t a, b;
Natanael Copa's avatar
Natanael Copa committed
56 57
	a = check_blob(L, 1);
	b = check_blob(L, 2);
Natanael Copa's avatar
Natanael Copa committed
58 59 60 61 62 63 64 65 66 67
	lua_pushstring(L, apk_version_op_string(apk_version_compare_blob(a, b)));
	return 1;
}

/* version_is_less(verstr1, verstr2)
   returns either '<', '=' or '>'
*/
static int Pversion_is_less(lua_State *L)
{
	apk_blob_t a, b;
Natanael Copa's avatar
Natanael Copa committed
68 69
	a = check_blob(L, 1);
	b = check_blob(L, 2);
Natanael Copa's avatar
Natanael Copa committed
70 71 72 73
	lua_pushboolean(L, apk_version_compare_blob(a, b) == APK_VERSION_LESS);
	return 1;
}

Natanael Copa's avatar
Natanael Copa committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
//static getfield(lua_State *L, const char *key)
//{
static const char *get_opt_string_field(lua_State *L, int index,
				        const char *key, const char *def)
{
	const char *value;
	lua_getfield(L, index, key);
	value = luaL_optstring(L, -1, def);
	lua_pop(L, 1);
	return value;
}

static int get_opt_int_field(lua_State *L, int index, const char *key, int def)
{
	int value;
	lua_getfield(L, index, key);
	value = luaL_optinteger(L, -1, def);
	lua_pop(L, 1);
	return value;
}

static int get_boolean_field(lua_State *L, int index, const char *key)
{
	int value;
	lua_getfield(L, index, key);
	value = lua_toboolean(L, -1);
	lua_pop(L, 1);
	return value;
}

static int get_dbopts(lua_State *L, int i, struct apk_db_options *o)
{
	struct flagmap *f;
	o->root = (char *)get_opt_string_field(L, i, "root", NULL);
	o->repositories_file = (char *)get_opt_string_field(L, i, "repositories_file", NULL);
	o->keys_dir = (char *)get_opt_string_field(L, i, "keys_dir", NULL);
	o->lock_wait = get_opt_int_field(L, i, "lock_wait", 0);
	for (f = opendb_flagmap; f->name != NULL; f++)
		if (get_boolean_field(L, i, f->name))
			o->open_flags |= f->flag;
	return 0;
}

Natanael Copa's avatar
Natanael Copa committed
117 118 119 120 121 122 123 124 125 126
static struct apk_database *checkdb(lua_State *L, int index)
{
	struct apk_database *db;
	luaL_checktype(L, index, LUA_TUSERDATA);
	db = (struct apk_database  *) luaL_checkudata(L, index, APKDB_META);
	if (db == NULL)
		luaL_typerror(L, index, APKDB_META);
	return db;
}

Natanael Copa's avatar
Natanael Copa committed
127 128 129
static int Papk_db_open(lua_State *L)
{
	struct apk_db_options opts;
Natanael Copa's avatar
Natanael Copa committed
130 131 132
	struct apk_database *db;
	int r;

Natanael Copa's avatar
Natanael Copa committed
133
	memset(&opts, 0, sizeof(opts));
Natanael Copa's avatar
Natanael Copa committed
134
	list_init(&opts.repository_list);
Natanael Copa's avatar
Natanael Copa committed
135 136
	if (lua_istable(L, 1))
		get_dbopts(L, 1, &opts);
Natanael Copa's avatar
Natanael Copa committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

	db = lua_newuserdata(L, sizeof(struct apk_database));
	luaL_getmetatable(L, APKDB_META);
	lua_setmetatable(L, -2);

	r = apk_db_open(db, &opts);
	if (r != 0)
		luaL_error(L, "apk_db_open() failed");
	return 1;
}

static int Papk_db_close(lua_State *L)
{
	struct apk_database *db = checkdb(L, 1);
	apk_db_close(db);
Natanael Copa's avatar
Natanael Copa committed
152 153
	return 0;
}
Natanael Copa's avatar
Natanael Copa committed
154

Natanael Copa's avatar
Natanael Copa committed
155 156

static const luaL_reg reg_apk_methods[] = {
Natanael Copa's avatar
Natanael Copa committed
157 158 159
	{"version_validate",	Pversion_validate},
	{"version_compare",	Pversion_compare},
	{"version_is_less",	Pversion_is_less},
Natanael Copa's avatar
Natanael Copa committed
160
	{"db_open",		Papk_db_open},
Natanael Copa's avatar
Natanael Copa committed
161 162 163
	{NULL,		NULL}
};

Natanael Copa's avatar
Natanael Copa committed
164 165 166 167 168
static const luaL_reg reg_apk_db_meta_methods[] = {
	{"__gc",	Papk_db_close},
	{NULL, NULL}
};

Natanael Copa's avatar
Natanael Copa committed
169 170
LUALIB_API int luaopen_apk(lua_State *L)
{
Natanael Copa's avatar
Natanael Copa committed
171
	luaL_register(L, LIBNAME, reg_apk_methods);
Natanael Copa's avatar
Natanael Copa committed
172 173 174
	lua_pushliteral(L, "version");
	lua_pushliteral(L, APK_VERSION);
	lua_settable(L, -3);
Natanael Copa's avatar
Natanael Copa committed
175 176 177 178 179 180 181 182 183 184 185 186

	luaL_newmetatable(L, APKDB_META);
	luaL_register(L, NULL, reg_apk_db_meta_methods);
	lua_pushliteral(L, "__index");
	lua_pushvalue(L, -3);
	lua_rawset(L, -3);

	lua_pushliteral(L, "__metatable");
	lua_pushvalue(L, -3);
	lua_rawset(L, -3);
	lua_pop(L, 1);

Natanael Copa's avatar
Natanael Copa committed
187 188 189
	return 1;
}