2015-04-01 09:34:31 +02:00
|
|
|
/* jshint wsh:true */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* readlineSync
|
|
|
|
* https://github.com/anseki/readline-sync
|
|
|
|
*
|
2016-01-27 08:42:29 +01:00
|
|
|
* Copyright (c) 2016 anseki
|
2015-04-01 09:34:31 +02:00
|
|
|
* Licensed under the MIT license.
|
|
|
|
*/
|
2015-03-13 10:34:41 +01:00
|
|
|
|
|
|
|
var
|
|
|
|
FSO_ForReading = 1, FSO_ForWriting = 2,
|
2015-03-27 13:12:00 +01:00
|
|
|
PS_MSG = 'Microsoft Windows PowerShell is required.' +
|
|
|
|
' https://technet.microsoft.com/en-us/library/hh847837.aspx',
|
2015-03-27 12:25:59 +01:00
|
|
|
|
2015-04-13 08:55:33 +02:00
|
|
|
input = '', fso, tty,
|
2015-03-27 12:25:59 +01:00
|
|
|
options = (function(conf) {
|
|
|
|
var options = {}, arg, args =// Array.prototype.slice.call(WScript.Arguments),
|
|
|
|
(function() {
|
|
|
|
var args = [], i, iLen;
|
|
|
|
for (i = 0, iLen = WScript.Arguments.length; i < iLen; i++)
|
|
|
|
{ args.push(WScript.Arguments(i)); }
|
|
|
|
return args;
|
|
|
|
})(),
|
|
|
|
confLc = {}, key;
|
|
|
|
|
2015-04-08 10:33:53 +02:00
|
|
|
function decodeArg(arg) {
|
2015-03-27 12:25:59 +01:00
|
|
|
return arg.replace(/#(\d+);/g, function(str, charCode) {
|
|
|
|
return String.fromCharCode(+charCode);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (key in conf) {
|
|
|
|
if (conf.hasOwnProperty(key))
|
|
|
|
{ confLc[key.toLowerCase()] = {key: key, type: conf[key]}; }
|
|
|
|
}
|
|
|
|
|
|
|
|
while (typeof(arg = args.shift()) === 'string') {
|
|
|
|
if (!(arg = (arg.match(/^\-+(.+)$/) || [])[1])) { continue; }
|
|
|
|
arg = arg.toLowerCase();
|
|
|
|
if (confLc[arg]) {
|
|
|
|
options[confLc[arg].key] =
|
|
|
|
confLc[arg].type === 'boolean' ? true :
|
|
|
|
confLc[arg].type === 'string' ? args.shift() : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (key in conf) {
|
|
|
|
if (conf.hasOwnProperty(key) && conf[key] === 'string') {
|
|
|
|
if (typeof options[key] !== 'string') { options[key] = ''; }
|
2015-04-08 10:33:53 +02:00
|
|
|
else { options[key] = decodeArg(options[key]); }
|
2015-03-27 12:25:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
})({
|
2015-03-31 13:27:02 +02:00
|
|
|
display: 'string',
|
2015-04-13 08:55:33 +02:00
|
|
|
displayOnly: 'boolean',
|
2015-03-31 13:27:02 +02:00
|
|
|
keyIn: 'boolean',
|
2015-04-07 11:38:14 +02:00
|
|
|
hideEchoBack: 'boolean',
|
2015-04-08 10:33:53 +02:00
|
|
|
mask: 'string'
|
2015-03-27 12:25:59 +01:00
|
|
|
});
|
2015-03-13 10:34:41 +01:00
|
|
|
|
2015-04-07 11:38:14 +02:00
|
|
|
if (!options.hideEchoBack && !options.keyIn) {
|
2015-04-03 18:14:55 +02:00
|
|
|
if (options.display) { writeTTY(options.display); }
|
2015-04-13 08:55:33 +02:00
|
|
|
if (!options.displayOnly) { input = readByFSO(); }
|
2015-04-07 11:38:14 +02:00
|
|
|
} else if (options.hideEchoBack && !options.keyIn && !options.mask) {
|
2015-04-03 18:14:55 +02:00
|
|
|
if (options.display) { writeTTY(options.display); }
|
2015-04-13 08:55:33 +02:00
|
|
|
if (!options.displayOnly) { input = readByPW(); }
|
2015-03-27 12:25:59 +01:00
|
|
|
} else {
|
|
|
|
WScript.StdErr.WriteLine(PS_MSG);
|
|
|
|
WScript.Quit(1);
|
2015-03-13 10:34:41 +01:00
|
|
|
}
|
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
WScript.StdOut.Write('\'' + input + '\'');
|
2015-03-13 10:34:41 +01:00
|
|
|
|
|
|
|
WScript.Quit();
|
|
|
|
|
2015-03-24 12:37:20 +01:00
|
|
|
function writeTTY(text) {
|
2015-03-13 10:34:41 +01:00
|
|
|
try {
|
2015-03-24 12:37:20 +01:00
|
|
|
tty = tty || getFso().OpenTextFile('CONOUT$', FSO_ForWriting, true);
|
|
|
|
tty.Write(text);
|
2015-03-13 10:34:41 +01:00
|
|
|
} catch (e) {
|
2015-03-24 12:37:20 +01:00
|
|
|
WScript.StdErr.WriteLine('TTY Write Error: ' + e.number +
|
2015-03-27 12:25:59 +01:00
|
|
|
'\n' + e.description + '\n' + PS_MSG);
|
2015-04-01 12:36:00 +02:00
|
|
|
WScript.Quit(e.number || 1);
|
2015-03-25 13:22:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-29 16:45:55 +02:00
|
|
|
function readByFSO() {
|
2015-03-24 12:37:20 +01:00
|
|
|
var text;
|
2015-03-13 10:34:41 +01:00
|
|
|
try {
|
2015-03-24 12:37:20 +01:00
|
|
|
text = getFso().OpenTextFile('CONIN$', FSO_ForReading).ReadLine();
|
2015-03-13 10:34:41 +01:00
|
|
|
} catch (e) {
|
2015-03-24 12:37:20 +01:00
|
|
|
WScript.StdErr.WriteLine('TTY Read Error: ' + e.number +
|
2015-03-27 12:25:59 +01:00
|
|
|
'\n' + e.description + '\n' + PS_MSG);
|
2015-04-01 12:36:00 +02:00
|
|
|
WScript.Quit(e.number || 1);
|
2015-03-13 10:34:41 +01:00
|
|
|
}
|
2015-03-24 12:37:20 +01:00
|
|
|
return text;
|
2015-03-13 10:34:41 +01:00
|
|
|
}
|
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
// TTY must be STDIN that is not redirected and not piped.
|
2015-03-25 13:22:44 +01:00
|
|
|
function readByPW() {
|
2015-03-27 12:25:59 +01:00
|
|
|
var text;
|
2015-03-13 10:34:41 +01:00
|
|
|
try {
|
2015-03-27 12:25:59 +01:00
|
|
|
text = WScript.CreateObject('ScriptPW.Password').GetPassword()
|
2015-03-13 10:34:41 +01:00
|
|
|
// Bug? Illegal data may be returned when user types before initializing.
|
|
|
|
.replace(/[\u4000-\u40FF]/g, function(chr) {
|
|
|
|
var charCode = chr.charCodeAt(0);
|
|
|
|
return charCode >= 0x4020 && charCode <= 0x407F ?
|
|
|
|
String.fromCharCode(charCode - 0x4000) : '';
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
WScript.StdErr.WriteLine('ScriptPW.Password Error: ' + e.number +
|
2015-03-27 12:25:59 +01:00
|
|
|
'\n' + e.description + '\n' + PS_MSG);
|
2015-04-01 12:36:00 +02:00
|
|
|
WScript.Quit(e.number || 1);
|
2015-03-13 10:34:41 +01:00
|
|
|
}
|
2015-03-24 12:37:20 +01:00
|
|
|
writeTTY('\n');
|
2015-03-27 12:25:59 +01:00
|
|
|
return text;
|
2015-03-13 10:34:41 +01:00
|
|
|
}
|
|
|
|
|
2015-03-24 12:37:20 +01:00
|
|
|
function getFso() {
|
|
|
|
if (!fso) { fso = new ActiveXObject('Scripting.FileSystemObject'); }
|
|
|
|
return fso;
|
|
|
|
}
|