diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-12-21 15:34:23 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-12-21 15:34:23 -0200 |
commit | 3daeabb60667adc8d4b9c570631704548099a7bf (patch) | |
tree | c2364e9fc53bf88d451247d984b75e27d9fd4903 /lua.c | |
parent | cfdebfbc5361ee9779f39e076afef7c7474fb3de (diff) | |
download | lua-3daeabb60667adc8d4b9c570631704548099a7bf.tar.gz lua-3daeabb60667adc8d4b9c570631704548099a7bf.tar.bz2 lua-3daeabb60667adc8d4b9c570631704548099a7bf.zip |
new way to pass arguments: with option `-f'
Diffstat (limited to 'lua.c')
-rw-r--r-- | lua.c | 61 |
1 files changed, 36 insertions, 25 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.c,v 1.28 1999/12/06 11:41:28 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.29 1999/12/20 13:03:20 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 | */ |
@@ -74,7 +74,7 @@ static void print_message (void) { | |||
74 | " -q interactive mode without prompt\n" | 74 | " -q interactive mode without prompt\n" |
75 | " -i interactive mode with prompt\n" | 75 | " -i interactive mode with prompt\n" |
76 | " - execute stdin as a file\n" | 76 | " - execute stdin as a file\n" |
77 | " -- start arguments for table `arg'\n" | 77 | " -f name dofile `name' with following arguments in table `arg'\n" |
78 | " a=b set global `a' with string `b'\n" | 78 | " a=b set global `a' with string `b'\n" |
79 | " name dofile `name'\n\n"); | 79 | " name dofile `name'\n\n"); |
80 | } | 80 | } |
@@ -96,27 +96,34 @@ static void assign (char *arg) { | |||
96 | 96 | ||
97 | static void getargs (int argc, char *argv[]) { | 97 | static void getargs (int argc, char *argv[]) { |
98 | lua_beginblock(); { | 98 | lua_beginblock(); { |
99 | int i, j; | 99 | int i; |
100 | lua_Object args = lua_createtable(); | 100 | lua_Object args = lua_createtable(); |
101 | lua_pushobject(args); | 101 | lua_pushobject(args); |
102 | lua_setglobal("arg"); | 102 | lua_setglobal("arg"); |
103 | for (i=0; i<argc; i++) | 103 | for (i=0; i<argc; i++) { |
104 | if (strcmp(argv[i], "--") == 0) break; | 104 | /* arg[i] = argv[i] */ |
105 | for (j = 0; j<argc; j++) { | 105 | lua_pushobject(args); lua_pushnumber(i); |
106 | /* arg[j-i] = argv[j] */ | 106 | lua_pushstring(argv[i]); lua_settable(); |
107 | lua_pushobject(args); lua_pushnumber(j-i); | ||
108 | lua_pushstring(argv[j]); lua_settable(); | ||
109 | } | 107 | } |
110 | /* arg.n = maximum index in table `arg' */ | 108 | /* arg.n = maximum index in table `arg' */ |
111 | lua_pushobject(args); lua_pushstring("n"); | 109 | lua_pushobject(args); lua_pushstring("n"); |
112 | lua_pushnumber(argc-(i+1)); lua_settable(); | 110 | lua_pushnumber(argc-1); lua_settable(); |
113 | /* arg.nn = minimum index in table `arg' */ | ||
114 | lua_pushobject(args); lua_pushstring("nn"); | ||
115 | lua_pushnumber(-i); lua_settable(); | ||
116 | } lua_endblock(); | 111 | } lua_endblock(); |
117 | } | 112 | } |
118 | 113 | ||
119 | 114 | ||
115 | static void file_input (char **argv, int arg) { | ||
116 | int result = ldo(lua_dofile, argv[arg]); | ||
117 | if (result) { | ||
118 | if (result == 2) { | ||
119 | fprintf(stderr, "lua: cannot execute file "); | ||
120 | perror(argv[arg]); | ||
121 | } | ||
122 | exit(1); | ||
123 | } | ||
124 | } | ||
125 | |||
126 | |||
120 | static void manual_input (int prompt) { | 127 | static void manual_input (int prompt) { |
121 | int cont = 1; | 128 | int cont = 1; |
122 | while (cont) { | 129 | while (cont) { |
@@ -157,7 +164,6 @@ int main (int argc, char *argv[]) { | |||
157 | int i; | 164 | int i; |
158 | lua_state = lua_newstate("stack", 1024, "builtin", 1, NULL); | 165 | lua_state = lua_newstate("stack", 1024, "builtin", 1, NULL); |
159 | lua_userinit(); | 166 | lua_userinit(); |
160 | getargs(argc, argv); | ||
161 | if (argc < 2) { /* no arguments? */ | 167 | if (argc < 2) { /* no arguments? */ |
162 | if (isatty(0)) { | 168 | if (isatty(0)) { |
163 | printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT); | 169 | printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT); |
@@ -187,11 +193,24 @@ int main (int argc, char *argv[]) { | |||
187 | break; | 193 | break; |
188 | case 'e': | 194 | case 'e': |
189 | i++; | 195 | i++; |
196 | if (i>=argc) { | ||
197 | print_message(); | ||
198 | exit(1); | ||
199 | } | ||
190 | if (ldo(lua_dostring, argv[i]) != 0) { | 200 | if (ldo(lua_dostring, argv[i]) != 0) { |
191 | fprintf(stderr, "lua: error running argument `%s'\n", argv[i]); | 201 | fprintf(stderr, "lua: error running argument `%s'\n", argv[i]); |
192 | return 1; | 202 | exit(1); |
193 | } | 203 | } |
194 | break; | 204 | break; |
205 | case 'f': | ||
206 | i++; | ||
207 | if (i>=argc) { | ||
208 | print_message(); | ||
209 | exit(1); | ||
210 | } | ||
211 | getargs(argc-i, argv+i); /* collect following arguments */ | ||
212 | file_input(argv, i); | ||
213 | return 0; /* stop running arguments */ | ||
195 | case '-': | 214 | case '-': |
196 | i = argc; /* end loop */ | 215 | i = argc; /* end loop */ |
197 | break; | 216 | break; |
@@ -202,16 +221,8 @@ int main (int argc, char *argv[]) { | |||
202 | } | 221 | } |
203 | else if (strchr(argv[i], '=')) | 222 | else if (strchr(argv[i], '=')) |
204 | assign(argv[i]); | 223 | assign(argv[i]); |
205 | else { | 224 | else |
206 | int result = ldo(lua_dofile, argv[i]); | 225 | file_input(argv, i); |
207 | if (result) { | ||
208 | if (result == 2) { | ||
209 | fprintf(stderr, "lua: cannot execute file "); | ||
210 | perror(argv[i]); | ||
211 | } | ||
212 | exit(1); | ||
213 | } | ||
214 | } | ||
215 | } | 226 | } |
216 | #ifdef DEBUG | 227 | #ifdef DEBUG |
217 | lua_close(); | 228 | lua_close(); |