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 | ca6954a | 1999-01-14 19:31:42 +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 | |
Guido van Rossum | 1aca4d8 | 1998-03-03 22:10:18 +0000 | [diff] [blame] | 9 | static char termios__doc__[] = "\ |
| 10 | This module provides an interface to the Posix calls for tty I/O control.\n\ |
| 11 | For a complete description of these calls, see the Posix or Unix manual\n\ |
| 12 | pages. It is only available for those Unix versions that support Posix\n\ |
| 13 | termios style tty I/O control (and then only if configured at installation\n\ |
| 14 | time).\n\ |
| 15 | \n\ |
| 16 | All functions in this module take a file descriptor fd as their first\n\ |
| 17 | argument. This must be an integer file descriptor, such as returned by\n\ |
| 18 | sys.stdin.fileno().\n\ |
| 19 | \n\ |
| 20 | This module should be used in conjunction with the TERMIOS module,\n\ |
| 21 | which defines the relevant symbolic constants."; |
| 22 | |
| 23 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 24 | #ifdef __BEOS__ |
| 25 | #include <unistd.h> |
| 26 | #endif |
| 27 | |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 28 | #define BAD "bad termios argument" |
| 29 | |
| 30 | static PyObject *TermiosError; |
| 31 | |
| 32 | /* termios = tcgetattr(fd) |
| 33 | termios is |
| 34 | [iflag, oflag, cflag, lflag, ispeed, ospeed, [cc[0], ..., cc[NCCS]]] |
| 35 | |
| 36 | Return the attributes of the terminal device. */ |
| 37 | |
Guido van Rossum | 1aca4d8 | 1998-03-03 22:10:18 +0000 | [diff] [blame] | 38 | static char termios_tcgetattr__doc__[] = "\ |
| 39 | tcgetattr(fd) -> list_of_attrs\n\ |
| 40 | Get the tty attributes for file descriptor fd, as follows:\n\ |
| 41 | [iflag, oflag, cflag, lflag, ispeed, ospeed, cc] where cc is a list\n\ |
| 42 | of the tty special characters (each a string of length 1, except the items\n\ |
| 43 | with indices VMIN and VTIME, which are integers when these fields are\n\ |
| 44 | defined). The interpretation of the flags and the speeds as well as the\n\ |
| 45 | indexing in the cc array must be done using the symbolic constants defined\n\ |
| 46 | in the TERMIOS module."; |
| 47 | |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 48 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 49 | termios_tcgetattr(PyObject *self, PyObject *args) |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 50 | { |
| 51 | int fd; |
| 52 | struct termios mode; |
| 53 | PyObject *cc; |
| 54 | speed_t ispeed, ospeed; |
| 55 | PyObject *v; |
| 56 | int i; |
| 57 | char ch; |
| 58 | |
| 59 | if (!PyArg_Parse(args, "i", &fd)) |
| 60 | return NULL; |
| 61 | |
| 62 | if (tcgetattr(fd, &mode) == -1) |
Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 63 | return PyErr_SetFromErrno(TermiosError); |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 64 | |
| 65 | ispeed = cfgetispeed(&mode); |
| 66 | ospeed = cfgetospeed(&mode); |
| 67 | |
| 68 | cc = PyList_New(NCCS); |
| 69 | if (cc == NULL) |
| 70 | return NULL; |
| 71 | for (i = 0; i < NCCS; i++) { |
| 72 | ch = (char)mode.c_cc[i]; |
| 73 | v = PyString_FromStringAndSize(&ch, 1); |
| 74 | if (v == NULL) |
Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 75 | goto err; |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 76 | PyList_SetItem(cc, i, v); |
| 77 | } |
| 78 | |
| 79 | /* Convert the MIN and TIME slots to integer. On some systems, the |
| 80 | MIN and TIME slots are the same as the EOF and EOL slots. So we |
| 81 | only do this in noncanonical input mode. */ |
Guido van Rossum | 36dd0d2 | 1996-12-10 15:23:00 +0000 | [diff] [blame] | 82 | if ((mode.c_lflag & ICANON) == 0) { |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 83 | v = PyInt_FromLong((long)mode.c_cc[VMIN]); |
| 84 | if (v == NULL) |
Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 85 | goto err; |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 86 | PyList_SetItem(cc, VMIN, v); |
| 87 | v = PyInt_FromLong((long)mode.c_cc[VTIME]); |
| 88 | if (v == NULL) |
Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 89 | goto err; |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 90 | PyList_SetItem(cc, VTIME, v); |
| 91 | } |
| 92 | |
Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 93 | if (!(v = PyList_New(7))) |
| 94 | goto err; |
| 95 | |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 96 | PyList_SetItem(v, 0, PyInt_FromLong((long)mode.c_iflag)); |
| 97 | PyList_SetItem(v, 1, PyInt_FromLong((long)mode.c_oflag)); |
| 98 | PyList_SetItem(v, 2, PyInt_FromLong((long)mode.c_cflag)); |
| 99 | PyList_SetItem(v, 3, PyInt_FromLong((long)mode.c_lflag)); |
| 100 | PyList_SetItem(v, 4, PyInt_FromLong((long)ispeed)); |
| 101 | PyList_SetItem(v, 5, PyInt_FromLong((long)ospeed)); |
| 102 | PyList_SetItem(v, 6, cc); |
Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 103 | if (PyErr_Occurred()){ |
| 104 | Py_DECREF(v); |
| 105 | goto err; |
| 106 | } |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 107 | return v; |
Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 108 | err: |
| 109 | Py_DECREF(cc); |
| 110 | return NULL; |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* tcsetattr(fd, when, termios) |
| 114 | Set the attributes of the terminal device. */ |
| 115 | |
Guido van Rossum | 1aca4d8 | 1998-03-03 22:10:18 +0000 | [diff] [blame] | 116 | static char termios_tcsetattr__doc__[] = "\ |
| 117 | tcsetattr(fd, when, attributes) -> None\n\ |
| 118 | Set the tty attributes for file descriptor fd.\n\ |
| 119 | The attributes to be set are taken from the attributes argument, which\n\ |
| 120 | is a list like the one returned by tcgetattr(). The when argument\n\ |
| 121 | determines when the attributes are changed: TERMIOS.TCSANOW to\n\ |
| 122 | change immediately, TERMIOS.TCSADRAIN to change after transmitting all\n\ |
| 123 | queued output, or TERMIOS.TCSAFLUSH to change after transmitting all\n\ |
| 124 | queued output and discarding all queued input. "; |
| 125 | |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 126 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 127 | termios_tcsetattr(PyObject *self, PyObject *args) |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 128 | { |
| 129 | int fd, when; |
| 130 | struct termios mode; |
| 131 | speed_t ispeed, ospeed; |
| 132 | PyObject *term, *cc, *v; |
| 133 | int i; |
| 134 | |
| 135 | if (!PyArg_Parse(args, "(iiO)", &fd, &when, &term)) |
| 136 | return NULL; |
| 137 | if (!PyList_Check(term) || PyList_Size(term) != 7) { |
| 138 | PyErr_SetString(PyExc_TypeError, BAD); |
| 139 | return NULL; |
| 140 | } |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 141 | |
Guido van Rossum | e7c4193 | 1998-06-12 14:26:18 +0000 | [diff] [blame] | 142 | /* Get the old mode, in case there are any hidden fields... */ |
| 143 | if (tcgetattr(fd, &mode) == -1) |
| 144 | return PyErr_SetFromErrno(TermiosError); |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 145 | mode.c_iflag = (tcflag_t) PyInt_AsLong(PyList_GetItem(term, 0)); |
| 146 | mode.c_oflag = (tcflag_t) PyInt_AsLong(PyList_GetItem(term, 1)); |
| 147 | mode.c_cflag = (tcflag_t) PyInt_AsLong(PyList_GetItem(term, 2)); |
| 148 | mode.c_lflag = (tcflag_t) PyInt_AsLong(PyList_GetItem(term, 3)); |
| 149 | ispeed = (speed_t) PyInt_AsLong(PyList_GetItem(term, 4)); |
| 150 | ospeed = (speed_t) PyInt_AsLong(PyList_GetItem(term, 5)); |
| 151 | cc = PyList_GetItem(term, 6); |
Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 152 | if (PyErr_Occurred()) |
| 153 | return NULL; |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 154 | |
| 155 | if (!PyList_Check(cc) || PyList_Size(cc) != NCCS) { |
| 156 | PyErr_SetString(PyExc_TypeError, BAD); |
| 157 | return NULL; |
| 158 | } |
| 159 | |
| 160 | for (i = 0; i < NCCS; i++) { |
| 161 | v = PyList_GetItem(cc, i); |
Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 162 | |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 163 | if (PyString_Check(v) && PyString_Size(v) == 1) |
| 164 | mode.c_cc[i] = (cc_t) * PyString_AsString(v); |
| 165 | else if (PyInt_Check(v)) |
| 166 | mode.c_cc[i] = (cc_t) PyInt_AsLong(v); |
| 167 | else { |
| 168 | PyErr_SetString(PyExc_TypeError, BAD); |
| 169 | return NULL; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if (cfsetispeed(&mode, (speed_t) ispeed) == -1) |
Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 174 | return PyErr_SetFromErrno(TermiosError); |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 175 | if (cfsetospeed(&mode, (speed_t) ospeed) == -1) |
Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 176 | return PyErr_SetFromErrno(TermiosError); |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 177 | if (tcsetattr(fd, when, &mode) == -1) |
Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 178 | return PyErr_SetFromErrno(TermiosError); |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 179 | |
| 180 | Py_INCREF(Py_None); |
| 181 | return Py_None; |
| 182 | } |
| 183 | |
| 184 | /* tcsendbreak(fd, duration) |
| 185 | Generate a break condition. */ |
| 186 | |
Guido van Rossum | 1aca4d8 | 1998-03-03 22:10:18 +0000 | [diff] [blame] | 187 | static char termios_tcsendbreak__doc__[] = "\ |
| 188 | tcsendbreak(fd, duration) -> None\n\ |
| 189 | Send a break on file descriptor fd.\n\ |
| 190 | A zero duration sends a break for 0.25-0.5 seconds; a nonzero duration \n\ |
| 191 | has a system dependent meaning. "; |
| 192 | |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 193 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 194 | termios_tcsendbreak(PyObject *self, PyObject *args) |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 195 | { |
| 196 | int fd, duration; |
| 197 | |
| 198 | if (!PyArg_Parse(args, "(ii)", &fd, &duration)) |
| 199 | return NULL; |
| 200 | if (tcsendbreak(fd, duration) == -1) |
Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 201 | return PyErr_SetFromErrno(TermiosError); |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 202 | |
| 203 | Py_INCREF(Py_None); |
| 204 | return Py_None; |
| 205 | } |
| 206 | |
| 207 | /* tcdrain(fd) |
| 208 | Wait until all queued output to the terminal has been |
| 209 | transmitted. */ |
| 210 | |
Guido van Rossum | 1aca4d8 | 1998-03-03 22:10:18 +0000 | [diff] [blame] | 211 | static char termios_tcdrain__doc__[] = "\ |
| 212 | tcdrain(fd) -> None\n\ |
| 213 | Wait until all output written to file descriptor fd has been transmitted. "; |
| 214 | |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 215 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 216 | termios_tcdrain(PyObject *self, PyObject *args) |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 217 | { |
| 218 | int fd; |
| 219 | |
| 220 | if (!PyArg_Parse(args, "i", &fd)) |
| 221 | return NULL; |
| 222 | if (tcdrain(fd) == -1) |
Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 223 | return PyErr_SetFromErrno(TermiosError); |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 224 | |
| 225 | Py_INCREF(Py_None); |
| 226 | return Py_None; |
| 227 | } |
| 228 | |
| 229 | /* tcflush(fd, queue) |
| 230 | Clear the input and/or output queues associated with |
| 231 | the terminal. */ |
| 232 | |
Guido van Rossum | 1aca4d8 | 1998-03-03 22:10:18 +0000 | [diff] [blame] | 233 | static char termios_tcflush__doc__[] = "\ |
| 234 | tcflush(fd, queue) -> None\n\ |
| 235 | Discard queued data on file descriptor fd.\n\ |
| 236 | The queue selector specifies which queue: TERMIOS.TCIFLUSH for the input\n\ |
| 237 | queue, TERMIOS.TCOFLUSH for the output queue, or TERMIOS.TCIOFLUSH for\n\ |
| 238 | both queues. "; |
| 239 | |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 240 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 241 | termios_tcflush(PyObject *self, PyObject *args) |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 242 | { |
| 243 | int fd, queue; |
| 244 | |
| 245 | if (!PyArg_Parse(args, "(ii)", &fd, &queue)) |
| 246 | return NULL; |
| 247 | if (tcflush(fd, queue) == -1) |
Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 248 | return PyErr_SetFromErrno(TermiosError); |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 249 | |
| 250 | Py_INCREF(Py_None); |
| 251 | return Py_None; |
| 252 | } |
| 253 | |
| 254 | /* tcflow(fd, action) |
| 255 | Perform operations relating to XON/XOFF flow control on |
| 256 | the terminal. */ |
| 257 | |
Guido van Rossum | 1aca4d8 | 1998-03-03 22:10:18 +0000 | [diff] [blame] | 258 | static char termios_tcflow__doc__[] = "\ |
| 259 | tcflow(fd, action) -> None\n\ |
| 260 | Suspend or resume input or output on file descriptor fd.\n\ |
| 261 | The action argument can be TERMIOS.TCOOFF to suspend output,\n\ |
| 262 | TERMIOS.TCOON to restart output, TERMIOS.TCIOFF to suspend input,\n\ |
| 263 | or TERMIOS.TCION to restart input. "; |
| 264 | |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 265 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 266 | termios_tcflow(PyObject *self, PyObject *args) |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 267 | { |
| 268 | int fd, action; |
| 269 | |
| 270 | if (!PyArg_Parse(args, "(ii)", &fd, &action)) |
| 271 | return NULL; |
| 272 | if (tcflow(fd, action) == -1) |
Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 273 | return PyErr_SetFromErrno(TermiosError); |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 274 | |
| 275 | Py_INCREF(Py_None); |
| 276 | return Py_None; |
| 277 | } |
| 278 | |
| 279 | static PyMethodDef termios_methods[] = |
| 280 | { |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 281 | {"tcgetattr", termios_tcgetattr, |
| 282 | METH_OLDARGS, termios_tcgetattr__doc__}, |
| 283 | {"tcsetattr", termios_tcsetattr, |
| 284 | METH_OLDARGS, termios_tcsetattr__doc__}, |
| 285 | {"tcsendbreak", termios_tcsendbreak, |
| 286 | METH_OLDARGS, termios_tcsendbreak__doc__}, |
| 287 | {"tcdrain", termios_tcdrain, |
| 288 | METH_OLDARGS, termios_tcdrain__doc__}, |
| 289 | {"tcflush", termios_tcflush, |
| 290 | METH_OLDARGS, termios_tcflush__doc__}, |
| 291 | {"tcflow", termios_tcflow, |
| 292 | METH_OLDARGS, termios_tcflow__doc__}, |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 293 | {NULL, NULL} |
| 294 | }; |
| 295 | |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 296 | |
Fred Drake | 9ef7fa8 | 2001-03-01 21:54:49 +0000 | [diff] [blame] | 297 | #if defined(VSWTCH) && !defined(VSWTC) |
| 298 | #define VSWTC VSWTCH |
| 299 | #endif |
| 300 | |
| 301 | #if defined(VSWTC) && !defined(VSWTCH) |
| 302 | #define VSWTCH VSWTC |
| 303 | #endif |
| 304 | |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 305 | static struct constant { |
| 306 | char *name; |
| 307 | long value; |
| 308 | } termios_constants[] = { |
| 309 | /* cfgetospeed(), cfsetospeed() constants */ |
| 310 | {"B0", B0}, |
| 311 | {"B50", B50}, |
| 312 | {"B75", B75}, |
| 313 | {"B110", B110}, |
| 314 | {"B134", B134}, |
| 315 | {"B150", B150}, |
| 316 | {"B200", B200}, |
| 317 | {"B300", B300}, |
| 318 | {"B600", B600}, |
| 319 | {"B1200", B1200}, |
| 320 | {"B1800", B1800}, |
| 321 | {"B2400", B2400}, |
| 322 | {"B4800", B4800}, |
| 323 | {"B9600", B9600}, |
| 324 | {"B19200", B19200}, |
| 325 | {"B38400", B38400}, |
| 326 | {"B57600", B57600}, |
| 327 | {"B115200", B115200}, |
Fred Drake | abb379e | 2001-03-01 03:28:08 +0000 | [diff] [blame] | 328 | #ifdef B230400 |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 329 | {"B230400", B230400}, |
Fred Drake | abb379e | 2001-03-01 03:28:08 +0000 | [diff] [blame] | 330 | #endif |
Fred Drake | 9ef7fa8 | 2001-03-01 21:54:49 +0000 | [diff] [blame] | 331 | #ifdef CBAUDEX |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 332 | {"CBAUDEX", CBAUDEX}, |
Fred Drake | 9ef7fa8 | 2001-03-01 21:54:49 +0000 | [diff] [blame] | 333 | #endif |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 334 | |
| 335 | /* tcsetattr() constants */ |
| 336 | {"TCSANOW", TCSANOW}, |
| 337 | {"TCSADRAIN", TCSADRAIN}, |
| 338 | {"TCSAFLUSH", TCSAFLUSH}, |
| 339 | |
| 340 | /* tcflush() constants */ |
| 341 | {"TCIFLUSH", TCIFLUSH}, |
| 342 | {"TCOFLUSH", TCOFLUSH}, |
| 343 | {"TCIOFLUSH", TCIOFLUSH}, |
| 344 | |
| 345 | /* tcflow() constants */ |
| 346 | {"TCOOFF", TCOOFF}, |
| 347 | {"TCOON", TCOON}, |
| 348 | {"TCIOFF", TCIOFF}, |
| 349 | {"TCION", TCION}, |
| 350 | |
| 351 | /* struct termios.c_iflag constants */ |
| 352 | {"IGNBRK", IGNBRK}, |
| 353 | {"BRKINT", BRKINT}, |
| 354 | {"IGNPAR", IGNPAR}, |
| 355 | {"PARMRK", PARMRK}, |
| 356 | {"INPCK", INPCK}, |
| 357 | {"ISTRIP", ISTRIP}, |
| 358 | {"INLCR", INLCR}, |
| 359 | {"IGNCR", IGNCR}, |
| 360 | {"ICRNL", ICRNL}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 361 | #ifdef IUCLC |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 362 | {"IUCLC", IUCLC}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 363 | #endif |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 364 | {"IXON", IXON}, |
| 365 | {"IXANY", IXANY}, |
| 366 | {"IXOFF", IXOFF}, |
| 367 | {"IMAXBEL", IMAXBEL}, |
| 368 | |
| 369 | /* struct termios.c_oflag constants */ |
| 370 | {"OPOST", OPOST}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 371 | #ifdef OLCUC |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 372 | {"OLCUC", OLCUC}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 373 | #endif |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 374 | {"ONLCR", ONLCR}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 375 | #ifdef OCRNL |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 376 | {"OCRNL", OCRNL}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 377 | #endif |
| 378 | #ifdef ONOCR |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 379 | {"ONOCR", ONOCR}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 380 | #endif |
| 381 | #ifdef ONLRET |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 382 | {"ONLRET", ONLRET}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 383 | #endif |
| 384 | #ifdef OFILL |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 385 | {"OFILL", OFILL}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 386 | #endif |
| 387 | #ifdef OFDEL |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 388 | {"OFDEL", OFDEL}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 389 | #endif |
| 390 | #ifdef NLDLY |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 391 | {"NLDLY", NLDLY}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 392 | #endif |
| 393 | #ifdef CRDLY |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 394 | {"CRDLY", CRDLY}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 395 | #endif |
| 396 | #ifdef TABDLY |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 397 | {"TABDLY", TABDLY}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 398 | #endif |
| 399 | #ifdef BSDLY |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 400 | {"BSDLY", BSDLY}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 401 | #endif |
| 402 | #ifdef VTDLY |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 403 | {"VTDLY", VTDLY}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 404 | #endif |
| 405 | #ifdef FFDLY |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 406 | {"FFDLY", FFDLY}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 407 | #endif |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 408 | |
| 409 | /* struct termios.c_oflag-related values (delay mask) */ |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 410 | #ifdef NL0 |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 411 | {"NL0", NL0}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 412 | #endif |
| 413 | #ifdef NL1 |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 414 | {"NL1", NL1}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 415 | #endif |
| 416 | #ifdef CR0 |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 417 | {"CR0", CR0}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 418 | #endif |
| 419 | #ifdef CR1 |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 420 | {"CR1", CR1}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 421 | #endif |
| 422 | #ifdef CR2 |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 423 | {"CR2", CR2}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 424 | #endif |
| 425 | #ifdef CR3 |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 426 | {"CR3", CR3}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 427 | #endif |
| 428 | #ifdef TAB0 |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 429 | {"TAB0", TAB0}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 430 | #endif |
| 431 | #ifdef TAB1 |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 432 | {"TAB1", TAB1}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 433 | #endif |
| 434 | #ifdef TAB2 |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 435 | {"TAB2", TAB2}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 436 | #endif |
| 437 | #ifdef TAB3 |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 438 | {"TAB3", TAB3}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 439 | #endif |
Fred Drake | dedbebf | 2001-03-02 06:50:58 +0000 | [diff] [blame] | 440 | #ifdef XTABS |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 441 | {"XTABS", XTABS}, |
Fred Drake | dedbebf | 2001-03-02 06:50:58 +0000 | [diff] [blame] | 442 | #endif |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 443 | #ifdef BS0 |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 444 | {"BS0", BS0}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 445 | #endif |
| 446 | #ifdef BS1 |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 447 | {"BS1", BS1}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 448 | #endif |
| 449 | #ifdef VT0 |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 450 | {"VT0", VT0}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 451 | #endif |
| 452 | #ifdef VT1 |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 453 | {"VT1", VT1}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 454 | #endif |
| 455 | #ifdef FF0 |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 456 | {"FF0", FF0}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 457 | #endif |
| 458 | #ifdef FF1 |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 459 | {"FF1", FF1}, |
Fred Drake | d85556c | 2001-03-03 18:08:52 +0000 | [diff] [blame] | 460 | #endif |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 461 | |
| 462 | /* struct termios.c_cflag constants */ |
| 463 | {"CSIZE", CSIZE}, |
| 464 | {"CSTOPB", CSTOPB}, |
| 465 | {"CREAD", CREAD}, |
| 466 | {"PARENB", PARENB}, |
| 467 | {"PARODD", PARODD}, |
| 468 | {"HUPCL", HUPCL}, |
| 469 | {"CLOCAL", CLOCAL}, |
Fred Drake | abb379e | 2001-03-01 03:28:08 +0000 | [diff] [blame] | 470 | #ifdef CIBAUD |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 471 | {"CIBAUD", CIBAUD}, |
Fred Drake | abb379e | 2001-03-01 03:28:08 +0000 | [diff] [blame] | 472 | #endif |
Fred Drake | dedbebf | 2001-03-02 06:50:58 +0000 | [diff] [blame] | 473 | #ifdef CRTSCTS |
| 474 | {"CRTSCTS", (long)CRTSCTS}, |
| 475 | #endif |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 476 | |
| 477 | /* struct termios.c_cflag-related values (character size) */ |
| 478 | {"CS5", CS5}, |
| 479 | {"CS6", CS6}, |
| 480 | {"CS7", CS7}, |
| 481 | {"CS8", CS8}, |
| 482 | |
| 483 | /* struct termios.c_lflag constants */ |
| 484 | {"ISIG", ISIG}, |
| 485 | {"ICANON", ICANON}, |
Fred Drake | abb379e | 2001-03-01 03:28:08 +0000 | [diff] [blame] | 486 | #ifdef XCASE |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 487 | {"XCASE", XCASE}, |
Fred Drake | abb379e | 2001-03-01 03:28:08 +0000 | [diff] [blame] | 488 | #endif |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 489 | {"ECHO", ECHO}, |
| 490 | {"ECHOE", ECHOE}, |
| 491 | {"ECHOK", ECHOK}, |
| 492 | {"ECHONL", ECHONL}, |
| 493 | {"ECHOCTL", ECHOCTL}, |
Fred Drake | abb379e | 2001-03-01 03:28:08 +0000 | [diff] [blame] | 494 | #ifdef ECHOPRT |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 495 | {"ECHOPRT", ECHOPRT}, |
Fred Drake | abb379e | 2001-03-01 03:28:08 +0000 | [diff] [blame] | 496 | #endif |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 497 | {"ECHOKE", ECHOKE}, |
| 498 | {"FLUSHO", FLUSHO}, |
| 499 | {"NOFLSH", NOFLSH}, |
| 500 | {"TOSTOP", TOSTOP}, |
Fred Drake | abb379e | 2001-03-01 03:28:08 +0000 | [diff] [blame] | 501 | #ifdef PENDIN |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 502 | {"PENDIN", PENDIN}, |
Fred Drake | abb379e | 2001-03-01 03:28:08 +0000 | [diff] [blame] | 503 | #endif |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 504 | {"IEXTEN", IEXTEN}, |
| 505 | |
| 506 | /* indexes into the control chars array returned by tcgetattr() */ |
| 507 | {"VINTR", VINTR}, |
| 508 | {"VQUIT", VQUIT}, |
| 509 | {"VERASE", VERASE}, |
| 510 | {"VKILL", VKILL}, |
| 511 | {"VEOF", VEOF}, |
| 512 | {"VTIME", VTIME}, |
| 513 | {"VMIN", VMIN}, |
Fred Drake | dedbebf | 2001-03-02 06:50:58 +0000 | [diff] [blame] | 514 | #ifdef VSWTC |
| 515 | /* The #defines above ensure that if either is defined, both are, |
| 516 | * but both may be omitted by the system headers. ;-( */ |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 517 | {"VSWTC", VSWTC}, |
Fred Drake | 9ef7fa8 | 2001-03-01 21:54:49 +0000 | [diff] [blame] | 518 | {"VSWTCH", VSWTCH}, |
Fred Drake | dedbebf | 2001-03-02 06:50:58 +0000 | [diff] [blame] | 519 | #endif |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 520 | {"VSTART", VSTART}, |
| 521 | {"VSTOP", VSTOP}, |
| 522 | {"VSUSP", VSUSP}, |
| 523 | {"VEOL", VEOL}, |
| 524 | {"VREPRINT", VREPRINT}, |
| 525 | {"VDISCARD", VDISCARD}, |
| 526 | {"VWERASE", VWERASE}, |
| 527 | {"VLNEXT", VLNEXT}, |
| 528 | {"VEOL2", VEOL2}, |
| 529 | |
| 530 | /* sentinel */ |
| 531 | {NULL, 0} |
| 532 | }; |
| 533 | |
| 534 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 535 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 536 | PyInit_termios(void) |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 537 | { |
| 538 | PyObject *m, *d; |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 539 | struct constant *constant = termios_constants; |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 540 | |
Guido van Rossum | 1aca4d8 | 1998-03-03 22:10:18 +0000 | [diff] [blame] | 541 | m = Py_InitModule4("termios", termios_methods, termios__doc__, |
| 542 | (PyObject *)NULL, PYTHON_API_VERSION); |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 543 | |
| 544 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 545 | TermiosError = PyErr_NewException("termios.error", NULL, NULL); |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 546 | PyDict_SetItemString(d, "error", TermiosError); |
Fred Drake | df48d14 | 2001-02-27 21:22:39 +0000 | [diff] [blame] | 547 | |
| 548 | while (constant->name != NULL) { |
| 549 | PyModule_AddIntConstant(m, constant->name, constant->value); |
| 550 | ++constant; |
| 551 | } |
Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 552 | } |