1 /* $Id: ZOOM.xs,v 1.47 2007-02-22 20:37:47 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 comments for ZOOM_connection_error() above
293 ZOOM_record_error(rec, cp, addinfo, diagset)
300 const char *ccp = "", *caddinfo = "", *cdset = "";
301 RETVAL = ZOOM_record_error(rec, &ccp, &caddinfo, &cdset);
303 addinfo = (char*) caddinfo;
304 diagset = (char*) cdset;
312 # See "typemap" for discussion of the "const char *" return-type.
314 ### but should use datachunk for in some (not all!) cases.
316 ZOOM_record_get(rec, type, len)
325 ZOOM_record_destroy(rec)
329 ZOOM_record_clone(srec)
336 ZOOM_query_destroy(s)
340 ZOOM_query_cql(s, str)
345 ZOOM_query_cql2rpn(s, str, conn)
351 ZOOM_query_ccl2rpn(s, query_str, config, errcode, errstr, errpos)
353 const char* query_str
365 ZOOM_query_prefix(s, str)
370 ZOOM_query_sortby(s, criteria)
372 const char * criteria
375 ZOOM_connection_scan(c, startterm)
377 const char* startterm
380 ZOOM_connection_scan1(c, startterm)
385 ZOOM_scanset_term(scan, pos, occ, len)
396 ZOOM_scanset_display_term(scan, pos, occ, len)
407 ZOOM_scanset_size(scan)
411 ZOOM_scanset_destroy(scan)
415 ZOOM_scanset_option_get(scan, key)
420 ZOOM_scanset_option_set(scan, key, val)
425 # We ignore the return value of ZOOM_options_set_callback(), since it
426 # is always just the address of the __ZOOM_option_callback() function.
427 # The information that we actually want -- the address of the Perl
428 # function in the callback_block -- is unavailable to us, as the
429 # underlying C function doesn't give the block back.
432 ZOOM_options_set_callback(opt, function, handle)
438 /* The tiny amount of memory allocated here is never
439 * released, as options_destroy() doesn't do anything
440 * to the callback information. Not a big deal.
441 * Also, I have no idea how to drive the Perl "mortal"
442 * reference-counting stuff, so I am just allocating
443 * copies which also never get released. Don't sue!
445 struct callback_block *block = (struct callback_block*)
446 xmalloc(sizeof *block);
447 block->function = function;
448 block->handle = handle;
449 SvREFCNT(block->function);
450 SvREFCNT(block->handle);
451 ZOOM_options_set_callback(opt, __ZOOM_option_callback,
456 ZOOM_options_create()
459 ZOOM_options_create_with_parent(parent)
463 ZOOM_options_create_with_parent2(parent1, parent2)
468 ZOOM_options_get(opt, name)
473 ZOOM_options_getl(opt, name, len)
478 RETVAL.data = (char*) ZOOM_options_getl(opt, name, &len);
485 ZOOM_options_set(opt, name, v)
491 ZOOM_options_setl(opt, name, value, len)
498 ZOOM_options_destroy(opt)
502 ZOOM_options_get_bool(opt, name, defa)
508 ZOOM_options_get_int(opt, name, defa)
514 ZOOM_options_set_int(opt, name, value)
520 ZOOM_connection_package(c, options)
525 ZOOM_package_destroy(p)
529 ZOOM_package_send(p, type)
534 ZOOM_package_option_get(p, key)
539 ZOOM_package_option_set(p, key, val)
544 # This has to be called with a single argument which is a _reference_
545 # to an array -- rather than directly with an array, which is of
546 # course identical to passing arbitrarily many arguments. This is
547 # because there doesn't seem to be a way to do varargs in an XS
559 /*printf("* in ZOOM_event(%p)\n", conns);*/
561 /*printf("* argument is not a reference\n");*/
564 realconns = SvRV(conns);
565 /*printf("* realconns = %p\n", realconns);*/
566 if (SvTYPE(realconns) != SVt_PVAV) {
567 /*printf("* reference is not to an array\n");*/
570 n = av_len((AV*) realconns);
571 n++; /* The av_len() return-value is zero-based */
573 /*printf("* No connections in referenced array\n");*/
577 /*printf("* n = %d\n", n);*/
578 if ((cs = (ZOOM_connection*) malloc(n * sizeof *cs)) == 0) {
579 /*printf("* Too many connections (%d)\n", (int) n);*/
583 for (i = 0; i < n; i++) {
584 SV **connp = av_fetch((AV*) realconns, i, (I32) 0);
586 /*printf("* %d of %d: connp = %p\n", (int) i, (int) n,connp);*/
589 /*printf("* conn = %p\n", conn);*/
591 * From here on, the tests and assertions seem to
592 * be ignored: if I pass in a reference to
593 * something other than a ZOOM_connection, or even
594 * if I pass a non-reference, the assertions still
595 * pass and everything seems to work until the
596 * segmentation fault bites.
598 assert(sv_derived_from(conn, "ZOOM_connection"));
599 /*printf("* passed assert(isa(ZOOM_connection))\n");*/
601 /*printf("* passed assert SvROK()\n");*/
602 sv = (SV*) SvRV(conn);
603 /*printf("* sv = %p\n", sv);*/
604 cs[i] = INT2PTR(ZOOM_connection, SvIV(sv));
605 /*printf("got cs[%d] of %d = %p\n", (int) i, (int) n, cs[i]);*/
607 RETVAL = ZOOM_event((int) n, cs);
613 ZOOM_connection_last_event(cs)
617 ZOOM_connection_is_idle(cs)
621 ZOOM_connection_peek_event(cs)
625 # ----------------------------------------------------------------------------
626 # What follows is the YAZ logging API. This is not strictly part of
627 # ZOOM, but it's so useful that it would be silly to omit.
630 yaz_log_mask_str(str)
634 yaz_log_module_level(name)
638 yaz_log_init(level, prefix, name)
644 yaz_log_init_file(fname)
648 yaz_log_init_level(level)
652 yaz_log_init_prefix(prefix)
656 yaz_log_time_format(fmt)
660 yaz_log_init_max_size(mx)
663 # <stdarg.h> interfaces are horrible to code for a Perl-C interface
664 # layer. Instead, we expect Perl applications to construct the
665 # message themselves, and pass it in as an opaque lump.
671 yaz_log(level, "%s", str);
673 # This is also not strictly part of ZOOM
675 yaz_version(version_str, sys_str)