diff options
| -rw-r--r-- | lua.c | 71 |
1 files changed, 38 insertions, 33 deletions
| @@ -432,32 +432,30 @@ static int handle_luainit (lua_State *L) { | |||
| 432 | 432 | ||
| 433 | 433 | ||
| 434 | /* | 434 | /* |
| 435 | ** lua_readline defines how to show a prompt and then read a line from | 435 | ** * lua_initreadline initializes the readline system. |
| 436 | ** the standard input. | 436 | ** * 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". | 437 | ** the standard input. |
| 438 | ** lua_freeline defines how to free a line read by lua_readline. | 438 | ** * lua_saveline defines how to "save" a read line in a "history". |
| 439 | ** * lua_freeline defines how to free a line read by lua_readline. | ||
| 440 | ** | ||
| 441 | ** If lua_readline is defined, all of them should be defined. | ||
| 439 | */ | 442 | */ |
| 440 | 443 | ||
| 441 | #if defined(LUA_USE_READLINE) | 444 | #if !defined(lua_readline) /* { */ |
| 442 | |||
| 443 | #include <readline/readline.h> | ||
| 444 | #include <readline/history.h> | ||
| 445 | #define lua_initreadline(L) ((void)L, rl_readline_name="lua") | ||
| 446 | #define lua_readline(b,p) ((void)b, readline(p)) | ||
| 447 | #define lua_saveline(line) add_history(line) | ||
| 448 | #define lua_freeline(b) free(b) | ||
| 449 | 445 | ||
| 450 | #endif | 446 | /* Code to use the readline library, either statically or dynamically linked */ |
| 451 | 447 | ||
| 448 | /* pointer to 'readline' function (if any) */ | ||
| 449 | typedef char *(*l_readlineT) (const char *prompt); | ||
| 450 | static l_readlineT l_readline = NULL; | ||
| 452 | 451 | ||
| 453 | #if !defined(lua_readline) /* { */ | 452 | /* pointer to 'add_history' function (if any) */ |
| 453 | typedef void (*l_addhistT) (const char *string); | ||
| 454 | static l_addhistT l_addhist = NULL; | ||
| 454 | 455 | ||
| 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 | 456 | ||
| 459 | static char *lua_readline (char *buff, const char *prompt) { | 457 | static char *lua_readline (char *buff, const char *prompt) { |
| 460 | if (l_readline != NULL) /* is there a dynamic 'readline'? */ | 458 | if (l_readline != NULL) /* is there a 'readline'? */ |
| 461 | return (*l_readline)(prompt); /* use it */ | 459 | return (*l_readline)(prompt); /* use it */ |
| 462 | else { /* emulate 'readline' over 'buff' */ | 460 | else { /* emulate 'readline' over 'buff' */ |
| 463 | fputs(prompt, stdout); | 461 | fputs(prompt, stdout); |
| @@ -467,33 +465,38 @@ static char *lua_readline (char *buff, const char *prompt) { | |||
| 467 | } | 465 | } |
| 468 | 466 | ||
| 469 | 467 | ||
| 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) { | 468 | static void lua_saveline (const char *line) { |
| 475 | if (l_addhist != NULL) /* is there a dynamic 'add_history'? */ | 469 | if (l_addhist != NULL) /* is there an 'add_history'? */ |
| 476 | (*l_addhist)(line); /* use it */ | 470 | (*l_addhist)(line); /* use it */ |
| 477 | /* else nothing to be done */ | 471 | /* else nothing to be done */ |
| 478 | } | 472 | } |
| 479 | 473 | ||
| 480 | 474 | ||
| 481 | static void lua_freeline (char *line) { | 475 | static void lua_freeline (char *line) { |
| 482 | if (l_readline != NULL) /* is there a dynamic 'readline'? */ | 476 | if (l_readline != NULL) /* is there a 'readline'? */ |
| 483 | free(line); /* free line created by it */ | 477 | free(line); /* free line created by it */ |
| 484 | /* else 'lua_readline' used an automatic buffer; nothing to free */ | 478 | /* else 'lua_readline' used an automatic buffer; nothing to free */ |
| 485 | } | 479 | } |
| 486 | 480 | ||
| 487 | 481 | ||
| 488 | #if !defined(LUA_USE_DLOPEN) || !defined(LUA_READLINELIB) | 482 | #if defined(LUA_USE_READLINE) /* { */ |
| 489 | 483 | ||
| 490 | #define lua_initreadline(L) ((void)L) | 484 | /* assume Lua will be linked with '-lreadline' */ |
| 485 | #include <readline/readline.h> | ||
| 486 | #include <readline/history.h> | ||
| 487 | |||
| 488 | static void lua_initreadline(lua_State *L) { | ||
| 489 | UNUSED(L); | ||
| 490 | rl_readline_name = "lua"; | ||
| 491 | l_readline = readline; | ||
| 492 | l_addhist = add_history; | ||
| 493 | } | ||
| 491 | 494 | ||
| 492 | #else /* { */ | 495 | #elif defined(LUA_USE_DLOPEN) && defined(LUA_READLINELIB) /* }{ */ |
| 493 | 496 | ||
| 497 | /* try to load 'readline' dynamically */ | ||
| 494 | #include <dlfcn.h> | 498 | #include <dlfcn.h> |
| 495 | 499 | ||
| 496 | |||
| 497 | static void lua_initreadline (lua_State *L) { | 500 | static void lua_initreadline (lua_State *L) { |
| 498 | void *lib = dlopen(LUA_READLINELIB, RTLD_NOW | RTLD_LOCAL); | 501 | void *lib = dlopen(LUA_READLINELIB, RTLD_NOW | RTLD_LOCAL); |
| 499 | if (lib == NULL) | 502 | if (lib == NULL) |
| @@ -502,14 +505,16 @@ static void lua_initreadline (lua_State *L) { | |||
| 502 | const char **name = cast(const char**, dlsym(lib, "rl_readline_name")); | 505 | const char **name = cast(const char**, dlsym(lib, "rl_readline_name")); |
| 503 | if (name != NULL) | 506 | if (name != NULL) |
| 504 | *name = "Lua"; | 507 | *name = "Lua"; |
| 505 | l_readline = cast(l_readline_t, cast_func(dlsym(lib, "readline"))); | 508 | l_readline = cast(l_readlineT, cast_func(dlsym(lib, "readline"))); |
| 506 | if (l_readline == NULL) | 509 | l_addhist = cast(l_addhistT, cast_func(dlsym(lib, "add_history"))); |
| 507 | lua_warning(L, "unable to load 'readline'", 0); | ||
| 508 | else | ||
| 509 | l_addhist = cast(l_addhist_t, cast_func(dlsym(lib, "add_history"))); | ||
| 510 | } | 510 | } |
| 511 | } | 511 | } |
| 512 | 512 | ||
| 513 | #else /* }{ */ | ||
| 514 | |||
| 515 | /* no readline; leave function pointers as NULL */ | ||
| 516 | #define lua_initreadline(L) cast(void, L) | ||
| 517 | |||
| 513 | #endif /* } */ | 518 | #endif /* } */ |
| 514 | 519 | ||
| 515 | #endif /* } */ | 520 | #endif /* } */ |
