@@ -89,14 +89,15 @@ static void print_usage (const char *badoption) {
8989 lua_writestringerror (
9090 "usage: %s [options] [script [args]]\n"
9191 "Available options are:\n"
92- " -e stat execute string 'stat'\n"
93- " -i enter interactive mode after executing 'script'\n"
94- " -l name require library 'name' into global 'name'\n"
95- " -v show version information\n"
96- " -E ignore environment variables\n"
97- " -W turn warnings on\n"
98- " -- stop handling options\n"
99- " - stop handling options and execute stdin\n"
92+ " -e stat execute string 'stat'\n"
93+ " -i enter interactive mode after executing 'script'\n"
94+ " -l mod require library 'mod' into global 'mod'\n"
95+ " -l g=mod require library 'mod' into global 'g'\n"
96+ " -v show version information\n"
97+ " -E ignore environment variables\n"
98+ " -W turn warnings on\n"
99+ " -- stop handling options\n"
100+ " - stop handling options and execute stdin\n"
100101 ,
101102 progname );
102103}
@@ -207,16 +208,22 @@ static int dostring (lua_State *L, const char *s, const char *name) {
207208
208209
209210/*
210- ** Calls 'require(name)' and stores the result in a global variable
211- ** with the given name.
211+ ** Receives 'globname[=modname]' and runs 'globname = require(modname)'.
212212*/
213- static int dolibrary (lua_State * L , const char * name ) {
213+ static int dolibrary (lua_State * L , char * globname ) {
214214 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+ }
215222 lua_getglobal (L , "require" );
216- lua_pushstring (L , name );
217- status = docall (L , 1 , 1 ); /* call 'require(name )' */
223+ lua_pushstring (L , modname );
224+ status = docall (L , 1 , 1 ); /* call 'require(modname )' */
218225 if (status == LUA_OK )
219- lua_setglobal (L , name ); /* global[name] = require return */
226+ lua_setglobal (L , globname ); /* globname = require(modname) */
220227 return report (L , status );
221228}
222229
@@ -327,7 +334,7 @@ static int runargs (lua_State *L, char **argv, int n) {
327334 switch (option ) {
328335 case 'e' : case 'l' : {
329336 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 */
331338 if (* extra == '\0' ) extra = argv [++ i ];
332339 lua_assert (extra != NULL );
333340 status = (option == 'e' )
0 commit comments