From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp41.i.mail.ru (smtp41.i.mail.ru [94.100.177.101]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 4F3AE42EF5C for ; Tue, 16 Jun 2020 17:40:36 +0300 (MSK) Date: Tue, 16 Jun 2020 17:39:50 +0300 From: Alexander Turenko Message-ID: <20200616143950.ktmylqrdov3kdfln@tkn_work_nb> References: <20200610094354.31831-1-arkholga@tarantool.org> <20200610094354.31831-3-arkholga@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20200610094354.31831-3-arkholga@tarantool.org> Subject: Re: [Tarantool-patches] [PATCH 2/2] console: add line carrying backslash List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Olga Arkhangelskaia Cc: tarantool-patches@dev.tarantool.org On Wed, Jun 10, 2020 at 12:43:54PM +0300, Olga Arkhangelskaia wrote: > When using interactive console(stdin) instead of \set 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