Browse Source

Windows line endings. s/\r//g

v0.7.4-release
isaacs 14 years ago
committed by Ryan Dahl
parent
commit
d40af91b30
  1. 638
      lib/tty_posix.js
  2. 326
      lib/tty_win32.js

638
lib/tty_posix.js

@ -1,319 +1,319 @@
var binding = process.binding('stdio'), var binding = process.binding('stdio'),
net = require('net'), net = require('net'),
inherits = require('util').inherits, inherits = require('util').inherits,
spawn = require('child_process').spawn; spawn = require('child_process').spawn;
exports.open = function(path, args) { exports.open = function(path, args) {
var fds = binding.openpty(); var fds = binding.openpty();
var slaveFD = fds[0]; var slaveFD = fds[0];
var masterFD = fds[1]; var masterFD = fds[1];
var env = { TERM: 'vt100' }; var env = { TERM: 'vt100' };
for (var k in process.env) { for (var k in process.env) {
env[k] = process.env[k]; env[k] = process.env[k];
} }
var stream = require('net').Stream(slaveFD); var stream = require('net').Stream(slaveFD);
stream.readable = stream.writable = true; stream.readable = stream.writable = true;
stream.resume(); stream.resume();
child = spawn(path, args, { child = spawn(path, args, {
env: env, env: env,
customFds: [masterFD, masterFD, masterFD], customFds: [masterFD, masterFD, masterFD],
setuid: true setuid: true
}); });
return [stream, child]; return [stream, child];
}; };
function ReadStream(fd) { function ReadStream(fd) {
if (!(this instanceof ReadStream)) return new ReadStream(fd); if (!(this instanceof ReadStream)) return new ReadStream(fd);
net.Socket.call(this, fd); net.Socket.call(this, fd);
var self = this, var self = this,
keypressListeners = this.listeners('keypress'); keypressListeners = this.listeners('keypress');
function onData(b) { function onData(b) {
if (keypressListeners.length) { if (keypressListeners.length) {
self._emitKey(b); self._emitKey(b);
} else { } else {
// Nobody's watching anyway // Nobody's watching anyway
self.removeListener('data', onData); self.removeListener('data', onData);
self.on('newlistener', onNewListener); self.on('newlistener', onNewListener);
} }
} }
function onNewListener(event) { function onNewListener(event) {
if (event == 'keypress') { if (event == 'keypress') {
self.on('data', onData); self.on('data', onData);
self.removeListener('newlistener', onNewListener); self.removeListener('newlistener', onNewListener);
} }
} }
this.on('newListener', onNewListener); this.on('newListener', onNewListener);
} }
inherits(ReadStream, net.Socket); inherits(ReadStream, net.Socket);
exports.ReadStream = ReadStream; exports.ReadStream = ReadStream;
ReadStream.prototype.isTTY = true; ReadStream.prototype.isTTY = true;
/* /*
Some patterns seen in terminal key escape codes, derived from combos seen Some patterns seen in terminal key escape codes, derived from combos seen
at http://www.midnight-commander.org/browser/lib/tty/key.c at http://www.midnight-commander.org/browser/lib/tty/key.c
ESC letter ESC letter
ESC [ letter ESC [ letter
ESC [ modifier letter ESC [ modifier letter
ESC [ 1 ; modifier letter ESC [ 1 ; modifier letter
ESC [ num char ESC [ num char
ESC [ num ; modifier char ESC [ num ; modifier char
ESC O letter ESC O letter
ESC O modifier letter ESC O modifier letter
ESC O 1 ; modifier letter ESC O 1 ; modifier letter
ESC N letter ESC N letter
ESC [ [ num ; modifier char ESC [ [ num ; modifier char
ESC [ [ 1 ; modifier letter ESC [ [ 1 ; modifier letter
ESC ESC [ num char ESC ESC [ num char
ESC ESC O letter ESC ESC O letter
- char is usually ~ but $ and ^ also happen with rxvt - char is usually ~ but $ and ^ also happen with rxvt
- modifier is 1 + - modifier is 1 +
(shift * 1) + (shift * 1) +
(left_alt * 2) + (left_alt * 2) +
(ctrl * 4) + (ctrl * 4) +
(right_alt * 8) (right_alt * 8)
- two leading ESCs apparently mean the same as one leading ESC - two leading ESCs apparently mean the same as one leading ESC
*/ */
// Regexes used for ansi escape code splitting // Regexes used for ansi escape code splitting
var metaKeyCodeRe = /^(?:\x1b)([a-zA-Z0-9])$/, var metaKeyCodeRe = /^(?:\x1b)([a-zA-Z0-9])$/,
functionKeyCodeRe = functionKeyCodeRe =
/^(?:\x1b+)(O|N|\[|\[\[)(?:(\d+)(?:;(\d+))?([~^$])|(?:1;(\d+))?([a-zA-Z]))/; /^(?:\x1b+)(O|N|\[|\[\[)(?:(\d+)(?:;(\d+))?([~^$])|(?:1;(\d+))?([a-zA-Z]))/;
ReadStream.prototype._emitKey = function(s) { ReadStream.prototype._emitKey = function(s) {
var char, var char,
key = { key = {
name: undefined, name: undefined,
ctrl: false, ctrl: false,
meta: false, meta: false,
shift: false shift: false
}, },
parts; parts;
if (Buffer.isBuffer(s)) { if (Buffer.isBuffer(s)) {
s = s.toString(this.encoding || 'utf-8'); s = s.toString(this.encoding || 'utf-8');
} }
if (s === '\r') { if (s === '\r') {
// enter // enter
key.name = 'enter'; key.name = 'enter';
} else if (s === '\t') { } else if (s === '\t') {
// tab // tab
key.name = 'tab'; key.name = 'tab';
} else if (s === '\b' || s === '\x7f') { } else if (s === '\b' || s === '\x7f') {
// backspace or ctrl+h // backspace or ctrl+h
key.name = 'backspace'; key.name = 'backspace';
} else if (s === '\x1b') { } else if (s === '\x1b') {
// escape key // escape key
key.name = 'escape'; key.name = 'escape';
} else if (s === ' ') { } else if (s === ' ') {
key.name = 'space'; key.name = 'space';
} else if (s <= '\x1a') { } else if (s <= '\x1a') {
// ctrl+letter // ctrl+letter
key.name = String.fromCharCode(s.charCodeAt(0) + 'a'.charCodeAt(0) - 1); key.name = String.fromCharCode(s.charCodeAt(0) + 'a'.charCodeAt(0) - 1);
key.ctrl = true; key.ctrl = true;
} else if (s >= 'a' && s <= 'z') { } else if (s >= 'a' && s <= 'z') {
// lowercase letter // lowercase letter
key.name = s; key.name = s;
} else if (s >= 'A' && s <= 'Z') { } else if (s >= 'A' && s <= 'Z') {
// shift+letter // shift+letter
key.name = s.toLowerCase(); key.name = s.toLowerCase();
key.shift = true; key.shift = true;
} else if (parts = metaKeyCodeRe.exec(s)) { } else if (parts = metaKeyCodeRe.exec(s)) {
// meta+character key // meta+character key
key.name = parts[1].toLowerCase(); key.name = parts[1].toLowerCase();
key.meta = true; key.meta = true;
key.shift = /^[A-Z]$/.test(parts[1]); key.shift = /^[A-Z]$/.test(parts[1]);
} else if (parts = functionKeyCodeRe.exec(s)) { } else if (parts = functionKeyCodeRe.exec(s)) {
// ansi escape sequence // ansi escape sequence
// reassemble the key code leaving out leading \x1b's, // reassemble the key code leaving out leading \x1b's,
// the modifier key bitflag and any meaningless "1;" sequence // the modifier key bitflag and any meaningless "1;" sequence
var code = (parts[1] || '') + (parts[2] || '') + var code = (parts[1] || '') + (parts[2] || '') +
(parts[4] || '') + (parts[6] || ''), (parts[4] || '') + (parts[6] || ''),
modifier = (parts[3] || parts[5] || 1) - 1; modifier = (parts[3] || parts[5] || 1) - 1;
// Parse the key modifier // Parse the key modifier
key.ctrl = !!(modifier & 4); key.ctrl = !!(modifier & 4);
key.meta = !!(modifier & 10); key.meta = !!(modifier & 10);
key.shift = !!(modifier & 1); key.shift = !!(modifier & 1);
// Parse the key itself // Parse the key itself
switch (code) { switch (code) {
/* xterm/gnome ESC O letter */ /* xterm/gnome ESC O letter */
case 'OP': key.name = 'f1'; break; case 'OP': key.name = 'f1'; break;
case 'OQ': key.name = 'f2'; break; case 'OQ': key.name = 'f2'; break;
case 'OR': key.name = 'f3'; break; case 'OR': key.name = 'f3'; break;
case 'OS': key.name = 'f4'; break; case 'OS': key.name = 'f4'; break;
/* xterm/rxvt ESC [ number ~ */ /* xterm/rxvt ESC [ number ~ */
case '[11~': key.name = 'f1'; break; case '[11~': key.name = 'f1'; break;
case '[12~': key.name = 'f2'; break; case '[12~': key.name = 'f2'; break;
case '[13~': key.name = 'f3'; break; case '[13~': key.name = 'f3'; break;
case '[14~': key.name = 'f4'; break; case '[14~': key.name = 'f4'; break;
/* common */ /* common */
case '[15~': key.name = 'f5'; break; case '[15~': key.name = 'f5'; break;
case '[17~': key.name = 'f6'; break; case '[17~': key.name = 'f6'; break;
case '[18~': key.name = 'f7'; break; case '[18~': key.name = 'f7'; break;
case '[19~': key.name = 'f8'; break; case '[19~': key.name = 'f8'; break;
case '[20~': key.name = 'f9'; break; case '[20~': key.name = 'f9'; break;
case '[21~': key.name = 'f10'; break; case '[21~': key.name = 'f10'; break;
case '[23~': key.name = 'f11'; break; case '[23~': key.name = 'f11'; break;
case '[24~': key.name = 'f12'; break; case '[24~': key.name = 'f12'; break;
/* xterm ESC [ letter */ /* xterm ESC [ letter */
case '[A': key.name = 'up'; break; case '[A': key.name = 'up'; break;
case '[B': key.name = 'down'; break; case '[B': key.name = 'down'; break;
case '[C': key.name = 'right'; break; case '[C': key.name = 'right'; break;
case '[D': key.name = 'left'; break; case '[D': key.name = 'left'; break;
case '[E': key.name = 'clear'; break; case '[E': key.name = 'clear'; break;
case '[F': key.name = 'end'; break; case '[F': key.name = 'end'; break;
case '[H': key.name = 'home'; break; case '[H': key.name = 'home'; break;
/* xterm/gnome ESC O letter */ /* xterm/gnome ESC O letter */
case 'OA': key.name = 'up'; break; case 'OA': key.name = 'up'; break;
case 'OB': key.name = 'down'; break; case 'OB': key.name = 'down'; break;
case 'OC': key.name = 'right'; break; case 'OC': key.name = 'right'; break;
case 'OD': key.name = 'left'; break; case 'OD': key.name = 'left'; break;
case 'OE': key.name = 'clear'; break; case 'OE': key.name = 'clear'; break;
case 'OF': key.name = 'end'; break; case 'OF': key.name = 'end'; break;
case 'OH': key.name = 'home'; break; case 'OH': key.name = 'home'; break;
/* xterm/rxvt ESC [ number ~ */ /* xterm/rxvt ESC [ number ~ */
case '[1~': key.name = 'home'; break; case '[1~': key.name = 'home'; break;
case '[2~': key.name = 'insert'; break; case '[2~': key.name = 'insert'; break;
case '[3~': key.name = 'delete'; break; case '[3~': key.name = 'delete'; break;
case '[4~': key.name = 'end'; break; case '[4~': key.name = 'end'; break;
case '[5~': key.name = 'pageup'; break; case '[5~': key.name = 'pageup'; break;
case '[6~': key.name = 'pagedown'; break; case '[6~': key.name = 'pagedown'; break;
/* putty */ /* putty */
case '[[5~': key.name = 'pageup'; break; case '[[5~': key.name = 'pageup'; break;
case '[[6~': key.name = 'pagedown'; break; case '[[6~': key.name = 'pagedown'; break;
/* rxvt */ /* rxvt */
case '[7~': key.name = 'home'; break; case '[7~': key.name = 'home'; break;
case '[8~': key.name = 'end'; break; case '[8~': key.name = 'end'; break;
/* rxvt keys with modifiers */ /* rxvt keys with modifiers */
case '[a': key.name = 'up'; key.shift = true; break; case '[a': key.name = 'up'; key.shift = true; break;
case '[b': key.name = 'down'; key.shift = true; break; case '[b': key.name = 'down'; key.shift = true; break;
case '[c': key.name = 'right'; key.shift = true; break; case '[c': key.name = 'right'; key.shift = true; break;
case '[d': key.name = 'left'; key.shift = true; break; case '[d': key.name = 'left'; key.shift = true; break;
case '[e': key.name = 'clear'; key.shift = true; break; case '[e': key.name = 'clear'; key.shift = true; break;
case '[2$': key.name = 'insert'; key.shift = true; break; case '[2$': key.name = 'insert'; key.shift = true; break;
case '[3$': key.name = 'delete'; key.shift = true; break; case '[3$': key.name = 'delete'; key.shift = true; break;
case '[5$': key.name = 'pageup'; key.shift = true; break; case '[5$': key.name = 'pageup'; key.shift = true; break;
case '[6$': key.name = 'pagedown'; key.shift = true; break; case '[6$': key.name = 'pagedown'; key.shift = true; break;
case '[7$': key.name = 'home'; key.shift = true; break; case '[7$': key.name = 'home'; key.shift = true; break;
case '[8$': key.name = 'end'; key.shift = true; break; case '[8$': key.name = 'end'; key.shift = true; break;
case 'Oa': key.name = 'up'; key.ctrl = true; break; case 'Oa': key.name = 'up'; key.ctrl = true; break;
case 'Ob': key.name = 'down'; key.ctrl = true; break; case 'Ob': key.name = 'down'; key.ctrl = true; break;
case 'Oc': key.name = 'right'; key.ctrl = true; break; case 'Oc': key.name = 'right'; key.ctrl = true; break;
case 'Od': key.name = 'left'; key.ctrl = true; break; case 'Od': key.name = 'left'; key.ctrl = true; break;
case 'Oe': key.name = 'clear'; key.ctrl = true; break; case 'Oe': key.name = 'clear'; key.ctrl = true; break;
case '[2^': key.name = 'insert'; key.ctrl = true; break; case '[2^': key.name = 'insert'; key.ctrl = true; break;
case '[3^': key.name = 'delete'; key.ctrl = true; break; case '[3^': key.name = 'delete'; key.ctrl = true; break;
case '[5^': key.name = 'pageup'; key.ctrl = true; break; case '[5^': key.name = 'pageup'; key.ctrl = true; break;
case '[6^': key.name = 'pagedown'; key.ctrl = true; break; case '[6^': key.name = 'pagedown'; key.ctrl = true; break;
case '[7^': key.name = 'home'; key.ctrl = true; break; case '[7^': key.name = 'home'; key.ctrl = true; break;
case '[8^': key.name = 'end'; key.ctrl = true; break; case '[8^': key.name = 'end'; key.ctrl = true; break;
/* misc. */ /* misc. */
case '[Z': key.name = 'tab'; key.shift = true; break; case '[Z': key.name = 'tab'; key.shift = true; break;
} }
} }
// Don't emit a key if no name was found // Don't emit a key if no name was found
if (key.name === undefined) { if (key.name === undefined) {
key = undefined; key = undefined;
} }
if (s.length === 1) { if (s.length === 1) {
char = s; char = s;
} }
if (key || char) { if (key || char) {
this.emit('keypress', char, key); this.emit('keypress', char, key);
} }
}; };
function WriteStream(fd) { function WriteStream(fd) {
if (!(this instanceof WriteStream)) return new WriteStream(fd); if (!(this instanceof WriteStream)) return new WriteStream(fd);
net.Socket.call(this, fd); net.Socket.call(this, fd);
} }
inherits(WriteStream, net.Socket); inherits(WriteStream, net.Socket);
exports.WriteStream = WriteStream; exports.WriteStream = WriteStream;
WriteStream.prototype.isTTY = true; WriteStream.prototype.isTTY = true;
WriteStream.prototype.cursorTo = function(x, y) { WriteStream.prototype.cursorTo = function(x, y) {
if (typeof x !== 'number' && typeof y !== 'number') if (typeof x !== 'number' && typeof y !== 'number')
return; return;
if (typeof x !== 'number') if (typeof x !== 'number')
throw new Error("Can't set cursor row without also setting it's column"); throw new Error("Can't set cursor row without also setting it's column");
if (typeof x === 'number') { if (typeof x === 'number') {
this.write('\x1b[' + (x + 1) + 'G'); this.write('\x1b[' + (x + 1) + 'G');
} else { } else {
this.write('\x1b[' + (y + 1) + ';' + (x + 1) + 'H'); this.write('\x1b[' + (y + 1) + ';' + (x + 1) + 'H');
} }
}; };
WriteStream.prototype.moveCursor = function(dx, dy) { WriteStream.prototype.moveCursor = function(dx, dy) {
if (dx < 0) { if (dx < 0) {
this.write('\x1b[' + (-dx) + 'D'); this.write('\x1b[' + (-dx) + 'D');
} else if (dx > 0) { } else if (dx > 0) {
this.write('\x1b[' + dx + 'C'); this.write('\x1b[' + dx + 'C');
} }
if (dy < 0) { if (dy < 0) {
this.write('\x1b[' + (-dy) + 'A'); this.write('\x1b[' + (-dy) + 'A');
} else if (dy > 0) { } else if (dy > 0) {
this.write('\x1b[' + dy + 'B'); this.write('\x1b[' + dy + 'B');
} }
}; };
WriteStream.prototype.clearLine = function(dir) { WriteStream.prototype.clearLine = function(dir) {
if (dir < 0) { if (dir < 0) {
// to the beginning // to the beginning
this.write('\x1b[1K'); this.write('\x1b[1K');
} else if (dir > 0) { } else if (dir > 0) {
// to the end // to the end
this.write('\x1b[0K'); this.write('\x1b[0K');
} else { } else {
// entire line // entire line
this.write('\x1b[2K'); this.write('\x1b[2K');
} }
}; };

326
lib/tty_win32.js

@ -1,164 +1,164 @@
var binding = process.binding('stdio'), var binding = process.binding('stdio'),
Stream = require('stream').Stream, Stream = require('stream').Stream,
inherits = require('util').inherits, inherits = require('util').inherits,
writeTTY = binding.writeTTY, writeTTY = binding.writeTTY,
closeTTY = binding.closeTTY; closeTTY = binding.closeTTY;
function ReadStream(fd) { function ReadStream(fd) {
if (!(this instanceof ReadStream)) return new ReadStream(fd); if (!(this instanceof ReadStream)) return new ReadStream(fd);
Stream.call(this); Stream.call(this);
var self = this; var self = this;
this.fd = fd; this.fd = fd;
this.paused = true; this.paused = true;
this.readable = true; this.readable = true;
var dataListeners = this.listeners('data'), var dataListeners = this.listeners('data'),
dataUseString = false; dataUseString = false;
function onError(err) { function onError(err) {
self.emit('error', err); self.emit('error', err);
} }
function onKeypress(char, key) { function onKeypress(char, key) {
self.emit('keypress', char, key); self.emit('keypress', char, key);
if (dataListeners.length && char) { if (dataListeners.length && char) {
self.emit('data', dataUseString ? char : new Buffer(char, 'utf-8')); self.emit('data', dataUseString ? char : new Buffer(char, 'utf-8'));
} }
} }
function onResize() { function onResize() {
process.emit('SIGWINCH'); process.emit('SIGWINCH');
} }
// A windows console stream is always UTF-16, actually // A windows console stream is always UTF-16, actually
// So it makes no sense to set the encoding, however someone could call // So it makes no sense to set the encoding, however someone could call
// setEncoding to tell us he wants strings not buffers // setEncoding to tell us he wants strings not buffers
this.setEncoding = function(encoding) { this.setEncoding = function(encoding) {
dataUseString = !!encoding; dataUseString = !!encoding;
} }
binding.initTTYWatcher(fd, onError, onKeypress, onResize); binding.initTTYWatcher(fd, onError, onKeypress, onResize);
} }
inherits(ReadStream, Stream); inherits(ReadStream, Stream);
exports.ReadStream = ReadStream; exports.ReadStream = ReadStream;
ReadStream.prototype.isTTY = true; ReadStream.prototype.isTTY = true;
ReadStream.prototype.pause = function() { ReadStream.prototype.pause = function() {
if (this.paused) if (this.paused)
return; return;
this.paused = true; this.paused = true;
binding.stopTTYWatcher(); binding.stopTTYWatcher();
}; };
ReadStream.prototype.resume = function() { ReadStream.prototype.resume = function() {
if (!this.readable) if (!this.readable)
throw new Error('Cannot resume() closed tty.ReadStream.'); throw new Error('Cannot resume() closed tty.ReadStream.');
if (!this.paused) if (!this.paused)
return; return;
this.paused = false; this.paused = false;
binding.startTTYWatcher(); binding.startTTYWatcher();
}; };
ReadStream.prototype.destroy = function() { ReadStream.prototype.destroy = function() {
if (!this.readable) if (!this.readable)
return; return;
this.readable = false; this.readable = false;
binding.destroyTTYWatcher(); binding.destroyTTYWatcher();
var self = this; var self = this;
process.nextTick(function() { process.nextTick(function() {
try { try {
closeTTY(self.fd); closeTTY(self.fd);
self.emit('close'); self.emit('close');
} catch (err) { } catch (err) {
self.emit('error', err); self.emit('error', err);
} }
}); });
}; };
ReadStream.prototype.destroySoon = ReadStream.prototype.destroy; ReadStream.prototype.destroySoon = ReadStream.prototype.destroy;
function WriteStream(fd) { function WriteStream(fd) {
if (!(this instanceof WriteStream)) return new WriteStream(fd); if (!(this instanceof WriteStream)) return new WriteStream(fd);
Stream.call(this); Stream.call(this);
var self = this; var self = this;
this.fd = fd; this.fd = fd;
this.writable = true; this.writable = true;
} }
inherits(WriteStream, Stream); inherits(WriteStream, Stream);
exports.WriteStream = WriteStream; exports.WriteStream = WriteStream;
WriteStream.prototype.isTTY = true; WriteStream.prototype.isTTY = true;
WriteStream.prototype.write = function(data, encoding) { WriteStream.prototype.write = function(data, encoding) {
if (!this.writable) { if (!this.writable) {
throw new Error('stream not writable'); throw new Error('stream not writable');
} }
if (Buffer.isBuffer(data)) { if (Buffer.isBuffer(data)) {
data = data.toString('utf-8'); data = data.toString('utf-8');
} }
try { try {
writeTTY(this.fd, data); writeTTY(this.fd, data);
} catch (err) { } catch (err) {
this.emit('error', err); this.emit('error', err);
} }
return true; return true;
}; };
WriteStream.prototype.end = function(data, encoding) { WriteStream.prototype.end = function(data, encoding) {
if (data) { if (data) {
this.write(data, encoding); this.write(data, encoding);
} }
this.destroy(); this.destroy();
}; };
WriteStream.prototype.destroy = function() { WriteStream.prototype.destroy = function() {
if (!this.writable) if (!this.writable)
return; return;
this.writable = false; this.writable = false;
var self = this; var self = this;
process.nextTick(function() { process.nextTick(function() {
var closed = false; var closed = false;
try { try {
closeTTY(self.fd); closeTTY(self.fd);
closed = true; closed = true;
} catch (err) { } catch (err) {
self.emit('error', err); self.emit('error', err);
} }
if (closed) { if (closed) {
self.emit('close'); self.emit('close');
} }
}); });
}; };
WriteStream.prototype.moveCursor = function(dx, dy) { WriteStream.prototype.moveCursor = function(dx, dy) {
if (!this.writable) if (!this.writable)
throw new Error('Stream not writable'); throw new Error('Stream not writable');
binding.moveCursor(this.fd, dx, dy); binding.moveCursor(this.fd, dx, dy);
}; };
WriteStream.prototype.cursorTo = function(x, y) { WriteStream.prototype.cursorTo = function(x, y) {
if (!this.writable) if (!this.writable)
throw new Error('Stream not writable'); throw new Error('Stream not writable');
binding.cursorTo(this.fd, x, y); binding.cursorTo(this.fd, x, y);
}; };
WriteStream.prototype.clearLine = function(direction) { WriteStream.prototype.clearLine = function(direction) {
if (!this.writable) if (!this.writable)
throw new Error('Stream not writable'); throw new Error('Stream not writable');
binding.clearLine(this.fd, direction || 0); binding.clearLine(this.fd, direction || 0);
}; };
Loading…
Cancel
Save