Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Lua interpreter in augtool #300

Open
wants to merge 52 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
10bd64a
Add a lua interpreter to augtool
raphink Sep 16, 2015
0bf4760
Use snprintf in lua_checkargs
raphink Sep 16, 2015
fac868b
Allow nil as third value in defnode
raphink Sep 16, 2015
22d60f5
Make auto_save work with lua
raphink Sep 16, 2015
4e7ea58
Add lua_aug_clear, lua_aug_clearm and lua_aug_touch
raphink Sep 17, 2015
9127af5
Remove comment
raphink Sep 17, 2015
18ef0fc
Continue on empty lines in lua
raphink Sep 17, 2015
3f4ead5
Make multiline work in lua REPL, irb style
raphink Sep 17, 2015
b351999
Make command as arg work with lua
raphink Sep 17, 2015
f8eeb22
Lua aliases for store/retrieve
raphink Sep 17, 2015
70e68d1
Pass arguments to lua script
raphink Sep 23, 2015
0f5851f
Use an aug table to assign lua methods
raphink Sep 23, 2015
ef23c22
Put lua code in src/auglua.c
raphink Sep 24, 2015
89abf80
Cleanup code
raphink Sep 24, 2015
74227f2
Rename setup_lua to luaopen_augeas
raphink Sep 24, 2015
ea2daf2
Reset Menu.txt
raphink Sep 24, 2015
1ebed13
Newlines DO work
raphink Sep 24, 2015
029b60d
Add aug_lua
raphink Sep 24, 2015
9b30c76
Use a better registry key
raphink Sep 24, 2015
731466e
better var name
raphink Sep 24, 2015
94a577f
Reset etc/passwd in root
raphink Sep 24, 2015
8c4d38d
Revert "Reset etc/passwd in root"
raphink Sep 24, 2015
3c11069
AUGEAS_0.21.0
raphink Dec 2, 2015
03b95f3
Remove unnecessary comments
raphink Dec 2, 2015
382e55f
Use lua_pushfstring
raphink Dec 2, 2015
bd23da0
Fix call of run_command()
raphink Dec 2, 2015
b40abf4
configure: scan for Lua versions
kunkku Nov 15, 2016
219dff8
move Lua extensions to libauglua
kunkku Nov 18, 2016
acbde1e
import laugeas.c from lua-augeas as luamod
kunkku Nov 16, 2016
c2e3a3b
luamod: add copyright notice
kunkku Nov 16, 2016
8ce7857
compile luamod
kunkku Nov 18, 2016
4afe954
rename luaopen_augeas back to setup_lua
kunkku Nov 18, 2016
6c17a37
luamod: define public interface
kunkku Nov 18, 2016
433ee86
luamod: userdata structure
kunkku Nov 18, 2016
f69a357
auglua: use lua_pushliteral with registry key
kunkku Nov 18, 2016
5886de4
auglua: use luamod to push instance to stack
kunkku Nov 18, 2016
682cd10
auglua: use luamod for missing functions
kunkku Nov 18, 2016
fc0c812
auglua: remove redundant functions
kunkku Nov 22, 2016
02624ef
auglua: move label to luamod
kunkku Nov 22, 2016
2b3d9e9
auglua: move cp to luamod
kunkku Nov 22, 2016
60f28b2
auglua: move rename to luamod
kunkku Nov 22, 2016
f6350cb
auglua: move clear to luamod
kunkku Nov 22, 2016
7d291e1
auglua: move clearm to luamod
kunkku Nov 22, 2016
3de1ad5
auglua: move touch to luamod
kunkku Nov 22, 2016
01778a8
auglua: move text_store to luamod
kunkku Nov 25, 2016
a3948ef
auglua: move text_retrieve to luamod
kunkku Nov 25, 2016
5ed0705
auglua: move transform to luamod
kunkku Nov 25, 2016
5967264
auglua: save: use luamod functions
kunkku Nov 25, 2016
7a402e3
make Lua module for all available versions
kunkku Nov 23, 2016
0908334
Merge pull request #1 from kunkku/lua-combined
raphink Nov 28, 2016
c04faa6
include config.h in auglua.c and luamod.c
kunkku Nov 28, 2016
d3210af
Merge pull request #2 from kunkku/lua-include-config
raphink Nov 29, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ addons:
packages:
- libxml2-dev
- libreadline-dev
- liblua5.2-dev
- valgrind
install:
- ./autogen.sh
Expand Down
28 changes: 28 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ if test x"$enable_debug" = x"yes"; then
fi

dnl Version info in libtool's notation
AC_SUBST([LIBAUGLUA_VERSION_INFO], [0:0:0])
AC_SUBST([LIBAUGEAS_VERSION_INFO], [20:0:20])
AC_SUBST([LIBFA_VERSION_INFO], [5:1:4])

Expand Down Expand Up @@ -110,12 +111,39 @@ gl_INIT
PKG_PROG_PKG_CONFIG
PKG_CHECK_MODULES([LIBXML], [libxml-2.0])

