calldll.c 24.4 KB
Newer Older
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
/***********************************************************
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.

******************************************************************/

32 33 34 35 36 37 38 39 40 41
/* Sanity check */
#ifndef __powerc
#error Please port this code to your architecture first...
#endif

/*
** Define to include testroutines (at the end)
*/
#define TESTSUPPORT

42 43 44
#include "Python.h"
#include "macglue.h"
#include "macdefs.h"
45
#include <CodeFragments.h>
46

47
/* Prototypes for routines not in any include file (shame, shame) */
48 49 50 51 52
extern PyObject *ResObj_New Py_PROTO((Handle));
extern int ResObj_Convert Py_PROTO((PyObject *, Handle *));

static PyObject *ErrorObject;

53 54
/* Debugging macro */
#ifdef TESTSUPPORT
55 56
#define PARANOID(arg) \
	if ( arg == 0 ) {PyErr_SetString(ErrorObject, "Internal error: NULL arg!"); return 0; }
57 58 59 60 61
#else
#define PARANOID(arg) /*pass*/
#endif

/* Prototypes we use for routines and arguments */
62 63 64 65

typedef long anything;
typedef anything (*anyroutine) Py_PROTO((...));

66
/* Other constants */
67 68 69 70
#define MAXNAME 31	/* Maximum size of names, for printing only */
#define MAXARG 8	/* Maximum number of arguments */

/*
71 72 73 74 75 76 77
** 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.
78 79 80
*/
typedef anything (*py2c_converter) Py_PROTO((PyObject *));
typedef PyObject *(*c2py_converter) Py_PROTO((anything));
81 82
typedef PyObject *(*rv2py_converter) Py_PROTO((anything));

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

/* 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;
}

112
/* Dummy routine for void return value */
113
static PyObject *
114
rv2py_none(arg)
115 116 117 118 119
	anything arg;
{
	return 0;
}

120
/* Routine to de-allocate storage for input-only arguments */
121
static PyObject *
122
c2py_free(arg)
123 124 125 126
	anything arg;
{
	if ( arg )
		free((char *)arg);
127
	return 0;
128 129 130
}

/*
131
** OSErr return value.
132 133
*/
static PyObject *
134
rv2py_oserr(arg)
135 136
	anything arg;
{
137
	OSErr err = (OSErr)arg;
138
	
139 140 141
	if (err)
		return PyMac_Error(err);
	return 0;
142 143 144
}

/*
145
** Input integers of all sizes (PPC only)
146 147 148 149 150 151 152 153
*/
static anything
py2c_in_int(arg)
	PyObject  *arg;
{
	return PyInt_AsLong(arg);
}

154 155 156 157 158 159 160 161 162 163 164 165 166
/*
** Integer return values of all sizes (PPC only)
*/
static PyObject *
rv2py_int(arg)
	anything arg;
{
	return PyInt_FromLong((long)arg);
}

/*
** Integer output parameters
*/
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
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;
}

265 266 267 268 269 270 271 272 273 274 275 276
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;
}

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
/*
** 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;
}

307 308 309 310 311 312 313 314 315 316 317 318
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;
}

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
/*
** 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;
}

349 350 351 352 353 354 355 356 357 358
static PyObject *
rv2py_handle(arg)
	anything arg;
{
	Handle rv = (Handle)arg;
	
	if ( rv == NULL ) return NULL;
	return ResObj_New(rv);
}

359 360 361 362 363 364 365 366
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[] = {
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
	{"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}
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
};

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;
}	

415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
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;
}	

430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
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;
}

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
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;
}

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
/* ----------------------------------------------------- */

/* Declarations for objects of type fragment */

typedef struct {
	PyObject_HEAD
	CFragConnectionID conn_id;
	char name[MAXNAME+1];
} cdfobject;

staticforward PyTypeObject Cdftype;



/* ---------------------------------------------------------------- */

/* Declarations for objects of type routine */

typedef struct {
	PyObject_HEAD
	anyroutine rtn;
	char name[MAXNAME+1];
} cdrobject;

staticforward PyTypeObject Cdrtype;



/* ---------------------------------------------------------------- */

/* Declarations for objects of type callable */

496

497 498
typedef struct {
	PyObject_HEAD
499 500 501 502 503
	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 */
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
} cdcobject;

