File: app.c

package info (click to toggle)
lua5.1-policy 33
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 140 kB
  • ctags: 16
  • sloc: sh: 45; ansic: 42; makefile: 30
file content (68 lines) | stat: -rw-r--r-- 1,334 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
// $Id: app.c 1547 2011-05-09 13:24:05Z gareuselesinge $
#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;

	// 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);

	// 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);
}