Imported from ../lua-2.4.tar.gz.
[lua.git] / clients / lua / lua.c
blobbae09e255699f353c86c256d48fc9fad03858a6a
1 /*
2 ** lua.c
3 ** Linguagem para Usuarios de Aplicacao
4 */
6 char *rcs_lua="$Id: lua.c,v 1.10 1996/05/03 19:20:17 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;
17 #ifdef POSIX
19 ** although this function is POSIX, there is no standard header file that
20 ** defines it
22 int isatty (int fd);
23 #else
24 #define isatty(x) (x==0) /* assume stdin is a tty */
25 #endif
28 %F Allow Lua code to access argv strings.
29 %i Receive from Lua the argument number (starting with 1).
30 %o Return to Lua the argument, or nil if it does not exist.
32 static void lua_getargv (void)
34 lua_Object lo = lua_getparam(1);
35 if (!lua_isnumber(lo))
36 lua_pushnil();
37 else
39 int n = (int)lua_getnumber(lo);
40 if (n < 1 || n > lua_argc) lua_pushnil();
41 else lua_pushstring(lua_argv[n]);
46 static void manual_input (void)
48 if (isatty(0))
50 char buffer[250];
51 while (fgets(buffer, sizeof(buffer), stdin) != 0)
52 lua_dostring(buffer);
54 else
55 lua_dofile(NULL); /* executes stdin as a file */
59 int main (int argc, char *argv[])
61 int i;
62 int result = 0;
63 iolib_open ();
64 strlib_open ();
65 mathlib_open ();
67 lua_register("argv", lua_getargv);
69 if (argc < 2)
70 manual_input();
71 else
73 for (i=1; i<argc; i++)
74 if (strcmp(argv[i], "--") == 0)
76 lua_argc = argc-i-1;
77 lua_argv = argv+i;
78 break;
80 for (i=1; i<argc; i++)
82 if (strcmp(argv[i], "--") == 0)
83 break;
84 else if (strcmp(argv[i], "-") == 0)
85 manual_input();
86 else if (strcmp(argv[i], "-v") == 0)
87 printf("%s %s\n(written by %s)\n\n",
88 LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
89 else
91 result = lua_dofile (argv[i]);
92 if (result)
93 fprintf(stderr, "lua: error trying to run file %s\n", argv[i]);
97 return result;