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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
/* vim:sts=4 sw=4 expandtab
*/
/*
* Copyright (c) 2011-2015 Rob Hoelz <rob@hoelz.ro>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include "linenoise.h"
#define LN_COMPLETION_TYPE "linenoiseCompletions*"
static int completion_func_ref;
static lua_State *completion_state;
static int handle_ln_error(lua_State *L)
{
lua_pushnil(L);
return 1;
}
static int handle_ln_ok(lua_State *L)
{
lua_pushboolean(L, 1);
return 1;
}
static void completion_callback_wrapper(const char *line, linenoiseCompletions *completions, int pos)
{
lua_State *L = completion_state;
lua_rawgeti(L, LUA_REGISTRYINDEX, completion_func_ref);
*((linenoiseCompletions **) lua_newuserdata(L, sizeof(linenoiseCompletions *))) = completions;
luaL_getmetatable(L, LN_COMPLETION_TYPE);
lua_setmetatable(L, -2);
lua_pushstring(L, line);
lua_pushinteger(L, pos);
lua_pcall(L, 3, 0, 0);
}
static int l_linenoise(lua_State *L)
{
const char *prompt = luaL_checkstring(L, 1);
char *line;
completion_state = L;
line = linenoise(prompt);
completion_state = NULL;
if(! line) {
return handle_ln_error(L);
}
lua_pushstring(L, line);
free(line);
return 1;
}
static int lines_next(lua_State *L)
{
lua_pushcfunction(L, l_linenoise);
lua_pushvalue(L, lua_upvalueindex(1));
lua_call(L, 1, 1);
return 1;
}
static int l_lines(lua_State *L)
{
luaL_checkstring(L, 1);
lua_pushcclosure(L, lines_next, 1);
return 1;
}
static int l_historyadd(lua_State *L)
{
const char *line = luaL_checkstring(L, 1);
if(! linenoiseHistoryAdd(line)) {
return handle_ln_error(L);
}
return handle_ln_ok(L);
}
static int l_preloadbuffer(lua_State *L)
{
const char *line = luaL_checkstring(L, 1);
linenoisePreloadBuffer(line);
return handle_ln_ok(L);
}
static int l_historysetmaxlen(lua_State *L)
{
int len = luaL_checkinteger(L, 1);
if(! linenoiseHistorySetMaxLen(len)) {
return handle_ln_error(L);
}
return handle_ln_ok(L);
}
static int l_historysave(lua_State *L)
{
const char *filename = luaL_checkstring(L, 1);
if(linenoiseHistorySave((char *) filename) < 0) {
return handle_ln_error(L);
}
return handle_ln_ok(L);
}
static int l_historyload(lua_State *L)
{
const char *filename = luaL_checkstring(L, 1);
if(linenoiseHistoryLoad((char *) filename) < 0) {
return handle_ln_error(L);
}
return handle_ln_ok(L);
}
static int l_clearscreen(lua_State *L)
{
linenoiseClearScreen();
return handle_ln_ok(L);
}
static int l_setcompletion(lua_State *L)
{
luaL_checktype(L, 1, LUA_TFUNCTION);
lua_pushvalue(L, 1);
completion_func_ref = luaL_ref(L, LUA_REGISTRYINDEX);
linenoiseSetCompletionCallback(completion_callback_wrapper);
return handle_ln_ok(L);
}
static int l_addcompletion(lua_State *L)
{
linenoiseCompletions *completions = *((linenoiseCompletions **) luaL_checkudata(L, 1, LN_COMPLETION_TYPE));
const char *entry = luaL_checkstring(L, 2);
int pos = luaL_checkinteger(L, 3);
linenoiseAddCompletion(completions, (char *) entry, pos);
return handle_ln_ok(L);
}
static int l_refresh(lua_State *L)
{
linenoiseRefresh();
return handle_ln_ok(L);
}
static int l_historyget(lua_State *L)
{
int len, i;
char **history = linenoiseHistory(&len);
lua_newtable(L);
for(i = 0; i < len; i++)
{
lua_pushstring(L, history[i]);
lua_rawseti(L, -2, i + 1);
}
return 1;
}
luaL_Reg linenoise_funcs[] = {
{ "linenoise", l_linenoise },
{ "historyadd", l_historyadd },
{ "historysetmaxlen", l_historysetmaxlen },
{ "historysave", l_historysave },
{ "historyload", l_historyload },
{ "historyget", l_historyget },
{ "clearscreen", l_clearscreen },
{ "setcompletion", l_setcompletion},
{ "addcompletion", l_addcompletion },
{ "preload", l_preloadbuffer },
{ "refresh", l_refresh },
/* Aliases for more consistent function names */
{ "addhistory", l_historyadd },
{ "sethistorymaxlen", l_historysetmaxlen },
{ "savehistory", l_historysave },
{ "loadhistory", l_historyload },
{ "line", l_linenoise },
{ "lines", l_lines },
{ NULL, NULL }
};
int luaopen_linenoise(lua_State *L)
{
lua_newtable(L);
luaL_newmetatable(L, LN_COMPLETION_TYPE);
lua_pushboolean(L, 0);
lua_setfield(L, -2, "__metatable");
lua_pop(L, 1);
#if LUA_VERSION_NUM > 501
luaL_setfuncs(L,linenoise_funcs,0);
#else
luaL_register(L, NULL, linenoise_funcs);
#endif
return 1;
}
|