From 52372d1812bc0409fc3cf361e8ef9dbf51e72a2d Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 21 Feb 2022 17:01:00 -0800 Subject: [PATCH] delete curses primitives to read whole lines They make it seem like you can use them to create simple REPL apps, but you can't, because standard Teliva shortcuts won't work. I _could_ make them work by emulating them using getch(), but that feels like an unnecessary abstraction for now. --- README.md | 1 + src/lcurses/curses.c | 30 ++++-------------------- src/lcurses/window.c | 54 ++------------------------------------------ 3 files changed, 8 insertions(+), 77 deletions(-) diff --git a/README.md b/README.md index 64ec9e6..1449105 100644 --- a/README.md +++ b/README.md @@ -193,6 +193,7 @@ Teliva also introduces some incompatibilities to protect computer owners: start out as stdin/stdout. - `io.input`, `io.read` - `io.output`, `io.write`, `io.flush` + - `curses.getstr()`, `curses.mvgetstr()` * Some functions in lcurses have [additional smarts](https://github.com/lcurses/lcurses/blob/master/lib/curses.lua). Teliva is [consistent with the underlying ncurses](https://github.com/akkartik/teliva/blob/main/src/lcurses/curses.lua). diff --git a/src/lcurses/curses.c b/src/lcurses/curses.c index 64c7633..c52adc2 100644 --- a/src/lcurses/curses.c +++ b/src/lcurses/curses.c @@ -27,25 +27,6 @@ /*** Full-screen Text Terminal Manipulation. - In the underlying curses C library, the following functions: - - getstr() (and wgetstr(), mvgetstr(), and mvwgetstr()) - inchstr() (and winchstr(), mvinchstr(), and mvwinchstr()) - instr() (and winstr(), mvinstr(), and mvwinstr()) - - are subject to buffer overflow attack. This is because you pass in the - buffer to be filled in, which has to be of finite length. But in this - Lua module, a buffer is assigned automatically and the function returns - the string, so there is no security issue. You may still use the alternate - functions: - - s = stdscr:getnstr() - s = stdscr:inchnstr() - s = stdscr:innstr() - - which take an extra "size of buffer" argument, in order to impose a maximum - length on the string the user may type in. - Some of the C functions beginning with "no" do not exist in Lua. You should use `curses.nl(false)` and `curses.nl(true)` instead of `nonl()` and `nl()`, and likewise `curses.echo(false)` and `curses.echo(true)` instead of @@ -60,12 +41,11 @@ if c < 256 then c = string.char(c) end Some Lua functions take a different set of parameters than their C - counterparts; for example, you should use `str = stdscr.getstr()` and - `y, x = stdscr.getyx()` instead of `getstr(str)` and `getyx(y, x)`, and - likewise for `getbegyx` and `getmaxyx` and `getparyx` and `pair_content`. - The Perl Curses module now uses the C-compatible parameters, so be aware of - this difference when translating code from Perl into Lua, as well as from C - into Lua. + counterparts; for example, you should use `y, x = stdscr.getyx()` instead of + `getstr(str)` and `getyx(y, x)`, and likewise for `getbegyx` and `getmaxyx` + and `getparyx` and `pair_content`. The Perl Curses module now uses the + C-compatible parameters, so be aware of this difference when translating code + from Perl into Lua, as well as from C into Lua. Many curses functions have variants starting with the prefixes `w-`, `mv-`, and/or `wmv-`. These variants differ only in the explicit addition of a diff --git a/src/lcurses/window.c b/src/lcurses/window.c index 4ce8bb7..3de25aa 100644 --- a/src/lcurses/window.c +++ b/src/lcurses/window.c @@ -1360,56 +1360,6 @@ Wmvgetch(lua_State *L) } -/*** -Read characters up to the next newline from the window input. -@function getstr -@int[opt] n -@treturn string string read from input buffer -@see wgetnstr(3x) -*/ -static int -Wgetstr(lua_State *L) -{ - WINDOW *w = checkwin(L, 1); - int n = optint(L, 2, 0); - char buf[LUAL_BUFFERSIZE]; - - if (n == 0 || n >= LUAL_BUFFERSIZE) - n = LUAL_BUFFERSIZE - 1; - if (wgetnstr(w, buf, n) == ERR) - return 0; - - return pushstringresult(buf); -} - - -/*** -Call @{move} then @{getstr}. -@function mvgetstr -@int y -@int x -@int[opt=-1] n -@treturn string string read from input buffer -@see mvwgetnstr(3x) -*/ -static int -Wmvgetstr(lua_State *L) -{ - WINDOW *w = checkwin(L, 1); - int y = checkint(L, 2); - int x = checkint(L, 3); - int n = optint(L, 4, -1); - char buf[LUAL_BUFFERSIZE]; - - if (n == 0 || n >= LUAL_BUFFERSIZE) - n = LUAL_BUFFERSIZE - 1; - if (mvwgetnstr(w, y, x, buf, n) == ERR) - return 0; - - return pushstringresult(buf); -} - - /*** Fetch the attributed character at the current cursor position. @function winch @@ -1860,7 +1810,7 @@ static const luaL_Reg curses_window_fns[] = LCURSES_FUNC( Wgetch ), LCURSES_FUNC( Wgetmaxyx ), LCURSES_FUNC( Wgetparyx ), - LCURSES_FUNC( Wgetstr ), + /* no 'getstr' because there's no way to hook standard Teliva hotkeys into it */ LCURSES_FUNC( Wgetyx ), LCURSES_FUNC( Whline ), LCURSES_FUNC( Widcok ), @@ -1881,7 +1831,7 @@ static const luaL_Reg curses_window_fns[] = LCURSES_FUNC( Wmvaddstr ), LCURSES_FUNC( Wmvdelch ), LCURSES_FUNC( Wmvgetch ), - LCURSES_FUNC( Wmvgetstr ), + /* no 'mvgetstr' because there's no way to hook standard Teliva hotkeys into it */ LCURSES_FUNC( Wmvhline ), LCURSES_FUNC( Wmvvline ), LCURSES_FUNC( Wmvwinch ),