aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/library.js7
-rw-r--r--src/library_tty.js36
-rw-r--r--tests/module/test_stdin.c48
-rw-r--r--tests/termios/test_tcgetattr.c61
-rw-r--r--tests/test_core.py4
-rw-r--r--tests/test_other.py17
-rw-r--r--tools/jsrun.py3
7 files changed, 149 insertions, 27 deletions
diff --git a/src/library.js b/src/library.js
index f0a4f53a..f6b3d5ef 100644
--- a/src/library.js
+++ b/src/library.js
@@ -2261,7 +2261,11 @@ LibraryManager.library = {
// void clearerr(FILE *stream);
// http://pubs.opengroup.org/onlinepubs/000095399/functions/clearerr.html
stream = FS.getStream(stream);
- if (stream) stream.error = false;
+ if (!stream) {
+ return;
+ }
+ stream.eof = false;
+ stream.error = false;
},
fclose__deps: ['close', 'fsync'],
fclose: function(stream) {
@@ -2322,7 +2326,6 @@ LibraryManager.library = {
if (streamObj.eof || streamObj.error) return -1;
var ret = _fread(_fgetc.ret, 1, 1, stream);
if (ret == 0) {
- streamObj.eof = true;
return -1;
} else if (ret == -1) {
streamObj.error = true;
diff --git a/src/library_tty.js b/src/library_tty.js
index 6b2368e6..53239989 100644
--- a/src/library_tty.js
+++ b/src/library_tty.js
@@ -1,17 +1,35 @@
mergeInto(LibraryManager.library, {
$TTY__deps: ['$FS'],
+ $TTY__postset: '__ATINIT__.unshift({ func: function() { TTY.init() } });' +
+ '__ATEXIT__.push({ func: function() { TTY.shutdown() } });' +
+ 'TTY.utf8 = new Runtime.UTF8Processor();',
$TTY: {
ttys: [],
+ init: function () {
+ if (ENVIRONMENT_IS_NODE) {
+ // currently, FS.init does not distinguish if process.stdin is a file or TTY
+ // device, it always assumes it's a TTY device. because of this, we're forcing
+ // process.stdin to UTF8 encoding to at least make stdin reading compatible
+ // with text files until FS.init can be refactored.
+ process['stdin']['setEncoding']('utf8');
+ }
+ },
+ shutdown: function() {
+ if (ENVIRONMENT_IS_NODE) {
+ // inolen: any idea as to why node -e 'process.stdin.read()' wouldn't exit immediately (with process.stdin being a tty)?
+ // isaacs: because now it's reading from the stream, you've expressed interest in it, so that read() kicks off a _read() which creates a ReadReq operation
+ // inolen: I thought read() in that case was a synchronous operation that just grabbed some amount of buffered data if it exists?
+ // isaacs: it is. but it also triggers a _read() call, which calls readStart() on the handle
+ // isaacs: do process.stdin.pause() and i'd think it'd probably close the pending call
+ process['stdin']['pause']();
+ }
+ },
register: function(dev, ops) {
TTY.ttys[dev] = { input: [], output: [], ops: ops };
FS.registerDevice(dev, TTY.stream_ops);
},
stream_ops: {
open: function(stream) {
- // this wouldn't be required if the library wasn't eval'd at first...
- if (!TTY.utf8) {
- TTY.utf8 = new Runtime.UTF8Processor();
- }
var tty = TTY.ttys[stream.node.rdev];
if (!tty) {
throw new FS.ErrnoError(ERRNO_CODES.ENODEV);
@@ -66,8 +84,6 @@ mergeInto(LibraryManager.library, {
return i;
}
},
- // NOTE: This is weird to support stdout and stderr
- // overrides in addition to print and printErr overrides.
default_tty_ops: {
// get_char has 3 particular return values:
// a.) the next character represented as an integer
@@ -77,11 +93,11 @@ mergeInto(LibraryManager.library, {
if (!tty.input.length) {
var result = null;
if (ENVIRONMENT_IS_NODE) {
- if (process.stdin._readableState && process.stdin._readableState.ended) {
- return null; // EOF
- }
- result = process.stdin.read();
+ result = process['stdin']['read']();
if (!result) {
+ if (process['stdin']['_readableState'] && process['stdin']['_readableState']['ended']) {
+ return null; // EOF
+ }
return undefined; // no data available
}
} else if (typeof window != 'undefined' &&
diff --git a/tests/module/test_stdin.c b/tests/module/test_stdin.c
new file mode 100644
index 00000000..ab84fa77
--- /dev/null
+++ b/tests/module/test_stdin.c
@@ -0,0 +1,48 @@
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#if EMSCRIPTEN
+#include <emscripten.h>
+#endif
+
+int line = 0;
+
+void main_loop(void *arg)
+{
+ char str[10] = {0};
+ int ret;
+
+ if (line == 0) {
+ ret = fgetc(stdin);
+ if (ret != EOF) putc(ret, stdout);
+ if (ret == '\n') line++;
+ } else if (line > 0) {
+ ret = scanf("%10s", str);
+ if (ret > 0) puts(str);
+ }
+
+ if (ferror(stdin) && errno != EAGAIN) {
+ puts("error");
+ exit(EXIT_FAILURE);
+ }
+
+ if (feof(stdin)) {
+ puts("eof");
+ exit(EXIT_SUCCESS);
+ }
+
+ clearerr(stdin);
+}
+
+int main(int argc, char const *argv[])
+{
+ fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
+#if EMSCRIPTEN
+ emscripten_set_main_loop(main_loop, 60, 0);
+#else
+ while (1) main_loop(NULL); sleep(1);
+#endif
+ return 0;
+} \ No newline at end of file
diff --git a/tests/termios/test_tcgetattr.c b/tests/termios/test_tcgetattr.c
new file mode 100644
index 00000000..2b3780ee
--- /dev/null
+++ b/tests/termios/test_tcgetattr.c
@@ -0,0 +1,61 @@
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <termios.h>
+#include <unistd.h>
+
+static void create_file(const char *path, const char *buffer, int mode) {
+ int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
+ assert(fd >= 0);
+
+ int err = write(fd, buffer, sizeof(char) * strlen(buffer));
+ assert(err == (sizeof(char) * strlen(buffer)));
+
+ close(fd);
+}
+
+void setup() {
+ create_file("test.txt", "abcdefg", 0666);
+}
+
+void cleanup() {
+ unlink("test.txt");
+}
+
+void test() {
+ struct termios tc;
+ int ret;
+ int fd;
+
+ fd = open("test.txt", O_RDONLY);
+
+ ret = tcgetattr(fd, &tc);
+ assert(ret == -1);
+ assert(errno = ENOTTY);
+
+ ret = tcgetattr(STDIN_FILENO, &tc);
+ assert(!ret);
+
+ ret = tcsetattr(fd, 0, &tc);
+ assert(ret == -1);
+ assert(errno = ENOTTY);
+
+ ret = tcsetattr(STDIN_FILENO, 0, &tc);
+ assert(!ret);
+
+ close(fd);
+
+ puts("success");
+}
+
+int main() {
+ atexit(cleanup);
+ signal(SIGABRT, cleanup);
+ setup();
+ test();
+ return EXIT_SUCCESS;
+}
diff --git a/tests/test_core.py b/tests/test_core.py
index 17707c44..51face6a 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -4670,6 +4670,10 @@ The current type of b is: 9
expected = open(path_from_root('tests', 'pthread', 'specific.c.txt'), 'r').read()
self.do_run(src, expected, force_c=True)
+ def test_tcgetattr(self):
+ src = open(path_from_root('tests', 'termios', 'test_tcgetattr.c'), 'r').read()
+ self.do_run(src, 'success', force_c=True)
+
def test_time(self):
# XXX Not sure what the right output is here. Looks like the test started failing with daylight savings changes. Modified it to pass again.
src = open(path_from_root('tests', 'time', 'src.c'), 'r').read()
diff --git a/tests/test_other.py b/tests/test_other.py
index a6813b07..f583cf07 100644
--- a/tests/test_other.py
+++ b/tests/test_other.py
@@ -929,20 +929,9 @@ f.close()
self.assertContained('libf1\nlibf2\n', run_js(os.path.join(self.get_dir(), 'a.out.js')))
def test_stdin(self):
- open('main.cpp', 'w').write(r'''
-#include <stdio.h>
-int main(int argc, char const *argv[])
-{
- char str[10] = {0};
- scanf("%10s", str);
- printf("%s\n", str);
- return 0;
-}
-''')
- Building.emcc('main.cpp', output_filename='a.out.js')
- open('in.txt', 'w').write('abc')
- # node's stdin support is broken
- self.assertContained('abc', Popen(listify(SPIDERMONKEY_ENGINE) + ['a.out.js'], stdin=open('in.txt'), stdout=PIPE, stderr=PIPE).communicate()[0])
+ Building.emcc(path_from_root('tests', 'module', 'test_stdin.c'), output_filename='a.out.js')
+ open('in.txt', 'w').write('abcdef\nghijkl')
+ self.assertContained('abcdef\nghijkl\neof', run_js(os.path.join(self.get_dir(), 'a.out.js'), stdin=open('in.txt')))
def test_ungetc_fscanf(self):
open('main.cpp', 'w').write(r'''
diff --git a/tools/jsrun.py b/tools/jsrun.py
index 571e9cee..91038f6e 100644
--- a/tools/jsrun.py
+++ b/tools/jsrun.py
@@ -12,13 +12,14 @@ def timeout_run(proc, timeout, note='unnamed process', full_output=False):
out = proc.communicate()
return '\n'.join(out) if full_output else out[0]
-def run_js(filename, engine=None, args=[], check_timeout=False, stdout=PIPE, stderr=None, cwd=None, full_output=False):
+def run_js(filename, engine=None, args=[], check_timeout=False, stdin=None, stdout=PIPE, stderr=None, cwd=None, full_output=False):
if type(engine) is not list:
engine = [engine]
command = engine + [filename] + (['--'] if 'd8' in engine[0] or 'jsc' in engine[0] else []) + args
return timeout_run(
Popen(
command,
+ stdin=stdin,
stdout=stdout,
stderr=stderr,
cwd=cwd),