Jack Jansen | 1dcbcc3 | 1995-11-14 11:34:17 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
| 25 | #include "Python.h" |
| 26 | #include "InterslipLib.h" |
| 27 | #include "macglue.h" |
| 28 | |
| 29 | static PyObject *ErrorObject; |
| 30 | |
| 31 | /* ----------------------------------------------------- */ |
| 32 | |
| 33 | static char pyis_open__doc__[] = |
| 34 | "Load the interslip driver (optional)" |
| 35 | ; |
| 36 | |
| 37 | static PyObject * |
| 38 | pyis_open(self, args) |
| 39 | PyObject *self; /* Not used */ |
| 40 | PyObject *args; |
| 41 | { |
| 42 | OSErr err; |
| 43 | |
| 44 | if (!PyArg_ParseTuple(args, "")) |
| 45 | return NULL; |
| 46 | err = is_open(); |
| 47 | if ( err ) { |
| 48 | PyErr_Mac(ErrorObject, err); |
| 49 | return NULL; |
| 50 | } |
| 51 | Py_INCREF(Py_None); |
| 52 | return Py_None; |
| 53 | } |
| 54 | |
| 55 | static char pyis_connect__doc__[] = |
| 56 | "Tell the driver to start a connect" |
| 57 | ; |
| 58 | |
| 59 | static PyObject * |
| 60 | pyis_connect(self, args) |
| 61 | PyObject *self; /* Not used */ |
| 62 | PyObject *args; |
| 63 | { |
| 64 | OSErr err; |
| 65 | |
| 66 | if (!PyArg_ParseTuple(args, "")) |
| 67 | return NULL; |
| 68 | err = is_connect(); |
| 69 | if ( err ) { |
| 70 | PyErr_Mac(ErrorObject, err); |
| 71 | return NULL; |
| 72 | } |
| 73 | Py_INCREF(Py_None); |
| 74 | return Py_None; |
| 75 | } |
| 76 | |
| 77 | static char pyis_disconnect__doc__[] = |
| 78 | "Tell the interslip driver to start a disconnect" |
| 79 | ; |
| 80 | |
| 81 | static PyObject * |
| 82 | pyis_disconnect(self, args) |
| 83 | PyObject *self; /* Not used */ |
| 84 | PyObject *args; |
| 85 | { |
| 86 | OSErr err; |
| 87 | |
| 88 | if (!PyArg_ParseTuple(args, "")) |
| 89 | return NULL; |
| 90 | err = is_disconnect(); |
| 91 | if ( err ) { |
| 92 | PyErr_Mac(ErrorObject, err); |
| 93 | return NULL; |
| 94 | } |
| 95 | Py_INCREF(Py_None); |
| 96 | return Py_None; |
| 97 | } |
| 98 | |
| 99 | static char pyis_status__doc__[] = |
| 100 | "Return (numeric_status, message_seqnum, message_string) status tuple" |
| 101 | ; |
| 102 | |
| 103 | static PyObject * |
| 104 | pyis_status(self, args) |
| 105 | PyObject *self; /* Not used */ |
| 106 | PyObject *args; |
| 107 | { |
| 108 | long status, seqnum; |
| 109 | StringPtr message; |
| 110 | OSErr err; |
| 111 | |
| 112 | if (!PyArg_ParseTuple(args, "")) |
| 113 | return NULL; |
| 114 | err = is_status(&status, &seqnum, &message); |
| 115 | if ( err ) { |
| 116 | PyErr_Mac(ErrorObject, err); |
| 117 | return NULL; |
| 118 | } |
| 119 | return Py_BuildValue("iiO&", (int)status, (int)seqnum, PyMac_BuildStr255, message); |
| 120 | } |
| 121 | |
| 122 | static char pyis_getconfig__doc__[] = |
| 123 | "Return configuration data (ibaud, obaud, flags, idrvname, odrvname, cfgname)" |
| 124 | ; |
| 125 | |
| 126 | static PyObject * |
| 127 | pyis_getconfig(self, args) |
| 128 | PyObject *self; /* Not used */ |
| 129 | PyObject *args; |
| 130 | { |
| 131 | long baudrate, flags; |
| 132 | Str255 idrvname, odrvname, cfgname; |
| 133 | OSErr err; |
| 134 | int ibaud, obaud; |
| 135 | |
| 136 | if (!PyArg_ParseTuple(args, "")) |
| 137 | return NULL; |
| 138 | err = is_getconfig(&baudrate, &flags, idrvname, odrvname, cfgname); |
| 139 | if ( err ) { |
| 140 | PyErr_Mac(ErrorObject, err); |
| 141 | return NULL; |
| 142 | } |
| 143 | ibaud = (baudrate >> 16) & 0xffff; |
| 144 | obaud = baudrate & 0xffff; |
| 145 | return Py_BuildValue("iiiO&O&O&", ibaud, obaud, (int)flags, PyMac_BuildStr255, idrvname, |
| 146 | PyMac_BuildStr255, odrvname, PyMac_BuildStr255, cfgname); |
| 147 | } |
| 148 | |
| 149 | static char pyis_setconfig__doc__[] = |
| 150 | "Set configuration data (ibaud, obaud, flags, idrvname, odrvname, cfgname)" |
| 151 | ; |
| 152 | |
| 153 | static PyObject * |
| 154 | pyis_setconfig(self, args) |
| 155 | PyObject *self; /* Not used */ |
| 156 | PyObject *args; |
| 157 | { |
| 158 | long baudrate; |
| 159 | int flags; |
| 160 | Str255 idrvname, odrvname, cfgname; |
| 161 | OSErr err; |
| 162 | int ibaud, obaud; |
| 163 | |
| 164 | if (!PyArg_ParseTuple(args, "iiiO&O&O&", &ibaud, &obaud, &flags, PyMac_GetStr255, idrvname, |
| 165 | PyMac_GetStr255, odrvname, PyMac_GetStr255, cfgname)) |
| 166 | return NULL; |
| 167 | baudrate = (ibaud << 16) | obaud; |
| 168 | err = is_setconfig(baudrate, (long)flags, idrvname, odrvname, cfgname); |
| 169 | if ( err ) { |
| 170 | PyErr_Mac(ErrorObject, err); |
| 171 | return NULL; |
| 172 | } |
| 173 | Py_INCREF(Py_None); |
| 174 | return Py_None; |
| 175 | } |
| 176 | |
| 177 | /* List of methods defined in the module */ |
| 178 | |
| 179 | static struct PyMethodDef pyis_methods[] = { |
| 180 | {"open", pyis_open, 1, pyis_open__doc__}, |
| 181 | {"connect", pyis_connect, 1, pyis_connect__doc__}, |
| 182 | {"disconnect", pyis_disconnect, 1, pyis_disconnect__doc__}, |
| 183 | {"status", pyis_status, 1, pyis_status__doc__}, |
| 184 | {"getconfig", pyis_getconfig, 1, pyis_getconfig__doc__}, |
| 185 | {"setconfig", pyis_setconfig, 1, pyis_setconfig__doc__}, |
| 186 | |
| 187 | {NULL, NULL} /* sentinel */ |
| 188 | }; |
| 189 | |
| 190 | |
| 191 | /* Initialization function for the module (*must* be called initinterslip) */ |
| 192 | |
| 193 | static char interslip_module_documentation[] = |
| 194 | "" |
| 195 | ; |
| 196 | |
| 197 | void |
| 198 | initinterslip() |
| 199 | { |
| 200 | PyObject *m, *d; |
| 201 | |
| 202 | /* Create the module and add the functions */ |
| 203 | m = Py_InitModule4("interslip", pyis_methods, |
| 204 | interslip_module_documentation, |
| 205 | (PyObject*)NULL,PYTHON_API_VERSION); |
| 206 | |
| 207 | /* Add some symbolic constants to the module */ |
| 208 | d = PyModule_GetDict(m); |
| 209 | ErrorObject = PyString_FromString("interslip.error"); |
| 210 | PyDict_SetItemString(d, "error", ErrorObject); |
| 211 | |
| 212 | /* XXXX Add constants here */ |
| 213 | |
| 214 | PyDict_SetItemString(d, "IDLE", PyInt_FromLong(IS_IDLE)); |
| 215 | PyDict_SetItemString(d, "WMODEM", PyInt_FromLong(IS_WMODEM)); |
| 216 | PyDict_SetItemString(d, "DIAL", PyInt_FromLong(IS_DIAL)); |
| 217 | PyDict_SetItemString(d, "LOGIN", PyInt_FromLong(IS_LOGIN)); |
| 218 | PyDict_SetItemString(d, "RUN", PyInt_FromLong(IS_RUN)); |
| 219 | PyDict_SetItemString(d, "DISC", PyInt_FromLong(IS_DISC)); |
| 220 | |
| 221 | /* Check for errors */ |
| 222 | if (PyErr_Occurred()) |
| 223 | Py_FatalError("can't initialize module interslip"); |
| 224 | } |
| 225 | |