mirror of https://github.com/rusefi/lua.git
new option "-x" to execute a string given as argument.
This commit is contained in:
parent
5cddb264d4
commit
0baa915343
94
lua.c
94
lua.c
|
@ -3,7 +3,7 @@
|
||||||
** Linguagem para Usuarios de Aplicacao
|
** Linguagem para Usuarios de Aplicacao
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char *rcs_lua="$Id: lua.c,v 1.9 1996/04/23 12:43:07 roberto Exp roberto $";
|
char *rcs_lua="$Id: lua.c,v 1.10 1996/05/03 19:20:17 roberto Exp roberto $";
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -11,37 +11,13 @@ char *rcs_lua="$Id: lua.c,v 1.9 1996/04/23 12:43:07 roberto Exp roberto $";
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
|
|
||||||
static int lua_argc;
|
|
||||||
static char **lua_argv;
|
|
||||||
|
|
||||||
#ifdef POSIX
|
#ifdef POSIX
|
||||||
/*
|
#include <unistd.h>
|
||||||
** although this function is POSIX, there is no standard header file that
|
|
||||||
** defines it
|
|
||||||
*/
|
|
||||||
int isatty (int fd);
|
|
||||||
#else
|
#else
|
||||||
#define isatty(x) (x==0) /* assume stdin is a tty */
|
#define isatty(x) (x==0) /* assume stdin is a tty */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
|
||||||
%F Allow Lua code to access argv strings.
|
|
||||||
%i Receive from Lua the argument number (starting with 1).
|
|
||||||
%o Return to Lua the argument, or nil if it does not exist.
|
|
||||||
*/
|
|
||||||
static void lua_getargv (void)
|
|
||||||
{
|
|
||||||
lua_Object lo = lua_getparam(1);
|
|
||||||
if (!lua_isnumber(lo))
|
|
||||||
lua_pushnil();
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int n = (int)lua_getnumber(lo);
|
|
||||||
if (n < 1 || n > lua_argc) lua_pushnil();
|
|
||||||
else lua_pushstring(lua_argv[n]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void manual_input (void)
|
static void manual_input (void)
|
||||||
{
|
{
|
||||||
|
@ -58,43 +34,37 @@ static void manual_input (void)
|
||||||
|
|
||||||
int main (int argc, char *argv[])
|
int main (int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int result = 0;
|
int result = 0;
|
||||||
iolib_open ();
|
iolib_open ();
|
||||||
strlib_open ();
|
strlib_open ();
|
||||||
mathlib_open ();
|
mathlib_open ();
|
||||||
|
|
||||||
lua_register("argv", lua_getargv);
|
if (argc < 2)
|
||||||
|
|
||||||
if (argc < 2)
|
|
||||||
manual_input();
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (i=1; i<argc; i++)
|
|
||||||
if (strcmp(argv[i], "--") == 0)
|
|
||||||
{
|
|
||||||
lua_argc = argc-i-1;
|
|
||||||
lua_argv = argv+i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
for (i=1; i<argc; i++)
|
|
||||||
{
|
|
||||||
if (strcmp(argv[i], "--") == 0)
|
|
||||||
break;
|
|
||||||
else if (strcmp(argv[i], "-") == 0)
|
|
||||||
manual_input();
|
manual_input();
|
||||||
else if (strcmp(argv[i], "-v") == 0)
|
else for (i=1; i<argc; i++) {
|
||||||
printf("%s %s\n(written by %s)\n\n",
|
if (strcmp(argv[i], "-") == 0)
|
||||||
LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
|
manual_input();
|
||||||
else
|
else if (strcmp(argv[i], "-v") == 0)
|
||||||
{
|
printf("%s %s\n(written by %s)\n\n",
|
||||||
result = lua_dofile (argv[i]);
|
LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
|
||||||
if (result)
|
else if (strcmp(argv[i], "-x") == 0) {
|
||||||
fprintf(stderr, "lua: error trying to run file %s\n", argv[i]);
|
if (lua_dostring(argv[++i]) != 0) {
|
||||||
}
|
fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result = lua_dofile (argv[i]);
|
||||||
|
if (result) {
|
||||||
|
if (result == 2) {
|
||||||
|
fprintf(stderr, "lua: cannot execute file `%s' - ", argv[i]);
|
||||||
|
perror(NULL);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
return result;
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue