db_bridge/console.cpp
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 17:58:11 +0100
changeset 19 933d86c1ff71
child 20 5fec678f931b
permissions -rw-r--r--
namespace console added for console tools, password entry and other stuff committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
#include <console.h>
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
int Console::getch ()
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
{
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
    static int ch = -1, fd = 0;
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
    struct termios neu, alt;
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
    fd = fileno (stdin);
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
    tcgetattr (fd, &alt);
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
    neu = alt;
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
    neu.c_lflag &= ~(ICANON | ECHO);
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
    tcsetattr (fd, TCSANOW, &neu);
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
    ch = getchar ();
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
    tcsetattr (fd, TCSANOW, &alt);
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
    return ch;
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
}
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
void Console::getpass (std::string & password)
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
{
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
    int i = 0;
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
    int c;
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
    char buffer[17];
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
    while ((c = getch ()) != '\n' && i < 17) {
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
        buffer[i++] = c;
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
        std::cout << "*";
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
    }
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
    buffer[i] = 0;
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
    password = std::string (buffer);
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    std::cout << std::endl;
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
}