summaryrefslogtreecommitdiff
path: root/server/src/luaresume.cc
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2012-01-26 12:08:39 +0100
committerBent Bisballe Nyeng <deva@aasimon.org>2012-01-26 12:08:39 +0100
commit4edae3f518353bb21a02fcda2dfcff83c5a72fc3 (patch)
tree7902e2b6af1dabdb5c49b906b8592874bfce407d /server/src/luaresume.cc
parente9ff9842e9a8c178f5e17c0cf5dde16db1a0d8fc (diff)
New onCommit scripting system.
Diffstat (limited to 'server/src/luaresume.cc')
-rw-r--r--server/src/luaresume.cc121
1 files changed, 13 insertions, 108 deletions
diff --git a/server/src/luaresume.cc b/server/src/luaresume.cc
index 49de8be..1db4fb3 100644
--- a/server/src/luaresume.cc
+++ b/server/src/luaresume.cc
@@ -26,122 +26,27 @@
*/
#include "luaresume.h"
+#include <lua.hpp>
+#include <lauxlib.h>
+
#include "luautil.h"
-#include "luapraxisd.h"
#include "debug.h"
#include <stdio.h>
-#define GLOBAL_POINTER "_pracroGlobalLUAObjectPointerThisShouldBeANameThatIsNotAccidentallyOverwritten"
-
-static int _value(lua_State *L)
-{
- Pracro::checkParameters(L,
- Pracro::T_STRING,
- Pracro::T_END);
-
- std::string name = lua_tostring(L, lua_gettop(L));
-
- lua_getglobal(L, GLOBAL_POINTER);
- LUAResume *lua = (LUAResume*)lua_touserdata(L, lua_gettop(L));
-
- if(!lua) {
- lua_pushstring(L, "No LUA pointer!");
- lua_error(L);
- return 1;
- }
-
- std::string value = lua->value(name);
- lua_pushstring(L, value.c_str());
-
- return 1;
-}
-
-LUAResume::LUAResume(Commit &c)
- : commit(c)
-{
- L = luaL_newstate();
- if(L == NULL) {
- ERR(luaresume, "Could not create LUA state.\n");
- return;
- }
-
- luaL_openlibs(L);
-
- lua_pushlightuserdata(L, this); // Push the pointer to 'this' instance
- lua_setglobal(L, GLOBAL_POINTER); // Assign it to a global lua var.
-
- lua_register(L, "value", _value);
-
- register_praxisd(L);
-}
-
-LUAResume::~LUAResume()
-{
- lua_close(L);
-}
-
-std::string LUAResume::value(std::string name)
+LUAResume::LUAResume(Transaction &t, Commit &c) : LUAScript()
{
- if(commit.fields.find(name) == commit.fields.end()) {
- ERR(luaresume, "LUAResume: No such field '%s'\n", name.c_str());
- return "";
+ setEnv(LUAScript::ENV_PATIENTID, t.patientid);
+ setEnv(LUAScript::ENV_TEMPLATE, c.templ);
+ setEnv(LUAScript::ENV_MACRO, c.macro);
+ setEnv(LUAScript::ENV_USER, t.user);
+
+ std::map<std::string, std::string>::iterator i = c.fields.begin();
+ while(i != c.fields.end()) {
+ addValue(i->first, i->second);
+ i++;
}
-
- return commit.fields[name];
-}
-
-std::string LUAResume::run(std::string program)
-{
- if(L == NULL) {
- ERR(luaresume, "LUA state not initialized!");
- return "";
- }
-
- DEBUG(luaresume, "Running %s\n", program.c_str());
-
- /*
- lua_pushstring(L, value.toStdString().c_str());
- lua_setglobal(L, "value");
-
- lua_pushstring(L, name.toStdString().c_str());
- lua_setglobal(L, "name");
- */
-
- int top = lua_gettop(L);
-
- if(luaL_loadbuffer(L, program.c_str(), program.size(),
- "lua resume generator")) {
- ERR(luaresume, "loadbufer: %s\n", lua_tostring(L, lua_gettop(L)));
- return "";
- }
-
- // Run the loaded code
- if(lua_pcall(L, 0, LUA_MULTRET, 0)) {
- ERR(luaresume, "pcall: %s\n" , lua_tostring(L, lua_gettop(L)));
- return "";
- }
-
- if(top != lua_gettop(L) - 1) {
- ERR(luaresume, "Program did not return a single value.\n");
- return "";
- }
-
- if(lua_isstring(L, lua_gettop(L)) == false) {
- ERR(luaresume, "Program did not return a string value.\n");
- return "";
- }
-
- std::string res = lua_tostring(L, lua_gettop(L));
- lua_pop(L, 1);
-
- return res;
-}
-
-void LUAResume::error(std::string message)
-{
- ERR(luaresume, "LUA ERROR: %s\n", message.c_str());
}
#ifdef TEST_LUARESUME