Imported from ../lua-2.2.tar.gz.
[lua.git] / clients / lua / lua.c
blob33c7adcbb72bb9c84c3934a3688bb281fbe9c291
1 /*
2 ** lua.c
3 ** Linguagem para Usuarios de Aplicacao
4 */
6 char *rcs_lua="$Id: lua.c,v 1.7 1995/10/31 17:05:35 roberto 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 ** although this function is POSIX, there is no standard header file that
19 ** defines it
21 int isatty (int fd);
24 %F Allow Lua code to access argv strings.
25 %i Receive from Lua the argument number (starting with 1).
26 %o Return to Lua the argument, or nil if it does not exist.
28 static void lua_getargv (void)
30 lua_Object lo = lua_getparam(1);
31 if (!lua_isnumber(lo))
32 lua_pushnil();
33 else
35 int n = (int)lua_getnumber(lo);
36 if (n < 1 || n > lua_argc) lua_pushnil();
37 else lua_pushstring(lua_argv[n]);
42 static void manual_input (void)
44 if (isatty(fileno(stdin)))
46 char buffer[250];
47 while (gets(buffer) != 0)
48 lua_dostring(buffer);
50 else
51 lua_dofile(NULL); /* executes stdin as a file */
55 int main (int argc, char *argv[])
57 int i;
58 int result = 0;
59 iolib_open ();
60 strlib_open ();
61 mathlib_open ();
63 lua_register("argv", lua_getargv);
65 if (argc < 2)
66 manual_input();
67 else
69 for (i=1; i<argc; i++)
70 if (strcmp(argv[i], "--") == 0)
72 lua_argc = argc-i-1;
73 lua_argv = argv+i;
74 break;
76 for (i=1; i<argc; i++)
78 if (strcmp(argv[i], "--") == 0)
79 break;
80 else if (strcmp(argv[i], "-") == 0)
81 manual_input();
82 else if (strcmp(argv[i], "-v") == 0)
83 printf("%s %s\n(written by %s)\n\n",
84 LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
85 else
86 result = lua_dofile (argv[i]);
89 return result;