db_bridge/console.cpp
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 17:58:13 +0100
changeset 20 5fec678f931b
parent 19 933d86c1ff71
child 21 403742321c65
permissions -rw-r--r--
getpass for windows does not work 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>
20
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
     2
#include <cstdio>
19
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
20
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
     4
#ifdef WIN32
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
     5
#include <conio.h>
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
     6
int Console::getch ()
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
     7
{
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
     8
    return::getch ();
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
     9
}
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
    10
#else
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
    11
#include <termios.h>
19
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
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
    13
{
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
    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
    15
    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
    16
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
    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
    18
    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
    19
    neu = alt;
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
    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
    21
    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
    22
    ch = getchar ();
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
    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
    24
    return ch;
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
}
20
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
    26
#endif
19
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
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
    29
{
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
    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
    31
    int c;
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
    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
    33
20
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
    34
    while ((c = Console::getch ()) != '\n' && i < 17) {
19
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
        buffer[i++] = c;
20
5fec678f931b getpass for windows does not work
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 19
diff changeset
    36
        std::cout << "\b*";
19
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
    }
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
    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
    39
933d86c1ff71 namespace console added for console tools, password entry and other stuff
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
    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
    41
    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
    42
}