diff options
| -rw-r--r-- | lua.c | 47 |
1 files changed, 25 insertions, 22 deletions
| @@ -438,13 +438,24 @@ static int handle_luainit (lua_State *L) { | |||
| 438 | ** the standard input. | 438 | ** the standard input. |
| 439 | ** * lua_saveline defines how to "save" a read line in a "history". | 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. | 440 | ** * lua_freeline defines how to free a line read by lua_readline. |
| 441 | ** | ||
| 442 | ** If lua_readline is defined, all of them should be defined. | ||
| 443 | */ | 441 | */ |
| 444 | 442 | ||
| 445 | #if !defined(lua_readline) /* { */ | 443 | #if !defined(lua_readline) /* { */ |
| 444 | /* Otherwise, all previously listed functions should be defined. */ | ||
| 446 | 445 | ||
| 447 | /* Code to use the readline library, either statically or dynamically linked */ | 446 | #if defined(LUA_USE_READLINE) /* { */ |
| 447 | /* Lua will be linked with '-lreadline' */ | ||
| 448 | |||
| 449 | #include <readline/readline.h> | ||
| 450 | #include <readline/history.h> | ||
| 451 | |||
| 452 | #define lua_initreadline(L) ((void)L, rl_readline_name="lua") | ||
| 453 | #define lua_readline(buff,prompt) ((void)buff, readline(prompt)) | ||
| 454 | #define lua_saveline(line) add_history(line) | ||
| 455 | #define lua_freeline(line) free(line) | ||
| 456 | |||
| 457 | #else /* }{ */ | ||
| 458 | /* use dynamically loaded readline (or nothing) */ | ||
| 448 | 459 | ||
| 449 | /* pointer to 'readline' function (if any) */ | 460 | /* pointer to 'readline' function (if any) */ |
| 450 | typedef char *(*l_readlineT) (const char *prompt); | 461 | typedef char *(*l_readlineT) (const char *prompt); |
| @@ -480,22 +491,9 @@ static void lua_freeline (char *line) { | |||
| 480 | } | 491 | } |
| 481 | 492 | ||
| 482 | 493 | ||
| 483 | #if defined(LUA_USE_READLINE) /* { */ | 494 | #if defined(LUA_USE_DLOPEN) && defined(LUA_READLINELIB) /* { */ |
| 484 | |||
| 485 | /* assume Lua will be linked with '-lreadline' */ | ||
| 486 | #include <readline/readline.h> | ||
| 487 | #include <readline/history.h> | ||
| 488 | |||
| 489 | static void lua_initreadline(lua_State *L) { | ||
| 490 | UNUSED(L); | ||
| 491 | rl_readline_name = "lua"; | ||
| 492 | l_readline = cast(l_readlineT, readline); | ||
| 493 | l_addhist = cast(l_addhistT, add_history); | ||
| 494 | } | ||
| 495 | |||
| 496 | #elif defined(LUA_USE_DLOPEN) && defined(LUA_READLINELIB) /* }{ */ | ||
| 497 | |||
| 498 | /* try to load 'readline' dynamically */ | 495 | /* try to load 'readline' dynamically */ |
| 496 | |||
| 499 | #include <dlfcn.h> | 497 | #include <dlfcn.h> |
| 500 | 498 | ||
| 501 | static void lua_initreadline (lua_State *L) { | 499 | static void lua_initreadline (lua_State *L) { |
| @@ -508,15 +506,20 @@ static void lua_initreadline (lua_State *L) { | |||
| 508 | *name = "lua"; | 506 | *name = "lua"; |
| 509 | l_readline = cast(l_readlineT, cast_func(dlsym(lib, "readline"))); | 507 | l_readline = cast(l_readlineT, cast_func(dlsym(lib, "readline"))); |
| 510 | l_addhist = cast(l_addhistT, cast_func(dlsym(lib, "add_history"))); | 508 | l_addhist = cast(l_addhistT, cast_func(dlsym(lib, "add_history"))); |
| 509 | if (l_readline == NULL) | ||
| 510 | lua_warning(L, "unable to load 'readline'", 0); | ||
| 511 | } | 511 | } |
| 512 | } | 512 | } |
| 513 | 513 | ||
| 514 | #else /* }{ */ | 514 | #else /* }{ */ |
| 515 | /* no dlopen or LUA_READLINELIB undefined */ | ||
| 515 | 516 | ||
| 516 | /* no readline; leave function pointers as NULL */ | 517 | /* Leave pointers with NULL */ |
| 517 | #define lua_initreadline(L) cast(void, L) | 518 | #define lua_initreadline(L) ((void)L) |
| 518 | 519 | ||
| 519 | #endif /* } */ | 520 | #endif /* } */ |
| 521 | |||
| 522 | #endif /* } */ | ||
| 520 | 523 | ||
| 521 | #endif /* } */ | 524 | #endif /* } */ |
| 522 | 525 | ||
