-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadline.c
37 lines (33 loc) · 860 Bytes
/
readline.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// build@ cc -shared readline.c -o readline.so
// Completely and absolutely minimal binding to the readline library
// Steve Donovan, 2007
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include <readline/readline.h>
#include <readline/history.h>
static int f_readline(lua_State* L)
{
const char* prompt = lua_tostring(L,1);
const char* line = readline(prompt);
lua_pushstring(L,line);
(void)free((void *)line); // Lua makes a copy...
return 1;
}
static int f_add_history(lua_State* L)
{
if (lua_strlen(L,1) > 0)
add_history(lua_tostring(L, 1));
return 0;
}
static const struct luaL_reg lib[] = {
{"readline", f_readline},
{"add_history",f_add_history},
{NULL, NULL},
};
int luaopen_readline (lua_State *L) {
luaL_openlib (L, "readline", lib, 0);
return 1;
}