2011-05-17 14:40:28 +00:00
|
|
|
/**
|
|
|
|
* @file termios_guard.hh
|
|
|
|
*/
|
2009-10-14 19:42:41 +00:00
|
|
|
|
|
|
|
#ifndef __termios_guard_hh
|
|
|
|
#define __termios_guard_hh
|
|
|
|
|
2012-06-05 20:18:59 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2011-05-17 14:40:28 +00:00
|
|
|
/**
|
|
|
|
* RAII class that saves the current termios for a tty and then restores them
|
|
|
|
* during destruction.
|
|
|
|
*/
|
2009-10-14 19:42:41 +00:00
|
|
|
class guard_termios {
|
|
|
|
public:
|
2011-05-17 14:40:28 +00:00
|
|
|
/**
|
|
|
|
* Store the TTY termios settings in this object.
|
|
|
|
*
|
|
|
|
* @param fd The tty file descriptor.
|
|
|
|
*/
|
|
|
|
guard_termios(const int fd) : gt_fd(fd) {
|
2009-10-14 19:42:41 +00:00
|
|
|
if (isatty(this->gt_fd) &&
|
|
|
|
tcgetattr(this->gt_fd, &this->gt_termios) == -1) {
|
|
|
|
perror("tcgetattr");
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2011-05-17 14:40:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Restore the TTY termios settings that were captured when this object was
|
|
|
|
* instantiated.
|
|
|
|
*/
|
2009-10-14 19:42:41 +00:00
|
|
|
~guard_termios() {
|
|
|
|
if (isatty(this->gt_fd) &&
|
|
|
|
tcsetattr(this->gt_fd, TCSANOW, &this->gt_termios) == -1) {
|
|
|
|
perror("tcsetattr");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2011-05-17 14:40:28 +00:00
|
|
|
const int gt_fd;
|
2009-10-14 19:42:41 +00:00
|
|
|
struct termios gt_termios;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|