| 1 | n/a | /* swi |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | RISC OS swi functions |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | 1.00 Chris Stretch |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | 1.01 12 May 1999 Laurence Tratt |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | * Changed swi.error to be a class based exception rather than string based |
|---|
| 11 | n/a | * Added swi.ArgError which is generated for errors when the user passes invalid arguments to |
|---|
| 12 | n/a | functions etc |
|---|
| 13 | n/a | * Added "errnum" attribute to swi.error, so one can now check to see what the error number was |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | 1.02 03 March 2002 Dietmar Schwertberger |
|---|
| 16 | n/a | * Added string, integer, integers, tuple and tuples |
|---|
| 17 | n/a | */ |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | #include "oslib/os.h" |
|---|
| 20 | n/a | #include <kernel.h> |
|---|
| 21 | n/a | #include "Python.h" |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | #define PyBlock_Check(op) ((op)->ob_type == &PyBlockType) |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | static PyObject *SwiError; /* Exception swi.error */ |
|---|
| 28 | n/a | static PyObject *ArgError; /* Exception swi.ArgError */ |
|---|
| 29 | n/a | static os_error *e; |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | static PyObject *swi_oserror(void) |
|---|
| 32 | n/a | { PyErr_SetString(SwiError,e->errmess); |
|---|
| 33 | n/a | PyObject_SetAttrString(PyErr_Occurred(), "errnum", PyInt_FromLong(e->errnum)); |
|---|
| 34 | n/a | return 0; |
|---|
| 35 | n/a | } |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | static PyObject *swi_error(char *s) |
|---|
| 38 | n/a | { PyErr_SetString(ArgError,s); |
|---|
| 39 | n/a | return 0; |
|---|
| 40 | n/a | } |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | typedef struct |
|---|
| 43 | n/a | { PyObject_HEAD |
|---|
| 44 | n/a | void *block; |
|---|
| 45 | n/a | int length; /*length in bytes*/ |
|---|
| 46 | n/a | int heap; |
|---|
| 47 | n/a | } PyBlockObject; |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | static PyTypeObject PyBlockType; |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | /* block commands */ |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | static PyObject *PyBlock_New(PyObject *self,PyObject *args) |
|---|
| 54 | n/a | { int size; |
|---|
| 55 | n/a | PyBlockObject *b; |
|---|
| 56 | n/a | PyObject *init=0; |
|---|
| 57 | n/a | if(!PyArg_ParseTuple(args,"i|O",&size,&init)) return NULL; |
|---|
| 58 | n/a | if(size<1) size=1; |
|---|
| 59 | n/a | b=PyObject_NEW(PyBlockObject,&PyBlockType); |
|---|
| 60 | n/a | if(!b) return NULL; |
|---|
| 61 | n/a | b->block=malloc(4*size); |
|---|
| 62 | n/a | if(!b->block) |
|---|
| 63 | n/a | { Py_DECREF(b); |
|---|
| 64 | n/a | return PyErr_NoMemory(); |
|---|
| 65 | n/a | } |
|---|
| 66 | n/a | b->length=4*size; |
|---|
| 67 | n/a | b->heap=1; |
|---|
| 68 | n/a | if(init) |
|---|
| 69 | n/a | { if(PyString_Check(init)) |
|---|
| 70 | n/a | { int n=PyString_Size(init); |
|---|
| 71 | n/a | if (n>4*size) n=4*size; |
|---|
| 72 | n/a | memcpy(b->block,PyString_AsString(init),n); |
|---|
| 73 | n/a | memset((char*)b->block+n,0,4*size-n); |
|---|
| 74 | n/a | } |
|---|
| 75 | n/a | else |
|---|
| 76 | n/a | { int n,k; |
|---|
| 77 | n/a | long *p=(long*)b->block; |
|---|
| 78 | n/a | if(!PyList_Check(init)) goto fail; |
|---|
| 79 | n/a | n=PyList_Size(init); |
|---|
| 80 | n/a | if (n>size) n=size; |
|---|
| 81 | n/a | for(k=0;k<n;k++) |
|---|
| 82 | n/a | { PyObject *q=PyList_GetItem(init,k); |
|---|
| 83 | n/a | if(!PyInt_Check(q)) goto fail; |
|---|
| 84 | n/a | p[k]=PyInt_AsLong(q); |
|---|
| 85 | n/a | } |
|---|
| 86 | n/a | for(;k<size;k++) p[k]=0; |
|---|
| 87 | n/a | } |
|---|
| 88 | n/a | } |
|---|
| 89 | n/a | return (PyObject *)b; |
|---|
| 90 | n/a | fail:PyErr_SetString(PyExc_TypeError, |
|---|
| 91 | n/a | "block initialiser must be string or list of integers"); |
|---|
| 92 | n/a | Py_DECREF(b); |
|---|
| 93 | n/a | return NULL; |
|---|
| 94 | n/a | } |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | static PyObject *PyRegister(PyObject *self,PyObject *args) |
|---|
| 97 | n/a | { int size,ptr; |
|---|
| 98 | n/a | PyBlockObject *b; |
|---|
| 99 | n/a | if(!PyArg_ParseTuple(args,"ii",&size,&ptr)) return NULL; |
|---|
| 100 | n/a | if(size<1) size=1; |
|---|
| 101 | n/a | b=PyObject_NEW(PyBlockObject,&PyBlockType); |
|---|
| 102 | n/a | if(!b) return NULL; |
|---|
| 103 | n/a | b->block=(void*)ptr; |
|---|
| 104 | n/a | b->length=4*size; |
|---|
| 105 | n/a | b->heap=0; |
|---|
| 106 | n/a | return (PyObject *)b; |
|---|
| 107 | n/a | } |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | static PyObject *PyBlock_ToString(PyBlockObject *self,PyObject *arg) |
|---|
| 110 | n/a | { int s=0,e=self->length; |
|---|
| 111 | n/a | if(!PyArg_ParseTuple(arg,"|ii",&s,&e)) return NULL; |
|---|
| 112 | n/a | if(s<0||e>self->length||s>e) |
|---|
| 113 | n/a | { PyErr_SetString(PyExc_IndexError,"block index out of range"); |
|---|
| 114 | n/a | return NULL; |
|---|
| 115 | n/a | } |
|---|
| 116 | n/a | return PyString_FromStringAndSize((char*)self->block+s,e-s); |
|---|
| 117 | n/a | } |
|---|
| 118 | n/a | |
|---|
| 119 | n/a | static PyObject *PyBlock_NullString(PyBlockObject *self,PyObject *arg) |
|---|
| 120 | n/a | { int s=0,e=self->length,i; |
|---|
| 121 | n/a | char *p=(char*)self->block; |
|---|
| 122 | n/a | if(!PyArg_ParseTuple(arg,"|ii",&s,&e)) return NULL; |
|---|
| 123 | n/a | if(s<0||e>self->length||s>e) |
|---|
| 124 | n/a | { PyErr_SetString(PyExc_IndexError,"block index out of range"); |
|---|
| 125 | n/a | return NULL; |
|---|
| 126 | n/a | } |
|---|
| 127 | n/a | for(i=s;i<e;i++) if(p[i]==0) break; |
|---|
| 128 | n/a | return PyString_FromStringAndSize((char*)self->block+s,i-s); |
|---|
| 129 | n/a | } |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | static PyObject *PyBlock_CtrlString(PyBlockObject *self,PyObject *arg) |
|---|
| 132 | n/a | { int s=0,e=self->length,i; |
|---|
| 133 | n/a | char *p=(char*)self->block; |
|---|
| 134 | n/a | if(!PyArg_ParseTuple(arg,"|ii",&s,&e)) return NULL; |
|---|
| 135 | n/a | if(s<0||e>self->length||s>e) |
|---|
| 136 | n/a | { PyErr_SetString(PyExc_IndexError,"block index out of range"); |
|---|
| 137 | n/a | return NULL; |
|---|
| 138 | n/a | } |
|---|
| 139 | n/a | for(i=s;i<e;i++) if(p[i]<32) break; |
|---|
| 140 | n/a | return PyString_FromStringAndSize((char*)self->block+s,i-s); |
|---|
| 141 | n/a | } |
|---|
| 142 | n/a | |
|---|
| 143 | n/a | static PyObject *PyBlock_PadString(PyBlockObject *self,PyObject *arg) |
|---|
| 144 | n/a | { int s=0,e=self->length,n,m; |
|---|
| 145 | n/a | char *str; |
|---|
| 146 | n/a | char c; |
|---|
| 147 | n/a | char *p=(char*)self->block; |
|---|
| 148 | n/a | if(!PyArg_ParseTuple(arg,"s#c|ii",&str,&n,&c,&s,&e)) return NULL; |
|---|
| 149 | n/a | if(s<0||e>self->length||s>e) |
|---|
| 150 | n/a | { PyErr_SetString(PyExc_IndexError,"block index out of range"); |
|---|
| 151 | n/a | return NULL; |
|---|
| 152 | n/a | } |
|---|
| 153 | n/a | m=e-s; |
|---|
| 154 | n/a | if(n>m) n=m; |
|---|
| 155 | n/a | memcpy(p+s,str,n);memset(p+s+n,c,m-n); |
|---|
| 156 | n/a | Py_INCREF(Py_None);return Py_None; |
|---|
| 157 | n/a | } |
|---|
| 158 | n/a | |
|---|
| 159 | n/a | static PyObject *PyBlock_BitSet(PyBlockObject *self,PyObject *arg) |
|---|
| 160 | n/a | { int i,x,y; |
|---|
| 161 | n/a | int *p=(int*)self->block; |
|---|
| 162 | n/a | if(!PyArg_ParseTuple(arg,"iii",&i,&x,&y)) return NULL; |
|---|
| 163 | n/a | if(i<0||i>=self->length/4) |
|---|
| 164 | n/a | { PyErr_SetString(PyExc_IndexError,"block index out of range"); |
|---|
| 165 | n/a | return NULL; |
|---|
| 166 | n/a | } |
|---|
| 167 | n/a | p[i]=(p[i]&y)^x; |
|---|
| 168 | n/a | Py_INCREF(Py_None);return Py_None; |
|---|
| 169 | n/a | } |
|---|
| 170 | n/a | |
|---|
| 171 | n/a | static PyObject *PyBlock_Resize(PyBlockObject *self,PyObject *arg) |
|---|
| 172 | n/a | { int n; |
|---|
| 173 | n/a | if(!PyArg_ParseTuple(arg,"i",&n)) return NULL; |
|---|
| 174 | n/a | if(n<1) n=1; |
|---|
| 175 | n/a | if(self->heap) |
|---|
| 176 | n/a | { void *v=realloc(self->block,4*n); |
|---|
| 177 | n/a | if (!v) return PyErr_NoMemory(); |
|---|
| 178 | n/a | self->block=v; |
|---|
| 179 | n/a | } |
|---|
| 180 | n/a | self->length=4*n; |
|---|
| 181 | n/a | Py_INCREF(Py_None);return Py_None; |
|---|
| 182 | n/a | } |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | static PyObject *PyBlock_ToFile(PyBlockObject *self,PyObject *arg) |
|---|
| 185 | n/a | { int s=0,e=self->length/4; |
|---|
| 186 | n/a | PyObject *f; |
|---|
| 187 | n/a | FILE *fp; |
|---|
| 188 | n/a | if(!PyArg_ParseTuple(arg,"O|ii",&f,&s,&e)) return NULL; |
|---|
| 189 | n/a | fp=PyFile_AsFile(f); |
|---|
| 190 | n/a | if (!fp) |
|---|
| 191 | n/a | { PyErr_SetString(PyExc_TypeError, "arg must be open file"); |
|---|
| 192 | n/a | return NULL; |
|---|
| 193 | n/a | } |
|---|
| 194 | n/a | fwrite((int*)(self->block)+s,4,e-s,fp); |
|---|
| 195 | n/a | Py_INCREF(Py_None);return Py_None; |
|---|
| 196 | n/a | } |
|---|
| 197 | n/a | |
|---|
| 198 | n/a | static struct PyMethodDef PyBlock_Methods[]= |
|---|
| 199 | n/a | { { "tostring",(PyCFunction)PyBlock_ToString,1}, |
|---|
| 200 | n/a | { "padstring",(PyCFunction)PyBlock_PadString,1}, |
|---|
| 201 | n/a | { "nullstring",(PyCFunction)PyBlock_NullString,1}, |
|---|
| 202 | n/a | { "ctrlstring",(PyCFunction)PyBlock_CtrlString,1}, |
|---|
| 203 | n/a | { "bitset",(PyCFunction)PyBlock_BitSet,1}, |
|---|
| 204 | n/a | { "resize",(PyCFunction)PyBlock_Resize,1}, |
|---|
| 205 | n/a | { "tofile",(PyCFunction)PyBlock_ToFile,1}, |
|---|
| 206 | n/a | { NULL,NULL} /* sentinel */ |
|---|
| 207 | n/a | }; |
|---|
| 208 | n/a | |
|---|
| 209 | n/a | static int block_len(PyBlockObject *b) |
|---|
| 210 | n/a | { return b->length/4; |
|---|
| 211 | n/a | } |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | static PyObject *block_concat(PyBlockObject *b,PyBlockObject *c) |
|---|
| 214 | n/a | { PyErr_SetString(PyExc_IndexError,"block concatenation not implemented"); |
|---|
| 215 | n/a | return NULL; |
|---|
| 216 | n/a | } |
|---|
| 217 | n/a | |
|---|
| 218 | n/a | static PyObject *block_repeat(PyBlockObject *b,Py_ssize_t i) |
|---|
| 219 | n/a | { PyErr_SetString(PyExc_IndexError,"block repetition not implemented"); |
|---|
| 220 | n/a | return NULL; |
|---|
| 221 | n/a | } |
|---|
| 222 | n/a | |
|---|
| 223 | n/a | static PyObject *block_item(PyBlockObject *b,Py_ssize_t i) |
|---|
| 224 | n/a | { if(i<0||4*i>=b->length) |
|---|
| 225 | n/a | { PyErr_SetString(PyExc_IndexError,"block index out of range"); |
|---|
| 226 | n/a | return NULL; |
|---|
| 227 | n/a | } |
|---|
| 228 | n/a | return PyInt_FromLong(((long*)(b->block))[i]); |
|---|
| 229 | n/a | } |
|---|
| 230 | n/a | |
|---|
| 231 | n/a | static PyObject *block_slice(PyBlockObject *b,Py_ssize_t i,Py_ssize_t j) |
|---|
| 232 | n/a | { Py_ssize_t n,k; |
|---|
| 233 | n/a | long *p=b->block; |
|---|
| 234 | n/a | PyObject *result; |
|---|
| 235 | n/a | if(j>b->length/4) j=b->length/4; |
|---|
| 236 | n/a | if(i<0||i>j) |
|---|
| 237 | n/a | { PyErr_SetString(PyExc_IndexError,"block index out of range"); |
|---|
| 238 | n/a | return NULL; |
|---|
| 239 | n/a | } |
|---|
| 240 | n/a | n=j-i; |
|---|
| 241 | n/a | result=PyList_New(n); |
|---|
| 242 | n/a | for(k=0;k<n;k++) PyList_SetItem(result,k,PyInt_FromSsize_t(p[i+k])); |
|---|
| 243 | n/a | return result; |
|---|
| 244 | n/a | } |
|---|
| 245 | n/a | |
|---|
| 246 | n/a | static int block_ass_item(PyBlockObject *b,Py_ssize_t i,PyObject *v) |
|---|
| 247 | n/a | { if(i<0||i>=b->length/4) |
|---|
| 248 | n/a | { PyErr_SetString(PyExc_IndexError,"block index out of range"); |
|---|
| 249 | n/a | return -1; |
|---|
| 250 | n/a | } |
|---|
| 251 | n/a | if(!PyInt_Check(v)) |
|---|
| 252 | n/a | { PyErr_SetString(PyExc_TypeError,"block item must be integer"); |
|---|
| 253 | n/a | return -1; |
|---|
| 254 | n/a | } |
|---|
| 255 | n/a | ((long*)(b->block))[i]=PyInt_AsLong(v); |
|---|
| 256 | n/a | return 0; |
|---|
| 257 | n/a | } |
|---|
| 258 | n/a | |
|---|
| 259 | n/a | static int block_ass_slice(PyBlockObject *b,Py_ssize_t i,Py_ssize_t j,PyObject *v) |
|---|
| 260 | n/a | { Py_ssize_t n,k; |
|---|
| 261 | n/a | long *p=b->block; |
|---|
| 262 | n/a | if(j>b->length/4) j=b->length/4; |
|---|
| 263 | n/a | if(i<0||i>j) |
|---|
| 264 | n/a | { PyErr_SetString(PyExc_IndexError,"block index out of range"); |
|---|
| 265 | n/a | return -1; |
|---|
| 266 | n/a | } |
|---|
| 267 | n/a | if(!PyList_Check(v)) goto fail; |
|---|
| 268 | n/a | n=PyList_Size(v); |
|---|
| 269 | n/a | if(n>j-i) n=j-i; |
|---|
| 270 | n/a | for(k=0;k<n;k++) |
|---|
| 271 | n/a | { PyObject *q=PyList_GetItem(v,k); |
|---|
| 272 | n/a | if(!PyInt_Check(q)) goto fail; |
|---|
| 273 | n/a | p[i+k]=PyInt_AsLong(q); |
|---|
| 274 | n/a | } |
|---|
| 275 | n/a | for(;k<j-i;k++) p[i+k]=0; |
|---|
| 276 | n/a | return 0; |
|---|
| 277 | n/a | fail:PyErr_SetString(PyExc_TypeError,"block slice must be integer list"); |
|---|
| 278 | n/a | return -1; |
|---|
| 279 | n/a | } |
|---|
| 280 | n/a | |
|---|
| 281 | n/a | static PySequenceMethods block_as_sequence= |
|---|
| 282 | n/a | { (inquiry)block_len, /*sq_length*/ |
|---|
| 283 | n/a | (binaryfunc)block_concat, /*sq_concat*/ |
|---|
| 284 | n/a | (ssizeargfunc)block_repeat, /*sq_repeat*/ |
|---|
| 285 | n/a | (ssizeargfunc)block_item, /*sq_item*/ |
|---|
| 286 | n/a | (ssizessizeargfunc)block_slice, /*sq_slice*/ |
|---|
| 287 | n/a | (ssizeobjargproc)block_ass_item, /*sq_ass_item*/ |
|---|
| 288 | n/a | (ssizessizeobjargproc)block_ass_slice, /*sq_ass_slice*/ |
|---|
| 289 | n/a | }; |
|---|
| 290 | n/a | |
|---|
| 291 | n/a | static PyObject *PyBlock_GetAttr(PyBlockObject *s,char *name) |
|---|
| 292 | n/a | { |
|---|
| 293 | n/a | if (!strcmp(name, "length")) return PyInt_FromLong((long)s->length); |
|---|
| 294 | n/a | if (!strcmp(name, "start")) return PyInt_FromLong((long)s->block); |
|---|
| 295 | n/a | if (!strcmp(name,"end")) return PyInt_FromLong(((long)(s->block)+s->length)); |
|---|
| 296 | n/a | if (!strcmp(name, "__members__")) |
|---|
| 297 | n/a | { PyObject *list = PyList_New(3); |
|---|
| 298 | n/a | if (list) |
|---|
| 299 | n/a | { PyList_SetItem(list, 0, PyString_FromString("length")); |
|---|
| 300 | n/a | PyList_SetItem(list, 1, PyString_FromString("start")); |
|---|
| 301 | n/a | PyList_SetItem(list, 2, PyString_FromString("end")); |
|---|
| 302 | n/a | if (PyErr_Occurred()) { Py_DECREF(list);list = NULL;} |
|---|
| 303 | n/a | } |
|---|
| 304 | n/a | return list; |
|---|
| 305 | n/a | } |
|---|
| 306 | n/a | return Py_FindMethod(PyBlock_Methods, (PyObject*) s,name); |
|---|
| 307 | n/a | } |
|---|
| 308 | n/a | |
|---|
| 309 | n/a | static void PyBlock_Dealloc(PyBlockObject *b) |
|---|
| 310 | n/a | { |
|---|
| 311 | n/a | if(b->heap) { |
|---|
| 312 | n/a | if (b->heap) |
|---|
| 313 | n/a | ; |
|---|
| 314 | n/a | else |
|---|
| 315 | n/a | PyMem_DEL(b->block); |
|---|
| 316 | n/a | } |
|---|
| 317 | n/a | PyMem_DEL(b); |
|---|
| 318 | n/a | } |
|---|
| 319 | n/a | |
|---|
| 320 | n/a | static PyTypeObject PyBlockType= |
|---|
| 321 | n/a | { PyObject_HEAD_INIT(&PyType_Type) |
|---|
| 322 | n/a | 0, /*ob_size*/ |
|---|
| 323 | n/a | "block", /*tp_name*/ |
|---|
| 324 | n/a | sizeof(PyBlockObject), /*tp_size*/ |
|---|
| 325 | n/a | 0, /*tp_itemsize*/ |
|---|
| 326 | n/a | /* methods */ |
|---|
| 327 | n/a | (destructor)PyBlock_Dealloc, /*tp_dealloc*/ |
|---|
| 328 | n/a | 0, /*tp_print*/ |
|---|
| 329 | n/a | (getattrfunc)PyBlock_GetAttr, /*tp_getattr*/ |
|---|
| 330 | n/a | 0, /*tp_setattr*/ |
|---|
| 331 | n/a | 0, /*tp_compare*/ |
|---|
| 332 | n/a | 0, /*tp_repr*/ |
|---|
| 333 | n/a | 0, /*tp_as_number*/ |
|---|
| 334 | n/a | &block_as_sequence, /*tp_as_sequence*/ |
|---|
| 335 | n/a | 0, /*tp_as_mapping*/ |
|---|
| 336 | n/a | 0, /*tp_hash*/ |
|---|
| 337 | n/a | }; |
|---|
| 338 | n/a | |
|---|
| 339 | n/a | /* swi commands */ |
|---|
| 340 | n/a | |
|---|
| 341 | n/a | static PyObject *swi_swi(PyObject *self,PyObject *args) |
|---|
| 342 | n/a | { PyObject *name,*format,*result,*v; |
|---|
| 343 | n/a | int swino,carry,rno=0,j,n; |
|---|
| 344 | n/a | char *swiname,*fmt,*outfmt; |
|---|
| 345 | n/a | _kernel_swi_regs r; |
|---|
| 346 | n/a | PyBlockObject *ao; |
|---|
| 347 | n/a | if(args==NULL||!PyTuple_Check(args)||(n=PyTuple_Size(args))<2) |
|---|
| 348 | n/a | { PyErr_BadArgument(); return NULL;} |
|---|
| 349 | n/a | name=PyTuple_GetItem(args,0); |
|---|
| 350 | n/a | if(!PyArg_Parse(name,"i",&swino)) |
|---|
| 351 | n/a | { PyErr_Clear(); |
|---|
| 352 | n/a | if(!PyArg_Parse(name,"s",&swiname)) return NULL; |
|---|
| 353 | n/a | e=xos_swi_number_from_string(swiname,&swino); |
|---|
| 354 | n/a | if(e) return swi_oserror(); |
|---|
| 355 | n/a | } |
|---|
| 356 | n/a | format=PyTuple_GetItem(args,1); |
|---|
| 357 | n/a | if(!PyArg_Parse(format,"s",&fmt)) return NULL; |
|---|
| 358 | n/a | j=2; |
|---|
| 359 | n/a | for(;;fmt++) |
|---|
| 360 | n/a | { switch(*fmt) |
|---|
| 361 | n/a | { case '.': rno++;continue; |
|---|
| 362 | n/a | case ';':case 0: goto swicall; |
|---|
| 363 | n/a | case '0':case '1':case '2':case '3':case '4': |
|---|
| 364 | n/a | case '5':case '6':case '7':case '8':case '9': |
|---|
| 365 | n/a | r.r[rno++]=*fmt-'0';continue; |
|---|
| 366 | n/a | case '-':r.r[rno++]=-1;continue; |
|---|
| 367 | n/a | } |
|---|
| 368 | n/a | if(j>=n) return swi_error("Too few arguments"); |
|---|
| 369 | n/a | v=PyTuple_GetItem(args,j++); |
|---|
| 370 | n/a | switch(*fmt) |
|---|
| 371 | n/a | { case 'i':if(!PyArg_Parse(v,"i",&r.r[rno])) return NULL; |
|---|
| 372 | n/a | break; |
|---|
| 373 | n/a | case 's':if(!PyArg_Parse(v,"s",(char**)(&r.r[rno]))) return NULL; |
|---|
| 374 | n/a | break; |
|---|
| 375 | n/a | case 'b':if(!PyArg_Parse(v,"O",(PyObject**)&ao)) return NULL; |
|---|
| 376 | n/a | if(!PyBlock_Check(v)) return swi_error("Not a block"); |
|---|
| 377 | n/a | r.r[rno]=(int)(ao->block); |
|---|
| 378 | n/a | break; |
|---|
| 379 | n/a | case 'e':if(!PyArg_Parse(v,"O",(PyObject**)&ao)) return NULL; |
|---|
| 380 | n/a | if(!PyBlock_Check(v)) return swi_error("Not a block"); |
|---|
| 381 | n/a | r.r[rno]=(int)(ao->block)+ao->length; |
|---|
| 382 | n/a | break; |
|---|
| 383 | n/a | default:return swi_error("Odd format character"); |
|---|
| 384 | n/a | } |
|---|
| 385 | n/a | rno++; |
|---|
| 386 | n/a | } |
|---|
| 387 | n/a | swicall:e=(os_error*)_kernel_swi_c(swino,&r,&r,&carry); |
|---|
| 388 | n/a | if(e) return swi_oserror(); |
|---|
| 389 | n/a | if(*fmt==0) { Py_INCREF(Py_None);return Py_None;} |
|---|
| 390 | n/a | n=0; |
|---|
| 391 | n/a | for(outfmt=++fmt;*outfmt;outfmt++) switch(*outfmt) |
|---|
| 392 | n/a | { case 'i':case 's':case '*':n++;break; |
|---|
| 393 | n/a | case '.':break; |
|---|
| 394 | n/a | default:return swi_error("Odd format character"); |
|---|
| 395 | n/a | } |
|---|
| 396 | n/a | if(n==0) { Py_INCREF(Py_None);return Py_None;} |
|---|
| 397 | n/a | if(n!=1) |
|---|
| 398 | n/a | { result=PyTuple_New(n); |
|---|
| 399 | n/a | if(!result) return NULL; |
|---|
| 400 | n/a | } |
|---|
| 401 | n/a | rno=0;j=0; |
|---|
| 402 | n/a | for(;*fmt;fmt++) |
|---|
| 403 | n/a | { switch(*fmt) |
|---|
| 404 | n/a | { case 'i':v=PyInt_FromLong((long)r.r[rno++]); break; |
|---|
| 405 | n/a | case 's':v=PyString_FromString((char*)(r.r[rno++])); break; |
|---|
| 406 | n/a | case '.':rno++; continue; |
|---|
| 407 | n/a | case '*':v=PyInt_FromLong((long)carry); break; |
|---|
| 408 | n/a | } |
|---|
| 409 | n/a | if(!v) goto fail; |
|---|
| 410 | n/a | if(n==1) return v; |
|---|
| 411 | n/a | PyTuple_SetItem(result,j,v); |
|---|
| 412 | n/a | j++; |
|---|
| 413 | n/a | } |
|---|
| 414 | n/a | return result; |
|---|
| 415 | n/a | fail:Py_DECREF(result);return 0; |
|---|
| 416 | n/a | } |
|---|
| 417 | n/a | |
|---|
| 418 | n/a | static PyObject *swi_string(PyObject *self, PyObject *arg) |
|---|
| 419 | n/a | { char *s; |
|---|
| 420 | n/a | int l=-1; |
|---|
| 421 | n/a | if(!PyArg_ParseTuple(arg,"i|i",(unsigned int *)&s, &l)) return NULL; |
|---|
| 422 | n/a | if (l==-1) |
|---|
| 423 | n/a | l = strlen(s); |
|---|
| 424 | n/a | return PyString_FromStringAndSize((char*)s, l); |
|---|
| 425 | n/a | } |
|---|
| 426 | n/a | |
|---|
| 427 | n/a | static char swi_string__doc__[] = |
|---|
| 428 | n/a | "string(address[, length]) -> string\n\ |
|---|
| 429 | n/a | Read a null terminated string from the given address."; |
|---|
| 430 | n/a | |
|---|
| 431 | n/a | |
|---|
| 432 | n/a | static PyObject *swi_integer(PyObject *self, PyObject *arg) |
|---|
| 433 | n/a | { int *i; |
|---|
| 434 | n/a | |
|---|
| 435 | n/a | if(!PyArg_ParseTuple(arg,"i",(unsigned int *)&i)) |
|---|
| 436 | n/a | return NULL; |
|---|
| 437 | n/a | return PyInt_FromLong(*i); |
|---|
| 438 | n/a | } |
|---|
| 439 | n/a | |
|---|
| 440 | n/a | static char swi_integer__doc__[] = |
|---|
| 441 | n/a | "integer(address) -> string\n\ |
|---|
| 442 | n/a | Read an integer from the given address."; |
|---|
| 443 | n/a | |
|---|
| 444 | n/a | |
|---|
| 445 | n/a | static PyObject *swi_integers(PyObject *self, PyObject *arg) |
|---|
| 446 | n/a | { int *i; |
|---|
| 447 | n/a | int c=-1; |
|---|
| 448 | n/a | PyObject *result, *result1; |
|---|
| 449 | n/a | |
|---|
| 450 | n/a | if(!PyArg_ParseTuple(arg,"i|i",(unsigned int *)&i, &c)) return NULL; |
|---|
| 451 | n/a | result=PyList_New(0); |
|---|
| 452 | n/a | if (result) { |
|---|
| 453 | n/a | while ( c>0 || (c==-1 && *i) ) { |
|---|
| 454 | n/a | result1 = PyInt_FromLong((long)*i); |
|---|
| 455 | n/a | if (!result1) { |
|---|
| 456 | n/a | Py_DECREF(result); |
|---|
| 457 | n/a | return NULL; |
|---|
| 458 | n/a | } |
|---|
| 459 | n/a | if (PyList_Append(result, result1)!=0) { |
|---|
| 460 | n/a | Py_DECREF(result); |
|---|
| 461 | n/a | Py_DECREF(result1); |
|---|
| 462 | n/a | return NULL; |
|---|
| 463 | n/a | }; |
|---|
| 464 | n/a | i++; |
|---|
| 465 | n/a | if (c!=-1) |
|---|
| 466 | n/a | c--; |
|---|
| 467 | n/a | } |
|---|
| 468 | n/a | } |
|---|
| 469 | n/a | return result; |
|---|
| 470 | n/a | } |
|---|
| 471 | n/a | |
|---|
| 472 | n/a | static char swi_integers__doc__[] = |
|---|
| 473 | n/a | "integers(address[, count]) -> string\n\ |
|---|
| 474 | n/a | Either read a null terminated list of integers or\n\ |
|---|
| 475 | n/a | a list of given length from the given address."; |
|---|
| 476 | n/a | |
|---|
| 477 | n/a | |
|---|
| 478 | n/a | static PyObject *swi_tuples(PyObject *self, PyObject *arg) |
|---|
| 479 | n/a | { |
|---|
| 480 | n/a | unsigned char *i; /* points to current */ |
|---|
| 481 | n/a | int c=-1, l=4, j, zero; /* count, length, index */ |
|---|
| 482 | n/a | PyObject *result, *result1, *result11; |
|---|
| 483 | n/a | |
|---|
| 484 | n/a | if(!PyArg_ParseTuple(arg,"i|ii",(unsigned int *)&i, &l, &c)) return NULL; |
|---|
| 485 | n/a | result=PyList_New(0); |
|---|
| 486 | n/a | if (result) { |
|---|
| 487 | n/a | while (c) { |
|---|
| 488 | n/a | result1 = PyTuple_New(l); |
|---|
| 489 | n/a | if (!result1) { |
|---|
| 490 | n/a | Py_DECREF(result); |
|---|
| 491 | n/a | return NULL; |
|---|
| 492 | n/a | } |
|---|
| 493 | n/a | zero = (c==-1); /* check for zeros? */ |
|---|
| 494 | n/a | for(j=0;j<l;j++) { |
|---|
| 495 | n/a | if (zero && *i) |
|---|
| 496 | n/a | zero = 0; /* non-zero found */ |
|---|
| 497 | n/a | result11 = PyInt_FromLong((long)(*i)); |
|---|
| 498 | n/a | if (!result11) { |
|---|
| 499 | n/a | Py_DECREF(result); |
|---|
| 500 | n/a | return NULL; |
|---|
| 501 | n/a | } |
|---|
| 502 | n/a | PyTuple_SetItem(result1, j, result11); |
|---|
| 503 | n/a | i++; |
|---|
| 504 | n/a | } |
|---|
| 505 | n/a | if (c==-1 && zero) { |
|---|
| 506 | n/a | Py_DECREF(result1); |
|---|
| 507 | n/a | c = 0; |
|---|
| 508 | n/a | break; |
|---|
| 509 | n/a | } |
|---|
| 510 | n/a | if (PyList_Append(result, result1)!=0) { |
|---|
| 511 | n/a | Py_DECREF(result1); |
|---|
| 512 | n/a | Py_DECREF(result); |
|---|
| 513 | n/a | return NULL; |
|---|
| 514 | n/a | } |
|---|
| 515 | n/a | if (c!=-1) |
|---|
| 516 | n/a | c--; |
|---|
| 517 | n/a | } |
|---|
| 518 | n/a | } |
|---|
| 519 | n/a | return result; |
|---|
| 520 | n/a | } |
|---|
| 521 | n/a | |
|---|
| 522 | n/a | static char swi_tuples__doc__[] = |
|---|
| 523 | n/a | "tuples(address[, length=4[, count]]) -> string\n\ |
|---|
| 524 | n/a | Either read a null terminated list of byte tuples or\n\ |
|---|
| 525 | n/a | a list of given length from the given address."; |
|---|
| 526 | n/a | |
|---|
| 527 | n/a | |
|---|
| 528 | n/a | static PyObject *swi_tuple(PyObject *self, PyObject *arg) |
|---|
| 529 | n/a | { |
|---|
| 530 | n/a | unsigned char *i; /* points to current */ |
|---|
| 531 | n/a | int c=1, j; |
|---|
| 532 | n/a | PyObject *result, *result1; |
|---|
| 533 | n/a | |
|---|
| 534 | n/a | if(!PyArg_ParseTuple(arg,"i|i",(unsigned int *)&i, &c)) return NULL; |
|---|
| 535 | n/a | result = PyTuple_New(c); |
|---|
| 536 | n/a | if (!result) |
|---|
| 537 | n/a | return NULL; |
|---|
| 538 | n/a | for(j=0;j<c;j++) { |
|---|
| 539 | n/a | result1 = PyInt_FromLong((long)(i[j])); |
|---|
| 540 | n/a | if (!result1) { |
|---|
| 541 | n/a | Py_DECREF(result); |
|---|
| 542 | n/a | return NULL; |
|---|
| 543 | n/a | } |
|---|
| 544 | n/a | PyTuple_SetItem(result, j, result1); |
|---|
| 545 | n/a | } |
|---|
| 546 | n/a | return result; |
|---|
| 547 | n/a | } |
|---|
| 548 | n/a | |
|---|
| 549 | n/a | static char swi_tuple__doc__[] = |
|---|
| 550 | n/a | "tuple(address[, count=1]]) -> tuple\n\ |
|---|
| 551 | n/a | Read count bytes from given address."; |
|---|
| 552 | n/a | |
|---|
| 553 | n/a | |
|---|
| 554 | n/a | static PyMethodDef SwiMethods[]= |
|---|
| 555 | n/a | { { "swi", swi_swi, METH_VARARGS}, |
|---|
| 556 | n/a | { "block", PyBlock_New, METH_VARARGS}, |
|---|
| 557 | n/a | { "register", PyRegister, METH_VARARGS}, |
|---|
| 558 | n/a | { "string", swi_string, METH_VARARGS, swi_string__doc__}, |
|---|
| 559 | n/a | { "integer", swi_integer, METH_VARARGS, swi_integer__doc__}, |
|---|
| 560 | n/a | { "integers", swi_integers, METH_VARARGS, swi_integers__doc__}, |
|---|
| 561 | n/a | { "tuples", swi_tuples, METH_VARARGS, swi_tuples__doc__}, |
|---|
| 562 | n/a | { "tuple", swi_tuple, METH_VARARGS, swi_tuple__doc__}, |
|---|
| 563 | n/a | { NULL,NULL,0,NULL} /* Sentinel */ |
|---|
| 564 | n/a | }; |
|---|
| 565 | n/a | |
|---|
| 566 | n/a | |
|---|
| 567 | n/a | void initswi() |
|---|
| 568 | n/a | { PyObject *m, *d; |
|---|
| 569 | n/a | m = Py_InitModule("swi", SwiMethods); |
|---|
| 570 | n/a | d = PyModule_GetDict(m); |
|---|
| 571 | n/a | SwiError=PyErr_NewException("swi.error", NULL, NULL); |
|---|
| 572 | n/a | PyDict_SetItemString(d,"error",SwiError); |
|---|
| 573 | n/a | ArgError=PyErr_NewException("swi.ArgError", NULL, NULL); |
|---|
| 574 | n/a | PyDict_SetItemString(d,"ArgError",ArgError); |
|---|
| 575 | n/a | } |
|---|