termios.cpp

(plain text)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <string>
#include <cassert>
#include <climits>
#include <cstdio>
#include <cstring>
#include <termios.h>
#include <unistd.h>

class Termios {
 public:
  explicit Termios (int fd)
    : myFd(fd) {
      assert(tcgetattr(myFd, &myStart) == 0);
      assert(tcgetattr(myFd, &myCurrent) == 0);
      reset();
    }

  struct input {
    enum {
      ignbrk = IGNBRK,
      brkint = BRKINT,
      ignpar = IGNPAR,
      parmrk = PARMRK,
      inpck = INPCK,
      istrip = ISTRIP,
      inlcr = INLCR,
      igncr = IGNCR,
      icrnl = ICRNL,
      ixon = IXON,
      ixoff = IXOFF,
      ixany = IXANY,
      imaxbel = IMAXBEL,
    };
  };

  struct output {
    enum {
      opost = OPOST,
      onlcr = ONLCR,
      ocrnl = OCRNL,
      oxtabs = OXTABS,
      onoeot = ONOEOT,
      onocr = ONOCR,
      onlret = ONLRET,
    };
  };

  struct control {
    enum {
      csize = CSIZE,
      cs5 = CS5,
      cs6 = CS6,
      cs7 = CS7,
      cs8 = CS8,
      cstopb = CSTOPB,
      cread = CREAD,
      parenb = PARENB,
      parodd = PARODD,
      hupcl = HUPCL,
      clocal = CLOCAL,
      ccts_oflow = CCTS_OFLOW,
      crtscts = CRTSCTS,
      crts_iflow = CRTS_IFLOW,
      mdmbuf = MDMBUF,
    };
  };

  struct local {
    enum {
      echoke = ECHOKE,
      echoe = ECHOE,
      echo = ECHO,
      echonl = ECHONL,
      echoprt = ECHOPRT,
      echoctl = ECHOCTL,
      isig = ISIG,
      icanon = ICANON,
      altwerase = ALTWERASE,
      iexten = IEXTEN,
      extproc = EXTPROC,
      tostop = TOSTOP,
      flusho = FLUSHO,
      nokerninfo = NOKERNINFO,
      pendin = PENDIN,
      noflsh = NOFLSH,
    };
  };

  tcflag_t input_flags () const { return myCurrent.c_iflag; }
  tcflag_t output_flags () const { return myCurrent.c_oflag; }
  tcflag_t control_flags () const { return myCurrent.c_cflag; }
  tcflag_t local_flags () const { return myCurrent.c_lflag; }
  const cc_t *control_chars () const { return myCurrent.c_cc; }
  speed_t input_speed () const { return myCurrent.c_ispeed; }
  speed_t output_speed () const { return myCurrent.c_ospeed; }

  void input_flags (tcflag_t flags) { myCurrent.c_iflag = flags; }
  void output_flags (tcflag_t flags) { myCurrent.c_oflag = flags; }
  void control_flags (tcflag_t flags) { myCurrent.c_cflag = flags; }
  void local_flags (tcflag_t flags) { myCurrent.c_lflag = flags; }
  cc_t *control_chars () { return myCurrent.c_cc; }
  void input_speed (speed_t speed) { myCurrent.c_ispeed = speed; }
  void output_speed (speed_t speed) { myCurrent.c_ospeed = speed; }

  //! Apply my current settings to the stream.
  void apply () { assert(tcsetattr(myFd, TCSANOW, &myCurrent) == 0); }

  //! Reset my current settings from the stream. 
  void reset () { assert(tcgetattr(myFd, &myCurrent) == 0); }

  //! Restore the original stream settings.
  ~Termios () { assert(tcsetattr(myFd, TCSANOW, &myStart) == 0); }

 private:
  int myFd;
  struct termios myStart, myCurrent;
  explicit Termios (const Termios &);
  Termios &operator= (const Termios &);
};

// char buffer which erases itself before deallocation
template <size_t N>
struct ClassifiedBuffer {
 public:
  char *buffer () { return myBuffer; }
  const char *buffer () const { return myBuffer; }
  char &operator[] (size_t i) { return myBuffer[i]; }
  const char &operator[] (size_t i) const { return myBuffer[i]; }

  ~ClassifiedBuffer () {
    std::memset(myBuffer, 0, N);

    // to prevent the compiler from eliding the memset()
    *const_cast<volatile char *>(myBuffer) = *const_cast<volatile char *>(myBuffer);
  }

 private:
  char myBuffer[N];
};

std::istream &noecho_getline (char *buf, std::size_t len) {
  Termios termios(STDIN_FILENO);
  termios.local_flags(termios.local_flags() & ~(Termios::local::echo | Termios::local::icanon));
  termios.apply();
  return std::cin.getline(buf, len);
}

int main () {
  setvbuf(stdin, 0, _IONBF, 0); // disable buffering in C library

  // disable buffering in C++ library
#  if defined(__GNUC__) && __GNUC__ < 3
  std::cin.rdbuf()->setbuf(0, 0);
#  else
  std::cin.rdbuf()->pubsetbuf(0, 0); 
#  endif

  ClassifiedBuffer<80> pwd;

  if (noecho_getline(pwd.buffer(), 80)) {
    std::cout.put('[').write(pwd.buffer(), std::strlen(pwd.buffer())).put(']') << '\n';
  }

  return 0;
}