Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

read.cs.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* jshint wsh:true */
  2. /*
  3. * readlineSync
  4. * https://github.com/anseki/readline-sync
  5. *
  6. * Copyright (c) 2019 anseki
  7. * Licensed under the MIT license.
  8. */
  9. var
  10. FSO_ForReading = 1, FSO_ForWriting = 2,
  11. PS_MSG = 'Microsoft Windows PowerShell is required.' +
  12. ' https://technet.microsoft.com/en-us/library/hh847837.aspx',
  13. input = '', fso, tty,
  14. options = (function(conf) {
  15. var options = {}, arg, args =// Array.prototype.slice.call(WScript.Arguments),
  16. (function() {
  17. var args = [], i, iLen;
  18. for (i = 0, iLen = WScript.Arguments.length; i < iLen; i++)
  19. { args.push(WScript.Arguments(i)); }
  20. return args;
  21. })(),
  22. confLc = {}, key;
  23. function decodeArg(arg) {
  24. return arg.replace(/#(\d+);/g, function(str, charCode) {
  25. return String.fromCharCode(+charCode);
  26. });
  27. }
  28. for (key in conf) {
  29. if (conf.hasOwnProperty(key))
  30. { confLc[key.toLowerCase()] = {key: key, type: conf[key]}; }
  31. }
  32. while (typeof(arg = args.shift()) === 'string') {
  33. if (!(arg = (arg.match(/^\-+(.+)$/) || [])[1])) { continue; }
  34. arg = arg.toLowerCase();
  35. if (confLc[arg]) {
  36. options[confLc[arg].key] =
  37. confLc[arg].type === 'boolean' ? true :
  38. confLc[arg].type === 'string' ? args.shift() : null;
  39. }
  40. }
  41. for (key in conf) {
  42. if (conf.hasOwnProperty(key) && conf[key] === 'string') {
  43. if (typeof options[key] !== 'string') { options[key] = ''; }
  44. else { options[key] = decodeArg(options[key]); }
  45. }
  46. }
  47. return options;
  48. })({
  49. display: 'string',
  50. displayOnly: 'boolean',
  51. keyIn: 'boolean',
  52. hideEchoBack: 'boolean',
  53. mask: 'string'
  54. });
  55. if (!options.hideEchoBack && !options.keyIn) {
  56. if (options.display) { writeTTY(options.display); }
  57. if (!options.displayOnly) { input = readByFSO(); }
  58. } else if (options.hideEchoBack && !options.keyIn && !options.mask) {
  59. if (options.display) { writeTTY(options.display); }
  60. if (!options.displayOnly) { input = readByPW(); }
  61. } else {
  62. WScript.StdErr.WriteLine(PS_MSG);
  63. WScript.Quit(1);
  64. }
  65. WScript.StdOut.Write('\'' + input + '\'');
  66. WScript.Quit();
  67. function writeTTY(text) {
  68. try {
  69. tty = tty || getFso().OpenTextFile('CONOUT$', FSO_ForWriting, true);
  70. tty.Write(text);
  71. } catch (e) {
  72. WScript.StdErr.WriteLine('TTY Write Error: ' + e.number +
  73. '\n' + e.description + '\n' + PS_MSG);
  74. WScript.Quit(e.number || 1);
  75. }
  76. }
  77. function readByFSO() {
  78. var text;
  79. try {
  80. text = getFso().OpenTextFile('CONIN$', FSO_ForReading).ReadLine();
  81. } catch (e) {
  82. WScript.StdErr.WriteLine('TTY Read Error: ' + e.number +
  83. '\n' + e.description + '\n' + PS_MSG);
  84. WScript.Quit(e.number || 1);
  85. }
  86. return text;
  87. }
  88. // TTY must be STDIN that is not redirected and not piped.
  89. function readByPW() {
  90. var text;
  91. try {
  92. text = WScript.CreateObject('ScriptPW.Password').GetPassword()
  93. // Bug? Illegal data may be returned when user types before initializing.
  94. .replace(/[\u4000-\u40FF]/g, function(chr) {
  95. var charCode = chr.charCodeAt(0);
  96. return charCode >= 0x4020 && charCode <= 0x407F ?
  97. String.fromCharCode(charCode - 0x4000) : '';
  98. });
  99. } catch (e) {
  100. WScript.StdErr.WriteLine('ScriptPW.Password Error: ' + e.number +
  101. '\n' + e.description + '\n' + PS_MSG);
  102. WScript.Quit(e.number || 1);
  103. }
  104. writeTTY('\n');
  105. return text;
  106. }
  107. function getFso() {
  108. if (!fso) { fso = new ActiveXObject('Scripting.FileSystemObject'); }
  109. return fso;
  110. }