diff options
Diffstat (limited to '')
-rw-r--r-- | lua.c | 71 |
1 files changed, 40 insertions, 31 deletions
@@ -303,7 +303,8 @@ static int collectargs (char **argv, int *first) { | |||
303 | case '-': /* '--' */ | 303 | case '-': /* '--' */ |
304 | if (argv[i][2] != '\0') /* extra characters after '--'? */ | 304 | if (argv[i][2] != '\0') /* extra characters after '--'? */ |
305 | return has_error; /* invalid option */ | 305 | return has_error; /* invalid option */ |
306 | *first = i + 1; | 306 | /* if there is a script name, it comes after '--' */ |
307 | *first = (argv[i + 1] != NULL) ? i + 1 : 0; | ||
307 | return args; | 308 | return args; |
308 | case '\0': /* '-' */ | 309 | case '\0': /* '-' */ |
309 | return args; /* script "name" is '-' */ | 310 | return args; /* script "name" is '-' */ |
@@ -432,32 +433,41 @@ static int handle_luainit (lua_State *L) { | |||
432 | 433 | ||
433 | 434 | ||
434 | /* | 435 | /* |
435 | ** lua_readline defines how to show a prompt and then read a line from | 436 | ** * lua_initreadline initializes the readline system. |
436 | ** the standard input. | 437 | ** * lua_readline defines how to show a prompt and then read a line from |
437 | ** lua_saveline defines how to "save" a read line in a "history". | 438 | ** the standard input. |
438 | ** lua_freeline defines how to free a line read by lua_readline. | 439 | ** * lua_saveline defines how to "save" a read line in a "history". |
440 | ** * lua_freeline defines how to free a line read by lua_readline. | ||
439 | */ | 441 | */ |
440 | 442 | ||
441 | #if defined(LUA_USE_READLINE) | 443 | #if !defined(lua_readline) /* { */ |
444 | /* Otherwise, all previously listed functions should be defined. */ | ||
445 | |||
446 | #if defined(LUA_USE_READLINE) /* { */ | ||
447 | /* Lua will be linked with '-lreadline' */ | ||
442 | 448 | ||
443 | #include <readline/readline.h> | 449 | #include <readline/readline.h> |
444 | #include <readline/history.h> | 450 | #include <readline/history.h> |
451 | |||
445 | #define lua_initreadline(L) ((void)L, rl_readline_name="lua") | 452 | #define lua_initreadline(L) ((void)L, rl_readline_name="lua") |
446 | #define lua_readline(b,p) ((void)b, readline(p)) | 453 | #define lua_readline(buff,prompt) ((void)buff, readline(prompt)) |
447 | #define lua_saveline(line) add_history(line) | 454 | #define lua_saveline(line) add_history(line) |
448 | #define lua_freeline(b) free(b) | 455 | #define lua_freeline(line) free(line) |
449 | 456 | ||
450 | #endif | 457 | #else /* }{ */ |
458 | /* use dynamically loaded readline (or nothing) */ | ||
451 | 459 | ||
460 | /* pointer to 'readline' function (if any) */ | ||
461 | typedef char *(*l_readlineT) (const char *prompt); | ||
462 | static l_readlineT l_readline = NULL; | ||
452 | 463 | ||
453 | #if !defined(lua_readline) /* { */ | 464 | /* pointer to 'add_history' function (if any) */ |
465 | typedef void (*l_addhistT) (const char *string); | ||
466 | static l_addhistT l_addhist = NULL; | ||
454 | 467 | ||
455 | /* pointer to dynamically loaded 'readline' function (if any) */ | ||
456 | typedef char *(*l_readline_t) (const char *prompt); | ||
457 | static l_readline_t l_readline = NULL; | ||
458 | 468 | ||
459 | static char *lua_readline (char *buff, const char *prompt) { | 469 | static char *lua_readline (char *buff, const char *prompt) { |
460 | if (l_readline != NULL) /* is there a dynamic 'readline'? */ | 470 | if (l_readline != NULL) /* is there a 'readline'? */ |
461 | return (*l_readline)(prompt); /* use it */ | 471 | return (*l_readline)(prompt); /* use it */ |
462 | else { /* emulate 'readline' over 'buff' */ | 472 | else { /* emulate 'readline' over 'buff' */ |
463 | fputs(prompt, stdout); | 473 | fputs(prompt, stdout); |
@@ -467,33 +477,25 @@ static char *lua_readline (char *buff, const char *prompt) { | |||
467 | } | 477 | } |
468 | 478 | ||
469 | 479 | ||
470 | /* pointer to dynamically loaded 'add_history' function (if any) */ | ||
471 | typedef void (*l_addhist_t) (const char *string); | ||
472 | static l_addhist_t l_addhist = NULL; | ||
473 | |||
474 | static void lua_saveline (const char *line) { | 480 | static void lua_saveline (const char *line) { |
475 | if (l_addhist != NULL) /* is there a dynamic 'add_history'? */ | 481 | if (l_addhist != NULL) /* is there an 'add_history'? */ |
476 | (*l_addhist)(line); /* use it */ | 482 | (*l_addhist)(line); /* use it */ |
477 | /* else nothing to be done */ | 483 | /* else nothing to be done */ |
478 | } | 484 | } |
479 | 485 | ||
480 | 486 | ||
481 | static void lua_freeline (char *line) { | 487 | static void lua_freeline (char *line) { |
482 | if (l_readline != NULL) /* is there a dynamic 'readline'? */ | 488 | if (l_readline != NULL) /* is there a 'readline'? */ |
483 | free(line); /* free line created by it */ | 489 | free(line); /* free line created by it */ |
484 | /* else 'lua_readline' used an automatic buffer; nothing to free */ | 490 | /* else 'lua_readline' used an automatic buffer; nothing to free */ |
485 | } | 491 | } |
486 | 492 | ||
487 | 493 | ||
488 | #if !defined(LUA_USE_DLOPEN) || !defined(LUA_READLINELIB) | 494 | #if defined(LUA_USE_DLOPEN) && defined(LUA_READLINELIB) /* { */ |
489 | 495 | /* try to load 'readline' dynamically */ | |
490 | #define lua_initreadline(L) ((void)L) | ||
491 | |||
492 | #else /* { */ | ||
493 | 496 | ||
494 | #include <dlfcn.h> | 497 | #include <dlfcn.h> |
495 | 498 | ||
496 | |||
497 | static void lua_initreadline (lua_State *L) { | 499 | static void lua_initreadline (lua_State *L) { |
498 | void *lib = dlopen(LUA_READLINELIB, RTLD_NOW | RTLD_LOCAL); | 500 | void *lib = dlopen(LUA_READLINELIB, RTLD_NOW | RTLD_LOCAL); |
499 | if (lib == NULL) | 501 | if (lib == NULL) |
@@ -501,16 +503,23 @@ static void lua_initreadline (lua_State *L) { | |||
501 | else { | 503 | else { |
502 | const char **name = cast(const char**, dlsym(lib, "rl_readline_name")); | 504 | const char **name = cast(const char**, dlsym(lib, "rl_readline_name")); |
503 | if (name != NULL) | 505 | if (name != NULL) |
504 | *name = "Lua"; | 506 | *name = "lua"; |
505 | l_readline = cast(l_readline_t, cast_func(dlsym(lib, "readline"))); | 507 | l_readline = cast(l_readlineT, cast_func(dlsym(lib, "readline"))); |
508 | l_addhist = cast(l_addhistT, cast_func(dlsym(lib, "add_history"))); | ||
506 | if (l_readline == NULL) | 509 | if (l_readline == NULL) |
507 | lua_warning(L, "unable to load 'readline'", 0); | 510 | lua_warning(L, "unable to load 'readline'", 0); |
508 | else | ||
509 | l_addhist = cast(l_addhist_t, cast_func(dlsym(lib, "add_history"))); | ||
510 | } | 511 | } |
511 | } | 512 | } |
512 | 513 | ||
513 | #endif /* } */ | 514 | #else /* }{ */ |
515 | /* no dlopen or LUA_READLINELIB undefined */ | ||
516 | |||
517 | /* Leave pointers with NULL */ | ||
518 | #define lua_initreadline(L) ((void)L) | ||
519 | |||
520 | #endif /* } */ | ||
521 | |||
522 | #endif /* } */ | ||
514 | 523 | ||
515 | #endif /* } */ | 524 | #endif /* } */ |
516 | 525 | ||