launcher.c 2.65 KB
Newer Older
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
/*
** $Id: lua.c,v 1.206 2012/09/29 20:07:06 roberto Exp $
** Lua stand-alone interpreter
** See Copyright Notice in lua.h
*/


#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501
#include <compat-5.2.h>
#include <luacompat52.h>
#endif

#include "openbuslua.h"
#include "extralibraries.h"




static int l_setlogpath (lua_State *L) {
  const char *path = luaL_checkstring(L, 1);
  if (openbuslua_setlogpath(path) != LUA_OK) {
    luaL_error(L, "unable to set log path");
  }
  return 0;
}


static int pmain (lua_State *L) {
  char **argv = (char **)lua_touserdata(L, 2);
  int debugmode = argv[0] && argv[1] && strcmp(argv[1], "DEBUG") == 0;
40
  int status;
41
  luapreload_extralibraries(L);
42 43 44 45
#ifdef OPENBUS_CODEREV
  lua_pushliteral(L, OPENBUS_CODEREV);
  lua_setglobal(L, "OPENBUS_CODEREV");
#endif
46
  status = openbuslua_init(L, 1, debugmode);
47 48 49 50 51 52 53 54 55 56 57 58 59
  if (status == LUA_OK) {
    lua_pushliteral(L, OPENBUS_PROGNAME);
    lua_setglobal(L, "OPENBUS_PROGNAME");
    lua_pushstring(L, OPENBUS_MAIN);
    lua_setglobal(L, "OPENBUS_MAIN");
    lua_pushstring(L, argv[0]);
    lua_setglobal(L, "OPENBUS_PROGPATH");
    lua_pushcfunction(L, l_setlogpath);
    lua_setglobal(L, "OPENBUS_SETLOGPATH");
    lua_getglobal(L, "require");
    lua_pushstring(L, OPENBUS_MAIN);
    status = lua_pcall(L, 1, 1, 0);
    if (status == LUA_OK) {
60
      int narg;
61
      if (debugmode) ++argv;
62
      narg = openbuslua_pushargs(L, argv);  /* collect arguments */
63 64 65 66
      status = openbuslua_call(L, narg+1, 1);
      if ( (status == LUA_OK) && lua_isnumber(L, -1) ) return 1;
    }
  }
67
  openbuslua_report(L, status);
68
  lua_pushinteger(L, status == LUA_OK ? 0 : EXIT_FAILURE);
69
  return 1;
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
}


int main (int argc, char **argv) {
  int status, result = EXIT_FAILURE;
  lua_State *L = luaL_newstate();  /* create state */
  if (L == NULL) {
    openbuslua_logmessage(argv[0], "cannot create Lua state: not enough memory");
  } else {
    /* call 'pmain' in protected mode */
    lua_pushcfunction(L, &pmain);
    lua_pushinteger(L, argc);  /* 1st argument */
    lua_pushlightuserdata(L, argv); /* 2nd argument */
    status = lua_pcall(L, 2, 1, 0);
    if (status == LUA_OK) {
85
      result = (int)lua_tointeger(L, -1);  /* get result */
86 87 88 89 90 91 92 93 94 95 96 97 98
    } else {
      const char *msg = (lua_type(L, -1) == LUA_TSTRING) ? lua_tostring(L, -1)
                                                         : NULL;
      if (msg == NULL) msg = "(error object is not a string)";
      openbuslua_logmessage(argv[0], msg);
      lua_pop(L, 1);
    }
    lua_close(L);
    openbuslua_setlogpath(NULL);
  }
  return result;
}