1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
|
/***********************************************************
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI or Corporation for National Research Initiatives or
CNRI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.
While CWI is the initial source for this software, a modified version
is made available by the Corporation for National Research Initiatives
(CNRI) at the Internet address ftp://ftp.python.org.
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
******************************************************************/
/* Sanity check */
#ifndef __powerc
#error Please port this code to your architecture first...
#endif
/*
** Define to include testroutines (at the end)
*/
#define TESTSUPPORT
#include "Python.h"
#include "macglue.h"
#include "macdefs.h"
#include <CodeFragments.h>
/* Prototypes for routines not in any include file (shame, shame) */
extern PyObject *ResObj_New(Handle);
extern int ResObj_Convert(PyObject *, Handle *);
static PyObject *ErrorObject;
/* Debugging macro */
#ifdef TESTSUPPORT
#define PARANOID(arg) \
if ( arg == 0 ) {PyErr_SetString(ErrorObject, "Internal error: NULL arg!"); return 0; }
#else
#define PARANOID(arg) /*pass*/
#endif
/* Prototypes we use for routines and arguments */
typedef long anything;
typedef anything (*anyroutine)(...);
/* Other constants */
#define MAXNAME 31 /* Maximum size of names, for printing only */
#define MAXARG 12 /* Maximum number of arguments */
/*
** Routines to convert arguments between Python and C.
** Note return-value converters return NULL if this argument (or return value)
** doesn't return anything. The call-wrapper code collects all return values,
** and does the expected thing based on the number of return values: return None, a single
** value or a tuple of values.
**
** Hence, optional return values are also implementable.
*/
typedef anything (*py2c_converter)(PyObject *);
typedef PyObject *(*c2py_converter)(anything);
typedef PyObject *(*rv2py_converter)(anything);
/* Dummy routine for arguments that are output-only */
static anything
py2c_dummy(arg)
PyObject *arg;
{
return 0;
}
/* Routine to allocate storage for output integers */
static anything
py2c_alloc(arg)
PyObject *arg;
{
char *ptr;
if( (ptr=malloc(sizeof(anything))) == 0 )
PyErr_NoMemory();
return (anything)ptr;
}
/* Dummy routine for arguments that are input-only */
static PyObject *
c2py_dummy(arg)
anything arg;
{
return 0;
}
/* Dummy routine for void return value */
static PyObject *
rv2py_none(arg)
anything arg;
{
return 0;
}
/* Routine to de-allocate storage for input-only arguments */
static PyObject *
c2py_free(arg)
anything arg;
{
if ( arg )
free((char *)arg);
return 0;
}
/*
** OSErr return value.
*/
static PyObject *
rv2py_oserr(arg)
anything arg;
{
OSErr err = (OSErr)arg;
if (err)
return PyMac_Error(err);
return 0;
}
/*
** Input integers of all sizes (PPC only)
*/
static anything
py2c_in_int(arg)
PyObject *arg;
{
return PyInt_AsLong(arg);
}
/*
** Integer return values of all sizes (PPC only)
*/
static PyObject *
rv2py_int(arg)
anything arg;
{
return PyInt_FromLong((long)arg);
}
/*
** Integer output parameters
*/
static PyObject *
c2py_out_long(arg)
anything arg;
{
PyObject *rv;
PARANOID(arg);
rv = PyInt_FromLong(*(long *)arg);
free((char *)arg);
return rv;
}
static PyObject *
c2py_out_short(arg)
anything arg;
{
PyObject *rv;
PARANOID(arg);
rv = PyInt_FromLong((long)*(short *)arg);
free((char *)arg);
return rv;
}
static PyObject *
c2py_out_byte(arg)
anything arg;
{
PyObject *rv;
PARANOID(arg);
rv = PyInt_FromLong((long)*(char *)arg);
free((char *)arg);
return rv;
}
/*
** Strings
*/
static anything
py2c_in_string(arg)
PyObject *arg;
{
return (anything)PyString_AsString(arg);
}
/*
** Pascal-style strings
*/
static anything
py2c_in_pstring(arg)
PyObject *arg;
{
unsigned char *p;
int size;
if( (size = PyString_Size(arg)) < 0)
return 0;
if ( size > 255 ) {
PyErr_SetString(ErrorObject, "Pstring must be <= 255 chars");
return 0;
}
if( (p=(unsigned char *)malloc(256)) == 0 ) {
PyErr_NoMemory();
return 0;
}
p[0] = size;
memcpy(p+1, PyString_AsString(arg), size);
return (anything)p;
}
static anything
py2c_out_pstring(arg)
PyObject *arg;
{
unsigned char *p;
if( (p=(unsigned char *)malloc(256)) == 0 ) {
PyErr_NoMemory();
return 0;
}
p[0] = 0;
return (anything)p;
}
static PyObject *
c2py_out_pstring(arg)
anything arg;
{
unsigned char *p = (unsigned char *)arg;
PyObject *rv;
PARANOID(arg);
rv = PyString_FromStringAndSize((char *)p+1, p[0]);
free(p);
return rv;
}
static PyObject *
rv2py_pstring(arg)
anything arg;
{
unsigned char *p = (unsigned char *)arg;
PyObject *rv;
if ( arg == NULL ) return NULL;
rv = PyString_FromStringAndSize((char *)p+1, p[0]);
return rv;
}
/*
** C objects.
*/
static anything
py2c_in_cobject(arg)
PyObject *arg;
{
if ( arg == Py_None )
return 0;
return (anything)PyCObject_AsVoidPtr(arg);
}
static PyObject *
c2py_out_cobject(arg)
anything arg;
{
void **ptr = (void **)arg;
PyObject *rv;
PARANOID(arg);
if ( *ptr == 0 ) {
Py_INCREF(Py_None);
rv = Py_None;
} else {
rv = PyCObject_FromVoidPtr(*ptr, 0);
}
free((char *)ptr);
return rv;
}
static PyObject *
rv2py_cobject(arg)
anything arg;
{
void *ptr = (void *)arg;
PyObject *rv;
if ( ptr == 0 ) return NULL;
rv = PyCObject_FromVoidPtr(ptr, 0);
return rv;
}
/*
** Handles.
*/
static anything
py2c_in_handle(arg)
PyObject *arg;
{
Handle h = 0;
ResObj_Convert(arg, &h);
return (anything)h;
}
static PyObject *
c2py_out_handle(arg)
anything arg;
{
Handle *rv = (Handle *)arg;
PyObject *prv;
PARANOID(arg);
if ( *rv == 0 ) {
Py_INCREF(Py_None);
prv = Py_None;
} else {
prv = ResObj_New(*rv);
}
free((char *)rv);
return prv;
}
static PyObject *
rv2py_handle(arg)
anything arg;
{
Handle rv = (Handle)arg;
if ( rv == NULL ) return NULL;
return ResObj_New(rv);
}
typedef struct {
char *name; /* Name */
py2c_converter get; /* Get argument */
int get_uses_arg; /* True if the above consumes an argument */
c2py_converter put; /* Put result value */
} conventry;
static conventry converters[] = {
{"InByte", py2c_in_int, 1, c2py_dummy},
{"InShort", py2c_in_int, 1, c2py_dummy},
{"InLong", py2c_in_int, 1, c2py_dummy},
{"OutLong", py2c_alloc, 0, c2py_out_long},
{"OutShort", py2c_alloc, 0, c2py_out_short},
{"OutByte", py2c_alloc, 0, c2py_out_byte},
{"InString", py2c_in_string, 1, c2py_dummy},
{"InPstring", py2c_in_pstring,1, c2py_free},
{"OutPstring", py2c_out_pstring,0, c2py_out_pstring},
{"InCobject", py2c_in_cobject,1, c2py_dummy},
{"OutCobject", py2c_alloc, 0, c2py_out_cobject},
{"InHandle", py2c_in_handle, 1, c2py_dummy},
{"OutHandle", py2c_alloc, 0, c2py_out_handle},
{0, 0, 0, 0}
};
typedef struct {
char *name;
rv2py_converter rtn;
} rvconventry;
static rvconventry rvconverters[] = {
{"None", rv2py_none},
{"OSErr", rv2py_oserr},
{"Byte", rv2py_int},
{"Short", rv2py_int},
{"Long", rv2py_int},
{"Pstring", rv2py_pstring},
{"Cobject", rv2py_cobject},
{"Handle", rv2py_handle},
{0, 0}
};
static conventry *
getconverter(name)
char *name;
{
int i;
char buf[256];
for(i=0; converters[i].name; i++ )
if ( strcmp(name, converters[i].name) == 0 )
return &converters[i];
sprintf(buf, "Unknown argtype: %s", name);
PyErr_SetString(ErrorObject, buf);
return 0;
}
static rvconventry *
getrvconverter(name)
char *name;
{
int i;
char buf[256];
for(i=0; rvconverters[i].name; i++ )
if ( strcmp(name, rvconverters[i].name) == 0 )
return &rvconverters[i];
sprintf(buf, "Unknown return value type: %s", name);
PyErr_SetString(ErrorObject, buf);
return 0;
}
static int
argparse_conv(obj, ptr)
PyObject *obj;
conventry **ptr;
{
char *name;
int i;
conventry *item;
if( (name=PyString_AsString(obj)) == NULL )
return 0;
if( (item=getconverter(name)) == NULL )
return 0;
*ptr = item;
return 1;
}
static int
argparse_rvconv(obj, ptr)
PyObject *obj;
rvconventry **ptr;
{
char *name;
int i;
rvconventry *item;
if( (name=PyString_AsString(obj)) == NULL )
return 0;
if( (item=getrvconverter(name)) == NULL )
return 0;
*ptr = item;
return 1;
}
/* ----------------------------------------------------- */
/* Declarations for objects of type fragment */
typedef struct {
PyObject_HEAD
CFragConnectionID conn_id;
char name[MAXNAME+1];
} cdfobject;
static PyTypeObject Cdftype;
/* ---------------------------------------------------------------- */
/* Declarations for objects of type routine */
typedef struct {
PyObject_HEAD
anyroutine rtn;
char name[MAXNAME+1];
} cdrobject;
static PyTypeObject Cdrtype;
/* ---------------------------------------------------------------- */
/* Declarations for objects of type callable */
typedef struct {
PyObject_HEAD
cdrobject *routine; /* The routine to call */
int npargs; /* Python argument count */
int ncargs; /* C argument count + 1 */
rvconventry *rvconv; /* Return value converter */
conventry *argconv[MAXARG]; /* Value converter list */
} cdcobject;
static PyTypeObject Cdctype;
/* -------------------------------------------------------- */
static struct PyMethodDef cdr_methods[] = {
{NULL, NULL} /* sentinel */
};
/* ---------- */
static cdrobject *
newcdrobject(name, routine)
unsigned char *name;
anyroutine routine;
{
cdrobject *self;
int nlen;
self = PyObject_NEW(cdrobject, &Cdrtype);
if (self == NULL)
return NULL;
if ( name[0] > MAXNAME )
nlen = MAXNAME;
else
nlen = name[0];
memcpy(self->name, name+1, nlen);
self->name[nlen] = '\0';
self->rtn = routine;
return self;
}
static void
cdr_dealloc(self)
cdrobject *self;
{
PyObject_DEL(self);
}
static PyObject *
cdr_repr(self)
cdrobject *self;
{
PyObject *s;
char buf[256];
sprintf(buf, "<Calldll routine %s address 0x%x>", self->name, self->rtn);
s = PyString_FromString(buf);
return s;
}
static char Cdrtype__doc__[] =
"C Routine address"
;
static PyTypeObject Cdrtype = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*ob_size*/
"calldll.routine", /*tp_name*/
sizeof(cdrobject), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)cdr_dealloc, /*tp_dealloc*/
(printfunc)0, /*tp_print*/
(getattrfunc)0, /*tp_getattr*/
(setattrfunc)0, /*tp_setattr*/
(cmpfunc)0, /*tp_compare*/
(reprfunc)cdr_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
(hashfunc)0, /*tp_hash*/
(ternaryfunc)0, /*tp_call*/
(reprfunc)0, /*tp_str*/
/* Space for future expansion */
0L,0L,0L,0L,
Cdrtype__doc__ /* Documentation string */
};
/* End of code for routine objects */
/* -------------------------------------------------------- */
static struct PyMethodDef cdc_methods[] = {
{NULL, NULL} /* sentinel */
};
/* ---------- */
static cdcobject *
newcdcobject(routine, npargs, ncargs, rvconv, argconv)
cdrobject *routine;
int npargs;
int ncargs;
rvconventry *rvconv;
conventry *argconv[];
{
cdcobject *self;
int i;
self = PyObject_NEW(cdcobject, &Cdctype);
if (self == NULL)
return NULL;
self->routine = routine;
Py_INCREF(routine);
self->npargs = npargs;
self->ncargs = ncargs;
self->rvconv = rvconv;
for(i=0; i<MAXARG; i++)
if ( i < ncargs )
self->argconv[i] = argconv[i];
else
self->argconv[i] = 0;
return self;
}
static void
cdc_dealloc(self)
cdcobject *self;
{
Py_XDECREF(self->routine);
PyObject_DEL(self);
}
static PyObject *
cdc_repr(self)
cdcobject *self;
{
PyObject *s;
char buf[256];
int i;
sprintf(buf, "<callable %s = %s(", self->rvconv->name, self->routine->name);
for(i=0; i< self->ncargs; i++) {
strcat(buf, self->argconv[i]->name);
if ( i < self->ncargs-1 )
strcat(buf, ", ");
}
strcat(buf, ") >");
s = PyString_FromString(buf);
return s;
}
/*
** And this is what we all do it for: call a C function.
*/
static PyObject *
cdc_call(self, args, kwargs)
cdcobject *self;
PyObject *args;
PyObject *kwargs;
{
char buf[256];
int i, pargindex;
anything c_args[MAXARG] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
anything c_rv;
conventry *cp;
PyObject *curarg;
anyroutine func;
PyObject *returnvalues[MAXARG+1];
PyObject *rv;
if( kwargs ) {
PyErr_SetString(PyExc_TypeError, "Keyword args not allowed");
return 0;
}
if( !PyTuple_Check(args) ) {
PyErr_SetString(PyExc_TypeError, "Arguments not in tuple");
return 0;
}
if( PyTuple_Size(args) != self->npargs ) {
sprintf(buf, "%d arguments, expected %d", PyTuple_Size(args), self->npargs);
PyErr_SetString(PyExc_TypeError, buf);
return 0;
}
/* Decode arguments */
pargindex = 0;
for(i=0; i<self->ncargs; i++) {
cp = self->argconv[i];
if ( cp->get_uses_arg ) {
curarg = PyTuple_GET_ITEM(args, pargindex);
pargindex++;
} else {
curarg = (PyObject *)NULL;
}
c_args[i] = (*cp->get)(curarg);
}
if (PyErr_Occurred())
return 0;
/* Call function */
func = self->routine->rtn;
c_rv = (*func)(c_args[0], c_args[1], c_args[2], c_args[3],
c_args[4], c_args[5], c_args[6], c_args[7],
c_args[8], c_args[9], c_args[10], c_args[11]);
/* Decode return value, and store into returnvalues if needed */
pargindex = 0;
curarg = (*self->rvconv->rtn)(c_rv);
if ( curarg )
returnvalues[pargindex++] = curarg;
/* Decode returnvalue parameters and cleanup any storage allocated */
for(i=0; i<self->ncargs; i++) {
cp = self->argconv[i];
curarg = (*cp->put)(c_args[i]);
if(curarg)
returnvalues[pargindex++] = curarg;
/* NOTE: We only check errors at the end (so we free() everything) */
}
if ( PyErr_Occurred() ) {
/* An error did occur. Free the python objects created */
for(i=0; i<pargindex; i++)
Py_XDECREF(returnvalues[i]);
return NULL;
}
/* Zero and one return values cases are special: */
if ( pargindex == 0 ) {
Py_INCREF(Py_None);
return Py_None;
}
if ( pargindex == 1 )
return returnvalues[0];
/* More than one return value: put in a tuple */
rv = PyTuple_New(pargindex);
for(i=0; i<pargindex; i++)
if(rv)
PyTuple_SET_ITEM(rv, i, returnvalues[i]);
else
Py_XDECREF(returnvalues[i]);
return rv;
}
static char Cdctype__doc__[] =
""
;
static PyTypeObject Cdctype = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*ob_size*/
"calldll.callable", /*tp_name*/
sizeof(cdcobject), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)cdc_dealloc, /*tp_dealloc*/
(printfunc)0, /*tp_print*/
(getattrfunc)0, /*tp_getattr*/
(setattrfunc)0, /*tp_setattr*/
(cmpfunc)0, /*tp_compare*/
(reprfunc)cdc_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
(hashfunc)0, /*tp_hash*/
(ternaryfunc)cdc_call, /*tp_call*/
(reprfunc)0, /*tp_str*/
/* Space for future expansion */
0L,0L,0L,0L,
Cdctype__doc__ /* Documentation string */
};
/* End of code for callable objects */
/* ---------------------------------------------------------------- */
static char cdf_keys__doc__[] =
"Return list of symbol names in fragment";
static PyObject *
cdf_keys(self, args)
cdfobject *self;
PyObject *args;
{
long symcount;
PyObject *rv, *obj;
Str255 symname;
Ptr dummy1;
CFragSymbolClass dummy2;
int i;
OSErr err;
if (!PyArg_ParseTuple(args, ""))
return NULL;
if ( (err=CountSymbols(self->conn_id, &symcount)) < 0 )
return PyMac_Error(err);
if ( (rv=PyList_New(symcount)) == NULL )
return NULL;
for (i=0; i<symcount; i++) {
if ((err=GetIndSymbol(self->conn_id, i, symname, &dummy1, &dummy2)) < 0 ) {
Py_XDECREF(rv);
return PyMac_Error(err);
}
if ((obj=PyString_FromStringAndSize((char *)symname+1, symname[0])) == NULL ) {
Py_XDECREF(rv);
return PyMac_Error(err);
}
if (PyList_SetItem(rv, i, obj) < 0 ) {
Py_XDECREF(rv);
return NULL;
}
}
return rv;
}
static struct PyMethodDef cdf_methods[] = {
{"keys", (PyCFunction)cdf_keys, METH_VARARGS,
cdf_keys__doc__},
{NULL, NULL} /* sentinel */
};
/* ---------- */
static cdfobject *
newcdfobject(conn_id, name)
CFragConnectionID conn_id;
unsigned char *name;
{
cdfobject *self;
int nlen;
self = PyObject_NEW(cdfobject, &Cdftype);
if (self == NULL)
return NULL;
self->conn_id = conn_id;
if ( name[0] > MAXNAME )
nlen = MAXNAME;
else
nlen = name[0];
strncpy(self->name, (char *)name+1, nlen);
self->name[nlen] = '\0';
return self;
}
static void
cdf_dealloc(self)
cdfobject *self;
{
PyObject_DEL(self);
}
static PyObject *
cdf_repr(self)
cdfobject *self;
{
PyObject *s;
char buf[256];
sprintf(buf, "<fragment %s connection, id 0x%x>", self->name, self->conn_id);
s = PyString_FromString(buf);
return s;
}
static PyObject *
cdf_getattr_helper(self, name)
cdfobject *self;
char *name;
{
unsigned char *rtn_name;
anyroutine rtn;
OSErr err;
Str255 errMessage;
CFragSymbolClass class;
char buf[256];
rtn_name = Pstring(name);
err = FindSymbol(self->conn_id, rtn_name, (Ptr *)&rtn, &class);
if ( err ) {
sprintf(buf, "%.*s: %s", rtn_name[0], rtn_name+1, PyMac_StrError(err));
PyErr_SetString(ErrorObject, buf);
return NULL;
}
if( class != kTVectorCFragSymbol ) {
PyErr_SetString(ErrorObject, "Symbol is not a routine");
return NULL;
}
return (PyObject *)newcdrobject(rtn_name, rtn);
}
static PyObject *
cdf_getattr(self, name)
cdfobject *self;
char *name;
{
PyObject *rv;
if ((rv=Py_FindMethod(cdf_methods, (PyObject *)self, name)))
return rv;
PyErr_Clear();
return cdf_getattr_helper(self, name);
}
/* -------------------------------------------------------- */
/* Code to access cdf objects as mappings */
static int
cdf_length(self)
cdfobject *self;
{
long symcount;
OSErr err;
err = CountSymbols(self->conn_id, &symcount);
if ( err ) {
PyMac_Error(err);
return -1;
}
return symcount;
}
static PyObject *
cdf_subscript(self, key)
cdfobject *self;
PyObject *key;
{
char *name;
if ((name=PyString_AsString(key)) == 0 )
return 0;
return cdf_getattr_helper(self, name);
}
static int
cdf_ass_sub(self, v, w)
cdfobject *self;
PyObject *v, *w;
{
/* XXXX Put w in self under key v */
return 0;
}
static PyMappingMethods cdf_as_mapping = {
(inquiry)cdf_length, /*mp_length*/
(binaryfunc)cdf_subscript, /*mp_subscript*/
(objobjargproc)cdf_ass_sub, /*mp_ass_subscript*/
};
/* -------------------------------------------------------- */
static char Cdftype__doc__[] =
"Code Fragment library symbol table"
;
static PyTypeObject Cdftype = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*ob_size*/
"calldll.fragment", /*tp_name*/
sizeof(cdfobject), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)cdf_dealloc, /*tp_dealloc*/
(printfunc)0, /*tp_print*/
(getattrfunc)cdf_getattr, /*tp_getattr*/
(setattrfunc)0, /*tp_setattr*/
(cmpfunc)0, /*tp_compare*/
(reprfunc)cdf_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
&cdf_as_mapping, /*tp_as_mapping*/
(hashfunc)0, /*tp_hash*/
(ternaryfunc)0, /*tp_call*/
(reprfunc)0, /*tp_str*/
/* Space for future expansion */
0L,0L,0L,0L,
Cdftype__doc__ /* Documentation string */
};
/* End of code for fragment objects */
/* -------------------------------------------------------- */
static char cdll_getlibrary__doc__[] =
"Load a shared library fragment and return the symbol table"
;
static PyObject *
cdll_getlibrary(self, args)
PyObject *self; /* Not used */
PyObject *args;
{
Str255 frag_name;
OSErr err;
Str255 errMessage;
Ptr main_addr;
CFragConnectionID conn_id;
char buf[256];
if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, frag_name))
return NULL;
/* Find the library connection ID */
err = GetSharedLibrary(frag_name, kCompiledCFragArch, kLoadCFrag, &conn_id, &main_addr,
errMessage);
if ( err ) {
sprintf(buf, "%.*s: %s", errMessage[0], errMessage+1, PyMac_StrError(err));
PyErr_SetString(ErrorObject, buf);
return NULL;
}
return (PyObject *)newcdfobject(conn_id, frag_name);
}
static char cdll_getdiskfragment__doc__[] =
"Load a fragment from a disk file and return the symbol table"
;
static PyObject *
cdll_getdiskfragment(self, args)
PyObject *self; /* Not used */
PyObject *args;
{
FSSpec fsspec;
Str255 frag_name;
OSErr err;
Str255 errMessage;
Ptr main_addr;
CFragConnectionID conn_id;
char buf[256];
Boolean isfolder, didsomething;
if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetFSSpec, &fsspec,
PyMac_GetStr255, frag_name))
return NULL;
err = ResolveAliasFile(&fsspec, 1, &isfolder, &didsomething);
if ( err )
return PyErr_Mac(ErrorObject, err);
/* Load the fragment (or return the connID if it is already loaded */
err = GetDiskFragment(&fsspec, 0, 0, frag_name,
kLoadCFrag, &conn_id, &main_addr,
errMessage);
if ( err ) {
sprintf(buf, "%.*s: %s", errMessage[0], errMessage+1, PyMac_StrError(err));
PyErr_SetString(ErrorObject, buf);
return NULL;
}
return (PyObject *)newcdfobject(conn_id, frag_name);
}
static char cdll_newcall__doc__[] =
""
;
static PyObject *
cdll_newcall(self, args)
PyObject *self; /* Not used */
PyObject *args;
{
cdrobject *routine;
conventry *argconv[MAXARG] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
rv2py_converter rvconv;
int npargs, ncargs;
/* Note: the next format depends on MAXARG */
if (!PyArg_ParseTuple(args, "O!O&|O&O&O&O&O&O&O&O&O&O&O&O&", &Cdrtype, &routine,
argparse_rvconv, &rvconv,
argparse_conv, &argconv[0], argparse_conv, &argconv[1],
argparse_conv, &argconv[2], argparse_conv, &argconv[3],
argparse_conv, &argconv[4], argparse_conv, &argconv[5],
argparse_conv, &argconv[6], argparse_conv, &argconv[7],
argparse_conv, &argconv[8], argparse_conv, &argconv[9],
argparse_conv, &argconv[10], argparse_conv, &argconv[11]))
return NULL;
npargs = 0;
for(ncargs=0; ncargs < MAXARG && argconv[ncargs]; ncargs++) {
if( argconv[ncargs]->get_uses_arg ) npargs++;
}
return (PyObject *)newcdcobject(routine, npargs, ncargs, rvconv, argconv);
}
/* List of methods defined in the module */
static struct PyMethodDef cdll_methods[] = {
{"getlibrary", (PyCFunction)cdll_getlibrary, METH_VARARGS,
cdll_getlibrary__doc__},
{"getdiskfragment", (PyCFunction)cdll_getdiskfragment, METH_VARARGS,
cdll_getdiskfragment__doc__},
{"newcall", (PyCFunction)cdll_newcall, METH_VARARGS,
cdll_newcall__doc__},
{NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
};
/* Initialization function for the module (*must* be called initcalldll) */
static char calldll_module_documentation[] =
""
;
void
initcalldll()
{
PyObject *m, *d;
/* Create the module and add the functions */
m = Py_InitModule4("calldll", cdll_methods,
calldll_module_documentation,
(PyObject*)NULL,PYTHON_API_VERSION);
/* Add some symbolic constants to the module */
d = PyModule_GetDict(m);
ErrorObject = PyString_FromString("calldll.error");
PyDict_SetItemString(d, "error", ErrorObject);
/* XXXX Add constants here */
/* Check for errors */
if (PyErr_Occurred())
Py_FatalError("can't initialize module calldll");
}
#ifdef TESTSUPPORT
/* Test routine */
int cdll_b_bbbbbbbb(char a1,char a2,char a3,char a4,char a5,char a6,char a7,char a8)
{
return a1+a2+a3+a4+a5+a6+a7+a8;
}
short cdll_h_hhhhhhhh(short a1,short a2,short a3,short a4,short a5,short a6,short a7,short a8)
{
return a1+a2+a3+a4+a5+a6+a7+a8;
}
int cdll_l_llllllll(int a1,int a2,int a3,int a4,int a5,int a6,int a7,int a8)
{
return a1+a2+a3+a4+a5+a6+a7+a8;
}
void cdll_N_ssssssss(char *a1,char *a2,char *a3,char *a4,char *a5,char *a6,char *a7,char *a8)
{
printf("cdll_N_ssssssss args: %s %s %s %s %s %s %s %s\n", a1, a2, a3, a4,
a5, a6, a7, a8);
}
OSErr cdll_o_l(long l)
{
return (OSErr)l;
}
void cdll_N_pp(unsigned char *in, unsigned char *out)
{
out[0] = in[0] + 5;
strcpy((char *)out+1, "Was: ");
memcpy(out+6, in+1, in[0]);
}
void cdll_N_bb(char a1, char *a2)
{
*a2 = a1;
}
void cdll_N_hh(short a1, short *a2)
{
*a2 = a1;
}
void cdll_N_ll(long a1, long *a2)
{
*a2 = a1;
}
void cdll_N_sH(char *a1, Handle a2)
{
int len;
len = strlen(a1);
SetHandleSize(a2, len);
HLock(a2);
memcpy(*a2, a1, len);
HUnlock(a2);
}
#endif
|