Skip to content

Commit

Permalink
console: add line carrying backslash
Browse files Browse the repository at this point in the history
When using interactive console(stdin) instead of \set delimiter <delimiter>
with "\", "\" in the end if line can be used.

Closes #4317
  • Loading branch information
Olga Arkhangelskaia committed Jun 3, 2020
1 parent 25cb324 commit dfbe415
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/box/lua/console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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)
Expand Down
53 changes: 53 additions & 0 deletions test/app-tap/gh-4317.test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env tarantool

local tap = require('tap')
local console = require('console')
local fio = require('fio')

test = tap.test("console")
test:plan(1)
-- The following cases use LD_PRELOAD, so will not work under
-- Mac OS X.
if jit.os == 'OSX' then return end

-- Allow to run the test from the repository root without
-- test-run:
--
-- $ ./src/tarantool test/app-tap/console.test.lua
--
-- It works at least for in-source build.
local is_under_test_run = pcall(require, 'test_run')
local isatty_lib_dir = is_under_test_run and '../..' or 'test'
local isatty_lib_path = fio.pathjoin(isatty_lib_dir, 'libisatty.so')
local saved_path = os.getenv('PATH')
if not is_under_test_run then
os.setenv('PATH', './src:', saved_path)
end

local tarantool_command = "local a = 0 \\\nfor i = 1, 10 do\na = a + i\nend \\\nprint(a)"
local result_str = [[tarantool> local a = 0 \
> for i = 1, 10 do
> a = a + i
> end \
> print(a)
55
---
...
tarantool> ]]
local cmd = ([[printf '%s\n' | tarantool -i ]] ..
[[2>/dev/null]]):format(tarantool_command)
local fh, err = io.popen(cmd, 'r')
-- Readline on CentOS 7 produces \e[?1034h escape
-- sequence before tarantool> prompt, remove it.
local result = fh:read('*a'):gsub('\x1b%[%?1034h', '')
print(err)
fh:close()
print("!!!!!!!")
print(result)
print("!!!!!!!")
print(tra_str)
test:is(result, result_str, "Backslash")

test:check()
os.exit(0)

0 comments on commit dfbe415

Please sign in to comment.