Tarantool development patches archive
 help / color / mirror / Atom feed
* [Tarantool-patches] [PATCH] fio: provide access to stdin/stdout/stderr
@ 2020-04-04  9:53 olegrok
  2020-04-26 17:47 ` Vladislav Shpilevoy
  0 siblings, 1 reply; 2+ messages in thread
From: olegrok @ 2020-04-04  9:53 UTC (permalink / raw)
  To: imun, v.shpilevoy; +Cc: tarantool-patches

From: Oleg Babin <babinoleg@mail.ru>

This patch introduces fio API for interaction with
stdin/stdout/stderr streams.

Closes #1338

@TarantoolBot document
Title: fio provides access to stdin/stdout/stderr

Now access to standard streams is available via:
  * fio.stdin
  * fio.stdout
  * fio.stderr

Each of this streams have usual fio file interface.

Example:
```lua
fio.stdin:read(5) -- read 5 bytes from stdin
fio.stdout:write("Hello") -- write "Hello" to stdout
fio.stderr:write("Error") -- write "Error" to stderr
```
---
Issue: https://github.com/tarantool/tarantool/issues/1338
Branch: https://github.com/tarantool/tarantool/tree/olegrok/1338-fio-std-streams

@ChangeLog
  - fio provides access to stdin/stdout/stderr (gh-1338)

 src/lua/fio.lua       | 10 ++++++++++
 test/app/fio.result   | 27 +++++++++++++++++++++++++++
 test/app/fio.test.lua | 11 +++++++++++
 3 files changed, 48 insertions(+)

diff --git a/src/lua/fio.lua b/src/lua/fio.lua
index 83fddaa0a..1eaa085ae 100644
--- a/src/lua/fio.lua
+++ b/src/lua/fio.lua
@@ -17,6 +17,12 @@ ffi.cdef[[
     };
 ]]
 
+local filenos = {
+    STDIN = 0,
+    STDOUT = 1,
+    STDERR = 2,
+}
+
 local const_char_ptr_t = ffi.typeof('const char *')
 
 local internal = fio.internal
@@ -522,6 +528,10 @@ fio.utime = function(path, atime, mtime)
     return internal.utime(path, atime, mtime)
 end
 
+fio.stdin = ffi.new('struct fio_handle', filenos.STDIN)
+fio.stdout = ffi.new('struct fio_handle', filenos.STDOUT)
+fio.stderr = ffi.new('struct fio_handle', filenos.STDERR)
+
 fio.path = {}
 fio.path.is_file = function(filename)
     local fs = fio.stat(filename)
diff --git a/test/app/fio.result b/test/app/fio.result
index 783fa4fab..3c6649c8b 100644
--- a/test/app/fio.result
+++ b/test/app/fio.result
@@ -1524,3 +1524,30 @@ tmpdir = nil
 os.setenv('TMPDIR', old_tmpdir)
 ---
 ...
+--
+-- gh-1338: provide access to stdin/stdout/stderr
+--
+fio.stdin.fh == 0
+---
+- true
+...
+fio.stdout.fh == 1
+---
+- true
+...
+fio.stderr.fh == 2
+---
+- true
+...
+fio.stdin:read(0)
+---
+- 
+...
+fio.stdout:write('\0')
+---
+- true
+...
+fio.stderr:write('\0')
+---
+- true
+...
diff --git a/test/app/fio.test.lua b/test/app/fio.test.lua
index 6825b882f..f55747c18 100644
--- a/test/app/fio.test.lua
+++ b/test/app/fio.test.lua
@@ -502,3 +502,14 @@ fio.tempdir()
 tmpdir = nil
 
 os.setenv('TMPDIR', old_tmpdir)
+
+--
+-- gh-1338: provide access to stdin/stdout/stderr
+--
+fio.stdin.fh == 0
+fio.stdout.fh == 1
+fio.stderr.fh == 2
+
+fio.stdin:read(0)
+fio.stdout:write('\0')
+fio.stderr:write('\0')
-- 
2.23.0

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Tarantool-patches] [PATCH] fio: provide access to stdin/stdout/stderr
  2020-04-04  9:53 [Tarantool-patches] [PATCH] fio: provide access to stdin/stdout/stderr olegrok
@ 2020-04-26 17:47 ` Vladislav Shpilevoy
  0 siblings, 0 replies; 2+ messages in thread
From: Vladislav Shpilevoy @ 2020-04-26 17:47 UTC (permalink / raw)
  To: olegrok, imun; +Cc: tarantool-patches

Hi! Thanks for the patch!

On 04/04/2020 11:53, olegrok@tarantool.org wrote:
> From: Oleg Babin <babinoleg@mail.ru>
> 
> This patch introduces fio API for interaction with
> stdin/stdout/stderr streams.
> 
> Closes #1338
> 
> @TarantoolBot document
> Title: fio provides access to stdin/stdout/stderr
> 
> Now access to standard streams is available via:
>   * fio.stdin
>   * fio.stdout
>   * fio.stderr
> 
> Each of this streams have usual fio file interface.
> 
> Example:
> ```lua
> fio.stdin:read(5) -- read 5 bytes from stdin

I tried these examples:

1. Start interactive console, call fio.stdin:read(5). When
I type any character, read() returns immediately, with 1
byte read. Not 5.

2. I tried starting Tarantool from a script, without the
console. In the script I did essentially the same - call
fio.stdin:read(5), print result, exit. It returned only
what I inserted until \n or 5 symbols, if I didn't print
\n.

Why?

> fio.stdout:write("Hello") -- write "Hello" to stdout
> fio.stderr:write("Error") -- write "Error" to stderr
> ```
> ---
> Issue: https://github.com/tarantool/tarantool/issues/1338
> Branch: https://github.com/tarantool/tarantool/tree/olegrok/1338-fio-std-streams
> 
> @ChangeLog
>   - fio provides access to stdin/stdout/stderr (gh-1338)
> 
>  src/lua/fio.lua       | 10 ++++++++++
>  test/app/fio.result   | 27 +++++++++++++++++++++++++++
>  test/app/fio.test.lua | 11 +++++++++++
>  3 files changed, 48 insertions(+)
> 
> diff --git a/src/lua/fio.lua b/src/lua/fio.lua
> index 83fddaa0a..1eaa085ae 100644
> --- a/src/lua/fio.lua
> +++ b/src/lua/fio.lua
> @@ -522,6 +528,10 @@ fio.utime = function(path, atime, mtime)
>      return internal.utime(path, atime, mtime)
>  end
>  
> +fio.stdin = ffi.new('struct fio_handle', filenos.STDIN)
> +fio.stdout = ffi.new('struct fio_handle', filenos.STDOUT)
> +fio.stderr = ffi.new('struct fio_handle', filenos.STDERR)

The problem is that when Tarantool will be terminated, Lua
is destroyed (probably it is disabled now, but I think will be
enabled at some day). Anyway, destruction of these handles leads
to closure of their descriptors, since fio handles now are
automatically closed when GC gets to them. That makes all printf(),
scanf(), fscanf(), etc, work with invalid descriptors, or even
already newly opened descriptors, not related to the standard
streams which took their place.

Better use dup() so as close of these descriptors wouldn't affect
other code.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-04-26 17:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-04  9:53 [Tarantool-patches] [PATCH] fio: provide access to stdin/stdout/stderr olegrok
2020-04-26 17:47 ` Vladislav Shpilevoy

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