diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-10-14 15:15:46 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-10-14 15:15:46 -0300 |
commit | fa41fafa6e18e304d8d2a30f890a6f9a73454468 (patch) | |
tree | 122a4804b09f6278de24202ea51593b58fb8792f /lua.c | |
parent | ebf646efb5d5c66939196c187a339fd6074d33f8 (diff) | |
download | lua-fa41fafa6e18e304d8d2a30f890a6f9a73454468.tar.gz lua-fa41fafa6e18e304d8d2a30f890a6f9a73454468.tar.bz2 lua-fa41fafa6e18e304d8d2a30f890a6f9a73454468.zip |
reorganization of argument handling
Diffstat (limited to 'lua.c')
-rw-r--r-- | lua.c | 184 |
1 files changed, 94 insertions, 90 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.c,v 1.149 2005/08/26 17:32:05 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.150 2005/09/06 17:19:33 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 | */ |
@@ -42,13 +42,14 @@ static void laction (int i) { | |||
42 | static void print_usage (void) { | 42 | static void print_usage (void) { |
43 | fprintf(stderr, | 43 | fprintf(stderr, |
44 | "usage: %s [options] [script [args]].\n" | 44 | "usage: %s [options] [script [args]].\n" |
45 | "'script' is a filename or '-' to execute stdin\n" | ||
45 | "Available options are:\n" | 46 | "Available options are:\n" |
46 | " - execute stdin as a file\n" | ||
47 | " -e stat execute string " LUA_QL("stat") "\n" | 47 | " -e stat execute string " LUA_QL("stat") "\n" |
48 | " -i enter interactive mode after executing " LUA_QL("script") "\n" | ||
49 | " -l name require library " LUA_QL("name") "\n" | 48 | " -l name require library " LUA_QL("name") "\n" |
49 | " -i enter interactive mode after executing " LUA_QL("script") "\n" | ||
50 | " -v show version information\n" | 50 | " -v show version information\n" |
51 | " -- stop handling options\n" , | 51 | " -- stop handling options\n" |
52 | , | ||
52 | progname); | 53 | progname); |
53 | fflush(stderr); | 54 | fflush(stderr); |
54 | } | 55 | } |
@@ -108,9 +109,12 @@ static void print_version (void) { | |||
108 | } | 109 | } |
109 | 110 | ||
110 | 111 | ||
111 | static int getargs (lua_State *L, int argc, char **argv, int n) { | 112 | static int getargs (lua_State *L, char **argv, int n) { |
112 | int narg = argc - (n + 1); /* number of arguments to the script */ | 113 | int narg; |
113 | int i; | 114 | int i; |
115 | int argc = 0; | ||
116 | while (argv[argc]) argc++; /* count total number of arguments */ | ||
117 | narg = argc - (n + 1); /* number of arguments to the script */ | ||
114 | luaL_checkstack(L, narg + 3, "too many arguments to script"); | 118 | luaL_checkstack(L, narg + 3, "too many arguments to script"); |
115 | for (i=n+1; i < argc; i++) | 119 | for (i=n+1; i < argc; i++) |
116 | lua_pushstring(L, argv[i]); | 120 | lua_pushstring(L, argv[i]); |
@@ -229,86 +233,71 @@ static void dotty (lua_State *L) { | |||
229 | } | 233 | } |
230 | 234 | ||
231 | 235 | ||
232 | #define clearinteractive(i) (*i &= 2) | 236 | static int handle_script (lua_State *L, char **argv, int n) { |
237 | int status; | ||
238 | const char *fname; | ||
239 | int narg = getargs(L, argv, n); /* collect arguments */ | ||
240 | lua_setglobal(L, "arg"); | ||
241 | fname = argv[n]; | ||
242 | if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0) | ||
243 | fname = NULL; /* stdin */ | ||
244 | status = luaL_loadfile(L, fname); | ||
245 | lua_insert(L, -(narg+1)); | ||
246 | if (status == 0) | ||
247 | status = docall(L, narg, 0); | ||
248 | else | ||
249 | lua_pop(L, narg); | ||
250 | return report(L, status); | ||
251 | } | ||
233 | 252 | ||
234 | static int handle_argv (lua_State *L, int argc, char **argv, int *interactive) { | 253 | |
235 | if (argv[1] == NULL) { /* no arguments? */ | 254 | static int collectargs (lua_State *L, char **argv, int *pi, int *pv, int *pe) { |
236 | *interactive = 0; | 255 | int i; |
237 | if (lua_stdin_is_tty()) | 256 | for (i = 1; argv[i] != NULL; i++) { |
238 | dotty(L); | 257 | if (argv[i][0] != '-') /* not an option? */ |
239 | else | 258 | return i; |
240 | dofile(L, NULL); /* executes stdin as a file */ | 259 | switch (argv[i][1]) { /* option */ |
241 | } | 260 | case '-': return (argv[i+1] != NULL ? i+1 : 0); |
242 | else { /* other arguments; loop over them */ | 261 | case '\0': return i; |
243 | int i; | 262 | case 'i': *pi = 1; break; |
244 | for (i = 1; argv[i] != NULL; i++) { | 263 | case 'v': *pv = 1; break; |
245 | if (argv[i][0] != '-') break; /* not an option? */ | 264 | case 'e': *pe = 1; /* go through */ |
246 | switch (argv[i][1]) { /* option */ | 265 | case 'l': |
247 | case '-': { /* `--' */ | 266 | if (argv[i][2] == '\0') { |
248 | if (argv[i][2] != '\0') { | 267 | i++; |
249 | print_usage(); | 268 | if (argv[i] == NULL) return -1; |
250 | return 1; | ||
251 | } | ||
252 | i++; /* skip this argument */ | ||
253 | goto endloop; /* stop handling arguments */ | ||
254 | } | ||
255 | case '\0': { | ||
256 | clearinteractive(interactive); | ||
257 | dofile(L, NULL); /* executes stdin as a file */ | ||
258 | break; | ||
259 | } | ||
260 | case 'i': { | ||
261 | *interactive = 2; /* force interactive mode after arguments */ | ||
262 | break; | ||
263 | } | ||
264 | case 'v': { | ||
265 | clearinteractive(interactive); | ||
266 | print_version(); | ||
267 | break; | ||
268 | } | ||
269 | case 'e': { | ||
270 | const char *chunk = argv[i] + 2; | ||
271 | clearinteractive(interactive); | ||
272 | if (*chunk == '\0') chunk = argv[++i]; | ||
273 | if (chunk == NULL) { | ||
274 | print_usage(); | ||
275 | return 1; | ||
276 | } | ||
277 | if (dostring(L, chunk, "=(command line)") != 0) | ||
278 | return 1; | ||
279 | break; | ||
280 | } | ||
281 | case 'l': { | ||
282 | const char *filename = argv[i] + 2; | ||
283 | if (*filename == '\0') filename = argv[++i]; | ||
284 | if (filename == NULL) { | ||
285 | print_usage(); | ||
286 | return 1; | ||
287 | } | ||
288 | if (dolibrary(L, filename)) | ||
289 | return 1; /* stop if file fails */ | ||
290 | break; | ||
291 | } | 269 | } |
292 | default: { | 270 | break; |
293 | clearinteractive(interactive); | 271 | default: return -1; /* invalid option */ |
294 | print_usage(); | 272 | } |
273 | } | ||
274 | return 0; | ||
275 | } | ||
276 | |||
277 | |||
278 | static int runargs (lua_State *L, char **argv, int n) { | ||
279 | int i; | ||
280 | for (i = 1; i < n; i++) { | ||
281 | if (argv[i] == NULL) continue; | ||
282 | lua_assert(argv[i][0] == '-'); | ||
283 | switch (argv[i][1]) { /* option */ | ||
284 | case 'e': { | ||
285 | const char *chunk = argv[i] + 2; | ||
286 | if (*chunk == '\0') chunk = argv[++i]; | ||
287 | lua_assert(chunk != NULL); | ||
288 | if (dostring(L, chunk, "=(command line)") != 0) | ||
295 | return 1; | 289 | return 1; |
296 | } | 290 | break; |
297 | } | 291 | } |
298 | } endloop: | 292 | case 'l': { |
299 | if (argv[i] != NULL) { | 293 | const char *filename = argv[i] + 2; |
300 | int status; | 294 | if (*filename == '\0') filename = argv[++i]; |
301 | const char *filename = argv[i]; | 295 | lua_assert(filename != NULL); |
302 | int narg = getargs(L, argc, argv, i); /* collect arguments */ | 296 | if (dolibrary(L, filename)) |
303 | lua_setglobal(L, "arg"); | 297 | return 1; /* stop if file fails */ |
304 | clearinteractive(interactive); | 298 | break; |
305 | status = luaL_loadfile(L, filename); | 299 | } |
306 | lua_insert(L, -(narg+1)); | 300 | default: break; |
307 | if (status == 0) | ||
308 | status = docall(L, narg, 0); | ||
309 | else | ||
310 | lua_pop(L, narg); | ||
311 | return report(L, status); | ||
312 | } | 301 | } |
313 | } | 302 | } |
314 | return 0; | 303 | return 0; |
@@ -334,17 +323,32 @@ struct Smain { | |||
334 | 323 | ||
335 | static int pmain (lua_State *L) { | 324 | static int pmain (lua_State *L) { |
336 | struct Smain *s = (struct Smain *)lua_touserdata(L, 1); | 325 | struct Smain *s = (struct Smain *)lua_touserdata(L, 1); |
337 | int status; | 326 | char **argv = s->argv; |
338 | int interactive = 1; | 327 | int script; |
339 | if (s->argv[0] && s->argv[0][0]) progname = s->argv[0]; | 328 | int has_i = 0, has_v = 0, has_e = 0; |
340 | globalL = L; | 329 | globalL = L; |
330 | if (argv[0] && argv[0][0]) progname = argv[0]; | ||
341 | luaL_openlibs(L); /* open libraries */ | 331 | luaL_openlibs(L); /* open libraries */ |
342 | status = handle_luainit(L); | 332 | s->status = handle_luainit(L); |
343 | if (status == 0) { | 333 | if (s->status != 0) return 0; |
344 | status = handle_argv(L, s->argc, s->argv, &interactive); | 334 | script = collectargs(L, argv, &has_i, &has_v, &has_e); |
345 | if (status == 0 && interactive) dotty(L); | 335 | if (script < 0) { /* invalid args? */ |
336 | print_usage(); | ||
337 | s->status = 1; | ||
338 | return 0; | ||
339 | } | ||
340 | if (has_v) print_version(); | ||
341 | s->status = runargs(L, argv, (script > 0) ? script : s->argc); | ||
342 | if (s->status != 0) return 0; | ||
343 | if (script) | ||
344 | s->status = handle_script(L, argv, script); | ||
345 | if (s->status != 0) return 0; | ||
346 | if (has_i) | ||
347 | dotty(L); | ||
348 | else if (script == 0 && !has_e && !has_v) { | ||
349 | if (lua_stdin_is_tty()) dotty(L); | ||
350 | else dofile(L, NULL); /* executes stdin as a file */ | ||
346 | } | 351 | } |
347 | s->status = status; | ||
348 | return 0; | 352 | return 0; |
349 | } | 353 | } |
350 | 354 | ||