1
mergeInto(LibraryManager.library, {
3
$TTY__postset: '__ATINIT__.unshift({ func: function() { TTY.init() } });' +
4
'__ATEXIT__.push({ func: function() { TTY.shutdown() } });' +
5
'TTY.utf8 = new Runtime.UTF8Processor();',
9
// https://github.com/kripken/emscripten/pull/1555
10
// if (ENVIRONMENT_IS_NODE) {
11
// // currently, FS.init does not distinguish if process.stdin is a file or TTY
12
// // device, it always assumes it's a TTY device. because of this, we're forcing
13
// // process.stdin to UTF8 encoding to at least make stdin reading compatible
14
// // with text files until FS.init can be refactored.
15
// process['stdin']['setEncoding']('utf8');
18
shutdown: function() {
19
// https://github.com/kripken/emscripten/pull/1555
20
// if (ENVIRONMENT_IS_NODE) {
21
// // inolen: any idea as to why node -e 'process.stdin.read()' wouldn't exit immediately (with process.stdin being a tty)?
22
// // 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
23
// // inolen: I thought read() in that case was a synchronous operation that just grabbed some amount of buffered data if it exists?
24
// // isaacs: it is. but it also triggers a _read() call, which calls readStart() on the handle
25
// // isaacs: do process.stdin.pause() and i'd think it'd probably close the pending call
26
// process['stdin']['pause']();
29
register: function(dev, ops) {
30
TTY.ttys[dev] = { input: [], output: [], ops: ops };
31
FS.registerDevice(dev, TTY.stream_ops);
34
open: function(stream) {
35
var tty = TTY.ttys[stream.node.rdev];
37
throw new FS.ErrnoError(ERRNO_CODES.ENODEV);
40
stream.seekable = false;
42
close: function(stream) {
43
// flush any pending line data
44
if (stream.tty.output.length) {
45
stream.tty.ops.put_char(stream.tty, {{{ charCode('\n') }}});
48
read: function(stream, buffer, offset, length, pos /* ignored */) {
49
if (!stream.tty || !stream.tty.ops.get_char) {
50
throw new FS.ErrnoError(ERRNO_CODES.ENXIO);
53
for (var i = 0; i < length; i++) {
56
result = stream.tty.ops.get_char(stream.tty);
58
throw new FS.ErrnoError(ERRNO_CODES.EIO);
60
if (result === undefined && bytesRead === 0) {
61
throw new FS.ErrnoError(ERRNO_CODES.EAGAIN);
63
if (result === null || result === undefined) break;
65
buffer[offset+i] = result;
68
stream.node.timestamp = Date.now();
72
write: function(stream, buffer, offset, length, pos) {
73
if (!stream.tty || !stream.tty.ops.put_char) {
74
throw new FS.ErrnoError(ERRNO_CODES.ENXIO);
76
for (var i = 0; i < length; i++) {
78
stream.tty.ops.put_char(stream.tty, buffer[offset+i]);
80
throw new FS.ErrnoError(ERRNO_CODES.EIO);
84
stream.node.timestamp = Date.now();
90
// get_char has 3 particular return values:
91
// a.) the next character represented as an integer
92
// b.) undefined to signal that no data is currently available
93
// c.) null to signal an EOF
94
get_char: function(tty) {
95
if (!tty.input.length) {
97
if (ENVIRONMENT_IS_NODE) {
98
result = process['stdin']['read']();
100
if (process['stdin']['_readableState'] && process['stdin']['_readableState']['ended']) {
103
return undefined; // no data available
105
} else if (typeof window != 'undefined' &&
106
typeof window.prompt == 'function') {
108
result = window.prompt('Input: '); // returns null on cancel
109
if (result !== null) {
112
} else if (typeof readline == 'function') {
115
if (result !== null) {
122
tty.input = intArrayFromString(result, true);
124
return tty.input.shift();
126
put_char: function(tty, val) {
127
if (val === null || val === {{{ charCode('\n') }}}) {
128
Module['print'](tty.output.join(''));
131
tty.output.push(TTY.utf8.processCChar(val));
136
put_char: function(tty, val) {
137
if (val === null || val === {{{ charCode('\n') }}}) {
138
Module['printErr'](tty.output.join(''));
141
tty.output.push(TTY.utf8.processCChar(val));
b'\\ No newline at end of file'