From 183e984d90e1546619e32aa1432689262a1fb964 Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Fri, 15 Oct 2010 06:44:23 +0000 Subject: support passing options to init Either as: handle = augeas.init(root, loadpath, augeas.save_backup + augeas.no_load) or: flags = {} flags.root = "/path/to/root" flags.loadpath = "/path/to/lenses" flags.save_backup = true flags.no_load = true handle = augeas.init(flags) --- laugeas.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/laugeas.c b/laugeas.c index 5195da2..db1b6d7 100644 --- a/laugeas.c +++ b/laugeas.c @@ -11,14 +11,65 @@ #define VERSION "unknown" #endif +struct aug_flagmap { + const char *name; + int value; +}; + +struct aug_flagmap Taug_flagmap[] = { + { "none", AUG_NONE }, + { "save_backup", AUG_SAVE_BACKUP }, + { "save_newfile", AUG_SAVE_NEWFILE }, + { "typecheck", AUG_TYPE_CHECK }, + { "no_stdinc", AUG_NO_STDINC }, + { "save_noop", AUG_SAVE_NOOP }, + { "no_load", AUG_NO_LOAD }, + { "no_modl_autoload", AUG_NO_MODL_AUTOLOAD }, + { NULL, 0 } +}; + +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_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 Paug_init(lua_State *L) { augeas **a; + struct aug_flagmap *f; + const char *root = NULL, *loadpath = NULL; + int flags = 0; + a = (augeas **) lua_newuserdata(L, sizeof(augeas *)); luaL_getmetatable(L, PAUG_META); lua_setmetatable(L, -2); - *a = aug_init(NULL, NULL, 0); + if (lua_istable(L, 1)) { + root = get_opt_string_field(L, 1, "root", NULL); + loadpath = get_opt_string_field(L, 1, "loadpath", NULL); + for (f = Taug_flagmap; f->name != NULL; f++) + if (get_boolean_field(L, 1, f->name)) + flags |= f->value; + } else { + root = luaL_optstring(L, 1, NULL); + loadpath = luaL_optstring(L, 2, NULL); + flags = luaL_optinteger(L, 3, AUG_NONE); + } + *a = aug_init(root, loadpath, flags); if (*a == NULL) luaL_error(L, "aug_init failed"); return 1; @@ -82,13 +133,22 @@ static const luaL_reg Luag_meta_methods[] = { {NULL, NULL} }; + LUALIB_API int luaopen_augeas(lua_State *L) { + struct aug_flagmap *f = Taug_flagmap; luaL_register(L, LIBNAME, Paug_methods); lua_pushliteral(L, "version"); lua_pushliteral(L, VERSION); lua_settable(L, -3); + while (f->name != NULL) { + lua_pushstring(L, f->name); + lua_pushinteger(L, f->value); + lua_settable(L, -3); + f++; + } + luaL_newmetatable(L, PAUG_META); luaL_register(L, NULL, Luag_meta_methods); lua_pushliteral(L, "__index"); -- cgit v1.2.3