From 16c0c476cdcf99863712608b3341692c3d02a7d6 Mon Sep 17 00:00:00 2001 From: anseki Date: Wed, 11 Mar 2015 17:06:27 +0900 Subject: [PATCH] Add `display` to shell script. --- lib/decodedos.js | 10 ++++ lib/read.bat | 110 ++++++++++++++++++++++++++----------------- lib/read.sh | 25 ++++++++-- lib/readline-sync.js | 80 +++++++++++++++++-------------- 4 files changed, 143 insertions(+), 82 deletions(-) create mode 100644 lib/decodedos.js diff --git a/lib/decodedos.js b/lib/decodedos.js new file mode 100644 index 0000000..e7aec8a --- /dev/null +++ b/lib/decodedos.js @@ -0,0 +1,10 @@ +process.stdout.write(decodeDOS(process.argv[2] /*text*/ || ''), + process.env.RLS_ENCODING || 'binary', function() { + process.exit(0); +}); + +function decodeDOS(arg) { + return arg.replace(/#(\d+);/g, function(str, charCode) { + return String.fromCharCode(+charCode); + }); +} diff --git a/lib/read.bat b/lib/read.bat index 0b883d0..dc8ad53 100644 --- a/lib/read.bat +++ b/lib/read.bat @@ -1,42 +1,68 @@ -@echo off -setlocal -setlocal ENABLEDELAYEDEXPANSION - -if "%~1"=="noechoback" ( - call :read_s - if ERRORLEVEL 1 exit /b 1 -) else ( - set /p INPUT=CON -) -set /p ="'%INPUT%'"NUL 2>&1 -:: Win <7 and CON +if "%display%" NEQ "" if "%NODE_EXEC_PATH%" NEQ "" ( + "%NODE_EXEC_PATH%" "%~dp0decodedos.js" "%display%" >CON + if ERRORLEVEL 1 exit /b 1 +) + +if "%noechoback%"=="1" ( + call :read_s + if ERRORLEVEL 1 exit /b 1 +) else ( + set /p input=CON +) +set /p ="'%input%'"NUL 2>&1 +:: Win <7 and /dev/null || \ stty -F /dev/tty -echo echonl 2>/dev/null || \ stty -f /dev/tty -echo echonl || exit 1 - IFS= read -r INPUT /dev/null || \ stty -F /dev/tty echo -echonl 2>/dev/null || \ stty -f /dev/tty echo -echonl || exit 1 } -if [ "$1" = "noechoback" ]; then +# getopt(s) +while [ $# -ge 1 ]; do + case "$1" in + "--noechoback") noechoback=1;; + "--keyin") keyin=1;; + "--display") shift; display=$1;; + esac + shift +done + +if [ -n "$display" ]; then + printf '%s' "$display" >/dev/tty +fi + +if [ "$noechoback" = "1" ]; then # Try `-s` option. *ksh have it that not `--silent`. Therefore, don't try it. if [ -n "$BASH_VERSION" ] || [ -n "$ZSH_VERSION" ]; then - IFS= read -rs INPUT /dev/null && printf '\n' >/dev/tty || read_s + IFS= read -rs input /dev/null && printf '\n' >/dev/tty || read_s else read_s fi else - IFS= read -r INPUT %Q%' + pathStatus + '%Q%%Q%) 2>%Q%' + pathStderr + '%Q%' + ' |%Q%' + process.execPath + '%Q% %Q%' + __dirname + '\\encrypt.js%Q%' + @@ -232,8 +233,9 @@ function _execSyncByFile(cmdArgs, execOptions) { } else { execArgs = ['-c', // Use `()`, not `{}` for `-c` (text param) - '(' + SHELL_PATH + ' "' + SHELL_CMD + '"' + - cmdArgs.map(function(arg) { return ' "' + arg + '"'; }).join('') + + '("' + SHELL_PATH + '" "' + SHELL_CMD + '"' + + cmdArgs.map(function(arg) + { return ' "' + arg.replace(/[\\"`]/g, '\\\$&') + '"'; }).join('') + '; echo $?>"' + pathStatus + '") 2>"' + pathStderr + '"' + ' |"' + process.execPath + '" "' + __dirname + '/encrypt.js"' + ' "' + ALGORITHM_CIPHER + '" "' + password + '"' + @@ -292,27 +294,35 @@ exports.setBufferSize = function(newBufSize) { }; exports.prompt = function(options) { - options = options || {}; - options.display = promptText + ''; - options.keyIn = false; - return _readlineSync(options); + var readOptions = { + display: promptText + '', + noEchoBack: options.noEchoBack, + keyIn: false, + noTrim: options.noTrim + }; + return _readlineSync(readOptions); }; exports.question = function(query, options) { - options = options || {}; + var readOptions = { /* jshint eqnull:true */ - options.display = query != null ? query + '' : ''; + display: query != null ? query + '' : '', /* jshint eqnull:false */ - options.keyIn = false; - return _readlineSync(options); + noEchoBack: options.noEchoBack, + keyIn: false, + noTrim: options.noTrim + }; + return _readlineSync(readOptions); }; exports.keyIn = function(query, options) { - options = options || {}; + var readOptions = { /* jshint eqnull:true */ - options.display = query != null ? query + '' : ''; + display: query != null ? query + '' : '', /* jshint eqnull:false */ - options.keyIn = true; - options.noEchoBack = false; - return _readlineSync(options); + noEchoBack: false, + keyIn: true, + noTrim: true + }; + return _readlineSync(readOptions); };