~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to src/library_tty.js

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-09-20 22:44:35 UTC
  • mfrom: (4.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130920224435-apuwj4fsl3fqv1a6
Tags: 1.5.6~20130920~6010666-1
* New snapshot release
* Update the list of supported architectures to the same as libv8
  (Closes: #723129)
* emlibtool has been removed from upstream.
* Fix warning syntax-error-in-dep5-copyright
* Refresh of the patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
mergeInto(LibraryManager.library, {
 
2
  $TTY__deps: ['$FS'],
 
3
  $TTY__postset: '__ATINIT__.unshift({ func: function() { TTY.init() } });' +
 
4
                 '__ATEXIT__.push({ func: function() { TTY.shutdown() } });' +
 
5
                 'TTY.utf8 = new Runtime.UTF8Processor();',
 
6
  $TTY: {
 
7
    ttys: [],
 
8
    init: function () {
 
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');
 
16
      // }
 
17
    },
 
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']();
 
27
      // }
 
28
    },
 
29
    register: function(dev, ops) {
 
30
      TTY.ttys[dev] = { input: [], output: [], ops: ops };
 
31
      FS.registerDevice(dev, TTY.stream_ops);
 
32
    },
 
33
    stream_ops: {
 
34
      open: function(stream) {
 
35
        var tty = TTY.ttys[stream.node.rdev];
 
36
        if (!tty) {
 
37
          throw new FS.ErrnoError(ERRNO_CODES.ENODEV);
 
38
        }
 
39
        stream.tty = tty;
 
40
        stream.seekable = false;
 
41
      },
 
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') }}});
 
46
        }
 
47
      },
 
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);
 
51
        }
 
52
        var bytesRead = 0;
 
53
        for (var i = 0; i < length; i++) {
 
54
          var result;
 
55
          try {
 
56
            result = stream.tty.ops.get_char(stream.tty);
 
57
          } catch (e) {
 
58
            throw new FS.ErrnoError(ERRNO_CODES.EIO);
 
59
          }
 
60
          if (result === undefined && bytesRead === 0) {
 
61
            throw new FS.ErrnoError(ERRNO_CODES.EAGAIN);
 
62
          }
 
63
          if (result === null || result === undefined) break;
 
64
          bytesRead++;
 
65
          buffer[offset+i] = result;
 
66
        }
 
67
        if (bytesRead) {
 
68
          stream.node.timestamp = Date.now();
 
69
        }
 
70
        return bytesRead;
 
71
      },
 
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);
 
75
        }
 
76
        for (var i = 0; i < length; i++) {
 
77
          try {
 
78
            stream.tty.ops.put_char(stream.tty, buffer[offset+i]);
 
79
          } catch (e) {
 
80
            throw new FS.ErrnoError(ERRNO_CODES.EIO);
 
81
          }
 
82
        }
 
83
        if (length) {
 
84
          stream.node.timestamp = Date.now();
 
85
        }
 
86
        return i;
 
87
      }
 
88
    },
 
89
    default_tty_ops: {
 
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) {
 
96
          var result = null;
 
97
          if (ENVIRONMENT_IS_NODE) {
 
98
            result = process['stdin']['read']();
 
99
            if (!result) {
 
100
              if (process['stdin']['_readableState'] && process['stdin']['_readableState']['ended']) {
 
101
                return null;  // EOF
 
102
              }
 
103
              return undefined;  // no data available
 
104
            }
 
105
          } else if (typeof window != 'undefined' &&
 
106
            typeof window.prompt == 'function') {
 
107
            // Browser.
 
108
            result = window.prompt('Input: ');  // returns null on cancel
 
109
            if (result !== null) {
 
110
              result += '\n';
 
111
            }
 
112
          } else if (typeof readline == 'function') {
 
113
            // Command line.
 
114
            result = readline();
 
115
            if (result !== null) {
 
116
              result += '\n';
 
117
            }
 
118
          }
 
119
          if (!result) {
 
120
            return null;
 
121
          }
 
122
          tty.input = intArrayFromString(result, true);
 
123
        }
 
124
        return tty.input.shift();
 
125
      },
 
126
      put_char: function(tty, val) {
 
127
        if (val === null || val === {{{ charCode('\n') }}}) {
 
128
          Module['print'](tty.output.join(''));
 
129
          tty.output = [];
 
130
        } else {
 
131
          tty.output.push(TTY.utf8.processCChar(val));
 
132
        }
 
133
      }
 
134
    },
 
135
    default_tty1_ops: {
 
136
      put_char: function(tty, val) {
 
137
        if (val === null || val === {{{ charCode('\n') }}}) {
 
138
          Module['printErr'](tty.output.join(''));
 
139
          tty.output = [];
 
140
        } else {
 
141
          tty.output.push(TTY.utf8.processCChar(val));
 
142
        }
 
143
      }
 
144
    }
 
145
  }
 
146
});
 
 
b'\\ No newline at end of file'