1 /* $Id: ZOOM.xs,v 1.49 2007-10-29 12:06:57 mike Exp $ */
8 #include <yaz/diagsrw.h>
9 #include <yaz/xmalloc.h>
12 /* Used by the *_setl() functions */
13 typedef char opaquechar;
15 /* Used as the return value of the *_getl() functions */
21 /* Used to package Perl function-pointer and user-data together */
22 struct callback_block {
27 /* The callback function used for ZOOM_options_set_callback(). I do
28 * not claim to fully understand all the stack-hacking magic, and less
29 * still the reference-counting/mortality stuff. Accordingly, the
30 * memory management here is best characterised as What I Could Get To
33 const char *__ZOOM_option_callback (void *handle, const char *name)
35 struct callback_block *cb = (struct callback_block*) handle;
48 XPUSHs(sv_2mortal(newSVpv(name, 0)));
50 /* Perl_sv_dump(0, cb->function); */
52 count = call_sv(cb->function, G_SCALAR);
57 croak("callback function for ZOOM_options_get() returned %d values: should have returned exactly one", count);
62 /* ### `res' never gets freed! I think it is
63 * impossible to solve this problem "correctly"
64 * because the ZOOM-C option callback interface is
79 MODULE = Net::Z3950::ZOOM PACKAGE = Net::Z3950::ZOOM PREFIX=ZOOM_
85 ZOOM_connection_new(host, portnum)
90 ZOOM_connection_create(options)
94 ZOOM_connection_connect(c, host, portnum)
100 ZOOM_connection_destroy(c)
104 ZOOM_connection_option_get(c, key)
109 ZOOM_connection_option_getl(c, key, len)
114 RETVAL.data = (char*) ZOOM_connection_option_getl(c, key, &len);
121 ZOOM_connection_option_set(c, key, val)
126 # In ZOOM-C, the `val' parameter is const char*. However, our typemap
127 # treats this as T_PV, i.e. it's "known" that it points to a
128 # NUL-terminated string. Instead, then, I here use opaquechar*, which
129 # is an opaque pointer. The underlying C function can then use this
130 # along with `len' to Do The Right Thing.
133 ZOOM_connection_option_setl(c, key, val, len)
139 # The reference parameters, `cp' and `addinfo', need to already have
140 # values when this function is called, otherwise an "uninitialised
141 # value" warning is generated. As far as I can see, there is no way
142 # around this: no way to specify in a prototype that an argument is
143 # allowed to be undefined, for example. Since these function will
144 # never be called directly by well-behaved client code, but only by
145 # our own wrapper classes, I think we can live with that.
147 # The poxing about with cpp and caddinfo is due to Perl XS's lack of
148 # support for const char**, but who can blame it? If you ask me, the
149 # whole "const" thing was well-intentioned by ghastly mistake.
152 ZOOM_connection_error(c, cp, addinfo)
158 const char *ccp, *caddinfo;
159 RETVAL = ZOOM_connection_error(c, &ccp, &caddinfo);
161 addinfo = (char*) caddinfo;
168 # See comments for ZOOM_connection_error() above
170 ZOOM_connection_error_x(c, cp, addinfo, diagset)
173 const char * &addinfo
174 const char * &diagset
177 const char *ccp, *caddinfo, *cdset;
178 RETVAL = ZOOM_connection_error_x(c, &ccp, &caddinfo, &cdset);
180 addinfo = (char*) caddinfo;
181 diagset = (char*) cdset;
190 ZOOM_connection_errcode(c)
194 ZOOM_connection_errmsg(c)
198 ZOOM_connection_addinfo(c)
202 ZOOM_connection_diagset(c)
210 ZOOM_diag_srw_str(error)
213 RETVAL = yaz_diag_srw_str(error);
218 ZOOM_connection_search(arg0, q)
223 ZOOM_connection_search_pqf(c, q)
228 ZOOM_resultset_destroy(r)
232 ZOOM_resultset_option_get(r, key)
237 ZOOM_resultset_option_set(r, key, val)
243 ZOOM_resultset_size(r)
247 ZOOM_resultset_records(r, start, count, return_records)
254 ZOOM_record *recs = 0;
256 recs = (ZOOM_record*) xmalloc(count * sizeof *recs);
257 ZOOM_resultset_records(r, recs, start, count);
258 if (return_records) {
261 for (i = 0; i < count; i++) {
263 sv_setref_pv(tmp, "ZOOM_record", (void*) recs[i]);
266 RETVAL = newRV((SV*) av);
268 RETVAL = &PL_sv_undef;
275 ZOOM_resultset_record(s, pos)
280 ZOOM_resultset_record_immediate(s, pos)
285 ZOOM_resultset_cache_reset(r)
288 # TESTED (but deprecated)
290 ZOOM_resultset_sort(r, sort_type, sort_spec)
292 const char* sort_type
293 const char* sort_spec
296 ZOOM_resultset_sort1(r, sort_type, sort_spec)
298 const char* sort_type
299 const char* sort_spec
301 # See comments for ZOOM_connection_error() above
303 ZOOM_record_error(rec, cp, addinfo, diagset)
310 const char *ccp = "", *caddinfo = "", *cdset = "";
311 RETVAL = ZOOM_record_error(rec, &ccp, &caddinfo, &cdset);
313 addinfo = (char*) caddinfo;
314 diagset = (char*) cdset;
322 # See "typemap" for discussion of the "const char *" return-type.
324 ### but should use datachunk for in some (not all!) cases.
326 ZOOM_record_get(rec, type, len)
335 ZOOM_record_destroy(rec)
339 ZOOM_record_clone(srec)
346 ZOOM_query_destroy(s)
350 ZOOM_query_cql(s, str)
355 ZOOM_query_cql2rpn(s, str, conn)
361 ZOOM_query_ccl2rpn(s, query_str, config, errcode, errstr, errpos)
363 const char* query_str
375 ZOOM_query_prefix(s, str)
380 ZOOM_query_sortby(s, criteria)
382 const char * criteria
385 ZOOM_connection_scan(c, startterm)
387 const char* startterm
390 ZOOM_connection_scan1(c, startterm)
395 ZOOM_scanset_term(scan, pos, occ, len)
406 ZOOM_scanset_display_term(scan, pos, occ, len)
417 ZOOM_scanset_size(scan)
421 ZOOM_scanset_destroy(scan)
425 ZOOM_scanset_option_get(scan, key)
430 ZOOM_scanset_option_set(scan, key, val)
435 # We ignore the return value of ZOOM_options_set_callback(), since it
436 # is always just the address of the __ZOOM_option_callback() function.
437 # The information that we actually want -- the address of the Perl
438 # function in the callback_block -- is unavailable to us, as the
439 # underlying C function doesn't give the block back.
442 ZOOM_options_set_callback(opt, function, handle)
448 /* The tiny amount of memory allocated here is never
449 * released, as options_destroy() doesn't do anything
450 * to the callback information. Not a big deal.
451 * Also, I have no idea how to drive the Perl "mortal"
452 * reference-counting stuff, so I am just allocating
453 * copies which also never get released. Don't sue!
455 struct callback_block *block = (struct callback_block*)
456 xmalloc(sizeof *block);
457 block->function = function;
458 block->handle = handle;
459 SvREFCNT(block->function);
460 SvREFCNT(block->handle);
461 ZOOM_options_set_callback(opt, __ZOOM_option_callback,
466 ZOOM_options_create()
469 ZOOM_options_create_with_parent(parent)
473 ZOOM_options_create_with_parent2(parent1, parent2)
478 ZOOM_options_get(opt, name)
483 ZOOM_options_getl(opt, name, len)
488 RETVAL.data = (char*) ZOOM_options_getl(opt, name, &len);
495 ZOOM_options_set(opt, name, v)
501 ZOOM_options_setl(opt, name, value, len)
508 ZOOM_options_destroy(opt)
512 ZOOM_options_get_bool(opt, name, defa)
518 ZOOM_options_get_int(opt, name, defa)
524 ZOOM_options_set_int(opt, name, value)
530 ZOOM_connection_package(c, options)
535 ZOOM_package_destroy(p)
539 ZOOM_package_send(p, type)
544 ZOOM_package_option_get(p, key)
549 ZOOM_package_option_set(p, key, val)
554 # This has to be called with a single argument which is a _reference_
555 # to an array -- rather than directly with an array, which is of
556 # course identical to passing arbitrarily many arguments. This is
557 # because there doesn't seem to be a way to do varargs in an XS
569 /*printf("* in ZOOM_event(%p)\n", conns);*/
571 /*printf("* argument is not a reference\n");*/
574 realconns = SvRV(conns);
575 /*printf("* realconns = %p\n", realconns);*/
576 if (SvTYPE(realconns) != SVt_PVAV) {
577 /*printf("* reference is not to an array\n");*/
580 n = av_len((AV*) realconns);
581 n++; /* The av_len() return-value is zero-based */
583 /*printf("* No connections in referenced array\n");*/
587 /*printf("* n = %d\n", n);*/
588 if ((cs = (ZOOM_connection*) malloc(n * sizeof *cs)) == 0) {
589 /*printf("* Too many connections (%d)\n", (int) n);*/
593 for (i = 0; i < n; i++) {
594 SV **connp = av_fetch((AV*) realconns, i, (I32) 0);
596 /*printf("* %d of %d: connp = %p\n", (int) i, (int) n,connp);*/
599 /*printf("* conn = %p\n", conn);*/
601 * From here on, the tests and assertions seem to
602 * be ignored: if I pass in a reference to
603 * something other than a ZOOM_connection, or even
604 * if I pass a non-reference, the assertions still
605 * pass and everything seems to work until the
606 * segmentation fault bites.
608 assert(sv_derived_from(conn, "ZOOM_connection"));
609 /*printf("* passed assert(isa(ZOOM_connection))\n");*/
611 /*printf("* passed assert SvROK()\n");*/
612 sv = (SV*) SvRV(conn);
613 /*printf("* sv = %p\n", sv);*/
614 cs[i] = INT2PTR(ZOOM_connection, SvIV(sv));
615 /*printf("got cs[%d] of %d = %p\n", (int) i, (int) n, cs[i]);*/
617 RETVAL = ZOOM_event((int) n, cs);
623 ZOOM_connection_last_event(cs)
627 ZOOM_connection_is_idle(cs)
631 ZOOM_connection_peek_event(cs)
635 # ----------------------------------------------------------------------------
636 # What follows is the YAZ logging API. This is not strictly part of
637 # ZOOM, but it's so useful that it would be silly to omit.
640 yaz_log_mask_str(str)
644 yaz_log_module_level(name)
648 yaz_log_init(level, prefix, name)
654 yaz_log_init_file(fname)
658 yaz_log_init_level(level)
662 yaz_log_init_prefix(prefix)
666 yaz_log_time_format(fmt)
670 yaz_log_init_max_size(mx)
673 # <stdarg.h> interfaces are horrible to code for a Perl-C interface
674 # layer. Instead, we expect Perl applications to construct the
675 # message themselves, and pass it in as an opaque lump.
681 yaz_log(level, "%s", str);
683 # This is also not strictly part of ZOOM
685 yaz_version(version_str, sys_str)