summaryrefslogtreecommitdiff
path: root/lua.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-06-10 16:35:46 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-06-10 16:35:46 -0300
commit0baa915343cbf7fd1f310ea640a0d72ec626273b (patch)
tree82e6e24877ee9f9064460410f2ea627db4cbb98c /lua.c
parent5cddb264d430d7c9b4defd63d823d98bf002f4d1 (diff)
downloadlua-0baa915343cbf7fd1f310ea640a0d72ec626273b.tar.gz
lua-0baa915343cbf7fd1f310ea640a0d72ec626273b.tar.bz2
lua-0baa915343cbf7fd1f310ea640a0d72ec626273b.zip
new option "-x" to execute a string given as argument.
Diffstat (limited to 'lua.c')
-rw-r--r--lua.c94
1 files changed, 32 insertions, 62 deletions
diff --git a/lua.c b/lua.c
index e755993c..28a65e7a 100644
--- a/lua.c
+++ b/lua.c
@@ -3,7 +3,7 @@
3** Linguagem para Usuarios de Aplicacao 3** Linguagem para Usuarios de Aplicacao
4*/ 4*/
5 5
6char *rcs_lua="$Id: lua.c,v 1.9 1996/04/23 12:43:07 roberto Exp roberto $"; 6char *rcs_lua="$Id: lua.c,v 1.10 1996/05/03 19:20:17 roberto Exp roberto $";
7 7
8#include <stdio.h> 8#include <stdio.h>
9#include <string.h> 9#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 $";
11#include "lua.h" 11#include "lua.h"
12#include "lualib.h" 12#include "lualib.h"
13 13
14static int lua_argc;
15static char **lua_argv;
16 14
17#ifdef POSIX 15#ifdef POSIX
18/* 16#include <unistd.h>
19** although this function is POSIX, there is no standard header file that
20** defines it
21*/
22int isatty (int fd);
23#else 17#else
24#define isatty(x) (x==0) /* assume stdin is a tty */ 18#define isatty(x) (x==0) /* assume stdin is a tty */
25#endif 19#endif
26 20
27/*
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.
31*/
32static void lua_getargv (void)
33{
34 lua_Object lo = lua_getparam(1);
35 if (!lua_isnumber(lo))
36 lua_pushnil();
37 else
38 {
39 int n = (int)lua_getnumber(lo);
40 if (n < 1 || n > lua_argc) lua_pushnil();
41 else lua_pushstring(lua_argv[n]);
42 }
43}
44
45 21
46static void manual_input (void) 22static void manual_input (void)
47{ 23{
@@ -58,43 +34,37 @@ static void manual_input (void)
58 34
59int main (int argc, char *argv[]) 35int main (int argc, char *argv[])
60{ 36{
61 int i; 37 int i;
62 int result = 0; 38 int result = 0;
63 iolib_open (); 39 iolib_open ();
64 strlib_open (); 40 strlib_open ();
65 mathlib_open (); 41 mathlib_open ();
66 42
67 lua_register("argv", lua_getargv); 43 if (argc < 2)
68
69 if (argc < 2)
70 manual_input();
71 else
72 {
73 for (i=1; i<argc; i++)
74 if (strcmp(argv[i], "--") == 0)
75 {
76 lua_argc = argc-i-1;
77 lua_argv = argv+i;
78 break;
79 }
80 for (i=1; i<argc; i++)
81 {
82 if (strcmp(argv[i], "--") == 0)
83 break;
84 else if (strcmp(argv[i], "-") == 0)
85 manual_input(); 44 manual_input();
86 else if (strcmp(argv[i], "-v") == 0) 45 else for (i=1; i<argc; i++) {
87 printf("%s %s\n(written by %s)\n\n", 46 if (strcmp(argv[i], "-") == 0)
88 LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS); 47 manual_input();
89 else 48 else if (strcmp(argv[i], "-v") == 0)
90 { 49 printf("%s %s\n(written by %s)\n\n",
91 result = lua_dofile (argv[i]); 50 LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
92 if (result) 51 else if (strcmp(argv[i], "-x") == 0) {
93 fprintf(stderr, "lua: error trying to run file %s\n", argv[i]); 52 if (lua_dostring(argv[++i]) != 0) {
94 } 53 fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
54 return 1;
55 }
56 }
57 else {
58 result = lua_dofile (argv[i]);
59 if (result) {
60 if (result == 2) {
61 fprintf(stderr, "lua: cannot execute file `%s' - ", argv[i]);
62 perror(NULL);
63 }
64 return 1;
65 }
66 }
95 } 67 }
96 } 68 return result;
97 return result;
98} 69}
99 70
100