readline-sync/lib/read.wsh.js

124 lines
3.6 KiB
JavaScript
Raw Normal View History

2015-04-01 09:34:31 +02:00
/* jshint wsh:true */
/*
* readlineSync
* https://github.com/anseki/readline-sync
*
2019-07-27 14:58:50 +02:00
* Copyright (c) 2019 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-04-13 08:55:33 +02:00
input = '', fso, tty,
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;
function decodeArg(arg) {
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] = ''; }
else { options[key] = decodeArg(options[key]); }
}
}
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',
mask: 'string'
});
2015-03-13 10:34:41 +01:00
2015-04-07 11:38:14 +02:00
if (!options.hideEchoBack && !options.keyIn) {
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) {
if (options.display) { writeTTY(options.display); }
2015-04-13 08:55:33 +02:00
if (!options.displayOnly) { input = readByPW(); }
} else {
WScript.StdErr.WriteLine(PS_MSG);
WScript.Quit(1);
2015-03-13 10:34:41 +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 +
'\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 +
'\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
}
// TTY must be STDIN that is not redirected and not piped.
2015-03-25 13:22:44 +01:00
function readByPW() {
var text;
2015-03-13 10:34:41 +01:00
try {
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 +
'\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');
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;
}