staticforward 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;
{
	PyMem_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*/
	"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 *
603
newcdcobject(routine, npargs, ncargs, rvconv, argconv)
604 605 606
	cdrobject *routine;
	int npargs;
	int ncargs;
607
	rvconventry *rvconv;
608 609 610 611 612 613 614 615 616 617 618 619
	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;
620 621
	self->rvconv = rvconv;
	for(i=0; i<MAXARG; i++)
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
		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);
	PyMem_DEL(self);
}


static PyObject *
cdc_repr(self)
	cdcobject *self;
{
	PyObject *s;
	char buf[256];
	int i;
	
646 647
	sprintf(buf, "<callable %s = %s(", self->rvconv->name, self->routine->name);
	for(i=0; i< self->ncargs; i++) {
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
		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;
669 670
	anything c_args[MAXARG] = {0, 0, 0, 0, 0, 0, 0, 0};
	anything c_rv;
671 672 673
	conventry *cp;
	PyObject *curarg;
	anyroutine func;
674 675
	PyObject *returnvalues[MAXARG+1];
	PyObject *rv;
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
	
	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;
708 709
	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]);
710

711
	/* Decode return value, and store into returnvalues if needed */
712
	pargindex = 0;
713 714 715 716 717
	curarg = (*self->rvconv->rtn)(c_rv);
	if ( curarg )
		returnvalues[pargindex++] = curarg;
		
	/* Decode returnvalue parameters and cleanup any storage allocated */
718 719 720
	for(i=0; i<self->ncargs; i++) {
		cp = self->argconv[i];
		curarg = (*cp->put)(c_args[i]);
721 722
		if(curarg)
			returnvalues[pargindex++] = curarg;
723 724 725
		/* NOTE: We only check errors at the end (so we free() everything) */
	}
	if ( PyErr_Occurred() ) {
726 727 728
		/* An error did occur. Free the python objects created */
		for(i=0; i<pargindex; i++)
			Py_XDECREF(returnvalues[i]);
729 730
		return NULL;
	}
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
	
	/* 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]);
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
	return rv;
}

static char Cdctype__doc__[] = 
""
;

static PyTypeObject Cdctype = {
	PyObject_HEAD_INIT(&PyType_Type)
	0,				/*ob_size*/
	"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 */
/* ---------------------------------------------------------------- */

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
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;
}
		

822
static struct PyMethodDef cdf_methods[] = {
823 824
	{"keys",		(PyCFunction)cdf_keys,		METH_VARARGS,	
							cdf_keys__doc__},
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
	
	{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;
{
	PyMem_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 *
873
cdf_getattr_helper(self, name)
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
	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);
}
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

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*/
};

957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
/* -------------------------------------------------------- */

static char Cdftype__doc__[] = 
"Code Fragment library symbol table"
;

static PyTypeObject Cdftype = {
	PyObject_HEAD_INIT(&PyType_Type)
	0,				/*ob_size*/
	"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*/
978
	&cdf_as_mapping,		/*tp_as_mapping*/
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
	(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 */
1012
	err = GetSharedLibrary(frag_name, kCompiledCFragArch, kLoadCFrag, &conn_id, &main_addr, 
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
			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;
1069 1070 1071
	conventry *argconv[MAXARG] = {0, 0, 0, 0, 0, 0, 0, 0};
	rv2py_converter rvconv;
	int npargs, ncargs;
1072

1073
	/* Note: the next format depends on MAXARG */
1074
	if (!PyArg_ParseTuple(args, "O!O&|O&O&O&O&O&O&O&O&", &Cdrtype, &routine,
1075
		argparse_rvconv, &rvconv,
1076 1077 1078
		argparse_conv, &argconv[0], argparse_conv, &argconv[1],
		argparse_conv, &argconv[2], argparse_conv, &argconv[3],
		argparse_conv, &argconv[4], argparse_conv, &argconv[5],
1079
		argparse_conv, &argconv[6], argparse_conv, &argconv[7]))
1080
		return NULL;
1081 1082
	npargs = 0;
	for(ncargs=0; ncargs < MAXARG && argconv[ncargs]; ncargs++) {
1083 1084
		if( argconv[ncargs]->get_uses_arg ) npargs++;
	}
1085
	return (PyObject *)newcdcobject(routine, npargs, ncargs, rvconv, argconv);
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
}

/* 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");
}

1130 1131
#ifdef TESTSUPPORT

1132
/* Test routine */
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
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)
1144
{
1145
	return a1+a2+a3+a4+a5+a6+a7+a8;
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
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