lua/lua.c

89 lines
1.8 KiB
C
Raw Normal View History

1993-07-28 06:18:00 -07:00
/*
** $Id: lua.c,v 1.3 1997/10/16 18:35:59 roberto Exp roberto $
1997-09-16 12:25:59 -07:00
** Lua stand-alone interpreter
** See Copyright Notice in lua.h
1993-07-28 06:18:00 -07:00
*/
1993-12-17 10:41:19 -08:00
1993-07-28 06:18:00 -07:00
#include <stdio.h>
1995-02-07 08:04:15 -08:00
#include <string.h>
1993-07-28 06:18:00 -07:00
#include "lua.h"
1997-09-16 12:25:59 -07:00
#include "luadebug.h"
1993-07-28 06:18:00 -07:00
#include "lualib.h"
1997-09-16 12:25:59 -07:00
#ifndef OLD_ANSI
#include <locale.h>
1996-05-03 13:10:59 -07:00
#else
1997-09-16 12:25:59 -07:00
#define setlocale(a,b) 0
1996-05-03 13:10:59 -07:00
#endif
1997-09-16 12:25:59 -07:00
#ifdef _POSIX_SOURCE
#include <unistd.h>
#else
1997-09-16 12:25:59 -07:00
#define isatty(x) (x==0) /* assume stdin is a tty */
#endif
static void manual_input (void)
{
if (isatty(0)) {
char buffer[250];
1997-10-06 07:51:32 -07:00
while (1) {
lua_beginblock();
1997-10-06 07:51:32 -07:00
printf("%s", lua_getstring(lua_getglobal("_PROMPT")));
if (fgets(buffer, sizeof(buffer), stdin) == 0)
break;
lua_dostring(buffer);
lua_endblock();
}
1997-10-06 07:51:32 -07:00
printf("\n");
}
else
lua_dofile(NULL); /* executes stdin as a file */
}
1994-11-28 09:12:49 -08:00
int main (int argc, char *argv[])
1993-07-28 06:18:00 -07:00
{
int i;
1997-07-01 12:32:41 -07:00
setlocale(LC_ALL, "");
1997-10-06 07:51:32 -07:00
lua_iolibopen();
lua_strlibopen();
lua_mathlibopen();
1997-10-16 11:35:59 -07:00
lua_pushstring("> "); lua_setglobal("_PROMPT");
1997-10-06 07:51:32 -07:00
if (argc < 2) {
printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
manual_input();
1997-10-06 07:51:32 -07:00
}
else for (i=1; i<argc; i++) {
if (strcmp(argv[i], "-") == 0)
manual_input();
1997-09-16 12:25:59 -07:00
else if (strcmp(argv[i], "-d") == 0)
lua_debug = 1;
else if (strcmp(argv[i], "-v") == 0)
printf("%s %s\n(written by %s)\n\n",
LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
else if ((strcmp(argv[i], "-e") == 0 && i++) || strchr(argv[i], '=')) {
if (lua_dostring(argv[i]) != 0) {
fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
return 1;
}
}
else {
1997-09-16 12:25:59 -07:00
int result = lua_dofile (argv[i]);
if (result) {
if (result == 2) {
fprintf(stderr, "lua: cannot execute file ");
perror(argv[i]);
}
return 1;
}
}
}
/* lua_close(); */
1997-09-16 12:25:59 -07:00
return 0;
1993-07-28 06:18:00 -07:00
}