예제 #1
0
int loader_C_luatex (lua_State *L, const char *name, const char *filename) {
  const char *funcname;
  funcname = mkfuncname(L, name);
  if (ll_loadfunc(L, filename, funcname,0) != 0)
    loaderror(L, filename);
  return 1;  /* library loaded successfully */
}
예제 #2
0
파일: xlua.cpp 프로젝트: jaquadro/XLua
extern "C" int loader_XCDirect (lua_State *L, const char* filename, const char* name) {
	const char *funcname;
	//const char *filename = luaL_checkstring(L, 1);
	//const char *name = luaL_checkstring(L, 2);
	DWORD dwsz;

	// Check existance of module
	LPRDATA rdPtr = XLuaGlobal::Get().GetStateByState(L)->rdList.front()->rdPtr;
	HANDLE hFile = rdPtr->rHo.hoAdRunHeader->rh4.rh4Mv->mvOpenHFile(filename, &dwsz, 0);
	if (hFile == INVALID_HANDLE_VALUE) {
		return 1;
	}
	rdPtr->rHo.hoAdRunHeader->rh4.rh4Mv->mvCloseHFile(hFile);

	// Extract + Load module
	char* tpath = (char*) calloc(_MAX_PATH, sizeof(char));
	rdPtr->rHo.hoAdRunHeader->rh4.rh4Mv->mvGetFile(filename, tpath, 0);

	funcname = mkfuncname(L, name);
	if (ll_loadfunc(L, tpath, funcname) != 0) {
		free(tpath);
	    loaderror(L, filename);
		return 0;
	}
	free(tpath);

	return 1;
}
예제 #3
0
static int loader_C (lua_State *L) {
  const char* funcname;
  const char* name = luaL_checkstring(L, 1);
  const char* filename = findfile(L, name, "cpath");
  if (filename == nullptr) return 1;  /* library not found in this path */
  funcname = mkfuncname(L, name);
  if (ll_loadfunc(L, filename, funcname) != 0)
    loaderror(L, filename);
  return 1;  /* library loaded successfully */
}
예제 #4
0
int loader_Call_luatex (lua_State *L, const char *name, const char *filename) {
  const char *funcname;
  int stat;
  if (filename == NULL) return 1;  /* root not found */
  funcname = mkfuncname(L, name);
  if ((stat = ll_loadfunc(L, filename, funcname,0)) != 0) {
    if (stat != PACKAGE_ERR_FUNC) loaderror(L, filename);  /* real error */
    lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
                       name, filename);
    return 1;  /* function not found */
  }
  return 1;  /* library loaded successfully */
}
예제 #5
0
static int loader_Croot (lua_State *L) {
  const char *funcname;
  const char *filename;
  const char *name = luaL_checkstring(L, 1);
  const char *p = strchr(name, '.');
  int stat;
  if (p == NULL) return 0;  /* is root */
  lua_pushlstring(L, name, p - name);
  filename = findfile(L, lua_tostring(L, -1), "cpath");
  if (filename == NULL) return 0;  /* root not found */
  funcname = mkfuncname(L, name);
  if ((stat = ll_loadfunc(L, filename, funcname)) != 0) {
    if (stat == ERRFUNC) return 0;  /* function not found */
    else
      loaderror(L);  /* real error */
  }
  return 1;
}
예제 #6
0
static int lj_cf_package_loader_croot(lua_State *L)
{
  const char *funcname;
  const char *filename;
  const char *name = luaL_checkstring(L, 1);
  const char *p = strchr(name, '.');
  int stat;
  if (p == NULL) return 0;  /* is root */
  lua_pushlstring(L, name, (size_t)(p - name));
  filename = findfile(L, lua_tostring(L, -1), "cpath");
  if (filename == NULL) return 1;  /* root not found */
  funcname = mkfuncname(L, name);
  if ((stat = ll_loadfunc(L, filename, funcname)) != 0) {
    if (stat != PACKAGE_ERR_FUNC) loaderror(L, filename);  /* real error */
    lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
		    name, filename);
    return 1;  /* function not found */
  }
  return 1;
}
예제 #7
0
파일: xlua.cpp 프로젝트: jaquadro/XLua
extern "C" int loader_XC (lua_State *L) {
	const char *funcname;
	const char *filename;
	const char *name = luaL_checkstring(L, 1);
	filename = x_findfile(L, name, "cpath");
	if (filename == NULL) return 1;  /* library not found in this path */

	char* tpath = (char*) calloc(_MAX_PATH, sizeof(char));

	LPRDATA rdPtr = XLuaGlobal::Get().GetStateByState(L)->rdList.front()->rdPtr;
	rdPtr->rHo.hoAdRunHeader->rh4.rh4Mv->mvGetFile(filename, tpath, 0);

	funcname = mkfuncname(L, name);
	if (ll_loadfunc(L, tpath, funcname) != 0) {
		free(tpath);
	    loaderror(L, filename);
		return 0;
	}

	free(tpath);
	return 1;
}
예제 #8
0
int main(int argc, char *argv[]) {
  lb_idtype lb_id;
  lzo_uint  lb_overhead;
  lzo_uint  lb_offset;
  lzo_bytep lb_data;
  lzo_bytep lb_temp;

  /* start up the stuff */
#ifdef _WIN32
  char selfname[MAX_PATH]; /* Flawfinder: ignore */
  if (GetModuleFileName(NULL,selfname,sizeof(selfname))==0)
    lb_error("cannot locate this executable");
  argv[0] = selfname;
#endif /* not _WIN32 */
  lb_progname = lb_fnname(argv[0]);
  const char *lb_path = lb_tmppath();
  if (lb_path == NULL) lb_cannot("find","tmp dir",NULL);

  if (lzo_init() != LZO_E_OK)
    lb_error("internal LZO error");
  if ((L = luaL_newstate()) == NULL) lb_error("not enough memory");
  luaL_openlibs(L);
  LBCONF_USERFUNC_INIT(L);

  /* open and load */
  FILE *lb_file = fopen(argv[0],"rb"); /* Flawfinder: ignore */
  if (lb_file == NULL)
    lb_cannot("open",argv[0],NULL);
  if (fseek(lb_file,-sizeof(lb_id),SEEK_END)!=0)
    lb_cannot("seek",argv[0],NULL);
  if (fread(&lb_id,sizeof(lb_id),1,lb_file)!=1)
    lb_cannot("read",argv[0],NULL);
  if (memcmp(lb_id.id,"LB02",4)!=0)
    lb_error("missing overlay");
  if (fseek(lb_file,-(sizeof(lb_id)+lb_id.nlen),SEEK_END)!=0)
    lb_cannot("seek",argv[0],NULL);
  if (lb_id.dlen != 0) {
    lb_overhead = lb_id.dlen / 16 + 64 + 3;
    lb_offset = lb_id.dlen + lb_overhead - lb_id.nlen;
    lb_temp = (lzo_bytep)alloca(lb_id.dlen + lb_overhead);
    lb_data = lb_temp + lb_offset;
  } else
    lb_data = (lzo_bytep)alloca(lb_id.nlen);
  if (fread(lb_data,lb_id.nlen,1,lb_file)!=1)
    lb_cannot("read",argv[0],NULL);
  fclose(lb_file);

  /* checksum */
  if (lzo_adler32(0,(lzo_bytep)lb_data,lb_id.nlen)!=lb_id.adler32)
    lb_error("bad checksum");

  /* decompress */
  if (lb_id.dlen != 0) {
    lzo_uint new_len;
    int r = lzo1x_decompress(lb_data,lb_id.nlen,lb_temp,&new_len,NULL);
    if (r != LZO_E_OK || new_len != lb_id.dlen)
      lb_error("overlapping decompression failed");
    lb_data = lb_temp;
  } else 
    lb_id.dlen = lb_id.nlen;

  /* set parameters (arg = argv) */
  lua_newtable(L);
  { int i;
    for (i=0;i <= argc; i++) {
      lua_pushstring(L,argv[i]);
      lua_rawseti(L,-2,i);
    }
  }
  lua_setglobal(L,"arg");

  /* setup the clean up */
  lua_newtable(L);
  lua_setfield(L,LUA_REGISTRYINDEX,"_LBCM");
  lua_getfield(L,LUA_REGISTRYINDEX,"_LBCM");   /* _LBCM is at index 1   */
  atexit(lb_quit);

  /* link, load and run loop */
  lua_getfield(L,LUA_REGISTRYINDEX,"_LOADED"); /* _LOADED is at index 2 */
  char *buf = lb_data;
  uint32_t ptr = 0;
  do {
    char  _type = *buf++;
    char  _nlen = *buf++;
    char *_name = alloca((size_t)_nlen+1);
    memcpy(_name,buf,_nlen); /* Flawfinder: ignore */
    _name[_nlen] = '\0';
    buf += _nlen;
    uint32_t _size;
    memcpy(&_size,buf,4); /* Flawfinder: ignore */
    buf += 4;
    switch (_type) {
      case LB_LUAMAIN: /* run */
        if (luaL_loadbuffer(L,buf,_size,_name))
          lb_error(lua_tostring(L,-1));
        int i = 1;
        for(;i <= argc;i++)
          lua_pushstring(L,argv[i]);
        if (lua_pcall(L,i-1,0,0))
          lb_error(lua_tostring(L,-1));
        return 0;
      case LB_LMODULE: /* load */
      case LB_CMODULE: /* link and load */
        lua_getfield(L,2,_name); /* _LOADED[name] */
        if (lua_toboolean(L,-1)) { /* is it there? */
          if (lua_touserdata(L,-1) == sentinel) { /* check loops */
            lua_pushfstring(L,"loop or previous error loading module '%s'",_name);
            lb_error(lua_tostring(L,-1));
          }
          lua_pop(L,2);
          break; /* package is already loaded */
        } else lua_pop(L,1);
        /* else must load it */
        if (_type == LB_LMODULE) {
          if (luaL_loadbuffer(L,buf,_size,_name))
            lb_error(lua_tostring(L,-1));
        } else {
          lua_pushfstring(L,"%s/%s%s",lb_path,_name,LB_DLEXT);
          lb_libcreate(lua_tostring(L,-1),buf,_size);
          void *_lib = ll_load(L,lua_tostring(L,-1));
          if (_lib == NULL) lb_cannot("load library",_name,lua_tostring(L,-1));
          lua_CFunction _fun = ll_sym(L,_lib,mkfuncname(L,_name));
          if (_fun == NULL) lb_cannot("find function",lua_tostring(L,-2),lua_tostring(L,-1));
          lua_pop(L,1); /* pop value from mkfuncname() */
          lua_pushlightuserdata(L,_lib);
          lua_settable(L,1); /* _LBCM[libname] = _lib */
          lua_pushcfunction(L,_fun);
        }
        lua_pushlightuserdata(L,sentinel);
        lua_setfield(L,2,_name); /* _LOADED[name] = sentinel */
        lua_pushstring(L,_name); /* pass name as argument to module */
        if (lua_pcall(L,1,1,0)) /* run loaded module */
          lb_error(lua_tostring(L,-1)); 
        if (!lua_isnil(L,-1)) /* non-nil return ? */
          lua_setfield(L,2,_name); /* _LOADED[name] = returned value */
        lua_getfield(L,2,_name);
        if (lua_touserdata(L,-1)==sentinel){ /* module did not set a value? */
          lua_pushboolean(L,1); /* use true as result */
          lua_setfield(L,2,_name); /* _LOADED[name] = true */
        }
        break;
      case LB_LIBRARY: /* link */
        lua_pushfstring(L,"%s/%s%s",lb_path,_name,LB_DLEXT);
        lb_libcreate(lua_tostring(L,-1),buf,_size);
        void *_lib = ll_load(L,lua_tostring(L,-1));
        if (_lib == NULL) lb_cannot("load library",_name,lua_tostring(L,-1));
        lua_pushlightuserdata(L,_lib);
        lua_settable(L,1); /* _LBCM[libname] = _lib */
        break;
    }
    buf += _size;
    ptr += 2 + _nlen + 4 + _size;
  } while (ptr < lb_id.dlen);
  return 0;
}