mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-01-24 14:23:36 -05:00
moved getpass() method into a cross-plateform file rskbdinput.h/cc
This commit is contained in:
parent
bd0e4d7fba
commit
c365a96489
@ -480,6 +480,7 @@ HEADERS += turtle/p3turtle.h \
|
||||
|
||||
HEADERS += util/folderiterator.h \
|
||||
util/rsdebug.h \
|
||||
util/rskbdinput.h \
|
||||
util/rsmemory.h \
|
||||
util/smallobject.h \
|
||||
util/rsdir.h \
|
||||
@ -626,6 +627,7 @@ SOURCES += turtle/p3turtle.cc \
|
||||
|
||||
SOURCES += util/folderiterator.cc \
|
||||
util/rsdebug.cc \
|
||||
util/rskbdinput.cc \
|
||||
util/rsexpr.cc \
|
||||
util/smallobject.cc \
|
||||
util/rsdir.cc \
|
||||
|
123
libretroshare/src/util/rskbdinput.cc
Normal file
123
libretroshare/src/util/rskbdinput.cc
Normal file
@ -0,0 +1,123 @@
|
||||
/*******************************************************************************
|
||||
* libretroshare/src/retroshare/util/rskbdinput.cc *
|
||||
* *
|
||||
* Copyright (C) 2019 Cyril Soler <csoler@users.sourceforge.net> *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU Lesser General Public License as *
|
||||
* published by the Free Software Foundation, either version 3 of the *
|
||||
* License, or (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU Lesser General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU Lesser General Public License *
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
||||
* *
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef __ANDROID__
|
||||
|
||||
#ifdef WINDOWS_SYS
|
||||
#include <conio.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define PASS_MAX 512
|
||||
|
||||
std::string rs_getpass(const std::string& prompt,bool no_echo)
|
||||
{
|
||||
static char getpassbuf [PASS_MAX + 1];
|
||||
size_t i = 0;
|
||||
int c;
|
||||
|
||||
if (!prompt.empty()) {
|
||||
std::cerr << prompt ;
|
||||
std::cerr.flush();
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
c = _getch ();
|
||||
if (c == '\r') {
|
||||
getpassbuf [i] = '\0';
|
||||
break;
|
||||
}
|
||||
else if (i < PASS_MAX) {
|
||||
getpassbuf[i++] = c;
|
||||
}
|
||||
|
||||
if (i >= PASS_MAX) {
|
||||
getpassbuf [i] = '\0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!prompt.empty()) {
|
||||
std::cerr << "\r\n" ;
|
||||
std::cerr.flush();
|
||||
}
|
||||
|
||||
return std::string(getpassbuf);
|
||||
}
|
||||
#else
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static int getch()
|
||||
{
|
||||
int ch;
|
||||
struct termios t_old, t_new;
|
||||
|
||||
tcgetattr(STDIN_FILENO, &t_old);
|
||||
t_new = t_old;
|
||||
t_new.c_lflag &= ~(ICANON | ECHO);
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &t_new);
|
||||
|
||||
ch = getchar();
|
||||
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &t_old);
|
||||
return ch;
|
||||
}
|
||||
|
||||
std::string rs_getpass(const char *prompt, bool no_echo)
|
||||
{
|
||||
const char BACKSPACE=127;
|
||||
const char RETURN=10;
|
||||
|
||||
std::string password;
|
||||
unsigned char ch=0;
|
||||
|
||||
std::cout <<prompt; std::cout.flush();
|
||||
|
||||
while((ch=getch())!=RETURN)
|
||||
{
|
||||
if(ch==BACKSPACE)
|
||||
{
|
||||
if(password.length()!=0)
|
||||
{
|
||||
if(no_echo)
|
||||
std::cout <<"\b \b";
|
||||
password.resize(password.length()-1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
password+=ch;
|
||||
if(no_echo)
|
||||
std::cout <<'*';
|
||||
else
|
||||
std::cout << ch,std::cout.flush();
|
||||
}
|
||||
}
|
||||
std::cout <<std::endl;
|
||||
|
||||
return std::string(password);
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
25
libretroshare/src/util/rskbdinput.h
Normal file
25
libretroshare/src/util/rskbdinput.h
Normal file
@ -0,0 +1,25 @@
|
||||
/*******************************************************************************
|
||||
* libretroshare/src/retroshare/util/rskbdinput.h *
|
||||
* *
|
||||
* Copyright (C) 2019 Cyril Soler <csoler@users.sourceforge.net> *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU Lesser General Public License as *
|
||||
* published by the Free Software Foundation, either version 3 of the *
|
||||
* License, or (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU Lesser General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU Lesser General Public License *
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
||||
* *
|
||||
*******************************************************************************/
|
||||
|
||||
namespace RsUtil {
|
||||
|
||||
std::string rs_getpass(const std::string& prompt,bool no_echo=true) ;
|
||||
|
||||
}
|
@ -18,6 +18,7 @@
|
||||
|
||||
#include "util/stacktrace.h"
|
||||
#include "util/argstream.h"
|
||||
#include "util/rskbdinput.h"
|
||||
#include "retroshare/rsinit.h"
|
||||
#include "jsonapi/jsonapi.h"
|
||||
|
||||
@ -26,10 +27,6 @@ CrashStackTrace gCrashStackTrace;
|
||||
#include <cmath>
|
||||
#include <csignal>
|
||||
|
||||
#ifndef __ANDROID__
|
||||
#include <termios.h>
|
||||
#endif
|
||||
|
||||
#ifdef __ANDROID__
|
||||
# include <QAndroidService>
|
||||
# include <QCoreApplication>
|
||||
@ -42,59 +39,6 @@ CrashStackTrace gCrashStackTrace;
|
||||
#include "retroshare/rsinit.h"
|
||||
#include "retroshare/rsiface.h"
|
||||
|
||||
#ifndef RS_JSONAPI
|
||||
# error Inconsistent build configuration retroshare_service needs rs_jsonapi
|
||||
#endif
|
||||
|
||||
int getch() {
|
||||
int ch;
|
||||
struct termios t_old, t_new;
|
||||
|
||||
tcgetattr(STDIN_FILENO, &t_old);
|
||||
t_new = t_old;
|
||||
t_new.c_lflag &= ~(ICANON | ECHO);
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &t_new);
|
||||
|
||||
ch = getchar();
|
||||
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &t_old);
|
||||
return ch;
|
||||
}
|
||||
|
||||
std::string readStringFromKeyboard(const char *prompt, bool show_asterisk=true)
|
||||
{
|
||||
const char BACKSPACE=127;
|
||||
const char RETURN=10;
|
||||
|
||||
std::string password;
|
||||
unsigned char ch=0;
|
||||
|
||||
std::cout <<prompt; std::cout.flush();
|
||||
|
||||
while((ch=getch())!=RETURN)
|
||||
{
|
||||
if(ch==BACKSPACE)
|
||||
{
|
||||
if(password.length()!=0)
|
||||
{
|
||||
if(show_asterisk)
|
||||
std::cout <<"\b \b";
|
||||
password.resize(password.length()-1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
password+=ch;
|
||||
if(show_asterisk)
|
||||
std::cout <<'*';
|
||||
else
|
||||
std::cout << ch,std::cout.flush();
|
||||
}
|
||||
}
|
||||
std::cout <<std::endl;
|
||||
return password;
|
||||
}
|
||||
|
||||
class RsServiceNotify: public NotifyClient
|
||||
{
|
||||
public:
|
||||
@ -104,7 +48,7 @@ public:
|
||||
virtual bool askForPassword(const std::string& title, const std::string& question, bool prev_is_bad, std::string& password,bool& cancel)
|
||||
{
|
||||
std::string question1=title + "\nPlease enter your PGP password for key:\n " + question + " :";
|
||||
password = readStringFromKeyboard(question1.c_str()) ;
|
||||
password = RsUtil::rs_getpass(question1.c_str()) ;
|
||||
cancel = false ;
|
||||
|
||||
return !password.empty();
|
||||
@ -184,8 +128,8 @@ int main(int argc, char* argv[])
|
||||
|
||||
for(;;)
|
||||
{
|
||||
webui_pass1 = readStringFromKeyboard("Please register a password for the web interface: ");
|
||||
webui_pass2 = readStringFromKeyboard("Please enter the same password again : ");
|
||||
webui_pass1 = RsUtil::rs_getpass("Please register a password for the web interface: ");
|
||||
webui_pass2 = RsUtil::rs_getpass("Please enter the same password again : ");
|
||||
|
||||
if(webui_pass1 != webui_pass2)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user