Tarantool development patches archive
 help / color / mirror / Atom feed
From: Alexander Turenko <alexander.turenko@tarantool.org>
To: Olga Arkhangelskaia <arkholga@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: Re: [Tarantool-patches] [PATCH 2/2] console: add line carrying backslash
Date: Tue, 16 Jun 2020 17:39:50 +0300	[thread overview]
Message-ID: <20200616143950.ktmylqrdov3kdfln@tkn_work_nb> (raw)
In-Reply-To: <20200610094354.31831-3-arkholga@tarantool.org>

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

  parent reply	other threads:[~2020-06-16 14:40 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-10  9:43 [Tarantool-patches] [PATCH v2 0/2] console: support of backslash Olga Arkhangelskaia
2020-06-10  9:43 ` [Tarantool-patches] [PATCH 1/2] test: add libisatty to test local console Olga Arkhangelskaia
2020-06-11 16:47   ` Igor Munkin
2020-06-10  9:43 ` [Tarantool-patches] [PATCH 2/2] console: add line carrying backslash Olga Arkhangelskaia
2020-06-11 16:48   ` Igor Munkin
2020-06-16 14:39   ` Alexander Turenko [this message]
2020-06-11 16:49 ` [Tarantool-patches] [PATCH v2 0/2] console: support of backslash Igor Munkin
2020-07-08 18:42 ` Alexander Turenko
2020-07-09  6:01   ` Olga Arkhangelskaia

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200616143950.ktmylqrdov3kdfln@tkn_work_nb \
    --to=alexander.turenko@tarantool.org \
    --cc=arkholga@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 2/2] console: add line carrying backslash' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox