| 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 |  | 
| 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 | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 24 | #define BAD "bad termios argument" | 
 | 25 |  | 
 | 26 | static PyObject *TermiosError; | 
 | 27 |  | 
 | 28 | /* termios = tcgetattr(fd) | 
 | 29 |    termios is | 
 | 30 |    [iflag, oflag, cflag, lflag, ispeed, ospeed, [cc[0], ..., cc[NCCS]]]  | 
 | 31 |  | 
 | 32 |    Return the attributes of the terminal device.  */ | 
 | 33 |  | 
| Guido van Rossum | 1aca4d8 | 1998-03-03 22:10:18 +0000 | [diff] [blame] | 34 | static char termios_tcgetattr__doc__[] = "\ | 
 | 35 | tcgetattr(fd) -> list_of_attrs\n\ | 
 | 36 | Get the tty attributes for file descriptor fd, as follows:\n\ | 
 | 37 | [iflag, oflag, cflag, lflag, ispeed, ospeed, cc] where cc is a list\n\ | 
 | 38 | of the tty special characters (each a string of length 1, except the items\n\ | 
 | 39 | with indices VMIN and VTIME, which are integers when these fields are\n\ | 
 | 40 | defined).  The interpretation of the flags and the speeds as well as the\n\ | 
 | 41 | indexing in the cc array must be done using the symbolic constants defined\n\ | 
 | 42 | in the TERMIOS module."; | 
 | 43 |  | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 44 | static PyObject * | 
 | 45 | termios_tcgetattr(self, args) | 
 | 46 | 	PyObject *self; | 
 | 47 | 	PyObject *args; | 
 | 48 | { | 
 | 49 | 	int fd; | 
 | 50 | 	struct termios mode; | 
 | 51 | 	PyObject *cc; | 
 | 52 | 	speed_t ispeed, ospeed; | 
 | 53 | 	PyObject *v; | 
 | 54 | 	int i; | 
 | 55 | 	char ch; | 
 | 56 |  | 
 | 57 | 	if (!PyArg_Parse(args, "i", &fd)) | 
 | 58 | 		return NULL; | 
 | 59 |  | 
 | 60 | 	if (tcgetattr(fd, &mode) == -1) | 
| Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 61 | 		return PyErr_SetFromErrno(TermiosError); | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 62 |  | 
 | 63 | 	ispeed = cfgetispeed(&mode); | 
 | 64 | 	ospeed = cfgetospeed(&mode); | 
 | 65 |  | 
 | 66 | 	cc = PyList_New(NCCS); | 
 | 67 | 	if (cc == NULL) | 
 | 68 | 		return NULL; | 
 | 69 | 	for (i = 0; i < NCCS; i++) { | 
 | 70 | 		ch = (char)mode.c_cc[i]; | 
 | 71 | 		v = PyString_FromStringAndSize(&ch, 1); | 
 | 72 | 		if (v == NULL) | 
| Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 73 | 			goto err; | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 74 | 		PyList_SetItem(cc, i, v); | 
 | 75 | 	} | 
 | 76 |  | 
 | 77 | 	/* Convert the MIN and TIME slots to integer.  On some systems, the | 
 | 78 | 	   MIN and TIME slots are the same as the EOF and EOL slots.  So we | 
 | 79 | 	   only do this in noncanonical input mode.  */ | 
| Guido van Rossum | 36dd0d2 | 1996-12-10 15:23:00 +0000 | [diff] [blame] | 80 | 	if ((mode.c_lflag & ICANON) == 0) { | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 81 | 		v = PyInt_FromLong((long)mode.c_cc[VMIN]); | 
 | 82 | 		if (v == NULL) | 
| Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 83 | 			goto err; | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 84 | 		PyList_SetItem(cc, VMIN, v); | 
 | 85 | 		v = PyInt_FromLong((long)mode.c_cc[VTIME]); | 
 | 86 | 		if (v == NULL) | 
| Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 87 | 			goto err; | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 88 | 		PyList_SetItem(cc, VTIME, v); | 
 | 89 | 	} | 
 | 90 |  | 
| Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 91 | 	if (!(v = PyList_New(7))) | 
 | 92 | 		goto err; | 
 | 93 |  | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 94 | 	PyList_SetItem(v, 0, PyInt_FromLong((long)mode.c_iflag)); | 
 | 95 | 	PyList_SetItem(v, 1, PyInt_FromLong((long)mode.c_oflag)); | 
 | 96 | 	PyList_SetItem(v, 2, PyInt_FromLong((long)mode.c_cflag)); | 
 | 97 | 	PyList_SetItem(v, 3, PyInt_FromLong((long)mode.c_lflag)); | 
 | 98 | 	PyList_SetItem(v, 4, PyInt_FromLong((long)ispeed)); | 
 | 99 | 	PyList_SetItem(v, 5, PyInt_FromLong((long)ospeed)); | 
 | 100 | 	PyList_SetItem(v, 6, cc); | 
| Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 101 | 	if (PyErr_Occurred()){ | 
 | 102 | 		Py_DECREF(v); | 
 | 103 | 		goto err; | 
 | 104 | 	} | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 105 | 	return v; | 
| Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 106 |   err: | 
 | 107 | 	Py_DECREF(cc); | 
 | 108 | 	return NULL; | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 109 | } | 
 | 110 |  | 
 | 111 | /* tcsetattr(fd, when, termios) | 
 | 112 |    Set the attributes of the terminal device.  */ | 
 | 113 |  | 
| Guido van Rossum | 1aca4d8 | 1998-03-03 22:10:18 +0000 | [diff] [blame] | 114 | static char termios_tcsetattr__doc__[] = "\ | 
 | 115 | tcsetattr(fd, when, attributes) -> None\n\ | 
 | 116 | Set the tty attributes for file descriptor fd.\n\ | 
 | 117 | The attributes to be set are taken from the attributes argument, which\n\ | 
 | 118 | is a list like the one returned by tcgetattr(). The when argument\n\ | 
 | 119 | determines when the attributes are changed: TERMIOS.TCSANOW to\n\ | 
 | 120 | change immediately, TERMIOS.TCSADRAIN to change after transmitting all\n\ | 
 | 121 | queued output, or TERMIOS.TCSAFLUSH to change after transmitting all\n\ | 
 | 122 | queued output and discarding all queued input. "; | 
 | 123 |  | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 124 | static PyObject * | 
 | 125 | termios_tcsetattr(self, args) | 
 | 126 | 	PyObject *self; | 
 | 127 | 	PyObject *args; | 
 | 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 |  | 
 | 142 | 	mode.c_iflag = (tcflag_t) PyInt_AsLong(PyList_GetItem(term, 0)); | 
 | 143 | 	mode.c_oflag = (tcflag_t) PyInt_AsLong(PyList_GetItem(term, 1)); | 
 | 144 | 	mode.c_cflag = (tcflag_t) PyInt_AsLong(PyList_GetItem(term, 2)); | 
 | 145 | 	mode.c_lflag = (tcflag_t) PyInt_AsLong(PyList_GetItem(term, 3)); | 
 | 146 | 	ispeed = (speed_t) PyInt_AsLong(PyList_GetItem(term, 4)); | 
 | 147 | 	ospeed = (speed_t) PyInt_AsLong(PyList_GetItem(term, 5)); | 
 | 148 | 	cc = PyList_GetItem(term, 6); | 
| Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 149 | 	if (PyErr_Occurred()) | 
 | 150 | 		return NULL; | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 151 |  | 
 | 152 | 	if (!PyList_Check(cc) || PyList_Size(cc) != NCCS) { | 
 | 153 | 		PyErr_SetString(PyExc_TypeError, BAD); | 
 | 154 | 		return NULL; | 
 | 155 | 	} | 
 | 156 |  | 
 | 157 | 	for (i = 0; i < NCCS; i++) { | 
 | 158 | 		v = PyList_GetItem(cc, i); | 
| Barry Warsaw | 5709dcf | 1997-01-10 18:42:18 +0000 | [diff] [blame] | 159 |  | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 160 | 		if (PyString_Check(v) && PyString_Size(v) == 1) | 
 | 161 | 			mode.c_cc[i] = (cc_t) * PyString_AsString(v); | 
 | 162 | 		else if (PyInt_Check(v)) | 
 | 163 | 			mode.c_cc[i] = (cc_t) PyInt_AsLong(v); | 
 | 164 | 		else { | 
 | 165 | 			PyErr_SetString(PyExc_TypeError, BAD); | 
 | 166 | 			return NULL; | 
 | 167 | 		} | 
 | 168 | 	} | 
 | 169 |  | 
 | 170 | 	if (cfsetispeed(&mode, (speed_t) ispeed) == -1) | 
| Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 171 | 		return PyErr_SetFromErrno(TermiosError); | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 172 | 	if (cfsetospeed(&mode, (speed_t) ospeed) == -1) | 
| Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 173 | 		return PyErr_SetFromErrno(TermiosError); | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 174 | 	if (tcsetattr(fd, when, &mode) == -1) | 
| Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 175 | 		return PyErr_SetFromErrno(TermiosError); | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 176 |  | 
 | 177 | 	Py_INCREF(Py_None); | 
 | 178 | 	return Py_None; | 
 | 179 | } | 
 | 180 |  | 
 | 181 | /* tcsendbreak(fd, duration) | 
 | 182 |    Generate a break condition.  */ | 
 | 183 |  | 
| Guido van Rossum | 1aca4d8 | 1998-03-03 22:10:18 +0000 | [diff] [blame] | 184 | static char termios_tcsendbreak__doc__[] = "\ | 
 | 185 | tcsendbreak(fd, duration) -> None\n\ | 
 | 186 | Send a break on file descriptor fd.\n\ | 
 | 187 | A zero duration sends a break for 0.25-0.5 seconds; a nonzero duration \n\ | 
 | 188 | has a system dependent meaning. "; | 
 | 189 |  | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 190 | static PyObject * | 
 | 191 | termios_tcsendbreak(self, args) | 
 | 192 | 	PyObject *self; | 
 | 193 | 	PyObject *args; | 
 | 194 | { | 
 | 195 | 	int fd, duration; | 
 | 196 |  | 
 | 197 | 	if (!PyArg_Parse(args, "(ii)", &fd, &duration)) | 
 | 198 | 		return NULL; | 
 | 199 | 	if (tcsendbreak(fd, duration) == -1) | 
| Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 200 | 		return PyErr_SetFromErrno(TermiosError); | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 201 |  | 
 | 202 | 	Py_INCREF(Py_None); | 
 | 203 | 	return Py_None; | 
 | 204 | } | 
 | 205 |  | 
 | 206 | /* tcdrain(fd)  | 
 | 207 |    Wait until all queued output to the terminal has been  | 
 | 208 |    transmitted.  */ | 
 | 209 |  | 
| Guido van Rossum | 1aca4d8 | 1998-03-03 22:10:18 +0000 | [diff] [blame] | 210 | static char termios_tcdrain__doc__[] = "\ | 
 | 211 | tcdrain(fd) -> None\n\ | 
 | 212 | Wait until all output written to file descriptor fd has been transmitted. "; | 
 | 213 |  | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 214 | static PyObject * | 
 | 215 | termios_tcdrain(self, args) | 
 | 216 | 	PyObject *self; | 
 | 217 | 	PyObject *args; | 
 | 218 | { | 
 | 219 | 	int fd; | 
 | 220 |  | 
 | 221 | 	if (!PyArg_Parse(args, "i", &fd)) | 
 | 222 | 		return NULL; | 
 | 223 | 	if (tcdrain(fd) == -1) | 
| Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 224 | 		return PyErr_SetFromErrno(TermiosError); | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 225 |  | 
 | 226 | 	Py_INCREF(Py_None); | 
 | 227 | 	return Py_None; | 
 | 228 | } | 
 | 229 |  | 
 | 230 | /* tcflush(fd, queue)  | 
 | 231 |    Clear the input and/or output queues associated with  | 
 | 232 |    the terminal.  */ | 
 | 233 |  | 
