File: app.c

package info (click to toggle)
dh-lua 15
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 172 kB
  • sloc: ansic: 98; perl: 81; makefile: 46; sh: 46
file content (79 lines) | stat: -rw-r--r-- 1,572 bytes parent folder | download | duplicates (2)
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
// Copyright: © 2012 Enrico Tassi <gareuselesinge@debian.org>
// License: MIT/X

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

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

// this should include all the needed libraries and
// define 
//   static void app_open(lua_State* L)
#include "app.c.conf"

int main(int argn,char** argv){
	int rc;
	int i;
	const char *init = getenv("LUA_INIT");

	// create a lua VM
	lua_State* L = lua_open();
	if (L == NULL) {
		fprintf(stderr,"Unable to allocate a lua_State");
		return 1;
	}

	// load stdlib
	lua_gc(L, LUA_GCSTOP, 0);  /* stop collector during initialization */
	luaL_openlibs(L);  /* open libraries */
	lua_gc(L, LUA_GCRESTART, 0);

	// LUA_INIT
	if (init != NULL && luaL_dostring(L, init)) {
		const char* error = NULL;
		error = lua_tostring(L,-1);
		fprintf(stderr,"app.c: %s\n",error);
		return 1;
	}

	// here the specific luaopen_MODULENAME
	app_open(L);

	// LOAD
	rc = luaL_loadfile(L,argv[1]);

	// check for errors
	if (rc != 0){
		const char* error = NULL;
		error = lua_tostring(L,-1);
		fprintf(stderr,"app.c: %s\n",error);
		return 1;
	}

	// RUN!
	lua_newtable(L);
	for(i=1;i<argn;i++){
		lua_pushnumber(L,i-1);
		lua_pushstring(L,argv[i]);
		lua_settable(L,-3);
	}
	lua_setglobal(L,"arg");

	for(i=2;i<argn;i++) lua_pushstring(L,argv[i]);
	rc = lua_pcall(L,argn-2,LUA_MULTRET,0);
	
	// check for errors
	if (rc != 0){
		const char* error = NULL;
		error = lua_tostring(L,-1);
		fprintf(stderr,"app.c: %s\n",error);
		return 1;
	}

	// shutdown lua VM
	lua_close(L);

	// bye!
	return (rc == 0 ? EXIT_SUCCESS : 1);
}