Change a method to open TTY.
This commit is contained in:
parent
0e22bc9f28
commit
f5d4a8231f
2 changed files with 141 additions and 62 deletions
|
@ -7,7 +7,6 @@ if "%~1"=="noechoback" (
|
||||||
if ERRORLEVEL 1 exit /b 1
|
if ERRORLEVEL 1 exit /b 1
|
||||||
) else (
|
) else (
|
||||||
set /p INPUT=<CON >CON
|
set /p INPUT=<CON >CON
|
||||||
if ERRORLEVEL 1 exit /b 1
|
|
||||||
)
|
)
|
||||||
set /p ="'%INPUT%'"<NUL
|
set /p ="'%INPUT%'"<NUL
|
||||||
endlocal
|
endlocal
|
||||||
|
|
|
@ -18,90 +18,163 @@ var
|
||||||
|
|
||||||
fs = require('fs'),
|
fs = require('fs'),
|
||||||
childProc = require('child_process'),
|
childProc = require('child_process'),
|
||||||
stdin = process.stdin,
|
|
||||||
stdout = process.stdout,
|
|
||||||
|
|
||||||
promptText = '> ',
|
promptText = '> ',
|
||||||
encoding = 'utf8',
|
encoding = 'utf8',
|
||||||
bufSize = 1024,
|
bufSize = 1024,
|
||||||
useShell = false, print, tempdir, salt = 0;
|
print,
|
||||||
|
tempdir, salt = 0, useShell = false;
|
||||||
|
|
||||||
function _readlineSync(display, options) {
|
function _readlineSync(options) { // options.display is string
|
||||||
var input = '', res, buffer = new Buffer(bufSize), rsize;
|
var input = '', fd, buffer, rsize, res, isOpened, fsBind, constBind;
|
||||||
options = options || {};
|
|
||||||
|
|
||||||
if (display !== '') { // null and undefined were excluded.
|
if (options.display !== '' && typeof print === 'function')
|
||||||
if (typeof print === 'function') { print(display, encoding); }
|
{ print(options.display, encoding); }
|
||||||
stdout.write(display + '', encoding);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (useShell || options.noEchoBack) { // Try reading via shell
|
|
||||||
|
|
||||||
|
if (useShell || options.noEchoBack) {
|
||||||
res = _readlineShell(options);
|
res = _readlineShell(options);
|
||||||
if (res.error) {
|
if (res.error) { throw res.error; }
|
||||||
if (display !== '') { stdout.write('\n', encoding); } // Return from prompt line.
|
|
||||||
throw res.error;
|
|
||||||
}
|
|
||||||
input = res.input;
|
input = res.input;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
stdin.resume();
|
if (IS_WIN) { // r/w mode not supported
|
||||||
while (true) {
|
if (process.stdin.isTTY && process.stdout.isTTY) {
|
||||||
rsize = 0;
|
|
||||||
|
console.warn('STD TRY');
|
||||||
|
if (options.display !== '') {
|
||||||
|
// process.stdout.write(options.display, encoding);
|
||||||
|
fs.writeSync(process.stdout.fd, options.display);
|
||||||
|
options.display = '';
|
||||||
|
}
|
||||||
|
fd = process.stdin.fd;
|
||||||
|
isOpened = true;
|
||||||
|
console.warn('STD OK');
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
try {
|
||||||
|
console.warn('CON TRY');
|
||||||
|
if (options.display !== '') {
|
||||||
|
fd = fs.openSync('\\\\.\\CON', 'w');
|
||||||
|
fs.writeSync(fd, options.display);
|
||||||
|
fs.closeSync(fd);
|
||||||
|
options.display = '';
|
||||||
|
}
|
||||||
|
fd = fs.openSync('\\\\.\\CON', 'rs');
|
||||||
|
isOpened = true;
|
||||||
|
console.warn('CON OK');
|
||||||
|
} catch (e) {}
|
||||||
|
|
||||||
|
if (!isOpened || options.display !== '') { // Retry
|
||||||
|
|
||||||
|
try {
|
||||||
|
console.warn('CONINOUT TRY');
|
||||||
|
// For raw device path
|
||||||
|
// On XP, 2000, 7 (x86), it might fail.
|
||||||
|
// 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;
|
||||||
|
console.warn('CONINOUT OK');
|
||||||
|
} catch (e) {}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
rsize = fs.readSync(stdin.fd, buffer, 0, bufSize);
|
console.warn('/dev/tty TRY');
|
||||||
} catch (e) {
|
fd = fs.openSync('/dev/tty', 'rs+');
|
||||||
if (e.code === 'EOF') { break; }
|
isOpened = true;
|
||||||
|
if (options.display !== '') {
|
||||||
// Try reading via shell
|
fs.writeSync(fd, options.display);
|
||||||
res = _readlineShell(options);
|
options.display = '';
|
||||||
if (res.error) {
|
|
||||||
if (display !== '') { stdout.write('\n', encoding); } // Return from prompt line.
|
|
||||||
throw res.error;
|
|
||||||
}
|
}
|
||||||
input += res.input;
|
console.warn('/dev/tty OK');
|
||||||
break;
|
} catch (e) {}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isOpened && options.display === '') {
|
||||||
|
|
||||||
|
buffer = new Buffer(bufSize);
|
||||||
|
while (true) {
|
||||||
|
rsize = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
rsize = fs.readSync(fd, buffer, 0, bufSize);
|
||||||
|
} catch (e) {
|
||||||
|
if (e.code === 'EOF') { break; }
|
||||||
|
|
||||||
|
res = _readlineShell(options);
|
||||||
|
if (res.error) { throw res.error; }
|
||||||
|
input += res.input;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rsize === 0) { break; }
|
||||||
|
input += buffer.toString(encoding, 0, rsize);
|
||||||
|
if (/[\r\n]$/.test(input)) { break; }
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rsize === 0) { break; }
|
} else {
|
||||||
input += buffer.toString(encoding, 0, rsize);
|
res = _readlineShell(options);
|
||||||
if (/[\r\n]$/.test(input)) { break; }
|
if (res.error) { throw res.error; }
|
||||||
|
input = res.input;
|
||||||
}
|
}
|
||||||
stdin.pause();
|
|
||||||
|
|
||||||
|
if (isOpened && !process.stdin.isTTY) { fs.closeSync(fd); }
|
||||||
}
|
}
|
||||||
|
|
||||||
return options.noTrim ? input.replace(/[\r\n]+$/, '') : input.trim();
|
return options.noTrim ? input.replace(/[\r\n]+$/, '') : input.trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
function _readlineShell(options) {
|
function _readlineShell(options) {
|
||||||
var args = [], res = {},
|
var cmdArgs = [], execArgs, res = {},
|
||||||
execOptions = {
|
execOptions = {
|
||||||
env: process.env,
|
env: process.env,
|
||||||
stdio: [stdin], // ScriptPW needs piped stdin
|
// ScriptPW (Win XP and Server2003) for `noEchoBack` needs TTY stream.
|
||||||
|
// In this case, If STDIN isn't TTY, an error is thrown.
|
||||||
|
stdio: [process.stdin],
|
||||||
encoding: encoding
|
encoding: encoding
|
||||||
};
|
};
|
||||||
|
|
||||||
if (options.noEchoBack) {
|
if (options.noEchoBack) {
|
||||||
args.push('noechoback');
|
cmdArgs.push('noechoback');
|
||||||
}
|
}
|
||||||
|
|
||||||
stdin.pause(); // re-start in child process
|
|
||||||
if (childProc.execFileSync) {
|
if (childProc.execFileSync) {
|
||||||
|
console.warn('execFileSync');
|
||||||
|
if (IS_WIN) {
|
||||||
|
process.env.Q = '"'; // The quote (") that isn't escaped.
|
||||||
|
execArgs = ['/V:ON', '/S', '/C',
|
||||||
|
'%Q%' + SHELL_CMD + '%Q%' +
|
||||||
|
cmdArgs.map(function(arg) { return ' %Q%' + arg + '%Q%'; }).join('') +
|
||||||
|
' & exit !ERRORLEVEL!'];
|
||||||
|
} else {
|
||||||
|
execArgs = [SHELL_CMD].concat(cmdArgs);
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
res.input = childProc.execFileSync(SHELL_PATH,
|
res.input = childProc.execFileSync(SHELL_PATH, execArgs, execOptions);
|
||||||
(IS_WIN ? ['/C', SHELL_CMD] : [SHELL_CMD]).concat(args), execOptions);
|
|
||||||
} catch (e) { // non-zero exit code
|
} catch (e) { // non-zero exit code
|
||||||
res.error = new Error(DEFAULT_ERR_MSG);
|
res.error = new Error(DEFAULT_ERR_MSG);
|
||||||
res.error.method = 'execFileSync';
|
res.error.method = 'execFileSync';
|
||||||
res.error.command = SHELL_CMD;
|
res.error.command = SHELL_CMD;
|
||||||
res.error.args = args;
|
res.error.args = cmdArgs;
|
||||||
res.error.shellMessage = e.stderr.trim();
|
res.error.shellMessage = e.stderr.trim();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
res = _execSyncByFile(args, execOptions);
|
console.warn('_execSyncByFile');
|
||||||
|
res = _execSyncByFile(cmdArgs, execOptions);
|
||||||
}
|
}
|
||||||
if (!res.error) { res.input = res.input.replace(/^'|'$/g, ''); }
|
if (!res.error) { res.input = res.input.replace(/^'|'$/g, ''); }
|
||||||
|
|
||||||
|
@ -109,7 +182,7 @@ function _readlineShell(options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// piping via files (node v0.10-)
|
// piping via files (node v0.10-)
|
||||||
function _execSyncByFile(args, execOptions) {
|
function _execSyncByFile(cmdArgs, execOptions) {
|
||||||
|
|
||||||
function getTempfile(name) {
|
function getTempfile(name) {
|
||||||
var path = require('path'), filepath, suffix = '', fd;
|
var path = require('path'), filepath, suffix = '', fd;
|
||||||
|
@ -133,7 +206,7 @@ function _execSyncByFile(args, execOptions) {
|
||||||
return filepath;
|
return filepath;
|
||||||
}
|
}
|
||||||
|
|
||||||
var cmdArgs, res = {},
|
var execArgs, res = {},
|
||||||
pathStdout = getTempfile('readline-sync.stdout'),
|
pathStdout = getTempfile('readline-sync.stdout'),
|
||||||
pathStderr = getTempfile('readline-sync.stderr'),
|
pathStderr = getTempfile('readline-sync.stderr'),
|
||||||
pathStatus = getTempfile('readline-sync.status'),
|
pathStatus = getTempfile('readline-sync.status'),
|
||||||
|
@ -146,27 +219,28 @@ function _execSyncByFile(args, execOptions) {
|
||||||
decipher = crypto.createDecipher(ALGORITHM_CIPHER, password);
|
decipher = crypto.createDecipher(ALGORITHM_CIPHER, password);
|
||||||
|
|
||||||
if (IS_WIN) {
|
if (IS_WIN) {
|
||||||
// The quote (") is escaped by node before parsed by shell. Then use ENV{Q}.
|
process.env.Q = '"'; // The quote (") that isn't escaped.
|
||||||
process.env.Q = '"';
|
|
||||||
// `()` for ignore space by echo
|
// `()` for ignore space by echo
|
||||||
cmdArgs = ['/V:ON', '/S', '/C',
|
execArgs = ['/V:ON', '/S', '/C',
|
||||||
'(' + SHELL_PATH + ' /V:ON /S /C %Q%%Q%' + SHELL_CMD + '%Q% ' + args.join(' ') +
|
'(' + SHELL_PATH + ' /V:ON /S /C %Q%%Q%' + SHELL_CMD + '%Q%' +
|
||||||
|
cmdArgs.map(function(arg) { return ' %Q%' + arg + '%Q%'; }).join('') +
|
||||||
' & (echo !ERRORLEVEL!)>%Q%' + pathStatus + '%Q%%Q%) 2>%Q%' + pathStderr + '%Q%' +
|
' & (echo !ERRORLEVEL!)>%Q%' + pathStatus + '%Q%%Q%) 2>%Q%' + pathStderr + '%Q%' +
|
||||||
' |%Q%' + process.execPath + '%Q% %Q%' + __dirname + '\\encrypt.js%Q%' +
|
' |%Q%' + process.execPath + '%Q% %Q%' + __dirname + '\\encrypt.js%Q%' +
|
||||||
' %Q%' + ALGORITHM_CIPHER + '%Q% %Q%' + password + '%Q%' +
|
' %Q%' + ALGORITHM_CIPHER + '%Q% %Q%' + password + '%Q%' +
|
||||||
' >%Q%' + pathStdout + '%Q%' +
|
' >%Q%' + pathStdout + '%Q%' +
|
||||||
' & (echo 1)>%Q%' + pathDone + '%Q%'];
|
' & (echo 1)>%Q%' + pathDone + '%Q%'];
|
||||||
} else {
|
} else {
|
||||||
cmdArgs = ['-c',
|
execArgs = ['-c',
|
||||||
// Use `()`, not `{}` for `-c` (text param)
|
// Use `()`, not `{}` for `-c` (text param)
|
||||||
'(' + SHELL_PATH + ' "' + SHELL_CMD + '" ' + args.join(' ') +
|
'(' + SHELL_PATH + ' "' + SHELL_CMD + '"' +
|
||||||
|
cmdArgs.map(function(arg) { return ' "' + arg + '"'; }).join('') +
|
||||||
'; echo $?>"' + pathStatus + '") 2>"' + pathStderr + '"' +
|
'; echo $?>"' + pathStatus + '") 2>"' + pathStderr + '"' +
|
||||||
' |"' + process.execPath + '" "' + __dirname + '/encrypt.js"' +
|
' |"' + process.execPath + '" "' + __dirname + '/encrypt.js"' +
|
||||||
' "' + ALGORITHM_CIPHER + '" "' + password + '"' +
|
' "' + ALGORITHM_CIPHER + '" "' + password + '"' +
|
||||||
' >"' + pathStdout + '"' +
|
' >"' + pathStdout + '"' +
|
||||||
'; echo 1 >"' + pathDone + '"'];
|
'; echo 1 >"' + pathDone + '"'];
|
||||||
}
|
}
|
||||||
childProc.spawn(SHELL_PATH, cmdArgs, execOptions);
|
childProc.spawn(SHELL_PATH, execArgs, execOptions);
|
||||||
|
|
||||||
while (fs.readFileSync(pathDone, {encoding: encoding}).trim() !== '1') {}
|
while (fs.readFileSync(pathDone, {encoding: encoding}).trim() !== '1') {}
|
||||||
if (fs.readFileSync(pathStatus, {encoding: encoding}).trim() === '0') {
|
if (fs.readFileSync(pathStatus, {encoding: encoding}).trim() === '0') {
|
||||||
|
@ -177,7 +251,7 @@ function _execSyncByFile(args, execOptions) {
|
||||||
res.error = new Error(DEFAULT_ERR_MSG);
|
res.error = new Error(DEFAULT_ERR_MSG);
|
||||||
res.error.method = '_execSyncByFile';
|
res.error.method = '_execSyncByFile';
|
||||||
res.error.command = SHELL_CMD;
|
res.error.command = SHELL_CMD;
|
||||||
res.error.args = args;
|
res.error.args = cmdArgs;
|
||||||
res.error.shellMessage = fs.readFileSync(pathStderr, {encoding: encoding}).trim();
|
res.error.shellMessage = fs.readFileSync(pathStderr, {encoding: encoding}).trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -218,21 +292,27 @@ exports.setBufferSize = function(newBufSize) {
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.prompt = function(options) {
|
exports.prompt = function(options) {
|
||||||
return _readlineSync(promptText, options);
|
options = options || {};
|
||||||
|
options.display = promptText + '';
|
||||||
|
options.keyIn = false;
|
||||||
|
return _readlineSync(options);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.question = function(query, options) {
|
exports.question = function(query, options) {
|
||||||
return _readlineSync(
|
options = options || {};
|
||||||
/* jshint eqnull:true */
|
/* jshint eqnull:true */
|
||||||
query != null ? query : '',
|
options.display = query != null ? query + '' : '';
|
||||||
/* jshint eqnull:false */
|
/* jshint eqnull:false */
|
||||||
options);
|
options.keyIn = false;
|
||||||
|
return _readlineSync(options);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.keyIn = function(query) {
|
exports.keyIn = function(query, options) {
|
||||||
return _readlineSync(
|
options = options || {};
|
||||||
/* jshint eqnull:true */
|
/* jshint eqnull:true */
|
||||||
query != null ? query : '',
|
options.display = query != null ? query + '' : '';
|
||||||
/* jshint eqnull:false */
|
/* jshint eqnull:false */
|
||||||
{keyIn: true});
|
options.keyIn = true;
|
||||||
|
options.noEchoBack = false;
|
||||||
|
return _readlineSync(options);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue