]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/lua/interpr/interpr.c
Inital import
[l4.git] / l4 / pkg / lua / interpr / interpr.c
1 /*
2  * Example of a C program that interfaces with Lua.
3  * Based on Lua 5.0 code by Pedro Martelletto in November, 2003.
4  * Updated to Lua 5.1. David Manura, January 2007.
5  *
6  * L4: Made it just call the interpreter.
7  */
8
9 #include <lua.h>
10 #include <lauxlib.h>
11 #include <lualib.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14
15 int
16 main(int argc, char **argv)
17 {
18   int status, result;
19   lua_State *L;
20
21   if (argc < 2)
22     {
23       fprintf(stderr, "Need arg\n");
24       return 1;
25     }
26
27   /*
28    * All Lua contexts are held in this structure. We work with it almost
29    * all the time.
30    */
31   L = luaL_newstate();
32
33   luaL_openlibs(L); /* Load Lua libraries */
34
35   /* Load the file containing the script we are going to run */
36   status = luaL_loadfile(L, argv[1]);
37   if (status)
38     {
39       (void)fprintf(stderr, "bad, bad file\n");
40       exit(1);
41     }
42
43   /* Ask Lua to run our little script */
44   result = lua_pcall(L, 0, LUA_MULTRET, 0);
45   if (result)
46     {
47       fprintf(stderr, "%s\n", lua_pushfstring(L, "error calling "
48             LUA_QL("print") " (%s)", lua_tostring(L, -1)));
49       exit(1);
50     }
51
52   lua_pop(L, 1);  /* Take  the returned value out of the stack */
53   lua_close(L);   /* Cya, Lua */
54
55   return 0;
56 }