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_query_sortby2(s, strategy, criteria)
395 const char * strategy
396 const char * criteria
399 ZOOM_connection_scan(c, startterm)
401 const char* startterm
404 ZOOM_connection_scan1(c, startterm)
409 ZOOM_scanset_term(scan, pos, occ, len)
420 ZOOM_scanset_display_term(scan, pos, occ, len)
431 ZOOM_scanset_size(scan)
435 ZOOM_scanset_destroy(scan)
439 ZOOM_scanset_option_get(scan, key)
444 ZOOM_scanset_option_set(scan, key, val)
449 # We ignore the return value of ZOOM_options_set_callback(), since it
450 # is always just the address of the __ZOOM_option_callback() function.
451 # The information that we actually want -- the address of the Perl
452 # function in the callback_block -- is unavailable to us, as the
453 # underlying C function doesn't give the block back.
456 ZOOM_options_set_callback(opt, function, handle)
462 /* The tiny amount of memory allocated here is never
463 * released, as options_destroy() doesn't do anything
464 * to the callback information. Not a big deal.
465 * Also, I have no idea how to drive the Perl "mortal"
466 * reference-counting stuff, so I am just allocating
467 * copies which also never get released. Don't sue!
469 struct callback_block *block = (struct callback_block*)
470 xmalloc(sizeof *block);
471 block->function = function;
472 block->handle = handle;
473 SvREFCNT(block->function);
474 SvREFCNT(block->handle);
475 ZOOM_options_set_callback(opt, __ZOOM_option_callback,
480 ZOOM_options_create()
483 ZOOM_options_create_with_parent(parent)
487 ZOOM_options_create_with_parent2(parent1, parent2)
492 ZOOM_options_get(opt, name)
497 ZOOM_options_getl(opt, name, len)
502 RETVAL.data = (char*) ZOOM_options_getl(opt, name, &len);
509 ZOOM_options_set(opt, name, v)
515 ZOOM_options_setl(opt, name, value, len)
522 ZOOM_options_destroy(opt)
526 ZOOM_options_get_bool(opt, name, defa)
532 ZOOM_options_get_int(opt, name, defa)
538 ZOOM_options_set_int(opt, name, value)
544 ZOOM_connection_package(c, options)
549 ZOOM_package_destroy(p)
553 ZOOM_package_send(p, type)
558 ZOOM_package_option_get(p, key)
563 ZOOM_package_option_set(p, key, val)
568 # This has to be called with a single argument which is a _reference_
569 # to an array -- rather than directly with an array, which is of
570 # course identical to passing arbitrarily many arguments. This is
571 # because there doesn't seem to be a way to do varargs in an XS
582 /*printf("* in ZOOM_event(%p)\n", conns);*/
584 /*printf("* argument is not a reference\n");*/
587 realconns = SvRV(conns);
588 /*printf("* realconns = %p\n", realconns);*/
589 if (SvTYPE(realconns) != SVt_PVAV) {
590 /*printf("* reference is not to an array\n");*/
593 n = av_len((AV*) realconns);
594 n++; /* The av_len() return-value is zero-based */
596 /*printf("* No connections in referenced array\n");*/
600 /*printf("* n = %d\n", n);*/
601 if ((cs = (ZOOM_connection*) malloc(n * sizeof *cs)) == 0) {
602 /*printf("* Too many connections (%d)\n", (int) n);*/
606 for (i = 0; i < n; i++) {
607 SV **connp = av_fetch((AV*) realconns, i, (I32) 0);
609 /*printf("* %d of %d: connp = %p\n", (int) i, (int) n,connp);*/
612 /*printf("* conn = %p\n", conn);*/
614 * From here on, the tests and assertions seem to
615 * be ignored: if I pass in a reference to
616 * something other than a ZOOM_connection, or even
617 * if I pass a non-reference, the assertions still
618 * pass and everything seems to work until the
619 * segmentation fault bites.
621 assert(sv_derived_from(conn, "ZOOM_connection"));
622 /*printf("* passed assert(isa(ZOOM_connection))\n");*/
624 /*printf("* passed assert SvROK()\n");*/
625 sv = (SV*) SvRV(conn);
626 /*printf("* sv = %p\n", sv);*/
627 cs[i] = INT2PTR(ZOOM_connection, SvIV(sv));
628 /*printf("got cs[%d] of %d = %p\n", (int) i, (int) n, cs[i]);*/
630 RETVAL = ZOOM_event((int) n, cs);
636 ZOOM_connection_last_event(cs)
640 ZOOM_connection_is_idle(cs)
644 ZOOM_connection_peek_event(cs)
648 # ----------------------------------------------------------------------------
649 # What follows is the YAZ logging API. This is not strictly part of
650 # ZOOM, but it's so useful that it would be silly to omit.
653 yaz_log_mask_str(str)
657 yaz_log_module_level(name)
661 yaz_log_init(level, prefix, name)
667 yaz_log_init_file(fname)
671 yaz_log_init_level(level)
675 yaz_log_init_prefix(prefix)
679 yaz_log_time_format(fmt)
683 yaz_log_init_max_size(mx)
686 # <stdarg.h> interfaces are horrible to code for a Perl-C interface
687 # layer. Instead, we expect Perl applications to construct the
688 # message themselves, and pass it in as an opaque lump.
694 yaz_log(level, "%s", str);
696 # This is also not strictly part of ZOOM
698 yaz_version(version_str, sys_str)