6 #include <yaz/diagsrw.h>
7 #include <yaz/xmalloc.h>
9 #include <yaz/yaz-version.h>
11 /* Used by the *_setl() functions */
12 typedef char opaquechar;
14 /* Used as the return value of the *_getl() functions */
20 /* Used to package Perl function-pointer and user-data together */
21 struct callback_block {
26 /* The callback function used for ZOOM_options_set_callback(). I do
27 * not claim to fully understand all the stack-hacking magic, and less
28 * still the reference-counting/mortality stuff. Accordingly, the
29 * memory management here is best characterised as What I Could Get To
32 const char *__ZOOM_option_callback (void *handle, const char *name)
34 struct callback_block *cb = (struct callback_block*) handle;
47 XPUSHs(sv_2mortal(newSVpv(name, 0)));
49 /* Perl_sv_dump(0, cb->function); */
51 count = call_sv(cb->function, G_SCALAR);
56 croak("callback function for ZOOM_options_get() returned %d values: should have returned exactly one", count);
61 /* ### `res' never gets freed! I think it is
62 * impossible to solve this problem "correctly"
63 * because the ZOOM-C option callback interface is
78 MODULE = Net::Z3950::ZOOM PACKAGE = Net::Z3950::ZOOM PREFIX=ZOOM_
84 ZOOM_connection_new(host, portnum)
89 ZOOM_connection_create(options)
93 ZOOM_connection_connect(c, host, portnum)
99 ZOOM_connection_destroy(c)
103 ZOOM_connection_option_get(c, key)
108 ZOOM_connection_option_getl(c, key, len)
113 RETVAL.data = (char*) ZOOM_connection_option_getl(c, key, &len);
120 ZOOM_connection_option_set(c, key, val)
125 # In ZOOM-C, the `val' parameter is const char*. However, our typemap
126 # treats this as T_PV, i.e. it's "known" that it points to a
127 # NUL-terminated string. Instead, then, I here use opaquechar*, which
128 # is an opaque pointer. The underlying C function can then use this
129 # along with `len' to Do The Right Thing.
132 ZOOM_connection_option_setl(c, key, val, len)
138 # The reference parameters, `cp' and `addinfo', need to already have
139 # values when this function is called, otherwise an "uninitialised
140 # value" warning is generated. As far as I can see, there is no way
141 # around this: no way to specify in a prototype that an argument is
142 # allowed to be undefined, for example. Since these function will
143 # never be called directly by well-behaved client code, but only by
144 # our own wrapper classes, I think we can live with that.
146 # The poxing about with cpp and caddinfo is due to Perl XS's lack of
147 # support for const char**, but who can blame it? If you ask me, the
148 # whole "const" thing was well-intentioned by ghastly mistake.
151 ZOOM_connection_error(c, cp, addinfo)
157 const char *ccp, *caddinfo;
158 RETVAL = ZOOM_connection_error(c, &ccp, &caddinfo);
160 addinfo = (char*) caddinfo;
167 # See comments for ZOOM_connection_error() above
169 ZOOM_connection_error_x(c, cp, addinfo, diagset)
172 const char * &addinfo
173 const char * &diagset
176 const char *ccp, *caddinfo, *cdset;
177 RETVAL = ZOOM_connection_error_x(c, &ccp, &caddinfo, &cdset);
179 addinfo = (char*) caddinfo;
180 diagset = (char*) cdset;
189 ZOOM_connection_errcode(c)
193 ZOOM_connection_errmsg(c)
197 ZOOM_connection_addinfo(c)
201 ZOOM_connection_diagset(c)
209 ZOOM_diag_srw_str(error)
212 RETVAL = yaz_diag_srw_str(error);
217 ZOOM_connection_search(arg0, q)
222 ZOOM_connection_search_pqf(c, q)
227 ZOOM_resultset_destroy(r)
231 ZOOM_resultset_option_get(r, key)
236 ZOOM_resultset_option_set(r, key, val)
242 ZOOM_resultset_size(r)
246 ZOOM_resultset_records(r, start, count, return_records)
253 ZOOM_record *recs = 0;
255 recs = (ZOOM_record*) xmalloc(count * sizeof *recs);
256 ZOOM_resultset_records(r, recs, start, count);
257 if (return_records) {
260 for (i = 0; i < count; i++) {
262 sv_setref_pv(tmp, "ZOOM_record", (void*) recs[i]);
265 RETVAL = newRV((SV*) av);
267 RETVAL = &PL_sv_undef;
274 ZOOM_resultset_record(s, pos)
279 ZOOM_resultset_record_immediate(s, pos)
284 ZOOM_resultset_cache_reset(r)
287 # TESTED (but deprecated)
289 ZOOM_resultset_sort(r, sort_type, sort_spec)
291 const char* sort_type
292 const char* sort_spec
295 ZOOM_resultset_sort1(r, sort_type, sort_spec)
297 const char* sort_type
298 const char* sort_spec
300 # See comments for ZOOM_connection_error() above
302 ZOOM_record_error(rec, cp, addinfo, diagset)
309 const char *ccp = "", *caddinfo = "", *cdset = "";
310 RETVAL = ZOOM_record_error(rec, &ccp, &caddinfo, &cdset);
312 addinfo = (char*) caddinfo;
313 diagset = (char*) cdset;
321 # See "typemap" for discussion of the "const char *" return-type.
323 ZOOM_record_get_string(rec, type)
329 RETVAL = ZOOM_record_get(rec, type, &len);
334 ZOOM_record_get_binary(rec, type)
338 RETVAL.data = (char*) ZOOM_record_get(rec, type, &RETVAL.len);
343 ZOOM_record_destroy(rec)
347 ZOOM_record_clone(srec)
354 ZOOM_query_destroy(s)
358 ZOOM_query_cql(s, str)
363 ZOOM_query_cql2rpn(s, str, conn)
369 ZOOM_query_ccl2rpn(s, query_str, config, errcode, errstr, errpos)
371 const char* query_str
383 ZOOM_query_prefix(s, str)
388 ZOOM_query_sortby(s, criteria)
390 const char * criteria
393 ZOOM_connection_scan(c, startterm)
395 const char* startterm
398 ZOOM_connection_scan1(c, startterm)
403 ZOOM_scanset_term(scan, pos, occ, len)
414 ZOOM_scanset_display_term(scan, pos, occ, len)
425 ZOOM_scanset_size(scan)
429 ZOOM_scanset_destroy(scan)
433 ZOOM_scanset_option_get(scan, key)
438 ZOOM_scanset_option_set(scan, key, val)
443 # We ignore the return value of ZOOM_options_set_callback(), since it
444 # is always just the address of the __ZOOM_option_callback() function.
445 # The information that we actually want -- the address of the Perl
446 # function in the callback_block -- is unavailable to us, as the
447 # underlying C function doesn't give the block back.
450 ZOOM_options_set_callback(opt, function, handle)
456 /* The tiny amount of memory allocated here is never
457 * released, as options_destroy() doesn't do anything
458 * to the callback information. Not a big deal.
459 * Also, I have no idea how to drive the Perl "mortal"
460 * reference-counting stuff, so I am just allocating
461 * copies which also never get released. Don't sue!
463 struct callback_block *block = (struct callback_block*)
464 xmalloc(sizeof *block);
465 block->function = function;
466 block->handle = handle;
467 SvREFCNT(block->function);
468 SvREFCNT(block->handle);
469 ZOOM_options_set_callback(opt, __ZOOM_option_callback,
474 ZOOM_options_create()
477 ZOOM_options_create_with_parent(parent)
481 ZOOM_options_create_with_parent2(parent1, parent2)
486 ZOOM_options_get(opt, name)
491 ZOOM_options_getl(opt, name, len)
496 RETVAL.data = (char*) ZOOM_options_getl(opt, name, &len);
503 ZOOM_options_set(opt, name, v)
509 ZOOM_options_setl(opt, name, value, len)
516 ZOOM_options_destroy(opt)
520 ZOOM_options_get_bool(opt, name, defa)
526 ZOOM_options_get_int(opt, name, defa)
532 ZOOM_options_set_int(opt, name, value)
538 ZOOM_connection_package(c, options)
543 ZOOM_package_destroy(p)
547 ZOOM_package_send(p, type)
552 ZOOM_package_option_get(p, key)
557 ZOOM_package_option_set(p, key, val)
562 # This has to be called with a single argument which is a _reference_
563 # to an array -- rather than directly with an array, which is of
564 # course identical to passing arbitrarily many arguments. This is
565 # because there doesn't seem to be a way to do varargs in an XS
576 /*printf("* in ZOOM_event(%p)\n", conns);*/
578 /*printf("* argument is not a reference\n");*/
581 realconns = SvRV(conns);
582 /*printf("* realconns = %p\n", realconns);*/
583 if (SvTYPE(realconns) != SVt_PVAV) {
584 /*printf("* reference is not to an array\n");*/
587 n = av_len((AV*) realconns);
588 n++; /* The av_len() return-value is zero-based */
590 /*printf("* No connections in referenced array\n");*/
594 /*printf("* n = %d\n", n);*/
595 if ((cs = (ZOOM_connection*) malloc(n * sizeof *cs)) == 0) {
596 /*printf("* Too many connections (%d)\n", (int) n);*/
600 for (i = 0; i < n; i++) {
601 SV **connp = av_fetch((AV*) realconns, i, (I32) 0);
603 /*printf("* %d of %d: connp = %p\n", (int) i, (int) n,connp);*/
606 /*printf("* conn = %p\n", conn);*/
608 * From here on, the tests and assertions seem to
609 * be ignored: if I pass in a reference to
610 * something other than a ZOOM_connection, or even
611 * if I pass a non-reference, the assertions still
612 * pass and everything seems to work until the
613 * segmentation fault bites.
615 assert(sv_derived_from(conn, "ZOOM_connection"));
616 /*printf("* passed assert(isa(ZOOM_connection))\n");*/
618 /*printf("* passed assert SvROK()\n");*/
619 sv = (SV*) SvRV(conn);
620 /*printf("* sv = %p\n", sv);*/
621 cs[i] = INT2PTR(ZOOM_connection, SvIV(sv));
622 /*printf("got cs[%d] of %d = %p\n", (int) i, (int) n, cs[i]);*/
624 RETVAL = ZOOM_event((int) n, cs);
630 ZOOM_connection_last_event(cs)
634 ZOOM_connection_is_idle(cs)
638 ZOOM_connection_peek_event(cs)
642 # ----------------------------------------------------------------------------
643 # What follows is the YAZ logging API. This is not strictly part of
644 # ZOOM, but it's so useful that it would be silly to omit.
647 yaz_log_mask_str(str)
651 yaz_log_module_level(name)
655 yaz_log_init(level, prefix, name)
661 yaz_log_init_file(fname)
665 yaz_log_init_level(level)
669 yaz_log_init_prefix(prefix)
673 yaz_log_time_format(fmt)
677 yaz_log_init_max_size(mx)
680 # <stdarg.h> interfaces are horrible to code for a Perl-C interface
681 # layer. Instead, we expect Perl applications to construct the
682 # message themselves, and pass it in as an opaque lump.
688 yaz_log(level, "%s", str);
690 # This is also not strictly part of ZOOM
692 yaz_version(version_str, sys_str)