Imported from ../lua-3.0.tar.gz.
[lua.git] / clients / lua / lua.c
blobafa2b560b48e23ac82d0cb47225d1eb054eb87eb
1 /*
2 ** lua.c
3 ** Linguagem para Usuarios de Aplicacao
4 */
6 char *rcs_lua="$Id: lua.c,v 1.18 1997/06/19 18:55:40 roberto Exp $";
8 #include <stdio.h>
9 #include <string.h>
11 #include "lua.h"
12 #include "auxlib.h"
13 #include "lualib.h"
16 #ifdef _POSIX_SOURCE
17 #include <unistd.h>
18 #else
19 #define isatty(x) (x==0) /* assume stdin is a tty */
20 #endif
23 #define DEBUG 0
25 static void testC (void)
27 #if DEBUG
28 #define getnum(s) ((*s++) - '0')
29 #define getname(s) (nome[0] = *s++, nome)
31 static int locks[10];
32 lua_Object reg[10];
33 char nome[2];
34 char *s = luaL_check_string(1);
35 nome[1] = 0;
36 while (1) {
37 switch (*s++) {
38 case '0': case '1': case '2': case '3': case '4':
39 case '5': case '6': case '7': case '8': case '9':
40 lua_pushnumber(*(s-1) - '0');
41 break;
43 case 'c': reg[getnum(s)] = lua_createtable(); break;
45 case 'P': reg[getnum(s)] = lua_pop(); break;
47 case 'g': { int n = getnum(s); reg[n] = lua_getglobal(getname(s)); break; }
49 case 'G': { int n = getnum(s);
50 reg[n] = lua_rawgetglobal(getname(s));
51 break;
54 case 'l': locks[getnum(s)] = lua_ref(1); break;
55 case 'L': locks[getnum(s)] = lua_ref(0); break;
57 case 'r': { int n = getnum(s); reg[n] = lua_getref(locks[getnum(s)]); break; }
59 case 'u': lua_unref(locks[getnum(s)]); break;
61 case 'p': { int n = getnum(s); reg[n] = lua_getparam(getnum(s)); break; }
63 case '=': lua_setglobal(getname(s)); break;
65 case 's': lua_pushstring(getname(s)); break;
67 case 'o': lua_pushobject(reg[getnum(s)]); break;
69 case 'f': lua_call(getname(s)); break;
71 case 'i': reg[getnum(s)] = lua_gettable(); break;
73 case 'I': reg[getnum(s)] = lua_rawgettable(); break;
75 case 't': lua_settable(); break;
77 case 'T': lua_rawsettable(); break;
79 default: luaL_verror("unknown command in `testC': %c", *(s-1));
82 if (*s == 0) return;
83 if (*s++ != ' ') lua_error("missing ` ' between commands in `testC'");
85 #else
86 lua_error("`testC' not active");
87 #endif
91 static void manual_input (void)
93 if (isatty(0)) {
94 char buffer[250];
95 while (fgets(buffer, sizeof(buffer), stdin) != 0) {
96 lua_beginblock();
97 lua_dostring(buffer);
98 lua_endblock();
101 else
102 lua_dofile(NULL); /* executes stdin as a file */
106 int main (int argc, char *argv[])
108 int i;
109 int result = 0;
110 iolib_open ();
111 strlib_open ();
112 mathlib_open ();
113 lua_register("testC", testC);
114 if (argc < 2)
115 manual_input();
116 else for (i=1; i<argc; i++) {
117 if (strcmp(argv[i], "-") == 0)
118 manual_input();
119 else if (strcmp(argv[i], "-v") == 0)
120 printf("%s %s\n(written by %s)\n\n",
121 LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
122 else if ((strcmp(argv[i], "-e") == 0 && i++) || strchr(argv[i], '=')) {
123 if (lua_dostring(argv[i]) != 0) {
124 fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
125 return 1;
128 else {
129 result = lua_dofile (argv[i]);
130 if (result) {
131 if (result == 2) {
132 fprintf(stderr, "lua: cannot execute file ");
133 perror(argv[i]);
135 return 1;
139 return result;