irods  4.2.8
About: iRODS (the integrated Rule Oriented Data System) is a distributed data-management system for creating data grids, digital libraries, persistent archives, and real-time data systems.
  Fossies Dox: irods-4.2.8.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

termiosUtil.hpp
Go to the documentation of this file.
1 #ifndef _TERMIOS_UTIL_HPP_
2 #define _TERMIOS_UTIL_HPP_
3 
4 #include <unistd.h>
5 #include <errno.h>
6 #include <termios.h>
7 
8 #ifdef __cplusplus
9 
10 namespace irods
11 {
12  // class termiosUtil - at this time, this class encapsulates the work
13  // which has to be done in order to turn echo on or off for a specifc
14  // file descriptor. The call can be expanded to encapsulate other
15  // functionality of termios(3).
16 
17  class termiosUtil {
18  public:
19  // Class has to be instantiated with a file descriptor.
20  // The default constructor is invalid.
21  explicit termiosUtil(int fd) :
22  valid_(false),
23  errno_copy_(0),
24  fd_(fd),
25  save_c_lflag_(0)
26  {
27  memset(&tty_, 0, sizeof(tty_));
28  if (isatty(fd_))
29  {
30  if (tcgetattr( fd_, &tty_ ) == 0)
31  {
32  save_c_lflag_ = tty_.c_lflag;
33  valid_ = true;
34  }
35  else
36  {
37  errno_copy_ = errno;
38  valid_ = false;
39  }
40  }
41  else
42  {
43  errno_copy_ = errno;
44  valid_ = false;
45  }
46  };
47 
48  public:
49  virtual ~termiosUtil(void) = default;
50  int getError(void) const { return errno_copy_; };
51  bool getValid(void) const { return valid_; };
52 
53  bool echoOff(void)
54  {
55  if (!valid_)
56  {
57  return false;
58  }
59  tty_.c_lflag &= ~ECHO;
60  if (tcsetattr( fd_, TCSANOW, &tty_ ) == 0)
61  {
62  return true;
63  }
64  errno_copy_ = errno;
65  valid_ = false;
66  return false;
67  }
68 
69  bool echoOn(void)
70  {
71  if (!valid_)
72  {
73  return false;
74  }
75  tty_.c_lflag = save_c_lflag_;
76  if (tcsetattr( fd_, TCSANOW, &tty_ ) == 0)
77  {
78  return true;
79  }
80  errno_copy_ = errno;
81  valid_ = false;
82  return false;
83  }
84 
85  private:
86  bool valid_; // fd_ >= 0, etc
87  int errno_copy_; // if fd_ is not a tty, or other termios error, this has the error
88  int fd_; // File descriptor for this tty_ structure
89  struct termios tty_;
90  tcflag_t save_c_lflag_;
91  };
92 }; // namespace irods
93 
94 #endif // __cplusplus
95 #endif // _TERMIOS_UTIL_HPP_
irods
Definition: apiHandler.hpp:35