some initial files for the coming lua binding
[mplib] / src / texk / web2c / mpdir / luamain.c
1 /* $Id$ */
2
3 /* This file a quick method to test the lua interface, it just loads
4  * and runs the lua script file at argv[1] in an interpreter with
5  * the mp module preloaded. I need to do it this way because the 
6  * primary tester in Hans Hagen who is on Windows, and the cross-compiler
7  * doesn't know (or rather, I don't) how to build lua for a dynamic library.
8  * 
9  */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include <lua.h>
16 #include <lauxlib.h>
17 #include <lualib.h>
18
19 extern void luaopen_mp (lua_State *L);
20
21 int main (int argc, char **argv){
22   int status;
23   lua_State *L;
24   if ((argc!=2) || argv[1][0] == '-') {
25         fprintf(stdout,"First and only argument should be a lua script.\n");
26         return EXIT_SUCCESS;
27   }
28   L = lua_open();
29   if (L==NULL) {
30         fprintf(stderr,"Can't create the Lua state.");
31         return EXIT_FAILURE;
32   }
33   luaL_openlibs(L);
34   luaopen_mp(L);
35   status = luaL_loadfile(L, argv[1]);
36   if (status == 0) {
37     status = lua_pcall(L, 0, 0, 0);
38         if (status) {
39           fprintf(stderr,"call of %s failed: %s\n",argv[1], lua_tostring(L,-1));
40         }
41   } else {
42         fprintf(stderr,"compile of %s failed\n",argv[1]);
43   }
44   lua_close(L);
45   return (status ? EXIT_FAILURE : EXIT_SUCCESS);
46 }