Example #1
0
int main(int argc, char **argv) {
	int i;

	if(argc < 2) {
		fprintf(stderr, "needs config file as first argument.\n");
		return -1;
	}

	/* set up Lua state */
	L = lua_open();
	if(L) {
		luaL_openlibs(L);

		luaopen_blitbuffer(L);
		luaopen_drawcontext(L);
		luaopen_einkfb(L);
		luaopen_pdf(L);
		luaopen_djvu(L);
		luaopen_cre(L);
		luaopen_input(L);
		luaopen_util(L);
		luaopen_ft(L);
		luaopen_mupdfimg(L);

		luaopen_lfs(L);

		lua_newtable(L);
		for(i=2; i < argc; i++) {
			lua_pushstring(L, argv[i]);
			lua_rawseti(L, -2, i-1);
		}
		lua_setglobal(L, "ARGV");

		if(luaL_dofile(L, argv[1])) {
			fprintf(stderr, "lua config error: %s\n", lua_tostring(L, -1));
			lua_close(L);
			L=NULL;
			return -1;
		}
	}

	return 0;
}
Example #2
0
int
sys_start(int argc, char *argv[])
{
	int status;
	const char* f;
	lua_State *L;
	
	if (argc<2) {
		printf("%s: <filename.lua>\n", argv[0]);
		return 1;
	}
	
    s_init();
    log_print("sound> initialized\n");
	
	L = lua_open();
	/*luaopen_base(L);*/
	luaL_openlibs(L);
	luaopen_sys(L);
	luaopen_log(L);
	luaopen_mat4(L);
	luaopen_quat(L);
	luaopen_vec3(L);
	luaopen_event(L);
	luaopen_image(L);
	luaopen_render(L);
	luaopen_shader(L);
	luaopen_sound(L);
	luaopen_net(L);
	luaopen_util(L);
	luaopen_font(L);
	luaopen_md2(L);
	//luaopen_blender(L);
	
	lua_getglobal(L, "package");
	if (LUA_TTABLE != lua_type(L, 1)) {
		log_print("lua> 'package' is not a table\n");
		return 1;
	}
	lua_getfield(L, 1, "path");
	if (LUA_TSTRING != lua_type(L, 2)) {
		log_print("lua> 'package.path' is not a string\n");
		lua_pop(L, 1);
		return 1;
	}
	lua_pop(L, 1);
	
#if defined(__IPHONE__)
	f = full_path();
#else
	f = ".";
#endif

	/* package.path = f .. "/?.lua" */
	lua_pushlstring(L, f, strlen(f));
	lua_pushliteral(L, "/?.lua");
	lua_concat(L, 2);
	lua_setfield(L, 1, "path");

	lua_bootstrap(L, f);

#if defined(__IPHONE__)
	f = full_path_to_file(argv[1]);
#else
	f = argv[1];
#endif

	log_print("lua> initialized\n");
	printf("lua> loading %s\n", f);
	
	if (luaL_loadfile(L,f)==0) { 
		/* function runme = file_content */
		lua_setglobal(L, "runme");
		lua_getglobal(L, "runme");

		status = lua_pcall(L,0,LUA_MULTRET,0);
		assert(0 == report(L, status));
        
        if (lua_gettop(L) > 0) {
            status = lua_toboolean(L, -1);
            if (!status) {
                printf("lua> %s returned false\n", f);
                sys_quit();
            }
        }
		
        log_print("lua> loaded\n");
	} else {
		printf("lua> unable to load %s\n",f);
		report(L, status);
		sys_quit();
	}

	lua_state = L;
	
	/* TODO: Double check this stuff some day when bored. */
	return status > 0 ? false : true;	
}