aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/3rdparty/winpty/misc/UnixEcho.cc
blob: 372e0451574691ad264d04575f3b99257ec83749 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Unix test code that puts the terminal into raw mode, then echos typed
 * characters to stdout.  Derived from sample code in the Stevens book, posted
 * online at http://www.lafn.org/~dave/linux/terminalIO.html.
 */

#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include "FormatChar.h"

static struct termios   save_termios;
static int              term_saved;

/* RAW! mode */
int tty_raw(int fd)
{
    struct termios  buf;

    if (tcgetattr(fd, &save_termios) < 0) /* get the original state */
        return -1;

    buf = save_termios;

    /* echo off, canonical mode off, extended input
       processing off, signal chars off */
    buf.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);

    /* no SIGINT on BREAK, CR-to-NL off, input parity
       check off, don't strip the 8th bit on input,
       ouput flow control off */
    buf.c_iflag &= ~(BRKINT | ICRNL | ISTRIP | IXON);

    /* clear size bits, parity checking off */
    buf.c_cflag &= ~(CSIZE | PARENB);

    /* set 8 bits/char */
    buf.c_cflag |= CS8;

    /* output processing off */
    buf.c_oflag &= ~(OPOST);

    buf.c_cc[VMIN] = 1;  /* 1 byte at a time */
    buf.c_cc[VTIME] = 0; /* no timer on input */

    if (tcsetattr(fd, TCSAFLUSH, &buf) < 0)
        return -1;

    term_saved = 1;

    return 0;
}


/* set it to normal! */
int tty_reset(int fd)
{
    if (term_saved)
        if (tcsetattr(fd, TCSAFLUSH, &save_termios) < 0)
            return -1;

    return 0;
}


int main()
{
    tty_raw(0);

    int count = 0;
    while (true) {
        char ch;
        char buf[16];
        int actual = read(0, &ch, 1);
        if (actual != 1) {
            perror("read error");
            break;
        }
        formatChar(buf, ch);
        fputs(buf, stdout);
        fflush(stdout);
        if (ch == 3) // Ctrl-C
            break;
    }

    tty_reset(0);
    return 0;
}