diff options
author | Waldemar Celes <celes@tecgraf.puc-rio.br> | 1994-12-14 17:58:20 -0200 |
---|---|---|
committer | Waldemar Celes <celes@tecgraf.puc-rio.br> | 1994-12-14 17:58:20 -0200 |
commit | 891cab8a31ec73dddb5aa896abedbac53b4c16f8 (patch) | |
tree | 3835a8d79cf20cef44ea94b8126927a057c07e9b | |
parent | 2486d677c94479a42231f445d95f11924feec6a2 (diff) | |
download | lua-891cab8a31ec73dddb5aa896abedbac53b4c16f8.tar.gz lua-891cab8a31ec73dddb5aa896abedbac53b4c16f8.tar.bz2 lua-891cab8a31ec73dddb5aa896abedbac53b4c16f8.zip |
Passar os argv apos '--' para Lua.
Captura-se cada argv com a funcao argv(i), onde i = 1, 2, ...
-rw-r--r-- | lua.c | 44 |
1 files changed, 42 insertions, 2 deletions
@@ -3,13 +3,34 @@ | |||
3 | ** Linguagem para Usuarios de Aplicacao | 3 | ** Linguagem para Usuarios de Aplicacao |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_lua="$Id: lua.c,v 1.1 1993/12/17 18:41:19 celes Stab roberto $"; | 6 | char *rcs_lua="$Id: lua.c,v 1.2 1994/11/28 17:12:49 roberto Exp celes $"; |
7 | 7 | ||
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | 9 | ||
10 | #include "lua.h" | 10 | #include "lua.h" |
11 | #include "lualib.h" | 11 | #include "lualib.h" |
12 | 12 | ||
13 | static int lua_argc; | ||
14 | static char **lua_argv; | ||
15 | |||
16 | /* | ||
17 | %F Allow Lua code to access argv strings. | ||
18 | %i Receive from Lua the argument number (starting with 1). | ||
19 | %o Return to Lua the argument, or nil if it does not exist. | ||
20 | */ | ||
21 | static void lua_getargv (void) | ||
22 | { | ||
23 | lua_Object lo = lua_getparam(1); | ||
24 | if (!lua_isnumber(lo)) | ||
25 | lua_pushnil(); | ||
26 | else | ||
27 | { | ||
28 | int n = (int)lua_getnumber(lo); | ||
29 | if (n < 1 || n > lua_argc) lua_pushnil(); | ||
30 | else lua_pushstring(lua_argv[n]); | ||
31 | } | ||
32 | } | ||
33 | |||
13 | 34 | ||
14 | int main (int argc, char *argv[]) | 35 | int main (int argc, char *argv[]) |
15 | { | 36 | { |
@@ -18,6 +39,9 @@ int main (int argc, char *argv[]) | |||
18 | iolib_open (); | 39 | iolib_open (); |
19 | strlib_open (); | 40 | strlib_open (); |
20 | mathlib_open (); | 41 | mathlib_open (); |
42 | |||
43 | lua_register("argv", lua_getargv); | ||
44 | |||
21 | if (argc < 2) | 45 | if (argc < 2) |
22 | { | 46 | { |
23 | char buffer[250]; | 47 | char buffer[250]; |
@@ -25,8 +49,24 @@ int main (int argc, char *argv[]) | |||
25 | result = lua_dostring(buffer); | 49 | result = lua_dostring(buffer); |
26 | } | 50 | } |
27 | else | 51 | else |
28 | for (i=1; i<argc; i++) | 52 | { |
53 | for (i=1; i<argc; i++) | ||
54 | { | ||
55 | if (strcmp(argv[i], "--") == 0) | ||
56 | { | ||
57 | lua_argc = argc-i-1; | ||
58 | lua_argv = argv+i; | ||
59 | break; | ||
60 | } | ||
61 | } | ||
62 | for (i=1; i<argc; i++) | ||
63 | { | ||
64 | if (strcmp(argv[i], "--") == 0) | ||
65 | break; | ||
66 | else | ||
29 | result = lua_dofile (argv[i]); | 67 | result = lua_dofile (argv[i]); |
68 | } | ||
69 | } | ||
30 | return result; | 70 | return result; |
31 | } | 71 | } |
32 | 72 | ||