Change: rewrite code to open TTY

This commit is contained in:
anseki 2015-03-20 12:03:58 +09:00
parent 583cb4048a
commit ecd6846f7c

View file

@ -17,112 +17,182 @@ var
DEFAULT_ERR_MSG = 'The platform doesn\'t support interactive reading', DEFAULT_ERR_MSG = 'The platform doesn\'t support interactive reading',
fs = require('fs'), fs = require('fs'),
TTY = process.binding('tty_wrap').TTY,
childProc = require('child_process'), childProc = require('child_process'),
promptText = '> ', promptText = '> ',
encoding = 'utf8', encoding = 'utf8',
bufSize = 1024, bufSize = 1024,
print, print,
tempdir, salt = 0, useShell = false; mask = '*',
useShell = false,
fdR = 'none', fdW, ttyR, isRawMode = false,
tempdir, salt = 0;
function _readlineSync(options) { // options.display is string function _readlineSync(options) { // options.display is string
var input = '', fd, buffer, rsize, res, isOpened, fsBind, constBind; var input = '', isEditable, displayInput;
if (options.display !== '' && typeof print === 'function') function tryShell() {
{ print(options.display, encoding); } console.warn('<tryShell>');
var res = _readlineShell(options);
if (useShell || options.noEchoBack || options.keyIn) {
res = _readlineShell(options);
if (res.error) { throw res.error; } if (res.error) { throw res.error; }
input = res.input; return res.input;
} else { }
if (IS_WIN) { // r/w mode not supported // Node v0.10- returns an error if same mode is set.
if (process.stdin.isTTY && process.stdout.isTTY) { function setRawMode(mode) {
if (options.display !== '') { if (mode === isRawMode) { return true; }
// process.stdout.write(options.display, encoding); if (ttyR.setRawMode(mode) !== 0) { return false; }
fs.writeSync(process.stdout.fd, options.display); isRawMode = mode;
options.display = ''; return true;
} }
fd = process.stdin.fd;
isOpened = true; (function() {
var fsB, constants;
function getFsB() {
if (!fsB) {
fsB = process.binding('fs'); // For raw device path
constants = process.binding('constants');
}
return fsB;
}
if (typeof fdR !== 'string') { return; }
fdR = null;
if (IS_WIN) {
if (process.stdin.isTTY) {
fdR = process.stdin.fd;
ttyR = process.stdin._handle;
console.warn('OPENED: STDIN');
} else { } else {
try { try {
if (options.display !== '') { // The stream by fs.openSync('\\\\.\\CON', 'r') can't switch to raw mode.
fd = fs.openSync('\\\\.\\CON', 'w'); // 'CONIN$' might fail on XP, 2000, 7 (x86).
fs.writeSync(fd, options.display); fdR = getFsB().open('CONIN$', constants.O_RDWR, parseInt('0666', 8));
fs.closeSync(fd); ttyR = new TTY(fdR, true);
options.display = ''; console.warn('OPENED: CONIN$');
}
fd = fs.openSync('\\\\.\\CON', 'rs');
isOpened = true;
} catch (e) {} } catch (e) {}
}
if (!isOpened || options.display !== '') { // Retry if (process.stdout.isTTY) {
fdW = process.stdout.fd;
console.warn('OPENED: STDOUT');
} else {
try {
fdW = fs.openSync('\\\\.\\CON', 'w');
console.warn('OPENED: \\\\.\\CON(W)');
} catch (e) {}
if (typeof fdW !== 'number') { // Retry
try { try {
// For raw device path fdW = getFsB().open('CONOUT$', constants.O_RDWR, parseInt('0666', 8));
// On XP, 2000, 7 (x86), it might fail. console.warn('OPENED: CONOUT$');
// And, process.binding('fs') might be no good.
fsBind = process.binding('fs');
constBind = process.binding('constants');
if (options.display !== '') {
fd = fsBind.open('CONOUT$',
constBind.O_RDWR | constBind.O_SYNC, parseInt('0666', 8));
fs.writeSync(fd, options.display);
fs.closeSync(fd);
options.display = '';
}
fd = fsBind.open('CONIN$',
constBind.O_RDWR | constBind.O_SYNC, parseInt('0666', 8));
isOpened = true;
} catch (e) {} } catch (e) {}
} }
} }
} else { } else {
try { if (process.stdin.isTTY) {
fd = fs.openSync('/dev/tty', 'rs+');
isOpened = true;
if (options.display !== '') {
fs.writeSync(fd, options.display);
options.display = '';
}
} catch (e) {}
}
if (isOpened && options.display === '') {
buffer = new Buffer(bufSize);
while (true) {
rsize = 0;
try { try {
rsize = fs.readSync(fd, buffer, 0, bufSize); fdR = fs.openSync('/dev/tty', 'r'); // device file, not process.stdin
} catch (e) { ttyR = process.stdin._handle;
if (e.code === 'EOF') { break; } console.warn('OPENED: /dev/tty(R) and STDIN handle');
} catch (e) {}
res = _readlineShell(options); } else {
if (res.error) { throw res.error; } // Node v0.12 read() fails.
input += res.input; try {
break; fdR = fs.openSync('/dev/tty', 'r');
} ttyR = new TTY(fdR, false);
console.warn('OPENED: /dev/tty(R)');
if (rsize === 0) { break; } } catch (e) {}
input += buffer.toString(encoding, 0, rsize);
if (/[\r\n]$/.test(input)) { break; }
} }
} else { if (process.stdout.isTTY) {
res = _readlineShell(options); fdW = process.stdout.fd;
if (res.error) { throw res.error; } console.warn('OPENED: STDOUT');
input = res.input; } else {
try {
fdW = fs.openSync('/dev/tty', 'w');
console.warn('OPENED: /dev/tty(W)');
} catch (e) {}
}
}
})();
// Call before tryShell()
if (options.display !== '' && typeof print === 'function')
{ print(options.display, encoding); }
isEditable = !options.noEchoBack && !options.keyIn;
(function() { // try read
var buffer, reqSize, readSize, chunk;
if (useShell || !ttyR ||
typeof fdW !== 'number' && (options.display !== '' || !isEditable)) {
input = tryShell();
return;
} }
if (isOpened && !process.stdin.isTTY) { fs.closeSync(fd); } if (options.display !== '') {
fs.writeSync(fdW, options.display);
options.display = '';
}
if (!setRawMode(!isEditable)) {
input = tryShell();
return;
}
buffer = new Buffer((reqSize = options.keyIn ? 1 : bufSize));
console.warn('<<isEditable>>: ' + isEditable);
console.warn('--------');
while (true) {
readSize = 0;
try {
readSize = fs.readSync(fdR, buffer, 0, reqSize);
} catch (e) {
if (e.code === 'EOF') { break; }
setRawMode(false);
input += tryShell();
return;
}
console.warn('readSize: ' + readSize);
if (readSize === 0) { break; }
chunk = buffer.toString(encoding, 0, readSize);
// other ctrl-chars
if ((chunk = chunk.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, '')) === '')
{ continue; }
if (!isEditable) {
displayInput = chunk.replace(/[\r\n]/g, '');
if (options.noEchoBack) { displayInput = displayInput.replace(/./g, mask); }
if (displayInput !== '') { fs.writeSync(fdW, displayInput); }
}
input += chunk;
console.warn('<<input>>: ' + input);
if (/[\r\n]$/.test(input) ||
options.keyIn && input.length >= reqSize) { break; }
}
if (!isEditable) { fs.writeSync(fdW, '\n'); }
setRawMode(false);
})();
if (typeof print === 'function') {
displayInput = input.replace(/[\r\n]/g, '');
print((options.noEchoBack ?
displayInput.replace(/./g, mask) : displayInput) + '\n', encoding);
} }
return options.noTrim ? input.replace(/[\r\n]+$/, '') : input.trim(); return options.noTrim || options.keyIn ?
input.replace(/[\r\n]+$/, '') : input.trim();
} }
function _readlineShell(options) { function _readlineShell(options) {