summaryrefslogtreecommitdiff
path: root/server/src/luascript.cc
blob: e46aa8d31d2ae86e1d0ad56bfd53b9eaf866fcc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set et sw=2 ts=2: */
/***************************************************************************
 *            luascript.cc
 *
 *  Tue Jan 10 14:43:39 CET 2012
 *  Copyright 2012 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of Pracro.
 *
 *  Pracro is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Pracro 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Pracro; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "luascript.h"

#include "configuration.h"
#include "debug.h"

#include "luautil.h"
#include "luapraxisd.h"

#define GLOBAL_POINTER "_pracroGlobalLUAObjectPointerThisShouldBeANameThatIsNotAccidentallyOverwritten"

static int _debug(lua_State *L)
{
  Pracro::checkParameters(L,
                          Pracro::T_STRING,
                          Pracro::T_END);

  std::string msg = lua_tostring(L, lua_gettop(L));

  DEBUG(luascript, "%s\n", msg.c_str());

  return 0;
}

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);
  LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L));

  if(!lua) {
    lua_pushstring(L, "No LUA pointer!");
    lua_error(L);
    return 1;
  }

  if(lua->hasValue(name)) {
    lua_pushstring(L, lua->value(name).c_str());
  } else {
    lua_pushnil(L);
  }

  return 1;
}

static int _patientid(lua_State *L)
{
  Pracro::checkParameters(L, Pracro::T_END);

  lua_getglobal(L, GLOBAL_POINTER);
  LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L));

  if(!lua) {
    lua_pushstring(L, "No LUA pointer!");
    lua_error(L);
    return 1;
  }

  lua_pushstring(L, lua->env(LUAScript::ENV_PATIENTID).c_str());

  return 1;
}

static int _template(lua_State *L)
{
  Pracro::checkParameters(L, Pracro::T_END);

  lua_getglobal(L, GLOBAL_POINTER);
  LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L));

  if(!lua) {
    lua_pushstring(L, "No LUA pointer!");
    lua_error(L);
    return 1;
  }

  lua_pushstring(L, lua->env(LUAScript::ENV_TEMPLATE).c_str());

  return 1;
}

static int _macro(lua_State *L)
{
  Pracro::checkParameters(L, Pracro::T_END);

  lua_getglobal(L, GLOBAL_POINTER);
  LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L));

  if(!lua) {
    lua_pushstring(L, "No LUA pointer!");
    lua_error(L);
    return 1;
  }

  lua_pushstring(L, lua->env(LUAScript::ENV_MACRO).c_str());

  return 1;
}

static int _user(lua_State *L)
{
  Pracro::checkParameters(L, Pracro::T_END);

  lua_getglobal(L, GLOBAL_POINTER);
  LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L));

  if(!lua) {
    lua_pushstring(L, "No LUA pointer!");
    lua_error(L);
    return 1;
  }

  lua_pushstring(L, lua->env(LUAScript::ENV_USER).c_str());

  return 1;
}

LUAScript::LUAScript()
{
  L = NULL;
}

void LUAScript::init()
  throw(Exception)
{
  if(L) return;

  L = luaL_newstate();
  if(L == NULL) {
    ERR(luascript, "Could not create LUA state.\n");
    throw Exception("Could not create LUA state.");
  }

  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);
  lua_register(L, "patientid", _patientid);
  lua_register(L, "template", _template);
  lua_register(L, "macro", _macro);
  lua_register(L, "user", _user);
  lua_register(L, "debug", _debug);

  register_praxisd(L);
}

void LUAScript::addFile(std::string src)
{
  std::string file =
    Conf::xml_basedir + "/include/" + src;
  FILE *fp = fopen(file.c_str(), "r");
  if(fp) {
    char buf[64];
    size_t sz;
    std::string inc;
    while((sz = fread(buf, 1, sizeof(buf), fp)) != 0) {
      inc.append(buf, sz);
    }
    fclose(fp);
    addCode(inc, file);
  }
}

void LUAScript::addCode(std::string c, std::string name)
{
  scripts.push_back(std::make_pair(c, name));
}

void LUAScript::addValue(std::string name, const std::string &value)
{
  values[name] = value;
}

void LUAScript::addScripts(std::vector< Script > &scripts)
{
  std::vector< Script >::iterator spi = scripts.begin();
  while(spi != scripts.end()) {
    if(spi->attributes.find("src") != spi->attributes.end()) {
      std::string src = spi->attributes["src"];
      addFile(src);
    } else {
      addCode(spi->code);
    }
    spi++;
  }
}

void LUAScript::run()
  throw(Exception)
{
  try {
    init();
  } catch(Exception &e) {
    throw Exception(e.msg);
  }

  if(L == NULL) {
    ERR(luascript, "LUA state not initialized!");
    return;
  }

  top = lua_gettop(L);

  std::vector<std::pair<std::string, std::string> >::iterator i =
    scripts.begin();
  while(i != scripts.end()) {
    std::string program = i->first;
    std::string codename = name();
    if(i->second != "") codename += ": " + i->second;

    DEBUG(luascript, "Running %s: %s\n", codename.c_str(), program.c_str());

    if(luaL_loadbuffer(L, program.c_str(), program.size(), codename.c_str())) {
      ERR(luascript, "loadbuffer: %s\n", lua_tostring(L, lua_gettop(L)));
      throw Exception(lua_tostring(L, lua_gettop(L)));
    }
    
    // Run the loaded code
    if(lua_pcall(L, 0, LUA_MULTRET, 0)) {
      ERR(luascript, "pcall: %s\n" , lua_tostring(L, lua_gettop(L)));
      throw Exception(lua_tostring(L, lua_gettop(L)));
    }

    i++;
  }
}

std::string LUAScript::resultString() throw(Exception)
{
  if(top != lua_gettop(L) - 1) {
    ERR(luascript, "Program did not return a single value.\n");
    throw Exception("Program did not return a single value.");
  }

  if(lua_isstring(L, lua_gettop(L)) == false) {
    ERR(luascript, "Program did not return a string value.\n");
    throw Exception("Program did not return a string value.");
  }

  std::string res = lua_tostring(L, lua_gettop(L));
  lua_pop(L, 1);

  return res;
}

bool LUAScript::hasValue(std::string name)
{
  return values.find(name) != values.end();
}

std::string LUAScript::value(std::string name)
{
  if(values.find(name) != values.end()) return values[name];
  return "";
}

std::string LUAScript::env(LUAScript::env_t id)
{
  if(_env.find(id) == _env.end()) return "";
  return _env[id];
}

void LUAScript::setEnv(LUAScript::env_t id, std::string value)
{
  _env[id] = value;
}

#ifdef TEST_LUASCRIPT
//deps: exception.cc debug.cc log.cc mutex.cc luapraxisd.cc praxisd.cc luautil.cc saxparser.cc configuration.cc
//cflags: -I.. $(PTHREAD_CFLAGS) $(LUA_CFLAGS) $(CURL_CFLAGS) $(EXPAT_CFLAGS)
//libs: $(PTHREAD_LIBS) $(LUA_LIBS) $(CURL_LIBS) $(EXPAT_LIBS)
#include "test.h"

TEST_BEGIN;

// TODO: Put some testcode here (see test.h for usable macros).
TEST_TRUE(false, "No tests yet!");

TEST_END;

#endif/*TEST_LUASCRIPT*/