blob: d192bfc99133951cad3770c0f6b766f97971c919 [file] [log] [blame]
Guido van Rossum9adae8e1994-09-12 10:41:22 +00001/* termiosmodule.c -- POSIX terminal I/O module implementation. */
2
Guido van Rossumca6954a1999-01-14 19:31:42 +00003#include "Python.h"
Guido van Rossum9adae8e1994-09-12 10:41:22 +00004
5#define PyInit_termios inittermios
6
Guido van Rossum9adae8e1994-09-12 10:41:22 +00007#include <termios.h>
Fred Drakeb638aaf2001-05-07 17:55:35 +00008#include <termio.h>
Fred Drake9b3bc492001-04-04 21:19:26 +00009#include <sys/ioctl.h>
Guido van Rossum1aca4d81998-03-03 22:10:18 +000010
Guido van Rossumbcc20741998-08-04 22:53:56 +000011#ifdef __BEOS__
12#include <unistd.h>
13#endif
14
Fred Drakeb638aaf2001-05-07 17:55:35 +000015static char termios__doc__[] = "\
16This module provides an interface to the Posix calls for tty I/O control.\n\
17For a complete description of these calls, see the Posix or Unix manual\n\
18pages. It is only available for those Unix versions that support Posix\n\
19termios style tty I/O control.\n\
20\n\
21All functions in this module take a file descriptor fd as their first\n\
22argument. This can be an integer file descriptor, such as returned by\n\
23sys.stdin.fileno(), or a file object, such as sys.stdin itself.";
Guido van Rossum9adae8e1994-09-12 10:41:22 +000024
25static PyObject *TermiosError;
26
Fred Drakeb638aaf2001-05-07 17:55:35 +000027static char* fname;
Guido van Rossum9adae8e1994-09-12 10:41:22 +000028
Fred Drakeb638aaf2001-05-07 17:55:35 +000029static int fdconv(PyObject* obj, void* p)
30{
31 int fd;
32
33 fd = PyObject_AsFileDescriptor(obj);
34 if (fd == -1) {
35 if (PyInt_Check(obj)) {
36 fd = PyInt_AS_LONG(obj);
37 }
38 else {
39 char* tname;
40
41 if (PyInstance_Check(obj)) {
42 tname = PyString_AS_STRING(
43 ((PyInstanceObject*)obj)->in_class->cl_name);
44 }
45 else {
46 tname = obj->ob_type->tp_name;
47 }
48
49 PyErr_Format(PyExc_TypeError,
50 "%s, arg 1: can't extract file descriptor from \"%.500s\"",
51 fname, tname);
52 return 0;
53 }
54 }
55
56 *(int*)p = fd;
57 return 1;
58}
Guido van Rossum9adae8e1994-09-12 10:41:22 +000059
Guido van Rossum1aca4d81998-03-03 22:10:18 +000060static char termios_tcgetattr__doc__[] = "\
61tcgetattr(fd) -> list_of_attrs\n\
Fred Drakeb638aaf2001-05-07 17:55:35 +000062\n\
Guido van Rossum1aca4d81998-03-03 22:10:18 +000063Get the tty attributes for file descriptor fd, as follows:\n\
64[iflag, oflag, cflag, lflag, ispeed, ospeed, cc] where cc is a list\n\
65of the tty special characters (each a string of length 1, except the items\n\
66with indices VMIN and VTIME, which are integers when these fields are\n\
67defined). The interpretation of the flags and the speeds as well as the\n\
68indexing in the cc array must be done using the symbolic constants defined\n\
Fred Drake29fd0312001-04-09 19:32:52 +000069in this module.";
Guido van Rossum1aca4d81998-03-03 22:10:18 +000070
Guido van Rossum9adae8e1994-09-12 10:41:22 +000071static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +000072termios_tcgetattr(PyObject *self, PyObject *args)
Guido van Rossum9adae8e1994-09-12 10:41:22 +000073{
74 int fd;
75 struct termios mode;
76 PyObject *cc;
77 speed_t ispeed, ospeed;
78 PyObject *v;
79 int i;
80 char ch;
81
Fred Drakeb638aaf2001-05-07 17:55:35 +000082 fname = "tcgetattr";
83
84 if (!PyArg_ParseTuple(args, "O&:tcgetattr",
85 fdconv, (void*)&fd))
Guido van Rossum9adae8e1994-09-12 10:41:22 +000086 return NULL;
87
88 if (tcgetattr(fd, &mode) == -1)
Guido van Rossumb8ad0241997-07-17 22:55:06 +000089 return PyErr_SetFromErrno(TermiosError);
Guido van Rossum9adae8e1994-09-12 10:41:22 +000090
91 ispeed = cfgetispeed(&mode);
92 ospeed = cfgetospeed(&mode);
93
94 cc = PyList_New(NCCS);
95 if (cc == NULL)
96 return NULL;
97 for (i = 0; i < NCCS; i++) {
98 ch = (char)mode.c_cc[i];
99 v = PyString_FromStringAndSize(&ch, 1);
100 if (v == NULL)
Barry Warsaw5709dcf1997-01-10 18:42:18 +0000101 goto err;
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000102 PyList_SetItem(cc, i, v);
103 }
104
105 /* Convert the MIN and TIME slots to integer. On some systems, the
106 MIN and TIME slots are the same as the EOF and EOL slots. So we
107 only do this in noncanonical input mode. */
Guido van Rossum36dd0d21996-12-10 15:23:00 +0000108 if ((mode.c_lflag & ICANON) == 0) {
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000109 v = PyInt_FromLong((long)mode.c_cc[VMIN]);
110 if (v == NULL)
Barry Warsaw5709dcf1997-01-10 18:42:18 +0000111 goto err;
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000112 PyList_SetItem(cc, VMIN, v);
113 v = PyInt_FromLong((long)mode.c_cc[VTIME]);
114 if (v == NULL)
Barry Warsaw5709dcf1997-01-10 18:42:18 +0000115 goto err;
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000116 PyList_SetItem(cc, VTIME, v);
117 }
118
Barry Warsaw5709dcf1997-01-10 18:42:18 +0000119 if (!(v = PyList_New(7)))
120 goto err;
121
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000122 PyList_SetItem(v, 0, PyInt_FromLong((long)mode.c_iflag));
123 PyList_SetItem(v, 1, PyInt_FromLong((long)mode.c_oflag));
124 PyList_SetItem(v, 2, PyInt_FromLong((long)mode.c_cflag));
125 PyList_SetItem(v, 3, PyInt_FromLong((long)mode.c_lflag));
126 PyList_SetItem(v, 4, PyInt_FromLong((long)ispeed));
127 PyList_SetItem(v, 5, PyInt_FromLong((long)ospeed));
128 PyList_SetItem(v, 6, cc);
Barry Warsaw5709dcf1997-01-10 18:42:18 +0000129 if (PyErr_Occurred()){
130 Py_DECREF(v);
131 goto err;
132 }
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000133 return v;
Barry Warsaw5709dcf1997-01-10 18:42:18 +0000134 err:
135 Py_DECREF(cc);
136 return NULL;
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000137}
138
Guido van Rossum1aca4d81998-03-03 22:10:18 +0000139static char termios_tcsetattr__doc__[] = "\
140tcsetattr(fd, when, attributes) -> None\n\
Fred Drakeb638aaf2001-05-07 17:55:35 +0000141\n\
Guido van Rossum1aca4d81998-03-03 22:10:18 +0000142Set the tty attributes for file descriptor fd.\n\
143The attributes to be set are taken from the attributes argument, which\n\
144is a list like the one returned by tcgetattr(). The when argument\n\
Fred Drake29fd0312001-04-09 19:32:52 +0000145determines when the attributes are changed: termios.TCSANOW to\n\
146change immediately, termios.TCSADRAIN to change after transmitting all\n\
147queued output, or termios.TCSAFLUSH to change after transmitting all\n\
Guido van Rossum1aca4d81998-03-03 22:10:18 +0000148queued output and discarding all queued input. ";
149
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000150static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000151termios_tcsetattr(PyObject *self, PyObject *args)
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000152{
153 int fd, when;
154 struct termios mode;
155 speed_t ispeed, ospeed;
156 PyObject *term, *cc, *v;
157 int i;
158
Fred Drakeb638aaf2001-05-07 17:55:35 +0000159 fname = "tcsetattr";
160
161 if (!PyArg_ParseTuple(args, "O&iO:tcsetattr",
162 fdconv, &fd, &when, &term))
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000163 return NULL;
164 if (!PyList_Check(term) || PyList_Size(term) != 7) {
Fred Drakeb638aaf2001-05-07 17:55:35 +0000165 PyErr_SetString(PyExc_TypeError,
166 "tcsetattr, arg 3: must be 7 element list");
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000167 return NULL;
168 }
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000169
Guido van Rossume7c41931998-06-12 14:26:18 +0000170 /* Get the old mode, in case there are any hidden fields... */
171 if (tcgetattr(fd, &mode) == -1)
172 return PyErr_SetFromErrno(TermiosError);
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000173 mode.c_iflag = (tcflag_t) PyInt_AsLong(PyList_GetItem(term, 0));
174 mode.c_oflag = (tcflag_t) PyInt_AsLong(PyList_GetItem(term, 1));
175 mode.c_cflag = (tcflag_t) PyInt_AsLong(PyList_GetItem(term, 2));
176 mode.c_lflag = (tcflag_t) PyInt_AsLong(PyList_GetItem(term, 3));
177 ispeed = (speed_t) PyInt_AsLong(PyList_GetItem(term, 4));
178 ospeed = (speed_t) PyInt_AsLong(PyList_GetItem(term, 5));
179 cc = PyList_GetItem(term, 6);
Barry Warsaw5709dcf1997-01-10 18:42:18 +0000180 if (PyErr_Occurred())
181 return NULL;
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000182
183 if (!PyList_Check(cc) || PyList_Size(cc) != NCCS) {
Fred Drakeb638aaf2001-05-07 17:55:35 +0000184 PyErr_Format(PyExc_TypeError,
185 "tcsetattr: attributes[6] must be %d element list",
186 NCCS);
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000187 return NULL;
188 }
189
190 for (i = 0; i < NCCS; i++) {
191 v = PyList_GetItem(cc, i);
Barry Warsaw5709dcf1997-01-10 18:42:18 +0000192
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000193 if (PyString_Check(v) && PyString_Size(v) == 1)
194 mode.c_cc[i] = (cc_t) * PyString_AsString(v);
195 else if (PyInt_Check(v))
196 mode.c_cc[i] = (cc_t) PyInt_AsLong(v);
197 else {
Fred Drakeb638aaf2001-05-07 17:55:35 +0000198 PyErr_SetString(PyExc_TypeError,
199 "tcsetattr: elements of attributes must be characters or integers");
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000200 return NULL;
201 }
202 }
203
204 if (cfsetispeed(&mode, (speed_t) ispeed) == -1)
Guido van Rossumb8ad0241997-07-17 22:55:06 +0000205 return PyErr_SetFromErrno(TermiosError);
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000206 if (cfsetospeed(&mode, (speed_t) ospeed) == -1)
Guido van Rossumb8ad0241997-07-17 22:55:06 +0000207 return PyErr_SetFromErrno(TermiosError);
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000208 if (tcsetattr(fd, when, &mode) == -1)
Guido van Rossumb8ad0241997-07-17 22:55:06 +0000209 return PyErr_SetFromErrno(TermiosError);
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000210
211 Py_INCREF(Py_None);
212 return Py_None;
213}
214
Guido van Rossum1aca4d81998-03-03 22:10:18 +0000215static char termios_tcsendbreak__doc__[] = "\
216tcsendbreak(fd, duration) -> None\n\
Fred Drakeb638aaf2001-05-07 17:55:35 +0000217\n\
Guido van Rossum1aca4d81998-03-03 22:10:18 +0000218Send a break on file descriptor fd.\n\
Fred Drakeb638aaf2001-05-07 17:55:35 +0000219A zero duration sends a break for 0.25-0.5 seconds; a nonzero duration\n\
220has a system dependent meaning.";
Guido van Rossum1aca4d81998-03-03 22:10:18 +0000221
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000222static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000223termios_tcsendbreak(PyObject *self, PyObject *args)
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000224{
225 int fd, duration;
226
Fred Drakeb638aaf2001-05-07 17:55:35 +0000227 fname = "tcsendbreak";
228
229 if (!PyArg_ParseTuple(args, "O&i:tcsendbreak",
230 fdconv, &fd, &duration))
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000231 return NULL;
232 if (tcsendbreak(fd, duration) == -1)
Guido van Rossumb8ad0241997-07-17 22:55:06 +0000233 return PyErr_SetFromErrno(TermiosError);
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000234
235 Py_INCREF(Py_None);
236 return Py_None;
237}
238
Guido van Rossum1aca4d81998-03-03 22:10:18 +0000239static char termios_tcdrain__doc__[] = "\
240tcdrain(fd) -> None\n\
Fred Drakeb638aaf2001-05-07 17:55:35 +0000241\n\
242Wait until all output written to file descriptor fd has been transmitted.";
Guido van Rossum1aca4d81998-03-03 22:10:18 +0000243
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000244static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000245termios_tcdrain(PyObject *self, PyObject *args)
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000246{
247 int fd;
248
Fred Drakeb638aaf2001-05-07 17:55:35 +0000249 fname = "tcdrain";
250
251 if (!PyArg_ParseTuple(args, "O&:tcdrain",
252 fdconv, &fd))
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000253 return NULL;
254 if (tcdrain(fd) == -1)
Guido van Rossumb8ad0241997-07-17 22:55:06 +0000255 return PyErr_SetFromErrno(TermiosError);
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000256
257 Py_INCREF(Py_None);
258 return Py_None;
259}
260
Guido van Rossum1aca4d81998-03-03 22:10:18 +0000261static char termios_tcflush__doc__[] = "\
262tcflush(fd, queue) -> None\n\
Fred Drakeb638aaf2001-05-07 17:55:35 +0000263\n\
Guido van Rossum1aca4d81998-03-03 22:10:18 +0000264Discard queued data on file descriptor fd.\n\
Fred Drake29fd0312001-04-09 19:32:52 +0000265The queue selector specifies which queue: termios.TCIFLUSH for the input\n\
266queue, termios.TCOFLUSH for the output queue, or termios.TCIOFLUSH for\n\
Guido van Rossum1aca4d81998-03-03 22:10:18 +0000267both queues. ";
268
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000269static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000270termios_tcflush(PyObject *self, PyObject *args)
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000271{
272 int fd, queue;
273
Fred Drakeb638aaf2001-05-07 17:55:35 +0000274 fname = "tcflush";
275
276 if (!PyArg_ParseTuple(args, "O&i:tcflush",
277 fdconv, &fd, &queue))
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000278 return NULL;
279 if (tcflush(fd, queue) == -1)
Guido van Rossumb8ad0241997-07-17 22:55:06 +0000280 return PyErr_SetFromErrno(TermiosError);
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000281
282 Py_INCREF(Py_None);
283 return Py_None;
284}
285
Guido van Rossum1aca4d81998-03-03 22:10:18 +0000286static char termios_tcflow__doc__[] = "\
287tcflow(fd, action) -> None\n\
Fred Drakeb638aaf2001-05-07 17:55:35 +0000288\n\
Guido van Rossum1aca4d81998-03-03 22:10:18 +0000289Suspend or resume input or output on file descriptor fd.\n\
Fred Drake29fd0312001-04-09 19:32:52 +0000290The action argument can be termios.TCOOFF to suspend output,\n\
291termios.TCOON to restart output, termios.TCIOFF to suspend input,\n\
292or termios.TCION to restart input.";
Guido van Rossum1aca4d81998-03-03 22:10:18 +0000293
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000294static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000295termios_tcflow(PyObject *self, PyObject *args)
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000296{
297 int fd, action;
298
Fred Drakeb638aaf2001-05-07 17:55:35 +0000299 fname = "tcflow";
300
301 if (!PyArg_ParseTuple(args, "O&i:tcflow",
302 fdconv, &fd, &action))
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000303 return NULL;
304 if (tcflow(fd, action) == -1)
Guido van Rossumb8ad0241997-07-17 22:55:06 +0000305 return PyErr_SetFromErrno(TermiosError);
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000306
307 Py_INCREF(Py_None);
308 return Py_None;
309}
310
311static PyMethodDef termios_methods[] =
312{
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000313 {"tcgetattr", termios_tcgetattr,
Fred Drakeb638aaf2001-05-07 17:55:35 +0000314 METH_VARARGS, termios_tcgetattr__doc__},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000315 {"tcsetattr", termios_tcsetattr,
Fred Drakeb638aaf2001-05-07 17:55:35 +0000316 METH_VARARGS, termios_tcsetattr__doc__},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000317 {"tcsendbreak", termios_tcsendbreak,
Fred Drakeb638aaf2001-05-07 17:55:35 +0000318 METH_VARARGS, termios_tcsendbreak__doc__},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000319 {"tcdrain", termios_tcdrain,
Fred Drakeb638aaf2001-05-07 17:55:35 +0000320 METH_VARARGS, termios_tcdrain__doc__},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000321 {"tcflush", termios_tcflush,
Fred Drakeb638aaf2001-05-07 17:55:35 +0000322 METH_VARARGS, termios_tcflush__doc__},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000323 {"tcflow", termios_tcflow,
Fred Drakeb638aaf2001-05-07 17:55:35 +0000324 METH_VARARGS, termios_tcflow__doc__},
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000325 {NULL, NULL}
326};
327
Fred Drakedf48d142001-02-27 21:22:39 +0000328
Fred Drake9ef7fa82001-03-01 21:54:49 +0000329#if defined(VSWTCH) && !defined(VSWTC)
330#define VSWTC VSWTCH
331#endif
332
333#if defined(VSWTC) && !defined(VSWTCH)
334#define VSWTCH VSWTC
335#endif
336
Fred Drakedf48d142001-02-27 21:22:39 +0000337static struct constant {
338 char *name;
339 long value;
340} termios_constants[] = {
341 /* cfgetospeed(), cfsetospeed() constants */
342 {"B0", B0},
343 {"B50", B50},
344 {"B75", B75},
345 {"B110", B110},
346 {"B134", B134},
347 {"B150", B150},
348 {"B200", B200},
349 {"B300", B300},
350 {"B600", B600},
351 {"B1200", B1200},
352 {"B1800", B1800},
353 {"B2400", B2400},
354 {"B4800", B4800},
355 {"B9600", B9600},
356 {"B19200", B19200},
357 {"B38400", B38400},
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000358#ifdef B57600
Fred Drakedf48d142001-02-27 21:22:39 +0000359 {"B57600", B57600},
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000360#endif
361#ifdef B115200
Fred Drakedf48d142001-02-27 21:22:39 +0000362 {"B115200", B115200},
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000363#endif
Fred Drakeabb379e2001-03-01 03:28:08 +0000364#ifdef B230400
Fred Drakedf48d142001-02-27 21:22:39 +0000365 {"B230400", B230400},
Fred Drakeabb379e2001-03-01 03:28:08 +0000366#endif
Fred Drake9ef7fa82001-03-01 21:54:49 +0000367#ifdef CBAUDEX
Fred Drakedf48d142001-02-27 21:22:39 +0000368 {"CBAUDEX", CBAUDEX},
Fred Drake9ef7fa82001-03-01 21:54:49 +0000369#endif
Fred Drakedf48d142001-02-27 21:22:39 +0000370
371 /* tcsetattr() constants */
372 {"TCSANOW", TCSANOW},
373 {"TCSADRAIN", TCSADRAIN},
374 {"TCSAFLUSH", TCSAFLUSH},
375
376 /* tcflush() constants */
377 {"TCIFLUSH", TCIFLUSH},
378 {"TCOFLUSH", TCOFLUSH},
379 {"TCIOFLUSH", TCIOFLUSH},
380
381 /* tcflow() constants */
382 {"TCOOFF", TCOOFF},
383 {"TCOON", TCOON},
384 {"TCIOFF", TCIOFF},
385 {"TCION", TCION},
386
387 /* struct termios.c_iflag constants */
388 {"IGNBRK", IGNBRK},
389 {"BRKINT", BRKINT},
390 {"IGNPAR", IGNPAR},
391 {"PARMRK", PARMRK},
392 {"INPCK", INPCK},
393 {"ISTRIP", ISTRIP},
394 {"INLCR", INLCR},
395 {"IGNCR", IGNCR},
396 {"ICRNL", ICRNL},
Fred Draked85556c2001-03-03 18:08:52 +0000397#ifdef IUCLC
Fred Drakedf48d142001-02-27 21:22:39 +0000398 {"IUCLC", IUCLC},
Fred Draked85556c2001-03-03 18:08:52 +0000399#endif
Fred Drakedf48d142001-02-27 21:22:39 +0000400 {"IXON", IXON},
401 {"IXANY", IXANY},
402 {"IXOFF", IXOFF},
403 {"IMAXBEL", IMAXBEL},
404
405 /* struct termios.c_oflag constants */
406 {"OPOST", OPOST},
Fred Draked85556c2001-03-03 18:08:52 +0000407#ifdef OLCUC
Fred Drakedf48d142001-02-27 21:22:39 +0000408 {"OLCUC", OLCUC},
Fred Draked85556c2001-03-03 18:08:52 +0000409#endif
Fred Drakedf48d142001-02-27 21:22:39 +0000410 {"ONLCR", ONLCR},
Fred Draked85556c2001-03-03 18:08:52 +0000411#ifdef OCRNL
Fred Drakedf48d142001-02-27 21:22:39 +0000412 {"OCRNL", OCRNL},
Fred Draked85556c2001-03-03 18:08:52 +0000413#endif
414#ifdef ONOCR
Fred Drakedf48d142001-02-27 21:22:39 +0000415 {"ONOCR", ONOCR},
Fred Draked85556c2001-03-03 18:08:52 +0000416#endif
417#ifdef ONLRET
Fred Drakedf48d142001-02-27 21:22:39 +0000418 {"ONLRET", ONLRET},
Fred Draked85556c2001-03-03 18:08:52 +0000419#endif
420#ifdef OFILL
Fred Drakedf48d142001-02-27 21:22:39 +0000421 {"OFILL", OFILL},
Fred Draked85556c2001-03-03 18:08:52 +0000422#endif
423#ifdef OFDEL
Fred Drakedf48d142001-02-27 21:22:39 +0000424 {"OFDEL", OFDEL},
Fred Draked85556c2001-03-03 18:08:52 +0000425#endif
426#ifdef NLDLY
Fred Drakedf48d142001-02-27 21:22:39 +0000427 {"NLDLY", NLDLY},
Fred Draked85556c2001-03-03 18:08:52 +0000428#endif
429#ifdef CRDLY
Fred Drakedf48d142001-02-27 21:22:39 +0000430 {"CRDLY", CRDLY},
Fred Draked85556c2001-03-03 18:08:52 +0000431#endif
432#ifdef TABDLY
Fred Drakedf48d142001-02-27 21:22:39 +0000433 {"TABDLY", TABDLY},
Fred Draked85556c2001-03-03 18:08:52 +0000434#endif
435#ifdef BSDLY
Fred Drakedf48d142001-02-27 21:22:39 +0000436 {"BSDLY", BSDLY},
Fred Draked85556c2001-03-03 18:08:52 +0000437#endif
438#ifdef VTDLY
Fred Drakedf48d142001-02-27 21:22:39 +0000439 {"VTDLY", VTDLY},
Fred Draked85556c2001-03-03 18:08:52 +0000440#endif
441#ifdef FFDLY
Fred Drakedf48d142001-02-27 21:22:39 +0000442 {"FFDLY", FFDLY},
Fred Draked85556c2001-03-03 18:08:52 +0000443#endif
Fred Drakedf48d142001-02-27 21:22:39 +0000444
445 /* struct termios.c_oflag-related values (delay mask) */
Fred Draked85556c2001-03-03 18:08:52 +0000446#ifdef NL0
Fred Drakedf48d142001-02-27 21:22:39 +0000447 {"NL0", NL0},
Fred Draked85556c2001-03-03 18:08:52 +0000448#endif
449#ifdef NL1
Fred Drakedf48d142001-02-27 21:22:39 +0000450 {"NL1", NL1},
Fred Draked85556c2001-03-03 18:08:52 +0000451#endif
452#ifdef CR0
Fred Drakedf48d142001-02-27 21:22:39 +0000453 {"CR0", CR0},
Fred Draked85556c2001-03-03 18:08:52 +0000454#endif
455#ifdef CR1
Fred Drakedf48d142001-02-27 21:22:39 +0000456 {"CR1", CR1},
Fred Draked85556c2001-03-03 18:08:52 +0000457#endif
458#ifdef CR2
Fred Drakedf48d142001-02-27 21:22:39 +0000459 {"CR2", CR2},
Fred Draked85556c2001-03-03 18:08:52 +0000460#endif
461#ifdef CR3
Fred Drakedf48d142001-02-27 21:22:39 +0000462 {"CR3", CR3},
Fred Draked85556c2001-03-03 18:08:52 +0000463#endif
464#ifdef TAB0
Fred Drakedf48d142001-02-27 21:22:39 +0000465 {"TAB0", TAB0},
Fred Draked85556c2001-03-03 18:08:52 +0000466#endif
467#ifdef TAB1
Fred Drakedf48d142001-02-27 21:22:39 +0000468 {"TAB1", TAB1},
Fred Draked85556c2001-03-03 18:08:52 +0000469#endif
470#ifdef TAB2
Fred Drakedf48d142001-02-27 21:22:39 +0000471 {"TAB2", TAB2},
Fred Draked85556c2001-03-03 18:08:52 +0000472#endif
473#ifdef TAB3
Fred Drakedf48d142001-02-27 21:22:39 +0000474 {"TAB3", TAB3},
Fred Draked85556c2001-03-03 18:08:52 +0000475#endif
Fred Drakededbebf2001-03-02 06:50:58 +0000476#ifdef XTABS
Fred Drakedf48d142001-02-27 21:22:39 +0000477 {"XTABS", XTABS},
Fred Drakededbebf2001-03-02 06:50:58 +0000478#endif
Fred Draked85556c2001-03-03 18:08:52 +0000479#ifdef BS0
Fred Drakedf48d142001-02-27 21:22:39 +0000480 {"BS0", BS0},
Fred Draked85556c2001-03-03 18:08:52 +0000481#endif
482#ifdef BS1
Fred Drakedf48d142001-02-27 21:22:39 +0000483 {"BS1", BS1},
Fred Draked85556c2001-03-03 18:08:52 +0000484#endif
485#ifdef VT0
Fred Drakedf48d142001-02-27 21:22:39 +0000486 {"VT0", VT0},
Fred Draked85556c2001-03-03 18:08:52 +0000487#endif
488#ifdef VT1
Fred Drakedf48d142001-02-27 21:22:39 +0000489 {"VT1", VT1},
Fred Draked85556c2001-03-03 18:08:52 +0000490#endif
491#ifdef FF0
Fred Drakedf48d142001-02-27 21:22:39 +0000492 {"FF0", FF0},
Fred Draked85556c2001-03-03 18:08:52 +0000493#endif
494#ifdef FF1
Fred Drakedf48d142001-02-27 21:22:39 +0000495 {"FF1", FF1},
Fred Draked85556c2001-03-03 18:08:52 +0000496#endif
Fred Drakedf48d142001-02-27 21:22:39 +0000497
498 /* struct termios.c_cflag constants */
499 {"CSIZE", CSIZE},
500 {"CSTOPB", CSTOPB},
501 {"CREAD", CREAD},
502 {"PARENB", PARENB},
503 {"PARODD", PARODD},
504 {"HUPCL", HUPCL},
505 {"CLOCAL", CLOCAL},
Fred Drakeabb379e2001-03-01 03:28:08 +0000506#ifdef CIBAUD
Fred Drakedf48d142001-02-27 21:22:39 +0000507 {"CIBAUD", CIBAUD},
Fred Drakeabb379e2001-03-01 03:28:08 +0000508#endif
Fred Drakededbebf2001-03-02 06:50:58 +0000509#ifdef CRTSCTS
510 {"CRTSCTS", (long)CRTSCTS},
511#endif
Fred Drakedf48d142001-02-27 21:22:39 +0000512
513 /* struct termios.c_cflag-related values (character size) */
514 {"CS5", CS5},
515 {"CS6", CS6},
516 {"CS7", CS7},
517 {"CS8", CS8},
518
519 /* struct termios.c_lflag constants */
520 {"ISIG", ISIG},
521 {"ICANON", ICANON},
Fred Drakeabb379e2001-03-01 03:28:08 +0000522#ifdef XCASE
Fred Drakedf48d142001-02-27 21:22:39 +0000523 {"XCASE", XCASE},
Fred Drakeabb379e2001-03-01 03:28:08 +0000524#endif
Fred Drakedf48d142001-02-27 21:22:39 +0000525 {"ECHO", ECHO},
526 {"ECHOE", ECHOE},
527 {"ECHOK", ECHOK},
528 {"ECHONL", ECHONL},
529 {"ECHOCTL", ECHOCTL},
Fred Drakeabb379e2001-03-01 03:28:08 +0000530#ifdef ECHOPRT
Fred Drakedf48d142001-02-27 21:22:39 +0000531 {"ECHOPRT", ECHOPRT},
Fred Drakeabb379e2001-03-01 03:28:08 +0000532#endif
Fred Drakedf48d142001-02-27 21:22:39 +0000533 {"ECHOKE", ECHOKE},
534 {"FLUSHO", FLUSHO},
535 {"NOFLSH", NOFLSH},
536 {"TOSTOP", TOSTOP},
Fred Drakeabb379e2001-03-01 03:28:08 +0000537#ifdef PENDIN
Fred Drakedf48d142001-02-27 21:22:39 +0000538 {"PENDIN", PENDIN},
Fred Drakeabb379e2001-03-01 03:28:08 +0000539#endif
Fred Drakedf48d142001-02-27 21:22:39 +0000540 {"IEXTEN", IEXTEN},
541
542 /* indexes into the control chars array returned by tcgetattr() */
543 {"VINTR", VINTR},
544 {"VQUIT", VQUIT},
545 {"VERASE", VERASE},
546 {"VKILL", VKILL},
547 {"VEOF", VEOF},
548 {"VTIME", VTIME},
549 {"VMIN", VMIN},
Fred Drakededbebf2001-03-02 06:50:58 +0000550#ifdef VSWTC
551 /* The #defines above ensure that if either is defined, both are,
552 * but both may be omitted by the system headers. ;-( */
Fred Drakedf48d142001-02-27 21:22:39 +0000553 {"VSWTC", VSWTC},
Fred Drake9ef7fa82001-03-01 21:54:49 +0000554 {"VSWTCH", VSWTCH},
Fred Drakededbebf2001-03-02 06:50:58 +0000555#endif
Fred Drakedf48d142001-02-27 21:22:39 +0000556 {"VSTART", VSTART},
557 {"VSTOP", VSTOP},
558 {"VSUSP", VSUSP},
559 {"VEOL", VEOL},
560 {"VREPRINT", VREPRINT},
561 {"VDISCARD", VDISCARD},
562 {"VWERASE", VWERASE},
563 {"VLNEXT", VLNEXT},
564 {"VEOL2", VEOL2},
565
Fred Drake55a00342001-03-26 17:14:02 +0000566
567#ifdef B460800
568 {"B460800", B460800},
569#endif
570#ifdef CBAUD
571 {"CBAUD", CBAUD},
572#endif
573#ifdef CDEL
574 {"CDEL", CDEL},
575#endif
576#ifdef CDSUSP
577 {"CDSUSP", CDSUSP},
578#endif
579#ifdef CEOF
580 {"CEOF", CEOF},
581#endif
582#ifdef CEOL
583 {"CEOL", CEOL},
584#endif
585#ifdef CEOL2
586 {"CEOL2", CEOL2},
587#endif
588#ifdef CEOT
589 {"CEOT", CEOT},
590#endif
591#ifdef CERASE
592 {"CERASE", CERASE},
593#endif
594#ifdef CESC
595 {"CESC", CESC},
596#endif
597#ifdef CFLUSH
598 {"CFLUSH", CFLUSH},
599#endif
600#ifdef CINTR
601 {"CINTR", CINTR},
602#endif
603#ifdef CKILL
604 {"CKILL", CKILL},
605#endif
606#ifdef CLNEXT
607 {"CLNEXT", CLNEXT},
608#endif
609#ifdef CNUL
610 {"CNUL", CNUL},
611#endif
612#ifdef COMMON
613 {"COMMON", COMMON},
614#endif
615#ifdef CQUIT
616 {"CQUIT", CQUIT},
617#endif
618#ifdef CRPRNT
619 {"CRPRNT", CRPRNT},
620#endif
621#ifdef CSTART
622 {"CSTART", CSTART},
623#endif
624#ifdef CSTOP
625 {"CSTOP", CSTOP},
626#endif
627#ifdef CSUSP
628 {"CSUSP", CSUSP},
629#endif
630#ifdef CSWTCH
631 {"CSWTCH", CSWTCH},
632#endif
633#ifdef CWERASE
634 {"CWERASE", CWERASE},
635#endif
636#ifdef EXTA
637 {"EXTA", EXTA},
638#endif
639#ifdef EXTB
640 {"EXTB", EXTB},
641#endif
642#ifdef FIOASYNC
643 {"FIOASYNC", FIOASYNC},
644#endif
645#ifdef FIOCLEX
646 {"FIOCLEX", FIOCLEX},
647#endif
648#ifdef FIONBIO
649 {"FIONBIO", FIONBIO},
650#endif
651#ifdef FIONCLEX
652 {"FIONCLEX", FIONCLEX},
653#endif
654#ifdef FIONREAD
655 {"FIONREAD", FIONREAD},
656#endif
657#ifdef IBSHIFT
658 {"IBSHIFT", IBSHIFT},
659#endif
660#ifdef INIT_C_CC
661 {"INIT_C_CC", INIT_C_CC},
662#endif
663#ifdef IOCSIZE_MASK
664 {"IOCSIZE_MASK", IOCSIZE_MASK},
665#endif
666#ifdef IOCSIZE_SHIFT
667 {"IOCSIZE_SHIFT", IOCSIZE_SHIFT},
668#endif
669#ifdef NCC
670 {"NCC", NCC},
671#endif
672#ifdef NCCS
673 {"NCCS", NCCS},
674#endif
675#ifdef NSWTCH
676 {"NSWTCH", NSWTCH},
677#endif
678#ifdef N_MOUSE
679 {"N_MOUSE", N_MOUSE},
680#endif
681#ifdef N_PPP
682 {"N_PPP", N_PPP},
683#endif
684#ifdef N_SLIP
685 {"N_SLIP", N_SLIP},
686#endif
687#ifdef N_STRIP
688 {"N_STRIP", N_STRIP},
689#endif
690#ifdef N_TTY
691 {"N_TTY", N_TTY},
692#endif
693#ifdef TCFLSH
694 {"TCFLSH", TCFLSH},
695#endif
696#ifdef TCGETA
697 {"TCGETA", TCGETA},
698#endif
699#ifdef TCGETS
700 {"TCGETS", TCGETS},
701#endif
702#ifdef TCSBRK
703 {"TCSBRK", TCSBRK},
704#endif
705#ifdef TCSBRKP
706 {"TCSBRKP", TCSBRKP},
707#endif
708#ifdef TCSETA
709 {"TCSETA", TCSETA},
710#endif
711#ifdef TCSETAF
712 {"TCSETAF", TCSETAF},
713#endif
714#ifdef TCSETAW
715 {"TCSETAW", TCSETAW},
716#endif
717#ifdef TCSETS
718 {"TCSETS", TCSETS},
719#endif
720#ifdef TCSETSF
721 {"TCSETSF", TCSETSF},
722#endif
723#ifdef TCSETSW
724 {"TCSETSW", TCSETSW},
725#endif
726#ifdef TCXONC
727 {"TCXONC", TCXONC},
728#endif
729#ifdef TIOCCONS
730 {"TIOCCONS", TIOCCONS},
731#endif
732#ifdef TIOCEXCL
733 {"TIOCEXCL", TIOCEXCL},
734#endif
735#ifdef TIOCGETD
736 {"TIOCGETD", TIOCGETD},
737#endif
738#ifdef TIOCGICOUNT
739 {"TIOCGICOUNT", TIOCGICOUNT},
740#endif
741#ifdef TIOCGLCKTRMIOS
742 {"TIOCGLCKTRMIOS", TIOCGLCKTRMIOS},
743#endif
744#ifdef TIOCGPGRP
745 {"TIOCGPGRP", TIOCGPGRP},
746#endif
747#ifdef TIOCGSERIAL
748 {"TIOCGSERIAL", TIOCGSERIAL},
749#endif
750#ifdef TIOCGSOFTCAR
751 {"TIOCGSOFTCAR", TIOCGSOFTCAR},
752#endif
753#ifdef TIOCGWINSZ
754 {"TIOCGWINSZ", TIOCGWINSZ},
755#endif
756#ifdef TIOCINQ
757 {"TIOCINQ", TIOCINQ},
758#endif
759#ifdef TIOCLINUX
760 {"TIOCLINUX", TIOCLINUX},
761#endif
762#ifdef TIOCMBIC
763 {"TIOCMBIC", TIOCMBIC},
764#endif
765#ifdef TIOCMBIS
766 {"TIOCMBIS", TIOCMBIS},
767#endif
768#ifdef TIOCMGET
769 {"TIOCMGET", TIOCMGET},
770#endif
771#ifdef TIOCMIWAIT
772 {"TIOCMIWAIT", TIOCMIWAIT},
773#endif
774#ifdef TIOCMSET
775 {"TIOCMSET", TIOCMSET},
776#endif
777#ifdef TIOCM_CAR
778 {"TIOCM_CAR", TIOCM_CAR},
779#endif
780#ifdef TIOCM_CD
781 {"TIOCM_CD", TIOCM_CD},
782#endif
783#ifdef TIOCM_CTS
784 {"TIOCM_CTS", TIOCM_CTS},
785#endif
786#ifdef TIOCM_DSR
787 {"TIOCM_DSR", TIOCM_DSR},
788#endif
789#ifdef TIOCM_DTR
790 {"TIOCM_DTR", TIOCM_DTR},
791#endif
792#ifdef TIOCM_LE
793 {"TIOCM_LE", TIOCM_LE},
794#endif
795#ifdef TIOCM_RI
796 {"TIOCM_RI", TIOCM_RI},
797#endif
798#ifdef TIOCM_RNG
799 {"TIOCM_RNG", TIOCM_RNG},
800#endif
801#ifdef TIOCM_RTS
802 {"TIOCM_RTS", TIOCM_RTS},
803#endif
804#ifdef TIOCM_SR
805 {"TIOCM_SR", TIOCM_SR},
806#endif
807#ifdef TIOCM_ST
808 {"TIOCM_ST", TIOCM_ST},
809#endif
810#ifdef TIOCNOTTY
811 {"TIOCNOTTY", TIOCNOTTY},
812#endif
813#ifdef TIOCNXCL
814 {"TIOCNXCL", TIOCNXCL},
815#endif
816#ifdef TIOCOUTQ
817 {"TIOCOUTQ", TIOCOUTQ},
818#endif
819#ifdef TIOCPKT
820 {"TIOCPKT", TIOCPKT},
821#endif
822#ifdef TIOCPKT_DATA
823 {"TIOCPKT_DATA", TIOCPKT_DATA},
824#endif
825#ifdef TIOCPKT_DOSTOP
826 {"TIOCPKT_DOSTOP", TIOCPKT_DOSTOP},
827#endif
828#ifdef TIOCPKT_FLUSHREAD
829 {"TIOCPKT_FLUSHREAD", TIOCPKT_FLUSHREAD},
830#endif
831#ifdef TIOCPKT_FLUSHWRITE
832 {"TIOCPKT_FLUSHWRITE", TIOCPKT_FLUSHWRITE},
833#endif
834#ifdef TIOCPKT_NOSTOP
835 {"TIOCPKT_NOSTOP", TIOCPKT_NOSTOP},
836#endif
837#ifdef TIOCPKT_START
838 {"TIOCPKT_START", TIOCPKT_START},
839#endif
840#ifdef TIOCPKT_STOP
841 {"TIOCPKT_STOP", TIOCPKT_STOP},
842#endif
843#ifdef TIOCSCTTY
844 {"TIOCSCTTY", TIOCSCTTY},
845#endif
846#ifdef TIOCSERCONFIG
847 {"TIOCSERCONFIG", TIOCSERCONFIG},
848#endif
849#ifdef TIOCSERGETLSR
850 {"TIOCSERGETLSR", TIOCSERGETLSR},
851#endif
852#ifdef TIOCSERGETMULTI
853 {"TIOCSERGETMULTI", TIOCSERGETMULTI},
854#endif
855#ifdef TIOCSERGSTRUCT
856 {"TIOCSERGSTRUCT", TIOCSERGSTRUCT},
857#endif
858#ifdef TIOCSERGWILD
859 {"TIOCSERGWILD", TIOCSERGWILD},
860#endif
861#ifdef TIOCSERSETMULTI
862 {"TIOCSERSETMULTI", TIOCSERSETMULTI},
863#endif
864#ifdef TIOCSERSWILD
865 {"TIOCSERSWILD", TIOCSERSWILD},
866#endif
867#ifdef TIOCSER_TEMT
868 {"TIOCSER_TEMT", TIOCSER_TEMT},
869#endif
870#ifdef TIOCSETD
871 {"TIOCSETD", TIOCSETD},
872#endif
873#ifdef TIOCSLCKTRMIOS
874 {"TIOCSLCKTRMIOS", TIOCSLCKTRMIOS},
875#endif
876#ifdef TIOCSPGRP
877 {"TIOCSPGRP", TIOCSPGRP},
878#endif
879#ifdef TIOCSSERIAL
880 {"TIOCSSERIAL", TIOCSSERIAL},
881#endif
882#ifdef TIOCSSOFTCAR
883 {"TIOCSSOFTCAR", TIOCSSOFTCAR},
884#endif
885#ifdef TIOCSTI
886 {"TIOCSTI", TIOCSTI},
887#endif
888#ifdef TIOCSWINSZ
889 {"TIOCSWINSZ", TIOCSWINSZ},
890#endif
891#ifdef TIOCTTYGSTRUCT
892 {"TIOCTTYGSTRUCT", TIOCTTYGSTRUCT},
893#endif
894
Fred Drakedf48d142001-02-27 21:22:39 +0000895 /* sentinel */
896 {NULL, 0}
897};
898
899
Guido van Rossum3886bb61998-12-04 18:50:17 +0000900DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000901PyInit_termios(void)
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000902{
903 PyObject *m, *d;
Fred Drakedf48d142001-02-27 21:22:39 +0000904 struct constant *constant = termios_constants;
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000905
Guido van Rossum1aca4d81998-03-03 22:10:18 +0000906 m = Py_InitModule4("termios", termios_methods, termios__doc__,
907 (PyObject *)NULL, PYTHON_API_VERSION);
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000908
909 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000910 TermiosError = PyErr_NewException("termios.error", NULL, NULL);
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000911 PyDict_SetItemString(d, "error", TermiosError);
Fred Drakedf48d142001-02-27 21:22:39 +0000912
913 while (constant->name != NULL) {
914 PyModule_AddIntConstant(m, constant->name, constant->value);
915 ++constant;
916 }
Guido van Rossum9adae8e1994-09-12 10:41:22 +0000917}