Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 1 | /* termiosmodule.c -- POSIX terminal I/O module implementation. */ |
| 2 | |
Guido van Rossum | 602099a | 1994-09-14 13:32:22 +0000 | [diff] [blame] | 3 | #include <Python.h> |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 4 | |
| 5 | #define PyInit_termios inittermios |
| 6 | |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 7 | #include <termios.h> |
| 8 | |
| 9 | #define BAD "bad termios argument" |
| 10 | |
| 11 | static PyObject *TermiosError; |
| 12 | |
| 13 | /* termios = tcgetattr(fd) |
| 14 | termios is |
| 15 | [iflag, oflag, cflag, lflag, ispeed, ospeed, [cc[0], ..., cc[NCCS]]] |
| 16 | |
| 17 | Return the attributes of the terminal device. */ |
| 18 | |
| 19 | static PyObject * |
| 20 | termios_tcgetattr(self, args) |
| 21 | PyObject *self; |
| 22 | PyObject *args; |
| 23 | { |
| 24 | int fd; |
| 25 | struct termios mode; |
| 26 | PyObject *cc; |
| 27 | speed_t ispeed, ospeed; |
| 28 | PyObject *v; |
| 29 | int i; |
| 30 | char ch; |
| 31 | |
| 32 | if (!PyArg_Parse(args, "i", &fd)) |
| 33 | return NULL; |
| 34 | |
| 35 | if (tcgetattr(fd, &mode) == -1) |
Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 36 | return PyErr_SetFromErrno(TermiosError); |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 37 | |
| 38 | ispeed = cfgetispeed(&mode); |
| 39 | ospeed = cfgetospeed(&mode); |
| 40 | |
| 41 | cc = PyList_New(NCCS); |
| 42 | if (cc == NULL) |
| 43 | return NULL; |
| 44 | for (i = 0; i < NCCS; i++) { |
| 45 | ch = (char)mode.c_cc[i]; |
| 46 | v = PyString_FromStringAndSize(&ch, 1); |
| 47 | if (v == NULL) |
Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 48 | goto err; |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 49 | PyList_SetItem(cc, i, v); |
| 50 | } |
| 51 | |
| 52 | /* Convert the MIN and TIME slots to integer. On some systems, the |
| 53 | MIN and TIME slots are the same as the EOF and EOL slots. So we |
| 54 | only do this in noncanonical input mode. */ |
Guido van Rossum | 36dd0d2 | 1996-12-10 15:23:00 +0000 | [diff] [blame] | 55 | if ((mode.c_lflag & ICANON) == 0) { |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 56 | v = PyInt_FromLong((long)mode.c_cc[VMIN]); |
| 57 | if (v == NULL) |
Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 58 | goto err; |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 59 | PyList_SetItem(cc, VMIN, v); |
| 60 | v = PyInt_FromLong((long)mode.c_cc[VTIME]); |
| 61 | if (v == NULL) |
Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 62 | goto err; |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 63 | PyList_SetItem(cc, VTIME, v); |
| 64 | } |
| 65 | |
Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 66 | if (!(v = PyList_New(7))) |
| 67 | goto err; |
| 68 | |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 69 | PyList_SetItem(v, 0, PyInt_FromLong((long)mode.c_iflag)); |
| 70 | PyList_SetItem(v, 1, PyInt_FromLong((long)mode.c_oflag)); |
| 71 | PyList_SetItem(v, 2, PyInt_FromLong((long)mode.c_cflag)); |
| 72 | PyList_SetItem(v, 3, PyInt_FromLong((long)mode.c_lflag)); |
| 73 | PyList_SetItem(v, 4, PyInt_FromLong((long)ispeed)); |
| 74 | PyList_SetItem(v, 5, PyInt_FromLong((long)ospeed)); |
| 75 | PyList_SetItem(v, 6, cc); |
Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 76 | if (PyErr_Occurred()){ |
| 77 | Py_DECREF(v); |
| 78 | goto err; |
| 79 | } |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 80 | return v; |
Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 81 | err: |
| 82 | Py_DECREF(cc); |
| 83 | return NULL; |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* tcsetattr(fd, when, termios) |
| 87 | Set the attributes of the terminal device. */ |
| 88 | |
| 89 | static PyObject * |
| 90 | termios_tcsetattr(self, args) |
| 91 | PyObject *self; |
| 92 | PyObject *args; |
| 93 | { |
| 94 | int fd, when; |
| 95 | struct termios mode; |
| 96 | speed_t ispeed, ospeed; |
| 97 | PyObject *term, *cc, *v; |
| 98 | int i; |
| 99 | |
| 100 | if (!PyArg_Parse(args, "(iiO)", &fd, &when, &term)) |
| 101 | return NULL; |
| 102 | if (!PyList_Check(term) || PyList_Size(term) != 7) { |
| 103 | PyErr_SetString(PyExc_TypeError, BAD); |
| 104 | return NULL; |
| 105 | } |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 106 | |
| 107 | mode.c_iflag = (tcflag_t) PyInt_AsLong(PyList_GetItem(term, 0)); |
| 108 | mode.c_oflag = (tcflag_t) PyInt_AsLong(PyList_GetItem(term, 1)); |
| 109 | mode.c_cflag = (tcflag_t) PyInt_AsLong(PyList_GetItem(term, 2)); |
| 110 | mode.c_lflag = (tcflag_t) PyInt_AsLong(PyList_GetItem(term, 3)); |
| 111 | ispeed = (speed_t) PyInt_AsLong(PyList_GetItem(term, 4)); |
| 112 | ospeed = (speed_t) PyInt_AsLong(PyList_GetItem(term, 5)); |
| 113 | cc = PyList_GetItem(term, 6); |
Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 114 | if (PyErr_Occurred()) |
| 115 | return NULL; |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 116 | |
| 117 | if (!PyList_Check(cc) || PyList_Size(cc) != NCCS) { |
| 118 | PyErr_SetString(PyExc_TypeError, BAD); |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | for (i = 0; i < NCCS; i++) { |
| 123 | v = PyList_GetItem(cc, i); |
Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 124 | |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 125 | if (PyString_Check(v) && PyString_Size(v) == 1) |
| 126 | mode.c_cc[i] = (cc_t) * PyString_AsString(v); |
| 127 | else if (PyInt_Check(v)) |
| 128 | mode.c_cc[i] = (cc_t) PyInt_AsLong(v); |
| 129 | else { |
| 130 | PyErr_SetString(PyExc_TypeError, BAD); |
| 131 | return NULL; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (cfsetispeed(&mode, (speed_t) ispeed) == -1) |
Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 136 | return PyErr_SetFromErrno(TermiosError); |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 137 | if (cfsetospeed(&mode, (speed_t) ospeed) == -1) |
Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 138 | return PyErr_SetFromErrno(TermiosError); |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 139 | if (tcsetattr(fd, when, &mode) == -1) |
Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 140 | return PyErr_SetFromErrno(TermiosError); |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 141 | |
| 142 | Py_INCREF(Py_None); |
| 143 | return Py_None; |
| 144 | } |
| 145 | |
| 146 | /* tcsendbreak(fd, duration) |
| 147 | Generate a break condition. */ |
| 148 | |
| 149 | static PyObject * |
| 150 | termios_tcsendbreak(self, args) |
| 151 | PyObject *self; |
| 152 | PyObject *args; |
| 153 | { |
| 154 | int fd, duration; |
| 155 | |
| 156 | if (!PyArg_Parse(args, "(ii)", &fd, &duration)) |
| 157 | return NULL; |
| 158 | if (tcsendbreak(fd, duration) == -1) |
Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 159 | return PyErr_SetFromErrno(TermiosError); |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 160 | |
| 161 | Py_INCREF(Py_None); |
| 162 | return Py_None; |
| 163 | } |
| 164 | |
| 165 | /* tcdrain(fd) |
| 166 | Wait until all queued output to the terminal has been |
| 167 | transmitted. */ |
| 168 | |
| 169 | static PyObject * |
| 170 | termios_tcdrain(self, args) |
| 171 | PyObject *self; |
| 172 | PyObject *args; |
| 173 | { |
| 174 | int fd; |
| 175 | |
| 176 | if (!PyArg_Parse(args, "i", &fd)) |
| 177 | return NULL; |
| 178 | if (tcdrain(fd) == -1) |
Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 179 | return PyErr_SetFromErrno(TermiosError); |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 180 | |
| 181 | Py_INCREF(Py_None); |
| 182 | return Py_None; |
| 183 | } |
| 184 | |
| 185 | /* tcflush(fd, queue) |
| 186 | Clear the input and/or output queues associated with |
| 187 | the terminal. */ |
| 188 | |
| 189 | static PyObject * |
| 190 | termios_tcflush(self, args) |
| 191 | PyObject *self; |
| 192 | PyObject *args; |
| 193 | { |
| 194 | int fd, queue; |
| 195 | |
| 196 | if (!PyArg_Parse(args, "(ii)", &fd, &queue)) |
| 197 | return NULL; |
| 198 | if (tcflush(fd, queue) == -1) |
Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 199 | return PyErr_SetFromErrno(TermiosError); |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 200 | |
| 201 | Py_INCREF(Py_None); |
| 202 | return Py_None; |
| 203 | } |
| 204 | |
| 205 | /* tcflow(fd, action) |
| 206 | Perform operations relating to XON/XOFF flow control on |
| 207 | the terminal. */ |
| 208 | |
| 209 | static PyObject * |
| 210 | termios_tcflow(self, args) |
| 211 | PyObject *self; |
| 212 | PyObject *args; |
| 213 | { |
| 214 | int fd, action; |
| 215 | |
| 216 | if (!PyArg_Parse(args, "(ii)", &fd, &action)) |
| 217 | return NULL; |
| 218 | if (tcflow(fd, action) == -1) |
Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 219 | return PyErr_SetFromErrno(TermiosError); |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 220 | |
| 221 | Py_INCREF(Py_None); |
| 222 | return Py_None; |
| 223 | } |
| 224 | |
| 225 | static PyMethodDef termios_methods[] = |
| 226 | { |
| 227 | {"tcgetattr", termios_tcgetattr}, |
| 228 | {"tcsetattr", termios_tcsetattr}, |
| 229 | {"tcsendbreak", termios_tcsendbreak}, |
| 230 | {"tcdrain", termios_tcdrain}, |
| 231 | {"tcflush", termios_tcflush}, |
| 232 | {"tcflow", termios_tcflow}, |
| 233 | {NULL, NULL} |
| 234 | }; |
| 235 | |
| 236 | void |
| 237 | PyInit_termios() |
| 238 | { |
| 239 | PyObject *m, *d; |
| 240 | |
| 241 | m = Py_InitModule("termios", termios_methods); |
| 242 | |
| 243 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 244 | TermiosError = PyErr_NewException("termios.error", NULL, NULL); |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 245 | PyDict_SetItemString(d, "error", TermiosError); |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 246 | } |