Qore Programming Language  1.12.0
QC_TermIOS.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QC_TermIOS.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2022 Qore Technologies
8 
9  Permission is hereby granted, free of charge, to any person obtaining a
10  copy of this software and associated documentation files (the "Software"),
11  to deal in the Software without restriction, including without limitation
12  the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  and/or sell copies of the Software, and to permit persons to whom the
14  Software is furnished to do so, subject to the following conditions:
15 
16  The above copyright notice and this permission notice shall be included in
17  all copies or substantial portions of the Software.
18 
19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  DEALINGS IN THE SOFTWARE.
26 
27  Note that the Qore library is released under a choice of three open-source
28  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29  information.
30 */
31 
32 #ifndef _QORE_QC_TERMIOS_H
33 #define _QORE_QC_TERMIOS_H
34 
35 DLLLOCAL extern QoreClass *QC_TERMIOS;
36 DLLEXPORT extern qore_classid_t CID_TERMIOS;
37 DLLLOCAL QoreClass *initTermIOSClass(QoreNamespace& ns);
38 
39 #ifdef HAVE_TERMIOS_H
40 
41 #include <cerrno>
42 #include <cstring>
43 #include <fcntl.h>
44 #include <sys/ioctl.h>
45 #include <termios.h>
46 #include <unistd.h>
47 
48 class QoreTermIOS : public AbstractPrivateData {
49 protected:
50  hashdecl termios ios;
51 
52  DLLLOCAL int check_offset(int64 offset, ExceptionSink *xsink) {
53  if (offset < 0) {
54  xsink->raiseException("TERMIOS-CC-ERROR", "cc offset (%lld) is < 0", offset);
55  return -1;
56  }
57 
58  if (offset > NCCS) {
59  xsink->raiseException("TERMIOS-CC-ERROR", "cc offset (%lld) is > NCCS (%d)", offset, NCCS);
60  return -1;
61  }
62  return 0;
63  }
64 
65 public:
66  DLLLOCAL QoreTermIOS() {
67  }
68 
69  DLLLOCAL QoreTermIOS(const QoreTermIOS &cp) {
70  memcpy(&ios, &cp.ios, sizeof(struct termios));
71  }
72 
73  DLLLOCAL bool is_equal(const QoreTermIOS *qtios) {
74  return !memcmp(&ios, &qtios->ios, sizeof(struct termios));
75  }
76 
77  DLLLOCAL int get(int fd, ExceptionSink *xsink) {
78  int rc = tcgetattr(fd, &ios);
79  if (rc) {
80  xsink->raiseException("TERMIOS-GET-ERROR", q_strerror(errno));
81  return rc;
82  }
83  return 0;
84  }
85 
86  DLLLOCAL int set(int fd, int action, ExceptionSink *xsink) {
87  // WARNING!: tcsetattr returns 0 if any changes were made, not if all
88  // changes were made
89  int rc = tcsetattr(fd, action, &ios);
90  if (rc) {
91  xsink->raiseException("TERMIOS-SET-ERROR", q_strerror(errno));
92  return rc;
93  }
94  return 0;
95  }
96 
97  DLLLOCAL void set_lflag(tcflag_t val) {
98  ios.c_lflag = val;
99  }
100 
101  DLLLOCAL void set_cflag(tcflag_t val) {
102  ios.c_cflag = val;
103  }
104 
105  DLLLOCAL void set_oflag(tcflag_t val) {
106  ios.c_oflag = val;
107  }
108 
109  DLLLOCAL void set_iflag(tcflag_t val) {
110  ios.c_iflag = val;
111  }
112 
113  DLLLOCAL tcflag_t get_lflag() const {
114  return ios.c_lflag;
115  }
116 
117  DLLLOCAL tcflag_t get_cflag() const {
118  return ios.c_cflag;
119  }
120 
121  DLLLOCAL tcflag_t get_oflag() const {
122  return ios.c_oflag;
123  }
124 
125  DLLLOCAL tcflag_t get_iflag() const {
126  return ios.c_iflag;
127  }
128 
129  DLLLOCAL cc_t get_cc(int64 offset, ExceptionSink *xsink) {
130  if (check_offset(offset, xsink))
131  return -1;
132 
133  return ios.c_cc[offset];
134  }
135 
136  DLLLOCAL int set_cc(int64 offset, cc_t val, ExceptionSink *xsink) {
137  if (check_offset(offset, xsink))
138  return -1;
139 
140  ios.c_cc[offset] = val;
141  return 0;
142  }
143 
144  DLLLOCAL static int getWindowSize(int &rows, int &columns, ExceptionSink *xsink) {
145  hashdecl winsize ws;
146 
147  int fd = open("/dev/tty", O_RDONLY);
148  if (fd == -1) {
149  xsink->raiseErrnoException("TERMIOS-GET-WINDOW-SIZE-ERROR", errno, "cannot open controlling terminal");
150  return -1;
151  }
152 
153  if (ioctl(fd, TIOCGWINSZ, &ws)) {
154  xsink->raiseErrnoException("TERMIOS-GET-WINDOW-SIZE-ERROR", errno, "error reading window size");
155 
156  if (close(fd))
157  xsink->raiseErrnoException("TERMIOS-GET-WINDOW-SIZE-ERROR", errno, "error closing controlling terminal");
158 
159  return -1;
160  }
161 
162  if (close(fd)) {
163  xsink->raiseErrnoException("TERMIOS-GET-WINDOW-SIZE-ERROR", errno, "error closing controlling terminal");
164  return -1;
165  }
166 
167  rows = ws.ws_row;
168  columns = ws.ws_col;
169  return 0;
170  }
171 };
172 
173 #endif // HAVE_TERMIOS_H
174 
175 #endif // _QORE_QC_TERMIOS_H
DLLEXPORT QoreStringNode * q_strerror(int errnum)
returns the error string as a QoreStringNode
the base class for all data to be used as private data of Qore objects
Definition: AbstractPrivateData.h:44
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
DLLEXPORT AbstractQoreNode * raiseErrnoException(const char *err, int en, const char *fmt,...)
appends a Qore-language exception to the list and appends the result of strerror(errno) to the descri...
DLLEXPORT AbstractQoreNode * raiseException(const char *err, const char *fmt,...)
appends a Qore-language exception to the list
defines a Qore-language class
Definition: QoreClass.h:249
contains constants, classes, and subnamespaces in QoreProgram objects
Definition: QoreNamespace.h:65
unsigned qore_classid_t
used for the unique class ID for QoreClass objects
Definition: common.h:79
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition: common.h:260