aboutsummaryrefslogtreecommitdiff
path: root/lua.c
diff options
context:
space:
mode:
Diffstat (limited to 'lua.c')
-rw-r--r--lua.c37
1 files changed, 22 insertions, 15 deletions
diff --git a/lua.c b/lua.c
index 46b48dba..0f190044 100644
--- a/lua.c
+++ b/lua.c
@@ -89,14 +89,15 @@ static void print_usage (const char *badoption) {
89 lua_writestringerror( 89 lua_writestringerror(
90 "usage: %s [options] [script [args]]\n" 90 "usage: %s [options] [script [args]]\n"
91 "Available options are:\n" 91 "Available options are:\n"
92 " -e stat execute string 'stat'\n" 92 " -e stat execute string 'stat'\n"
93 " -i enter interactive mode after executing 'script'\n" 93 " -i enter interactive mode after executing 'script'\n"
94 " -l name require library 'name' into global 'name'\n" 94 " -l mod require library 'mod' into global 'mod'\n"
95 " -v show version information\n" 95 " -l g=mod require library 'mod' into global 'g'\n"
96 " -E ignore environment variables\n" 96 " -v show version information\n"
97 " -W turn warnings on\n" 97 " -E ignore environment variables\n"
98 " -- stop handling options\n" 98 " -W turn warnings on\n"
99 " - stop handling options and execute stdin\n" 99 " -- stop handling options\n"
100 " - stop handling options and execute stdin\n"
100 , 101 ,
101 progname); 102 progname);
102} 103}
@@ -207,16 +208,22 @@ static int dostring (lua_State *L, const char *s, const char *name) {
207 208
208 209
209/* 210/*
210** Calls 'require(name)' and stores the result in a global variable 211** Receives 'globname[=modname]' and runs 'globname = require(modname)'.
211** with the given name.
212*/ 212*/
213static int dolibrary (lua_State *L, const char *name) { 213static int dolibrary (lua_State *L, char *globname) {
214 int status; 214 int status;
215 char *modname = strchr(globname, '=');
216 if (modname == NULL) /* no explicit name? */
217 modname = globname; /* module name is equal to global name */
218 else {
219 *modname = '\0'; /* global name ends here */
220 modname++; /* module name starts after the '=' */
221 }
215 lua_getglobal(L, "require"); 222 lua_getglobal(L, "require");
216 lua_pushstring(L, name); 223 lua_pushstring(L, modname);
217 status = docall(L, 1, 1); /* call 'require(name)' */ 224 status = docall(L, 1, 1); /* call 'require(modname)' */
218 if (status == LUA_OK) 225 if (status == LUA_OK)
219 lua_setglobal(L, name); /* global[name] = require return */ 226 lua_setglobal(L, globname); /* globname = require(modname) */
220 return report(L, status); 227 return report(L, status);
221} 228}
222 229
@@ -327,7 +334,7 @@ static int runargs (lua_State *L, char **argv, int n) {
327 switch (option) { 334 switch (option) {
328 case 'e': case 'l': { 335 case 'e': case 'l': {
329 int status; 336 int status;
330 const char *extra = argv[i] + 2; /* both options need an argument */ 337 char *extra = argv[i] + 2; /* both options need an argument */
331 if (*extra == '\0') extra = argv[++i]; 338 if (*extra == '\0') extra = argv[++i];
332 lua_assert(extra != NULL); 339 lua_assert(extra != NULL);
333 status = (option == 'e') 340 status = (option == 'e')