aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-08-29 18:17:40 -0700
committerAlon Zakai <alonzakai@gmail.com>2013-08-29 18:17:40 -0700
commit77c4a7eb74cc51419331009ca83671395f263c6c (patch)
tree3afe00e915fb253e975cc359365d8340f0d350f8 /src
parent8f998042dc594b59129a77556d1c98160220b585 (diff)
parent0d23384af497877ad156af27e6f4aa731acea461 (diff)
Merge pull request #1555 from inolen/tty_fixes
added stubs for tcgetattr and tcsetattr, minor node tty fixes
Diffstat (limited to 'src')
-rw-r--r--src/library.js38
-rw-r--r--src/library_tty.js41
2 files changed, 68 insertions, 11 deletions
diff --git a/src/library.js b/src/library.js
index 3ba2f56b..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;
@@ -5154,6 +5157,37 @@ LibraryManager.library = {
},
// ==========================================================================
+ // termios.h
+ // ==========================================================================
+ tcgetattr: function(fildes, termios_p) {
+ // http://pubs.opengroup.org/onlinepubs/009695399/functions/tcgetattr.html
+ var stream = FS.getStream(fildes);
+ if (!stream) {
+ ___setErrNo(ERRNO_CODES.EBADF);
+ return -1;
+ }
+ if (!stream.tty) {
+ ___setErrNo(ERRNO_CODES.ENOTTY);
+ return -1;
+ }
+ return 0;
+ },
+
+ tcsetattr: function(fildes, optional_actions, termios_p) {
+ // http://pubs.opengroup.org/onlinepubs/7908799/xsh/tcsetattr.html
+ var stream = FS.getStream(fildes);
+ if (!stream) {
+ ___setErrNo(ERRNO_CODES.EBADF);
+ return -1;
+ }
+ if (!stream.tty) {
+ ___setErrNo(ERRNO_CODES.ENOTTY);
+ return -1;
+ }
+ return 0;
+ },
+
+ // ==========================================================================
// time.h
// ==========================================================================
diff --git a/src/library_tty.js b/src/library_tty.js
index 8f44cd07..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,17 +84,22 @@ 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
+ // b.) undefined to signal that no data is currently available
+ // c.) null to signal an EOF
get_char: function(tty) {
if (!tty.input.length) {
var result = null;
if (ENVIRONMENT_IS_NODE) {
- if (process.stdin.destroyed) {
- return undefined;
+ result = process['stdin']['read']();
+ if (!result) {
+ if (process['stdin']['_readableState'] && process['stdin']['_readableState']['ended']) {
+ return null; // EOF
+ }
+ return undefined; // no data available
}
- result = process.stdin.read();
} else if (typeof window != 'undefined' &&
typeof window.prompt == 'function') {
// Browser.