31#include "nlua_audio.h"
33#include "nlua_camera.h"
35#include "nlua_colour.h"
36#include "nlua_linopt.h"
37#include "nlua_music.h"
44#define BUTTON_WIDTH 50
45#define BUTTON_HEIGHT 20
58#define CLI_MAX_INPUT STRMAX_SHORT
59#define CLI_WIDTH (SCREEN_W - 100)
60#define CLI_HEIGHT (SCREEN_H - 100)
67static int cli_height = 0;
68static int cli_max_lines = 0;
70#define CLI_MAX_LINES (cli_max_lines)
85static int cli_keyhandler(
unsigned int wid, SDL_Keycode key, SDL_Keymod mod,
int isrepeat );
86static void cli_render(
double bx,
double by,
double w,
double h,
void *data );
88static int cli_printCore( lua_State *L,
int cli_only,
int escape );
92static int cli_initLua (
void);
94static char* cli_escapeString(
int *len_out,
const char *s,
int len )
96 char *buf = malloc( 2*len+1 );
98 for (
int i=0; i<len; i++) {
99 if (s[i]==FONT_COLOUR_CODE)
100 buf[b++] = FONT_COLOUR_CODE;
119 char *buf = cli_escapeString( &len, &s[iter.l_begin], iter.
l_end - iter.l_begin );
133 int n = lua_gettop(L);
135 lua_getglobal(L,
"tostring");
136 for (
int i=1; i<=n; i++) {
138 lua_pushvalue(L, -1);
140 if (lua_pcall(L, 1, 1, 0) != 0) {
141 WARN(_(
"Error calling 'tostring':\n%s"), lua_tostring(L,-1));
145 s = lua_tostring(L, -1);
147 return NLUA_ERROR(L, LUA_QL(
"tostring")
" must return a string to "
168 const char *msg = luaL_checkstring(L,1);
204 fname = luaL_checkstring(L, 1);
209 nlua_getenv( L,
cli_env,
"_LOADED" );
210 if (lua_istable(L,-1)) {
212 while (lua_next(L, -2) != 0) {
224 if (luaL_loadbuffer( L, buf, blen, fname ) != 0)
231 if (lua_pcall(L, 0, LUA_MULTRET, 0) != 0) {
232 WARN(_(
"Error running 'script':\n%s"), lua_tostring(L,-1));
236 return lua_gettop(L) - n;
250 buf = strdup((msg != NULL) ? msg :
"");
267 buf =
strndup((msg != NULL) ? msg :
"", l);
275static void cli_render(
double bx,
double by,
double w,
double h,
void *data )
279 const glColour col = COL_ALPHA( cBlack, 0.5 );
297static int cli_keyhandler(
unsigned int wid, SDL_Keycode key, SDL_Keymod mod,
int isrepeat )
311 strcmp(window_getInput(wid,
"inpInput"), str) == 0) {
315 window_setInput( wid,
"inpInput", str );
327 window_setInput( wid,
"inpInput", NULL );
335 window_setInput( wid,
"inpInput", str );
341 window_setInput( wid,
"inpInput", NULL );
377 const char *match, *old;
378 char *str, *cur, *
new;
380 old = window_getInput( wid,
"inpInput" );
385 for (
int i=0; str[i] !=
'\0'; i++) {
386 if (str[i] ==
'.' || str[i] ==
':') {
388 lua_getfield(naevL, -1, cur);
391 if (!lua_istable(naevL, -1)) {
392 if (luaL_getmetafield(naevL, -1,
"__index")) {
393 if (lua_istable(naevL, -1)) {
395 lua_remove(naevL, -2);
406 lua_remove(naevL, -2);
409 }
else if (!isalnum(str[i]) && str[i] !=
'_') {
416 if (strlen(cur) > 0) {
418 while (lua_next(naevL, -2) != 0) {
419 if (lua_isstring(naevL, -2)) {
420 match = lua_tostring(naevL, -2);
421 if (strncmp(cur, match, strlen(cur)) == 0) {
422 new = malloc(strlen(old) + strlen(match) - strlen(cur) + 1);
424 strcat(
new, match + strlen(cur));
425 window_setInput( wid,
"inpInput",
new);
438static int cli_initLua (
void)
458 nlua_pushenv( naevL,
cli_env );
460 lua_settop( naevL, 0 );
464 status = nlua_dobufenv(
cli_env, buf, blen,
"@rep.lua" );
467 lua_settop( naevL, 0 );
470 if (lua_gettop( naevL ) != 1) {
471 WARN( _(
"rep.lua failed to return a single coroutine.") );
472 lua_settop( naevL, 0 );
478 WARN( _(
"rep.lua failed to return a single coroutine.") );
479 lua_settop( naevL, 0 );
485 WARN( _(
"REPL thread exited.") );
488 else if (status != LUA_YIELD) {
543static void cli_input(
unsigned int wid,
const char *unused )
547 const char *str, *prompt;
549 char buf[CLI_MAX_INPUT+7];
552 str = window_getInput( wid,
"inpInput" );
559 escaped = cli_escapeString( &len, str, strlen(str) );
564 snprintf( buf, CLI_MAX_INPUT+7,
"#C%s %s#0", prompt, escaped );
582 if (status != LUA_YIELD)
589 lua_pushliteral(naevL,
"\n");
592 lua_pushstring( naevL, str );
596 lua_concat(naevL, 3);
598 status = luaL_loadbuffer( naevL, lua_tostring(naevL,-1), lua_strlen(naevL,-1),
"=cli" );
601 if (status == LUA_ERRSYNTAX) {
603 const char *msg = lua_tolstring(naevL, -1, &lmsg);
604 const char *tp = msg + lmsg - (
sizeof(LUA_QL(
"<eof>")) - 1);
605 if (strstr(msg, LUA_QL(
"<eof>")) == tp) {
612 const char *s = lua_tostring(naevL, -1);
615 lua_settop(naevL, 0);
620 else if (status == 0) {
624 lua_setfenv(naevL, -2);
626 if (nlua_pcall(
cli_env, 0, LUA_MULTRET)) {
631 if (lua_gettop(naevL) > 0) {
632 nlua_getenv(naevL,
cli_env,
"print");
633 lua_insert(naevL, 1);
634 if (lua_pcall(naevL, lua_gettop(naevL)-1, 0, 0) != 0)
639 lua_settop(naevL, 0);
645 window_setInput( wid,
"inpInput", NULL );
688 window_addInput( wid, 20, 20,
690 "inpInput", CLI_MAX_INPUT, 1,
cli_font );
699 cli_height = line_height * cli_max_lines - (line_height -
cli_font->
h);
700 window_addCust( wid, 20, -40,
702 "cstConsole", 0,
cli_render, NULL, NULL, NULL, NULL );
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
#define array_grow(ptr_array)
Increases the number of elements by one and returns the last element.
#define array_create(basic_type)
Creates a new dynamic array of ‘basic_type’.
void cli_open(void)
Opens the console.
int cli_warn(lua_State *L)
Barebones warn implementation for Lua, allowing scripts to print warnings to stderr.
static int cli_printCore(lua_State *L, int cli_only, int escape)
Back end for the Lua print functionality.
int cli_printRaw(lua_State *L)
Prints raw markup to the console.
int cli_print(lua_State *L)
Replacement for the internal Lua print to print to both the console and the terminal.
static void cli_addMessageMax(const char *msg, const int l)
Adds a message to the buffer.
void cli_exit(void)
Destroys the CLI environment.
static char ** cli_buffer
static lua_State * cli_thread
int cli_init(void)
Initializes the CLI environment.
static int cli_keyhandler(unsigned int wid, SDL_Keycode key, SDL_Keymod mod, int isrepeat)
Key handler for the console window.
static void cli_printCoreString(const char *s, int escape)
Prints a string.
static const luaL_Reg cli_methods[]
static void cli_addMessage(const char *msg)
Adds a message to the buffer.
static void cli_render(double bx, double by, double w, double h, void *data)
Render function for the custom widget.
void cli_tabComplete(unsigned int wid)
Basic tab completion for console.
static void cli_input(unsigned int wid, const char *unused)
Handles the CLI input.
static int cli_thread_ref
static int cli_script(lua_State *L)
Would be like "dofile" from the base Lua lib.
static int cli_scroll_pos
int gl_printLineIteratorNext(glPrintLineIterator *iter)
Updates iter with the next line's information.
void gl_freeFont(glFont *font)
Frees a loaded font. Caution: its glFontStash still has a slot in avail_fonts. At the time of writing...
void gl_printLineIteratorInit(glPrintLineIterator *iter, const glFont *ft_font, const char *text, int width)
Initialize an iterator object for breaking text into lines.
int gl_printMaxRaw(const glFont *ft_font, const int max, double x, double y, const glColour *c, double outlineR, const char *text)
Behaves like gl_printRaw but stops displaying text after a certain distance.
int gl_fontInit(glFont *font, const char *fname, const unsigned int h, const char *prefix, unsigned int flags)
Initializes a font.
Header file with generic functions and naev-specifics.
const char * naev_version(int long_version)
Returns the version in a human readable string.
void * ndata_read(const char *path, size_t *filesize)
Reads a file from the ndata (will be NUL terminated).
int nlua_loadStandard(nlua_env env)
Loads the standard Naev Lua API.
int nlua_loadBackground(nlua_env env)
Loads the graphics library.
int nlua_loadCamera(nlua_env env)
Loads the camera library.
int nlua_loadCLI(nlua_env env)
Loads the CLI Lua library.
int nlua_loadCol(nlua_env env)
Loads the colour library.
int nlua_loadLinOpt(nlua_env env)
Loads the linopt library.
int nlua_loadMusic(nlua_env env)
Music Lua module.
int nlua_loadTex(nlua_env env)
Loads the texture library.
int nlua_loadTk(nlua_env env)
Loads the Toolkit Lua library.
char * strndup(const char *s, size_t n)
Return a pointer to a new string, which is a duplicate of the string s (or, if necessary,...
void gl_renderRect(double x, double y, double w, double h, const glColour *c)
Renders a rectangle.
Represents a font in memory.
The state of a line iteration. This matches the process of rendering text into an on-screen box: An e...