| Guido van Rossum | 1aca4d8 | 1998-03-03 22:10:18 +0000 | [diff] [blame] | 234 | static char termios_tcflush__doc__[] = "\ | 
 | 235 | tcflush(fd, queue) -> None\n\ | 
 | 236 | Discard queued data on file descriptor fd.\n\ | 
 | 237 | The queue selector specifies which queue: TERMIOS.TCIFLUSH for the input\n\ | 
 | 238 | queue, TERMIOS.TCOFLUSH for the output queue, or TERMIOS.TCIOFLUSH for\n\ | 
 | 239 | both queues. "; | 
 | 240 |  | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 241 | static PyObject * | 
 | 242 | termios_tcflush(self, args) | 
 | 243 | 	PyObject *self; | 
 | 244 | 	PyObject *args; | 
 | 245 | { | 
 | 246 | 	int fd, queue; | 
 | 247 |  | 
 | 248 | 	if (!PyArg_Parse(args, "(ii)", &fd, &queue)) | 
 | 249 | 		return NULL; | 
 | 250 | 	if (tcflush(fd, queue) == -1) | 
| Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 251 | 		return PyErr_SetFromErrno(TermiosError); | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 252 |  | 
 | 253 | 	Py_INCREF(Py_None); | 
 | 254 | 	return Py_None; | 
 | 255 | } | 
 | 256 |  | 
 | 257 | /* tcflow(fd, action)  | 
 | 258 |    Perform operations relating to XON/XOFF flow control on  | 
 | 259 |    the terminal.  */ | 
 | 260 |  | 
| Guido van Rossum | 1aca4d8 | 1998-03-03 22:10:18 +0000 | [diff] [blame] | 261 | static char termios_tcflow__doc__[] = "\ | 
 | 262 | tcflow(fd, action) -> None\n\ | 
 | 263 | Suspend or resume input or output on file descriptor fd.\n\ | 
 | 264 | The action argument can be TERMIOS.TCOOFF to suspend output,\n\ | 
 | 265 | TERMIOS.TCOON to restart output, TERMIOS.TCIOFF to suspend input,\n\ | 
 | 266 | or TERMIOS.TCION to restart input. "; | 
 | 267 |  | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 268 | static PyObject * | 
 | 269 | termios_tcflow(self, args) | 
 | 270 | 	PyObject *self; | 
 | 271 | 	PyObject *args; | 
 | 272 | { | 
 | 273 | 	int fd, action; | 
 | 274 |  | 
 | 275 | 	if (!PyArg_Parse(args, "(ii)", &fd, &action)) | 
 | 276 | 		return NULL; | 
 | 277 | 	if (tcflow(fd, action) == -1) | 
| Guido van Rossum | b8ad024 | 1997-07-17 22:55:06 +0000 | [diff] [blame] | 278 | 		return PyErr_SetFromErrno(TermiosError); | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 279 |  | 
 | 280 | 	Py_INCREF(Py_None); | 
 | 281 | 	return Py_None; | 
 | 282 | } | 
 | 283 |  | 
 | 284 | static PyMethodDef termios_methods[] = | 
 | 285 | { | 
| Guido van Rossum | 1aca4d8 | 1998-03-03 22:10:18 +0000 | [diff] [blame] | 286 | 	{"tcgetattr", termios_tcgetattr, 0, termios_tcgetattr__doc__}, | 
 | 287 | 	{"tcsetattr", termios_tcsetattr, 0, termios_tcsetattr__doc__}, | 
 | 288 | 	{"tcsendbreak", termios_tcsendbreak, 0, termios_tcsendbreak__doc__}, | 
 | 289 | 	{"tcdrain", termios_tcdrain, 0, termios_tcdrain__doc__}, | 
 | 290 | 	{"tcflush", termios_tcflush, 0, termios_tcflush__doc__}, | 
 | 291 | 	{"tcflow", termios_tcflow, 0, termios_tcflow__doc__}, | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 292 | 	{NULL, NULL} | 
 | 293 | }; | 
 | 294 |  | 
 | 295 | void | 
 | 296 | PyInit_termios() | 
 | 297 | { | 
 | 298 | 	PyObject *m, *d; | 
 | 299 |  | 
| Guido van Rossum | 1aca4d8 | 1998-03-03 22:10:18 +0000 | [diff] [blame] | 300 | 	m = Py_InitModule4("termios", termios_methods, termios__doc__, | 
 | 301 |                            (PyObject *)NULL, PYTHON_API_VERSION); | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 302 |  | 
 | 303 | 	d = PyModule_GetDict(m); | 
| Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 304 | 	TermiosError = PyErr_NewException("termios.error", NULL, NULL); | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 305 | 	PyDict_SetItemString(d, "error", TermiosError); | 
| Guido van Rossum | 9adae8e | 1994-09-12 10:41:22 +0000 | [diff] [blame] | 306 | } |