- Add new function Net::Z3950::ZOOM::query_ccl2rpn(), for
[ZOOM-Perl-moved-to-github.git] / ZOOM.xs
1 /* $Id: ZOOM.xs,v 1.41 2006-06-13 16:44:21 mike Exp $ */
2
3 #include "EXTERN.h"
4 #include "perl.h"
5 #include "XSUB.h"
6
7 #include <yaz/zoom.h>
8 #include <yaz/xmalloc.h>
9
10 /* Used by the *_setl() functions */
11 typedef char opaquechar;
12
13 /* Used as the return value of the *_getl() functions */
14 struct datachunk {
15         char *data;
16         int len;
17 };
18
19 /* Used to package Perl function-pointer and user-data together */
20 struct callback_block {
21         SV *function;
22         SV *handle;
23 };
24
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
29  * Work, More Or Less.
30  */
31 const char *__ZOOM_option_callback (void *handle, const char *name)
32 {
33         struct callback_block *cb = (struct callback_block*) handle;
34         int count;
35         SV *ret;
36         char *s;
37         char *res;
38
39         dSP;
40
41         ENTER;
42         SAVETMPS;
43
44         PUSHMARK(SP);
45         XPUSHs(cb->handle);
46         XPUSHs(sv_2mortal(newSVpv(name, 0)));
47         PUTBACK;
48         /* Perl_sv_dump(0, cb->function); */
49
50         count = call_sv(cb->function, G_SCALAR);
51
52         SPAGAIN;
53
54         if (count != 1)
55                 croak("callback function for ZOOM_options_get() returned %d values: should have returned exactly one", count);
56
57         ret = POPs;
58         if (SvPOK(ret)) {
59                 s = SvPV_nolen(ret);
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
63                  * inadequate. */
64                 res = xstrdup(s);
65         } else {
66                 res = 0;
67         }
68
69         PUTBACK;
70         FREETMPS;
71         LEAVE;
72
73         return res;
74 }
75
76
77 MODULE = Net::Z3950::ZOOM               PACKAGE = Net::Z3950::ZOOM              PREFIX=ZOOM_
78
79 PROTOTYPES: ENABLE
80
81
82 ZOOM_connection
83 ZOOM_connection_new(host, portnum)
84         const char* host
85         int portnum
86
87 ZOOM_connection
88 ZOOM_connection_create(options)
89         ZOOM_options options
90
91 void
92 ZOOM_connection_connect(c, host, portnum)
93         ZOOM_connection c
94         const char* host
95         int portnum
96
97 void
98 ZOOM_connection_destroy(c)
99         ZOOM_connection c
100
101 const char *
102 ZOOM_connection_option_get(c, key)
103         ZOOM_connection c
104         const char *key
105
106 struct datachunk
107 ZOOM_connection_option_getl(c, key, len)
108         ZOOM_connection c
109         const char* key
110         int &len
111         CODE:
112                 RETVAL.data = (char*) ZOOM_connection_option_getl(c, key, &len);
113                 RETVAL.len = len;
114         OUTPUT:
115                 RETVAL
116                 len
117
118 void
119 ZOOM_connection_option_set(c, key, val)
120         ZOOM_connection c
121         const char *key
122         const char *val
123
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.
129 #
130 void
131 ZOOM_connection_option_setl(c, key, val, len)
132         ZOOM_connection c
133         const char* key
134         opaquechar* val
135         int len
136
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.
144 #
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.
148 #
149 int
150 ZOOM_connection_error(c, cp, addinfo)
151         ZOOM_connection c
152         char* &cp
153         char* &addinfo
154         CODE:
155                 {
156                 const char *ccp, *caddinfo;
157                 RETVAL = ZOOM_connection_error(c, &ccp, &caddinfo);
158                 cp = (char*) ccp;
159                 addinfo = (char*) caddinfo;
160                 }
161         OUTPUT:
162                 RETVAL
163                 cp
164                 addinfo
165
166 # See comments for ZOOM_connection_error() above
167 int
168 ZOOM_connection_error_x(c, cp, addinfo, diagset)
169         ZOOM_connection c
170         const char * &cp
171         const char * &addinfo
172         const char * &diagset
173         CODE:
174                 {
175                 const char *ccp, *caddinfo, *cdset;
176                 RETVAL = ZOOM_connection_error_x(c, &ccp, &caddinfo, &cdset);
177                 cp = (char*) ccp;
178                 addinfo = (char*) caddinfo;
179                 diagset = (char*) cdset;
180                 }
181         OUTPUT:
182                 RETVAL
183                 cp
184                 addinfo
185                 diagset
186
187 int
188 ZOOM_connection_errcode(c)
189         ZOOM_connection c
190
191 const char *
192 ZOOM_connection_errmsg(c)
193         ZOOM_connection c
194
195 const char *
196 ZOOM_connection_addinfo(c)
197         ZOOM_connection c
198
199 const char *
200 ZOOM_connection_diagset(c)
201         ZOOM_connection c
202
203 const char *
204 ZOOM_diag_str(error)
205         int error
206
207 ZOOM_resultset
208 ZOOM_connection_search(arg0, q)
209         ZOOM_connection arg0
210         ZOOM_query q
211
212 ZOOM_resultset
213 ZOOM_connection_search_pqf(c, q)
214         ZOOM_connection c
215         const char *q
216
217 void
218 ZOOM_resultset_destroy(r)
219         ZOOM_resultset r
220
221 const char *
222 ZOOM_resultset_option_get(r, key)
223         ZOOM_resultset r
224         const char* key
225
226 void
227 ZOOM_resultset_option_set(r, key, val)
228         ZOOM_resultset r
229         const char* key
230         const char* val
231
232 size_t
233 ZOOM_resultset_size(r)
234         ZOOM_resultset r
235
236 SV *
237 ZOOM_resultset_records(r, start, count, return_records)
238         ZOOM_resultset r
239         size_t start
240         size_t count
241         int return_records
242         CODE:
243                 {
244                 ZOOM_record *recs = 0;
245                 if (return_records)
246                         recs = (ZOOM_record*) xmalloc(count * sizeof *recs);
247                 ZOOM_resultset_records(r, recs, start, count);
248                 if (return_records) {
249                         AV *av = newAV();
250                         int i;
251                         for (i = 0; i < count; i++) {
252                                 SV *tmp = newSV(0);
253                                 sv_setref_pv(tmp, "ZOOM_record", (void*) recs[i]);
254                                 av_push(av, tmp);
255                         }
256                         RETVAL = newRV((SV*) av);
257                 } else {
258                         RETVAL = &PL_sv_undef;
259                 }
260                 }
261         OUTPUT:
262                 RETVAL
263
264 ZOOM_record
265 ZOOM_resultset_record(s, pos)
266         ZOOM_resultset s
267         size_t pos
268
269 ZOOM_record
270 ZOOM_resultset_record_immediate(s, pos)
271         ZOOM_resultset s
272         size_t pos
273
274 void
275 ZOOM_resultset_cache_reset(r)
276         ZOOM_resultset r
277
278 # TESTED (but deprecated)
279 void
280 ZOOM_resultset_sort(r, sort_type, sort_spec)
281         ZOOM_resultset r
282         const char* sort_type
283         const char* sort_spec
284
285 int
286 ZOOM_resultset_sort1(r, sort_type, sort_spec)
287         ZOOM_resultset r
288         const char* sort_type
289         const char* sort_spec
290
291 # See "typemap" for discussion of the "const char *" return-type.
292 #
293 ### but should use datachunk for in some (not all!) cases.
294 const char *
295 ZOOM_record_get(rec, type, len)
296         ZOOM_record rec
297         const char* type
298         int &len
299         OUTPUT:
300                 RETVAL
301                 len
302
303 void
304 ZOOM_record_destroy(rec)
305         ZOOM_record rec
306
307 ZOOM_record
308 ZOOM_record_clone(srec)
309         ZOOM_record srec
310
311 ZOOM_query
312 ZOOM_query_create()
313
314 void
315 ZOOM_query_destroy(s)
316         ZOOM_query s
317
318 int
319 ZOOM_query_cql(s, str)
320         ZOOM_query s
321         const char* str
322
323 int
324 ZOOM_query_cql2rpn(s, str, conn)
325         ZOOM_query s
326         const char* str
327         ZOOM_connection conn
328
329 int
330 ZOOM_query_ccl2rpn(s, str, conn)
331         ZOOM_query s
332         const char* str
333         ZOOM_connection conn
334
335 int
336 ZOOM_query_prefix(s, str)
337         ZOOM_query s
338         const char* str
339
340 int
341 ZOOM_query_sortby(s, criteria)
342         ZOOM_query      s
343         const char *    criteria
344
345 ZOOM_scanset
346 ZOOM_connection_scan(c, startterm)
347         ZOOM_connection c
348         const char* startterm
349
350 ZOOM_scanset
351 ZOOM_connection_scan1(c, startterm)
352         ZOOM_connection c
353         ZOOM_query startterm
354
355 const char *
356 ZOOM_scanset_term(scan, pos, occ, len)
357         ZOOM_scanset scan
358         size_t pos
359         int& occ
360         int& len
361         OUTPUT:
362                 RETVAL
363                 occ
364                 len
365
366 const char *
367 ZOOM_scanset_display_term(scan, pos, occ, len)
368         ZOOM_scanset scan
369         size_t pos
370         int& occ
371         int& len
372         OUTPUT:
373                 RETVAL
374                 occ
375                 len
376
377 size_t
378 ZOOM_scanset_size(scan)
379         ZOOM_scanset scan
380
381 void
382 ZOOM_scanset_destroy(scan)
383         ZOOM_scanset scan
384
385 const char *
386 ZOOM_scanset_option_get(scan, key)
387         ZOOM_scanset    scan
388         const char *    key
389
390 void
391 ZOOM_scanset_option_set(scan, key, val)
392         ZOOM_scanset    scan
393         const char *    key
394         const char *    val
395
396 # We ignore the return value of ZOOM_options_set_callback(), since it
397 # is always just the address of the __ZOOM_option_callback() function.
398 # The information that we actually want -- the address of the Perl
399 # function in the callback_block -- is unavailable to us, as the
400 # underlying C function doesn't give the block back.
401 #
402 void
403 ZOOM_options_set_callback(opt, function, handle)
404         ZOOM_options opt
405         SV* function;
406         SV* handle;
407         CODE:
408                 {
409                 /* The tiny amount of memory allocated here is never
410                  * released, as options_destroy() doesn't do anything
411                  * to the callback information.  Not a big deal.
412                  * Also, I have no idea how to drive the Perl "mortal"
413                  * reference-counting stuff, so I am just allocating
414                  * copies which also never get released.  Don't sue!
415                  */
416                 struct callback_block *block = (struct callback_block*)
417                         xmalloc(sizeof *block);
418                 block->function = function;
419                 block->handle = handle;
420                 SvREFCNT(block->function);
421                 SvREFCNT(block->handle);
422                 ZOOM_options_set_callback(opt, __ZOOM_option_callback,
423                                           (void*) block);
424                 }
425
426 ZOOM_options
427 ZOOM_options_create()
428
429 ZOOM_options
430 ZOOM_options_create_with_parent(parent)
431         ZOOM_options parent
432
433 ZOOM_options
434 ZOOM_options_create_with_parent2(parent1, parent2)
435         ZOOM_options parent1
436         ZOOM_options parent2
437
438 const char *
439 ZOOM_options_get(opt, name)
440         ZOOM_options opt
441         const char* name
442
443 struct datachunk
444 ZOOM_options_getl(opt, name, len)
445         ZOOM_options opt
446         const char* name
447         int &len
448         CODE:
449                 RETVAL.data = (char*) ZOOM_options_getl(opt, name, &len);
450                 RETVAL.len = len;
451         OUTPUT:
452                 RETVAL
453                 len
454
455 void
456 ZOOM_options_set(opt, name, v)
457         ZOOM_options opt
458         const char* name
459         const char* v
460
461 void
462 ZOOM_options_setl(opt, name, value, len)
463         ZOOM_options opt
464         const char* name
465         opaquechar* value
466         int len
467
468 void
469 ZOOM_options_destroy(opt)
470         ZOOM_options opt
471
472 int
473 ZOOM_options_get_bool(opt, name, defa)
474         ZOOM_options opt
475         const char* name
476         int defa
477
478 int
479 ZOOM_options_get_int(opt, name, defa)
480         ZOOM_options opt
481         const char* name
482         int defa
483
484 void
485 ZOOM_options_set_int(opt, name, value)
486         ZOOM_options opt
487         const char* name
488         int value
489
490 ZOOM_package
491 ZOOM_connection_package(c, options)
492         ZOOM_connection c
493         ZOOM_options    options
494
495 void
496 ZOOM_package_destroy(p)
497         ZOOM_package    p
498
499 void
500 ZOOM_package_send(p, type)
501         ZOOM_package    p
502         const char *    type
503
504 const char *
505 ZOOM_package_option_get(p, key)
506         ZOOM_package    p
507         const char *    key
508
509 void
510 ZOOM_package_option_set(p, key, val)
511         ZOOM_package    p
512         const char *    key
513         const char *    val
514
515 # UNTESTED
516 #
517 # This has to be called with a single argument which is a _reference_
518 # to an array -- rather than directly with an array, which is of
519 # course identical to passing arbitrarily many arguments.  This is
520 # because there doesn't seem to be a way to do varargs in an XS
521 # function.
522 #
523 int
524 ZOOM_event(conns)
525         SV* conns
526         INIT:
527                 SV *realconns;
528                 I32 n, i;
529                 int res;
530                 ZOOM_connection cs[100];
531         CODE:
532                 /*printf("* in ZOOM_event(%p)\n", conns);*/
533                 if (!SvROK(conns)) {
534                         /*printf("* argument is not a reference\n");*/
535                         XSRETURN_IV(-1);
536                 }
537                 realconns = SvRV(conns);
538                 /*printf("* realconns = %p\n", realconns);*/
539                 if (SvTYPE(realconns) != SVt_PVAV) {
540                         /*printf("* reference is not to an array\n");*/
541                         XSRETURN_IV(-2);
542                 }
543                 n = av_len((AV*) realconns);
544                 n++; /* The av_len() return-value is zero-based */
545                 if (n == 0) {
546                         /*printf("* No connections in referenced array\n");*/
547                         XSRETURN_IV(-3);
548                 } else if (n >= sizeof(cs)/sizeof(cs[0])) {
549                         /*printf("* Too many connections (%d)\n", (int) n);*/
550                         XSRETURN_IV(-4);
551                 }
552
553                 /*printf("* n = %d\n", n);*/
554                 for (i = 0; i < n; i++) {
555                     SV **connp = av_fetch((AV*) realconns, i, (I32) 0);
556                     SV *conn, *sv;
557                     /*printf("* %d of %d: connp = %p\n", (int) i, (int) n,connp);*/
558                     assert(connp != 0);
559                     conn = *connp;
560                     /*printf("* conn = %p\n", conn);*/
561                     /*
562                      * From here on, the tests and assertions seem to
563                      * be ignored: if I pass in a reference to
564                      * something other than a ZOOM_connection, or even
565                      * if I pass a non-reference, the assertions still
566                      * pass and everything seems to work until the
567                      * segmentation fault bites.
568                      */
569                     assert(sv_derived_from(conn, "ZOOM_connection"));
570                     /*printf("* passed assert(isa(ZOOM_connection))\n");*/
571                     assert(SvROK(conn));
572                     /*printf("* passed assert SvROK()\n");*/
573                     sv = (SV*) SvRV(conn);
574                     /*printf("* sv = %p\n", sv);*/
575                     cs[i] = INT2PTR(ZOOM_connection, SvIV(sv));
576                     /*printf("got cs[%d] of %d = %p\n", (int) i, (int) n, cs[i]);*/
577                 }
578                 RETVAL = ZOOM_event((int) n, cs);
579         OUTPUT:
580                 RETVAL
581
582 # UNTESTED
583 int
584 ZOOM_connection_last_event(cs)
585         ZOOM_connection cs
586
587 # ----------------------------------------------------------------------------
588 # What follows is the YAZ logging API.  This is not strictly part of
589 # ZOOM, but it's so useful that it would be silly to omit.
590
591 int
592 yaz_log_mask_str(str)
593         const char *str
594
595 int
596 yaz_log_module_level(name)
597         const char *name
598
599 void
600 yaz_log_init(level, prefix, name)
601         int level
602         const char *prefix
603         const char *name
604
605 void
606 yaz_log_init_file(fname)
607         const char *fname
608
609 void
610 yaz_log_init_level(level)
611         int level
612
613 void
614 yaz_log_init_prefix(prefix)
615         const char *prefix
616
617 void
618 yaz_log_time_format(fmt)
619         const char *fmt
620
621 void
622 yaz_log_init_max_size(mx)
623         int mx
624
625 # <stdarg.h> interfaces are horrible to code for a Perl-C interface
626 # layer.  Instead, we expect Perl applications to construct the
627 # message themselves, and pass it in as an opaque lump.
628 void
629 yaz_log(level, str)
630         int level
631         const char *str
632         CODE:
633                 yaz_log(level, "%s", str);
634
635 # This is also not strictly part of ZOOM
636 unsigned long
637 yaz_version(version_str, sys_str)
638         char *version_str
639         char *sys_str
640         OUTPUT:
641                 RETVAL
642                 version_str
643                 sys_str
644