LUA_SUBDIRS=
LUA_MAKEFILES=
m4_foreach_w([LUAPKG], [lua5.1 lua5.2 lua5.3 lua], [
AC_MSG_CHECKING([for LUAPKG])
PKG_CHECK_EXISTS(LUAPKG, [
AC_MSG_RESULT([yes])
LIBLUA_CFLAGS=$($PKG_CONFIG --cflags LUAPKG)
LIBLUA_LIBS=$($PKG_CONFIG --libs LUAPKG)
LUA_SUBDIRS="$LUA_SUBDIRS LUAPKG"

LUA_SUBDIR=src/LUAPKG
AS_MKDIR_P([$LUA_SUBDIR])

LUA_MAKEFILE=$LUA_SUBDIR/Makefile
ln -fs ../Makefile.luamod $LUA_MAKEFILE.am
eval $AUTOMAKE $LUA_MAKEFILE
LUA_MAKEFILES="$LUA_MAKEFILES $LUA_MAKEFILE"
], [AC_MSG_RESULT([no])])
])
if test -z "$LIBLUA_LIBS"; then
AC_MSG_ERROR([no Lua libraries found])
fi
AC_SUBST([LIBLUA_CFLAGS])
AC_SUBST([LIBLUA_LIBS])
AC_SUBST([LUA_SUBDIRS])

AC_CHECK_FUNCS([strerror_r fsync])

