diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-08-18 14:40:54 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-08-18 14:40:54 -0300 |
commit | b2c89ed2d21b50dea460415003e3f2f4d7188818 (patch) | |
tree | 8da39b62e211465da24171b1663ac12b1bb408cf /lua.c | |
parent | 2a03170ebd096288c833e034000dc177784f2040 (diff) | |
download | lua-b2c89ed2d21b50dea460415003e3f2f4d7188818.tar.gz lua-b2c89ed2d21b50dea460415003e3f2f4d7188818.tar.bz2 lua-b2c89ed2d21b50dea460415003e3f2f4d7188818.zip |
new mechanism to access argv from a script
Diffstat (limited to 'lua.c')
-rw-r--r-- | lua.c | 35 |
1 files changed, 30 insertions, 5 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.c,v 1.21 1999/07/02 18:22:38 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.22 1999/08/16 20:52:00 roberto Exp roberto $ |
3 | ** Lua stand-alone interpreter | 3 | ** Lua stand-alone interpreter |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -69,8 +69,9 @@ static void print_message (void) { | |||
69 | " -e stat dostring `stat'\n" | 69 | " -e stat dostring `stat'\n" |
70 | " -q interactive mode without prompt\n" | 70 | " -q interactive mode without prompt\n" |
71 | " -i interactive mode with prompt\n" | 71 | " -i interactive mode with prompt\n" |
72 | " - executes stdin as a file\n" | 72 | " - execute stdin as a file\n" |
73 | " a=b sets global `a' with string `b'\n" | 73 | " -- start arguments for table `arg'\n" |
74 | " a=b set global `a' with string `b'\n" | ||
74 | " name dofile `name'\n\n"); | 75 | " name dofile `name'\n\n"); |
75 | } | 76 | } |
76 | 77 | ||
@@ -89,6 +90,27 @@ static void assign (char *arg) { | |||
89 | } | 90 | } |
90 | 91 | ||
91 | 92 | ||
93 | static void getargs (int argc, char *argv[]) { | ||
94 | int i, j; | ||
95 | lua_Object args = lua_createtable(); | ||
96 | lua_pushobject(args); | ||
97 | lua_setglobal("arg"); | ||
98 | for (i=0; i<argc; i++) | ||
99 | if (strcmp(argv[i], "--") == 0) break; | ||
100 | for (j = 0; j<argc; j++) { | ||
101 | /* arg[j-i] = argv[j] */ | ||
102 | lua_pushobject(args); lua_pushnumber(j-i); | ||
103 | lua_pushstring(argv[j]); lua_settable(); | ||
104 | } | ||
105 | /* arg.n = maximum index in table `arg' */ | ||
106 | lua_pushobject(args); lua_pushstring("n"); | ||
107 | lua_pushnumber(argc-(i+1)); lua_settable(); | ||
108 | /* arg.nn = minimum index in table `arg' */ | ||
109 | lua_pushobject(args); lua_pushstring("nn"); | ||
110 | lua_pushnumber(-i); lua_settable(); | ||
111 | } | ||
112 | |||
113 | |||
92 | static void manual_input (int prompt) { | 114 | static void manual_input (int prompt) { |
93 | int cont = 1; | 115 | int cont = 1; |
94 | while (cont) { | 116 | while (cont) { |
@@ -122,12 +144,12 @@ static void manual_input (int prompt) { | |||
122 | } | 144 | } |
123 | 145 | ||
124 | 146 | ||
125 | int main (int argc, char *argv[]) | 147 | int main (int argc, char *argv[]) { |
126 | { | ||
127 | int i; | 148 | int i; |
128 | lua_open(); | 149 | lua_open(); |
129 | lua_pushstring("> "); lua_setglobal("_PROMPT"); | 150 | lua_pushstring("> "); lua_setglobal("_PROMPT"); |
130 | lua_userinit(); | 151 | lua_userinit(); |
152 | getargs(argc, argv); | ||
131 | if (argc < 2) { /* no arguments? */ | 153 | if (argc < 2) { /* no arguments? */ |
132 | if (isatty(0)) { | 154 | if (isatty(0)) { |
133 | printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT); | 155 | printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT); |
@@ -162,6 +184,9 @@ int main (int argc, char *argv[]) | |||
162 | return 1; | 184 | return 1; |
163 | } | 185 | } |
164 | break; | 186 | break; |
187 | case '-': | ||
188 | i = argc; /* end loop */ | ||
189 | break; | ||
165 | default: | 190 | default: |
166 | print_message(); | 191 | print_message(); |
167 | exit(1); | 192 | exit(1); |