1 /* $Id: ZOOM.xs,v 1.43 2006-10-04 17:14:12 mike Exp $ */
8 #include <yaz/xmalloc.h>
10 /* Used by the *_setl() functions */
11 typedef char opaquechar;
13 /* Used as the return value of the *_getl() functions */
19 /* Used to package Perl function-pointer and user-data together */
20 struct callback_block {
25 /* The callback function used for ZOOM_options_set_callback(). I do
26 * not claim to fully understand all the stack-hacking magic, and less
27 * still the reference-counting/mortality stuff. Accordingly, the
28 * memory management here is best characterised as What I Could Get To
31 const char *__ZOOM_option_callback (void *handle, const char *name)
33 struct callback_block *cb = (struct callback_block*) handle;
46 XPUSHs(sv_2mortal(newSVpv(name, 0)));
48 /* Perl_sv_dump(0, cb->function); */
50 count = call_sv(cb->function, G_SCALAR);
55 croak("callback function for ZOOM_options_get() returned %d values: should have returned exactly one", count);
60 /* ### `res' never gets freed! I think it is
61 * impossible to solve this problem "correctly"
62 * because the ZOOM-C option callback interface is
77 MODULE = Net::Z3950::ZOOM PACKAGE = Net::Z3950::ZOOM PREFIX=ZOOM_
83 ZOOM_connection_new(host, portnum)
88 ZOOM_connection_create(options)
92 ZOOM_connection_connect(c, host, portnum)
98 ZOOM_connection_destroy(c)
102 ZOOM_connection_option_get(c, key)
107 ZOOM_connection_option_getl(c, key, len)
112 RETVAL.data = (char*) ZOOM_connection_option_getl(c, key, &len);
119 ZOOM_connection_option_set(c, key, val)
124 # In ZOOM-C, the `val' parameter is const char*. However, our typemap
125 # treats this as T_PV, i.e. it's "known" that it points to a
126 # NUL-terminated string. Instead, then, I here use opaquechar*, which
127 # is an opaque pointer. The underlying C function can then use this
128 # along with `len' to Do The Right Thing.
131 ZOOM_connection_option_setl(c, key, val, len)
137 # The reference parameters, `cp' and `addinfo', need to already have
138 # values when this function is called, otherwise an "uninitialised
139 # value" warning is generated. As far as I can see, there is no way
140 # around this: no way to specify in a prototype that an argument is
141 # allowed to be undefined, for example. Since these function will
142 # never be called directly by well-behaved client code, but only by
143 # our own wrapper classes, I think we can live with that.
145 # The poxing about with cpp and caddinfo is due to Perl XS's lack of
146 # support for const char**, but who can blame it? If you ask me, the
147 # whole "const" thing was well-intentioned by ghastly mistake.
150 ZOOM_connection_error(c, cp, addinfo)
156 const char *ccp, *caddinfo;
157 RETVAL = ZOOM_connection_error(c, &ccp, &caddinfo);
159 addinfo = (char*) caddinfo;
166 # See comments for ZOOM_connection_error() above
168 ZOOM_connection_error_x(c, cp, addinfo, diagset)
171 const char * &addinfo
172 const char * &diagset
175 const char *ccp, *caddinfo, *cdset;
176 RETVAL = ZOOM_connection_error_x(c, &ccp, &caddinfo, &cdset);
178 addinfo = (char*) caddinfo;
179 diagset = (char*) cdset;
188 ZOOM_connection_errcode(c)
192 ZOOM_connection_errmsg(c)
196 ZOOM_connection_addinfo(c)
200 ZOOM_connection_diagset(c)
208 ZOOM_connection_search(arg0, q)
213 ZOOM_connection_search_pqf(c, q)
218 ZOOM_resultset_destroy(r)
222 ZOOM_resultset_option_get(r, key)
227 ZOOM_resultset_option_set(r, key, val)
233 ZOOM_resultset_size(r)
237 ZOOM_resultset_records(r, start, count, return_records)
244 ZOOM_record *recs = 0;
246 recs = (ZOOM_record*) xmalloc(count * sizeof *recs);
247 ZOOM_resultset_records(r, recs, start, count);
248 if (return_records) {
251 for (i = 0; i < count; i++) {
253 sv_setref_pv(tmp, "ZOOM_record", (void*) recs[i]);
256 RETVAL = newRV((SV*) av);
258 RETVAL = &PL_sv_undef;
265 ZOOM_resultset_record(s, pos)
270 ZOOM_resultset_record_immediate(s, pos)
275 ZOOM_resultset_cache_reset(r)
278 # TESTED (but deprecated)
280 ZOOM_resultset_sort(r, sort_type, sort_spec)
282 const char* sort_type
283 const char* sort_spec
286 ZOOM_resultset_sort1(r, sort_type, sort_spec)
288 const char* sort_type
289 const char* sort_spec
291 # See "typemap" for discussion of the "const char *" return-type.
293 ### but should use datachunk for in some (not all!) cases.
295 ZOOM_record_get(rec, type, len)
304 ZOOM_record_destroy(rec)
308 ZOOM_record_clone(srec)
315 ZOOM_query_destroy(s)
319 ZOOM_query_cql(s, str)
324 ZOOM_query_cql2rpn(s, str, conn)
330 ZOOM_query_ccl2rpn(s, query_str, config, errcode, errstr, errpos)
332 const char* query_str
344 ZOOM_query_prefix(s, str)
349 ZOOM_query_sortby(s, criteria)
351 const char * criteria
354 ZOOM_connection_scan(c, startterm)
356 const char* startterm
359 ZOOM_connection_scan1(c, startterm)
364 ZOOM_scanset_term(scan, pos, occ, len)
375 ZOOM_scanset_display_term(scan, pos, occ, len)
386 ZOOM_scanset_size(scan)
390 ZOOM_scanset_destroy(scan)
394 ZOOM_scanset_option_get(scan, key)
399 ZOOM_scanset_option_set(scan, key, val)
404 # We ignore the return value of ZOOM_options_set_callback(), since it
405 # is always just the address of the __ZOOM_option_callback() function.
406 # The information that we actually want -- the address of the Perl
407 # function in the callback_block -- is unavailable to us, as the
408 # underlying C function doesn't give the block back.
411 ZOOM_options_set_callback(opt, function, handle)
417 /* The tiny amount of memory allocated here is never
418 * released, as options_destroy() doesn't do anything
419 * to the callback information. Not a big deal.
420 * Also, I have no idea how to drive the Perl "mortal"
421 * reference-counting stuff, so I am just allocating
422 * copies which also never get released. Don't sue!
424 struct callback_block *block = (struct callback_block*)
425 xmalloc(sizeof *block);
426 block->function = function;
427 block->handle = handle;
428 SvREFCNT(block->function);
429 SvREFCNT(block->handle);
430 ZOOM_options_set_callback(opt, __ZOOM_option_callback,
435 ZOOM_options_create()
438 ZOOM_options_create_with_parent(parent)
442 ZOOM_options_create_with_parent2(parent1, parent2)
447 ZOOM_options_get(opt, name)
452 ZOOM_options_getl(opt, name, len)
457 RETVAL.data = (char*) ZOOM_options_getl(opt, name, &len);
464 ZOOM_options_set(opt, name, v)
470 ZOOM_options_setl(opt, name, value, len)
477 ZOOM_options_destroy(opt)
481 ZOOM_options_get_bool(opt, name, defa)
487 ZOOM_options_get_int(opt, name, defa)
493 ZOOM_options_set_int(opt, name, value)
499 ZOOM_connection_package(c, options)
504 ZOOM_package_destroy(p)
508 ZOOM_package_send(p, type)
513 ZOOM_package_option_get(p, key)
518 ZOOM_package_option_set(p, key, val)
523 # This has to be called with a single argument which is a _reference_
524 # to an array -- rather than directly with an array, which is of
525 # course identical to passing arbitrarily many arguments. This is
526 # because there doesn't seem to be a way to do varargs in an XS
536 ZOOM_connection cs[100];
538 /*printf("* in ZOOM_event(%p)\n", conns);*/
540 /*printf("* argument is not a reference\n");*/
543 realconns = SvRV(conns);
544 /*printf("* realconns = %p\n", realconns);*/
545 if (SvTYPE(realconns) != SVt_PVAV) {
546 /*printf("* reference is not to an array\n");*/
549 n = av_len((AV*) realconns);
550 n++; /* The av_len() return-value is zero-based */
552 /*printf("* No connections in referenced array\n");*/
554 } else if (n >= sizeof(cs)/sizeof(cs[0])) {
555 /*printf("* Too many connections (%d)\n", (int) n);*/
559 /*printf("* n = %d\n", n);*/
560 for (i = 0; i < n; i++) {
561 SV **connp = av_fetch((AV*) realconns, i, (I32) 0);
563 /*printf("* %d of %d: connp = %p\n", (int) i, (int) n,connp);*/
566 /*printf("* conn = %p\n", conn);*/
568 * From here on, the tests and assertions seem to
569 * be ignored: if I pass in a reference to
570 * something other than a ZOOM_connection, or even
571 * if I pass a non-reference, the assertions still
572 * pass and everything seems to work until the
573 * segmentation fault bites.
575 assert(sv_derived_from(conn, "ZOOM_connection"));
576 /*printf("* passed assert(isa(ZOOM_connection))\n");*/
578 /*printf("* passed assert SvROK()\n");*/
579 sv = (SV*) SvRV(conn);
580 /*printf("* sv = %p\n", sv);*/
581 cs[i] = INT2PTR(ZOOM_connection, SvIV(sv));
582 /*printf("got cs[%d] of %d = %p\n", (int) i, (int) n, cs[i]);*/
584 RETVAL = ZOOM_event((int) n, cs);
589 ZOOM_connection_last_event(cs)
593 ZOOM_connection_is_idle(cs)
597 # ----------------------------------------------------------------------------
598 # What follows is the YAZ logging API. This is not strictly part of
599 # ZOOM, but it's so useful that it would be silly to omit.
602 yaz_log_mask_str(str)
606 yaz_log_module_level(name)
610 yaz_log_init(level, prefix, name)
616 yaz_log_init_file(fname)
620 yaz_log_init_level(level)
624 yaz_log_init_prefix(prefix)
628 yaz_log_time_format(fmt)
632 yaz_log_init_max_size(mx)
635 # <stdarg.h> interfaces are horrible to code for a Perl-C interface
636 # layer. Instead, we expect Perl applications to construct the
637 # message themselves, and pass it in as an opaque lump.
643 yaz_log(level, "%s", str);
645 # This is also not strictly part of ZOOM
647 yaz_version(version_str, sys_str)