Skip to content
Permalink
Browse files

console, feat: support console.getpass.

  • Loading branch information...
xicilion committed Jul 2, 2019
1 parent 34b7e61 commit 0fee0ead6cf43c5d0be533f2a4674b732cb9b673
Showing with 90 additions and 9 deletions.
  1. +26 −1 fibjs/include/ifs/console.h
  2. +57 −7 fibjs/src/console/console.cpp
  3. +6 −0 idl/zh-cn/console.idl
  4. +1 −1 vender
@@ -83,6 +83,7 @@ class console_base : public object_base {
static result_t mouseDown(exlib::string button);
static result_t clickMouse(exlib::string button, bool dbclick);
static result_t readLine(exlib::string msg, exlib::string& retVal, AsyncEvent* ac);
static result_t getpass(exlib::string msg, exlib::string& retVal, AsyncEvent* ac);

public:
static void s__new(const v8::FunctionCallbackInfo<v8::Value>& args)
@@ -130,9 +131,11 @@ class console_base : public object_base {
static void s_mouseDown(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_clickMouse(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_readLine(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_getpass(const v8::FunctionCallbackInfo<v8::Value>& args);

public:
ASYNC_STATICVALUE2(console_base, readLine, exlib::string, exlib::string);
ASYNC_STATICVALUE2(console_base, getpass, exlib::string, exlib::string);
};
}

@@ -170,7 +173,9 @@ inline ClassInfo& console_base::class_info()
{ "mouseDown", s_mouseDown, true },
{ "clickMouse", s_clickMouse, true },
{ "readLine", s_readLine, true },
{ "readLineSync", s_readLine, true }
{ "readLineSync", s_readLine, true },
{ "getpass", s_getpass, true },
{ "getpassSync", s_getpass, true }
};

static ClassData::ClassProperty s_property[] = {
@@ -769,6 +774,26 @@ inline void console_base::s_readLine(const v8::FunctionCallbackInfo<v8::Value>&

METHOD_RETURN();
}

inline void console_base::s_getpass(const v8::FunctionCallbackInfo<v8::Value>& args)
{
exlib::string vr;

METHOD_NAME("console.getpass");
METHOD_ENTER();

ASYNC_METHOD_OVER(1, 0);

OPT_ARG(exlib::string, 0, "");

if (!cb.IsEmpty()) {
acb_getpass(v0, cb);
hr = CALL_RETURN_NULL;
} else
hr = ac_getpass(v0, vr);

METHOD_RETURN();
}
}

#endif
@@ -19,6 +19,7 @@

#ifdef _WIN32
#include <windows.h>
#include <conio.h>

static LARGE_INTEGER systemFrequency;

@@ -41,6 +42,8 @@ inline int64_t Ticks()

#include "utils.h" // for ARRAYSIZE()

#define _getch getchar

inline int64_t Ticks()
{
struct timeval tv;
@@ -488,11 +491,44 @@ result_t console_base::clear()

#endif

char* read_line()
#define INPUT_BUFFER_SIZE 1024

char* getpass()
{
char* p;
int c;
char* password = (char*)malloc(INPUT_BUFFER_SIZE);

for (p = password; (c = _getch()) != 13 && c != EOF;) {
if (p < &password[INPUT_BUFFER_SIZE - 1]) {
if (c == 0 || c == 0xE0) {
/* FN Keys (0 or E0) are a sentinal for a FN code */
c = (c << 4) | _getch();
/* Catch {DELETE}, {<--}, Num{DEL} and Num{<--} */
if ((c == 0xE53 || c == 0xE4B || c == 0x053 || c == 0x04b) && p > password)
p--;
} else if ((c == '\b' || c == 127)) /* BS/DEL */ {
if (p > password)
p--;
} else
*p++ = c;
} else
break;
}
*p = '\0';
puts("");

return password;
}

char* read_line(bool no_echo)
{
char* text = (char*)malloc(1024);
if (no_echo)
return getpass();

char* text = (char*)malloc(INPUT_BUFFER_SIZE);

if (fgets(text, 1024, stdin) != NULL) {
if (fgets(text, INPUT_BUFFER_SIZE, stdin) != NULL) {
int32_t textLen = (int32_t)qstrlen(text);
if (textLen > 0 && text[textLen - 1] == '\n')
text[textLen - 1] = '\0'; // getting rid of newline character
@@ -503,8 +539,7 @@ char* read_line()
return NULL;
}

result_t console_base::readLine(exlib::string msg, exlib::string& retVal,
AsyncEvent* ac)
result_t readInput(exlib::string msg, exlib::string& retVal, AsyncEvent* ac, bool no_echo)
{
if (ac->isSync())
return CHECK_ERROR(CALL_E_LONGSYNC);
@@ -516,6 +551,7 @@ result_t console_base::readLine(exlib::string msg, exlib::string& retVal,
char* line;
const char* lfptr = qstrrchr(msg.c_str(), '\n');

el_no_echo = no_echo;
if (lfptr != NULL) {
puts(msg.substr(0, lfptr - msg.c_str()).c_str());
line = readline(lfptr + 1);
@@ -525,6 +561,9 @@ result_t console_base::readLine(exlib::string msg, exlib::string& retVal,
if (!line)
return CHECK_ERROR(LastError());

if (no_echo)
puts("");

if (*line) {
add_history(line);
retVal = line;
@@ -534,8 +573,7 @@ result_t console_base::readLine(exlib::string msg, exlib::string& retVal,
#endif
{
std_logger::out(msg);
char* line = read_line();

char* line = read_line(no_echo);
if (!line)
return CHECK_ERROR(LastError());

@@ -545,4 +583,16 @@ result_t console_base::readLine(exlib::string msg, exlib::string& retVal,

return 0;
}

result_t console_base::readLine(exlib::string msg, exlib::string& retVal,
AsyncEvent* ac)
{
return readInput(msg, retVal, ac, false);
}

result_t console_base::getpass(exlib::string msg, exlib::string& retVal,
AsyncEvent* ac)
{
return readInput(msg, retVal, ac, true);
}
};
@@ -427,4 +427,10 @@ module console
@return 返回用户输入的信息
*/
static String readLine(String msg = "") async;

/*! @brief 从控制台读取用户输入的密码
@param msg 提示信息
@return 返回用户输入的密码
*/
static String getpass(String msg = "") async;
};
2 vender

0 comments on commit 0fee0ea

Please sign in to comment.
You can’t perform that action at this time.