[Tarantool-patches] [PATCH 2/2] console: add line carrying backslash
Alexander Turenko
alexander.turenko at tarantool.org
Tue Jun 16 17:39:50 MSK 2020
On Wed, Jun 10, 2020 at 12:43:54PM +0300, Olga Arkhangelskaia wrote:
> When using interactive console(stdin) instead of \set delimiter <delimiter>
> with "\", "\" in the end if line can be used.
local_read() also works on a console client side, so, say, `tarantoolctl
connect` will work as expected in interactive mode.
However I think it is good to left more freedom for scripting with, say, netcat
and support the feature at console server side too. It looks quite simple:
@@ -640,9 +644,12 @@ local function local_print(self, output)
end
--
--- Read command from connected client console.listen()
+-- Read a line or a chunk if delimiter is not empty.
--
-local function client_read(self)
+-- The value returned without delimiter (if any) and trailing
+-- newline character.
+--
+local function client_read_line(self)
--
-- Byte sequences that come over the network and come from
-- the local client console may have a different terminal
@@ -663,6 +670,34 @@ local function client_read(self)
return buf:sub(1, -#self.delimiter-2)
end
+--
+-- Read command from connected client console.listen()
+--
+local function client_read(self)
+ local buf = ''
+ while true do
+ local line = client_read_line(self)
+ if line == nil then
+ -- EOF or error.
+ --
+ -- Note: Even if `buf` is not empty, skip unfinished
+ -- command.
+ return nil
+ end
+ buf = buf .. line
+ if line == '' then
+ -- Continue if the line is empty.
+ elseif self.delimiter == '' and buf:endswith('\\') then
+ -- Continue reading if the line ends with the
+ -- backslash and no delimiter is set.
+ buf = buf:sub(1, -2)
+ else
+ break
+ end
+ end
+ return buf
+end
+
--
-- Print result to connected client from console.listen()
--
>
> Closes #4317
> ---
> src/box/lua/console.lua | 18 ++++++++-----
> test/app-tap/gh-4317.test.lua | 51 +++++++++++++++++++++++++++++++++++
> 2 files changed, 63 insertions(+), 6 deletions(-)
> create mode 100755 test/app-tap/gh-4317.test.lua
>
> diff --git a/src/box/lua/console.lua b/src/box/lua/console.lua
> index 17e2c91b2..4c2c7a132 100644
> --- a/src/box/lua/console.lua
> +++ b/src/box/lua/console.lua
> @@ -573,14 +573,20 @@ local function local_read(self)
> break
> end
> if delim == "" then
> - local lang = box.session.language
> - if not lang or lang == 'lua' then
> - -- stop once a complete Lua statement is entered
> - if local_check_lua(buf) then
> + -- if no delim is set and line ends with the backslash
> + -- continue reading
> + if buf:sub(-1, -1) == '\\' then
> + buf = buf:sub(0, #buf - 1)
I suggest to use -2 instead of #buf -1 to eliminate extra string length
operation. Also, all indices in Lua starts from 1.
> + else
> + local lang = box.session.language
> + if not lang or lang == 'lua' then
> + -- stop once a complete Lua statement is entered
> + if local_check_lua(buf) then
> + break
> + end
> + else
> break
> end
> - else
> - break
> end
> elseif #buf >= #delim and buf:sub(#buf - #delim + 1) == delim then
> buf = buf:sub(0, #buf - #delim)
We can decrease nesting level this way:
diff --git a/src/box/lua/console.lua b/src/box/lua/console.lua
index 6ea27a393..effb0f0b5 100644
--- a/src/box/lua/console.lua
+++ b/src/box/lua/console.lua
@@ -604,7 +604,11 @@ local function local_read(self)
if buf:sub(1, 1) == '\\' then
break
end
- if delim == "" then
+ if delim == '' and line:endswith('\\') then
+ -- Continue reading if the line ends with the
+ -- backslash and no delimiter is set.
+ buf = buf:sub(1, -2)
+ elseif delim == "" then
local lang = box.session.language
if not lang or lang == 'lua' then
-- stop once a complete Lua statement is entered
More information about the Tarantool-patches
mailing list