Imported from ../lua-2.1.tar.gz.
[lua.git] / clients / lua / lua.c
blob1983664619b0bd4073be0370dca8c1f5f624b009
1 /*
2 ** lua.c
3 ** Linguagem para Usuarios de Aplicacao
4 */
6 char *rcs_lua="$Id: lua.c,v 1.4 1995/02/07 16:04:15 lhf Exp $";
8 #include <stdio.h>
9 #include <string.h>
11 #include "lua.h"
12 #include "lualib.h"
14 static int lua_argc;
15 static char **lua_argv;
18 %F Allow Lua code to access argv strings.
19 %i Receive from Lua the argument number (starting with 1).
20 %o Return to Lua the argument, or nil if it does not exist.
22 static void lua_getargv (void)
24 lua_Object lo = lua_getparam(1);
25 if (!lua_isnumber(lo))
26 lua_pushnil();
27 else
29 int n = (int)lua_getnumber(lo);
30 if (n < 1 || n > lua_argc) lua_pushnil();
31 else lua_pushstring(lua_argv[n]);
36 int main (int argc, char *argv[])
38 int i;
39 int result = 0;
40 iolib_open ();
41 strlib_open ();
42 mathlib_open ();
44 lua_register("argv", lua_getargv);
46 if (argc < 2)
48 char buffer[250];
49 while (gets(buffer) != 0)
50 result = lua_dostring(buffer);
52 else
54 for (i=1; i<argc; i++)
56 if (strcmp(argv[i], "--") == 0)
58 lua_argc = argc-i-1;
59 lua_argv = argv+i;
60 break;
63 for (i=1; i<argc; i++)
65 if (strcmp(argv[i], "--") == 0)
66 break;
67 else
68 result = lua_dofile (argv[i]);
71 return result;