AC_OUTPUT(Makefile \
gnulib/lib/Makefile \
gnulib/tests/Makefile \
src/Makefile \
$LUA_MAKEFILES \
man/Makefile \
tests/Makefile \
examples/Makefile \
Expand Down
17 changes: 13 additions & 4 deletions src/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
SUBDIRS = . $(LUA_SUBDIRS)

GNULIB= ../gnulib/lib/libgnu.la
GNULIB_CFLAGS= -I $(top_builddir)/gnulib/lib -I $(top_srcdir)/gnulib/lib

AM_CFLAGS = @AUGEAS_CFLAGS@ @WARN_CFLAGS@ $(GNULIB_CFLAGS) $(LIBXML_CFLAGS)
AM_CFLAGS = @AUGEAS_CFLAGS@ @WARN_CFLAGS@ $(GNULIB_CFLAGS) $(LIBXML_CFLAGS) $(LIBLUA_CFLAGS)

AM_YFLAGS=-d -p spec_

Expand All @@ -11,7 +13,7 @@ BUILT_SOURCES = datadir.h

DISTCLEANFILES = datadir.h

lib_LTLIBRARIES = libfa.la libaugeas.la
lib_LTLIBRARIES = libfa.la libaugeas.la libauglua.la
noinst_LTLIBRARIES = liblexer.la

bin_PROGRAMS = augtool augparse
Expand All @@ -26,9 +28,11 @@ libaugeas_la_SOURCES = augeas.h augeas.c augrun.c pathx.c \
info.c info.h errcode.c errcode.h jmt.h jmt.c

if USE_VERSION_SCRIPT
AUGLUA_VERSION_SCRIPT = $(VERSION_SCRIPT_FLAGS)$(srcdir)/auglua_sym.version
AUGEAS_VERSION_SCRIPT = $(VERSION_SCRIPT_FLAGS)$(srcdir)/augeas_sym.version
FA_VERSION_SCRIPT = $(VERSION_SCRIPT_FLAGS)$(srcdir)/fa_sym.version
else
AUGLUA_VERSION_SCRIPT =
AUGEAS_VERSION_SCRIPT =
FA_VERSION_SCRIPT =
endif
Expand All @@ -37,11 +41,16 @@ libaugeas_la_LDFLAGS = $(AUGEAS_VERSION_SCRIPT) \
-version-info $(LIBAUGEAS_VERSION_INFO)
libaugeas_la_LIBADD = liblexer.la libfa.la $(LIB_SELINUX) $(LIBXML_LIBS) $(GNULIB)

libauglua_la_LDFLAGS = $(AUGLUA_VERSION_SCRIPT) \
-version-info $(LIBAUGLUA_VERSION_INFO)
libauglua_la_SOURCES = augeas.h auglua.c auglua.h internal.h luamod.c luamod.h
libauglua_la_LIBADD = libaugeas.la $(LIBLUA_LIBS)

augtool_SOURCES = augtool.c
augtool_LDADD = libaugeas.la $(READLINE_LIBS) $(LIBXML_LIBS) $(GNULIB)
augtool_LDADD = libaugeas.la libauglua.la $(READLINE_LIBS) $(GNULIB)

augparse_SOURCES = augparse.c
augparse_LDADD = libaugeas.la $(LIBXML_LIBS) $(GNULIB)
augparse_LDADD = libaugeas.la $(LIBXML_LIBS) $(LIBLUA_LIBS) $(GNULIB)

libfa_la_SOURCES = fa.c fa.h hash.c hash.h memory.c memory.h ref.h ref.c
libfa_la_LIBADD = $(LIB_SELINUX) $(GNULIB)
Expand Down
13 changes: 13 additions & 0 deletions src/Makefile.luamod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright (C) 2016 Kaarle Ritvanen

AUTOMAKE_OPTIONS = subdir-objects

LUA_PACKAGE = $(notdir $(CURDIR))

luadir = $(libdir)/lua/$(shell $(PKG_CONFIG) --variable=V $(LUA_PACKAGE))
lua_LTLIBRARIES = augeas.la

augeas_la_SOURCES = ../augeas.h ../luamod.c ../luamod.h
augeas_la_CFLAGS = $(AM_CFLAGS) $(LIBXML_CFLAGS) $(shell $(PKG_CONFIG) --cflags $(LUA_PACKAGE))
augeas_la_LIBADD = ../libaugeas.la
augeas_la_LDFLAGS = $(AM_LDFLAGS) -avoid-version -module -shared
139 changes: 139 additions & 0 deletions src/auglua.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* auglua.c: lua integration for augtool
*
* Copyright (C) 2015 Raphaël Pinson
* Copyright (C) 2016 Kaarle Ritvanen
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Raphaël Pinson <[email protected]>
*/

#include <config.h>
#include "internal.h"
#include "augeas.h"
#include "luamod.h"
#include "auglua.h"
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stdbool.h>


#define REG_KEY_MODULE "augeas-module"
#define REG_KEY_INSTANCE "augeas-instance"

static void push_reg_value(lua_State *L, const char *key) {
lua_pushstring(L, key);
lua_gettable(L, LUA_REGISTRYINDEX);
}

static int call_function(lua_State *L, const char *name) {
push_reg_value(L, REG_KEY_MODULE);
lua_pushstring(L, name);
lua_gettable(L, -2);
lua_insert(L, 1);

push_reg_value(L, REG_KEY_INSTANCE);
lua_insert(L, 2);

lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
return lua_gettop(L);
}

static int call_bound_function(lua_State *L) {
int vals = call_function(L, lua_tostring(L, lua_upvalueindex(1)));
if (lua_isnil(L, 1) && lua_isstring(L, 2)) {
lua_pushvalue(L, 2);
lua_error(L);
}
return vals;
}

static int bind_function(lua_State *L) {
lua_pushcclosure(L, call_bound_function, 1);
return 1;
}

static int lua_aug_save(lua_State *L) {
int n;

call_function(L, "save");

if (lua_isnil(L, 1)) {
lua_pushstring(L, "saving failed (run 'errors' for details)");
lua_error(L);
} else {
lua_settop(L, 0);
lua_pushliteral(L, "/augeas/events/saved");
call_function(L, "match");

if (!lua_isnil(L, 1)) {
n = (int) lua_tointeger(L, 2);
if (n > 0)
printf("Saved %d file(s)\n", n);
}
}

return 0;
}

struct lua_State *setup_lua(augeas *a) {
lua_State *L = luaL_newstate();
luaL_openlibs(L);

lua_pushliteral(L, REG_KEY_MODULE);
luaopen_augeas(L);
lua_settable(L, LUA_REGISTRYINDEX);

lua_pushliteral(L, REG_KEY_INSTANCE);
luamod_push_augeas(L, a);
lua_settable(L, LUA_REGISTRYINDEX);

static const luaL_Reg augfuncs[] = {
//{ "span", lua_aug_span },
{ "save", lua_aug_save },
//{ "escape_name", lua_aug_escape_name },
//{ "to_xml", lua_aug_to_xml },
//{ "errors", lua_aug_errors },
{ NULL, NULL }
};

luaL_newlib(L, augfuncs);

static const luaL_Reg meta[] = {{"__index", bind_function}, {NULL, NULL}};
luaL_newlib(L, meta);
lua_setmetatable(L, -2);

lua_setglobal(L, "aug");

return L;
}

int aug_lua(lua_State *L, const char *text) {
int code = luaL_loadbuffer(L, text, strlen(text), "line") || lua_pcall(L, 0, 0, 0);
return code;
}


/*
* Local variables:
* indent-tabs-mode: nil
* c-indent-level: 4
* c-basic-offset: 4
* tab-width: 4
* End:
*/

48 changes: 48 additions & 0 deletions src/auglua.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* auglua.h: Lua helper lib for Augeas
*
* Copyright (C) 2015 Raphaël Pinson
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Raphaël Pinson <[email protected]>
*/
#include <lua.h>

#ifndef AUGLUA_H_
#define AUGLUA_H_

#ifdef __cplusplus
extern "C" {
#endif
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the rest of the project written in C++, or why are you adding this protective extern?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of augeas has that, I suppose to allow using the lib in c++

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The functions in auglua.h aren't meant to become part of the public augeas API, or are they ? I.e., when would somebody else write include "auglua.h" in their source ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to expose an aug_lua call, which would allow e.g. Puppet to take lua commands in the augeas type?


struct lua_State *setup_lua(augeas *a);

int aug_lua(lua_State *L, const char *text);

#ifdef __cplusplus
}
#endif

#endif

/*
* Local variables:
* indent-tabs-mode: nil
* c-indent-level: 4
* c-basic-offset: 4
* tab-width: 4
* End:
*/
6 changes: 6 additions & 0 deletions src/auglua_sym.version
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
AUGLUA_0.0.0 {
global:
setup_lua;
aug_lua;
local: *;
};
Loading