History

Lua 5.2.0-work3

Lua Team committed 16 years ago

Commit d618ddd1 parent be77f2777 files · +3825 −3949Browse files

Showing the changes to src/lua.c (1 of 77 files). Show all

Unified · Split

src/lua.c+27−25
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lua.c,v 1.182 2009/12/22 16:47:12 roberto Exp $
2+** $Id: lua.c,v 1.190 2010/04/14 15:14:21 roberto Exp $
33 ** Lua stand-alone interpreter
44 ** See Copyright Notice in lua.h
55 */
@@ -102,26 +102,31 @@ static void laction (int i) { · 96 unmodified lines
102102 }
103103
104104
105−static void print_usage (void) {
106− fprintf(stderr,
105+static void print_usage (const char *badoption) {
106+ if (badoption[1] == 'e' || badoption[1] == 'l') {
107+ luai_writestringerror("%s: ", progname);
108+ luai_writestringerror("'%s' needs argument\n", badoption);
109+ } else {
110+ luai_writestringerror("%s: ", progname);
111+ luai_writestringerror("unrecognized option '%s'\n", badoption);
112+ }
113+ luai_writestringerror(
107114 "usage: %s [options] [script [args]]\n"
108115 "Available options are:\n"
109116 " -e stat execute string " LUA_QL("stat") "\n"
110− " -l name require library " LUA_QL("name") "\n"
111117 " -i enter interactive mode after executing " LUA_QL("script") "\n"
118+ " -l name require library " LUA_QL("name") "\n"
112119 " -v show version information\n"
113120 " -- stop handling options\n"
114− " - execute stdin and stop handling options\n"
121+ " - stop handling options and execute stdin\n"
115122 ,
116123 progname);
117− fflush(stderr);
118124 }
119125
120126
121127 static void l_message (const char *pname, const char *msg) {
122− if (pname) fprintf(stderr, "%s: ", pname);
123− fprintf(stderr, "%s\n", msg);
124− fflush(stderr);
128+ if (pname) luai_writestringerror("%s: ", pname);
129+ luai_writestringerror("%s\n", msg);
125130 }
126131
127132
@@ -214,7 +219,7 @@ static int dostring (lua_State *L, const char *s, const char *name) { · 86 unmodified lines
214219
215220
216221 static int dolibrary (lua_State *L, const char *name) {
217− lua_getfield(L, LUA_ENVIRONINDEX, "require");
222+ lua_getglobal(L, "require");
218223 lua_pushstring(L, name);
219224 return report(L, docall(L, 1, 1));
220225 }
@@ -222,7 +227,7 @@ static int dolibrary (lua_State *L, const char *name) { · 1 unmodified line
222227
223228 static const char *get_prompt (lua_State *L, int firstline) {
224229 const char *p;
225− lua_getfield(L, LUA_ENVIRONINDEX, firstline ? "_PROMPT" : "_PROMPT2");
230+ lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
226231 p = lua_tostring(L, -1);
227232 if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
228233 lua_pop(L, 1); /* remove global */
@@ -296,7 +301,7 @@ static void dotty (lua_State *L) { · 67 unmodified lines
296301 report(L, status);
297302 if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */
298303 luaL_checkstack(L, LUA_MINSTACK, "too many results to print");
299− lua_getfield(L, LUA_ENVIRONINDEX, "print");
304+ lua_getglobal(L, "print");
300305 lua_insert(L, 1);
301306 if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != LUA_OK)
302307 l_message(progname, lua_pushfstring(L,
@@ -306,7 +311,6 @@ static void dotty (lua_State *L) { · 3 unmodified lines
306311 }
307312 lua_settop(L, 0); /* clear stack */
308313 luai_writestring("\n", 1);
309− fflush(stdout);
310314 progname = oldprogname;
311315 }
312316
@@ -315,7 +319,7 @@ static int handle_script (lua_State *L, char **argv, int n) { · 2 unmodified lines
315319 int status;
316320 const char *fname;
317321 int narg = getargs(L, argv, n); /* collect arguments */
318− lua_setfield(L, LUA_ENVIRONINDEX, "arg");
322+ lua_setglobal(L, "arg");
319323 fname = argv[n];
320324 if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0)
321325 fname = NULL; /* stdin */
@@ -356,10 +360,11 @@ static int collectargs (char **argv, int *pi, int *pv, int *pe) { · 34 unmodified lines
356360 case 'l':
357361 if (argv[i][2] == '\0') {
358362 i++;
359− if (argv[i] == NULL) return -1;
363+ if (argv[i] == NULL) return -(i - 1);
360364 }
361365 break;
362− default: return -1; /* invalid option */
366+ default: /* invalid option; return its index... */
367+ return -i; /* ...as a negative value */
363368 }
364369 }
365370 return 0;
@@ -369,7 +374,6 @@ static int collectargs (char **argv, int *pi, int *pv, int *pe) { · 3 unmodified lines
369374 static int runargs (lua_State *L, char **argv, int n) {
370375 int i;
371376 for (i = 1; i < n; i++) {
372− if (argv[i] == NULL) continue;
373377 lua_assert(argv[i][0] == '-');
374378 switch (argv[i][1]) { /* option */
375379 case 'e': {
@@ -412,8 +416,8 @@ static int pmain (lua_State *L) { · 36 unmodified lines
412416 int has_i = 0, has_v = 0, has_e = 0;
413417 if (argv[0] && argv[0][0]) progname = argv[0];
414418 script = collectargs(argv, &has_i, &has_v, &has_e);
415− if (script < 0) { /* invalid args? */
416− print_usage();
419+ if (script < 0) { /* invalid arg? */
420+ print_usage(argv[-script]);
417421 return 0;
418422 }
419423 if (has_v) print_version();
@@ -443,7 +447,6 @@ static int pmain (lua_State *L) { · 23 unmodified lines
443447
444448
445449 int main (int argc, char **argv) {
446− static lua_CFunction ppmain = &pmain;
447450 int status, result;
448451 lua_State *L = luaL_newstate(); /* create state */
449452 if (L == NULL) {
@@ -451,11 +454,10 @@ int main (int argc, char **argv) { · 1 unmodified line
451454 return EXIT_FAILURE;
452455 }
453456 /* call 'pmain' in protected mode */
454− lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_CPCALL); /* calling function */
455− lua_pushlightuserdata(L, &ppmain);
456− lua_pushinteger(L, argc);
457− lua_pushlightuserdata(L, argv);
458− status = lua_pcall(L, 3, 1, 0);
457+ lua_pushcfunction(L, &pmain);
458+ lua_pushinteger(L, argc); /* 1st argument */
459+ lua_pushlightuserdata(L, argv); /* 2nd argument */
460+ status = lua_pcall(L, 2, 1, 0);
459461 result = lua_toboolean(L, -1); /* get result */
460462 finalreport(L, status);
461463 lua_close(L);