Add ZOOM_connection_is_idle()
[ZOOM-Perl-moved-to-github.git] / ZOOM.xs
1 /* $Id: ZOOM.xs,v 1.43 2006-10-04 17:14:12 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, query_str, config, errcode, errstr, errpos)
331         ZOOM_query s
332         const char* query_str
333         const char* config
334         int &errcode
335         const char* &errstr
336         int &errpos
337         OUTPUT:
338                 RETVAL
339                 errcode
340                 errstr
341                 errpos
342
343 int
344 ZOOM_query_prefix(s, str)
345         ZOOM_query s
346         const char* str
347
348 int
349 ZOOM_query_sortby(s, criteria)
350         ZOOM_query      s
351         const char *    criteria
352
353 ZOOM_scanset
354 ZOOM_connection_scan(c, startterm)
355         ZOOM_connection c
356         const char* startterm
357
358 ZOOM_scanset
359 ZOOM_connection_scan1(c, startterm)
360         ZOOM_connection c
361         ZOOM_query startterm
362
363 const char *
364 ZOOM_scanset_term(scan, pos, occ, len)
365         ZOOM_scanset scan
366         size_t pos
367         int& occ
368         int& len
369         OUTPUT:
370                 RETVAL
371                 occ
372                 len
373
374 const char *
375 ZOOM_scanset_display_term(scan, pos, occ, len)
376         ZOOM_scanset scan
377         size_t pos
378         int& occ
379         int& len
380         OUTPUT:
381                 RETVAL
382                 occ
383                 len
384
385 size_t
386 ZOOM_scanset_size(scan)
387         ZOOM_scanset scan
388
389 void
390 ZOOM_scanset_destroy(scan)
391         ZOOM_scanset scan
392
393 const char *
394 ZOOM_scanset_option_get(scan, key)
395         ZOOM_scanset    scan
396         const char *    key
397
398 void
399 ZOOM_scanset_option_set(scan, key, val)
400         ZOOM_scanset    scan
401         const char *    key
402         const char *    val
403
404 # We ignore the return value of ZOOM_options_set_callback(), since it
405 # is always just the address of the __ZOOM_option_callback() function.
406 # The information that we actually want -- the address of the Perl
407 # function in the callback_block -- is unavailable to us, as the
408 # underlying C function doesn't give the block back.
409 #
410 void
411 ZOOM_options_set_callback(opt, function, handle)
412         ZOOM_options opt
413         SV* function;
414         SV* handle;
415         CODE:
416                 {
417                 /* The tiny amount of memory allocated here is never
418                  * released, as options_destroy() doesn't do anything
419                  * to the callback information.  Not a big deal.
420                  * Also, I have no idea how to drive the Perl "mortal"
421                  * reference-counting stuff, so I am just allocating
422                  * copies which also never get released.  Don't sue!
423                  */
424                 struct callback_block *block = (struct callback_block*)
425                         xmalloc(sizeof *block);
426                 block->function = function;
427                 block->handle = handle;
428                 SvREFCNT(block->function);
429                 SvREFCNT(block->handle);
430                 ZOOM_options_set_callback(opt, __ZOOM_option_callback,
431                                           (void*) block);
432                 }
433
434 ZOOM_options
435 ZOOM_options_create()
436
437 ZOOM_options
438 ZOOM_options_create_with_parent(parent)
439         ZOOM_options parent
440
441 ZOOM_options
442 ZOOM_options_create_with_parent2(parent1, parent2)
443         ZOOM_options parent1
444         ZOOM_options parent2
445
446 const char *
447 ZOOM_options_get(opt, name)
448         ZOOM_options opt
449         const char* name
450
451 struct datachunk
452 ZOOM_options_getl(opt, name, len)
453         ZOOM_options opt
454         const char* name
455         int &len
456         CODE:
457                 RETVAL.data = (char*) ZOOM_options_getl(opt, name, &len);
458                 RETVAL.len = len;
459         OUTPUT:
460                 RETVAL
461                 len
462
463 void
464 ZOOM_options_set(opt, name, v)
465         ZOOM_options opt
466         const char* name
467         const char* v
468
469 void
470 ZOOM_options_setl(opt, name, value, len)
471         ZOOM_options opt
472         const char* name
473         opaquechar* value
474         int len
475
476 void
477 ZOOM_options_destroy(opt)
478         ZOOM_options opt
479
480 int
481 ZOOM_options_get_bool(opt, name, defa)
482         ZOOM_options opt
483         const char* name
484         int defa
485
486 int
487 ZOOM_options_get_int(opt, name, defa)
488         ZOOM_options opt
489         const char* name
490         int defa
491
492 void
493 ZOOM_options_set_int(opt, name, value)
494         ZOOM_options opt
495         const char* name
496         int value
497
498 ZOOM_package
499 ZOOM_connection_package(c, options)
500         ZOOM_connection c
501         ZOOM_options    options
502
503 void
504 ZOOM_package_destroy(p)
505         ZOOM_package    p
506
507 void
508 ZOOM_package_send(p, type)
509         ZOOM_package    p
510         const char *    type
511
512 const char *
513 ZOOM_package_option_get(p, key)
514         ZOOM_package    p
515         const char *    key
516
517 void
518 ZOOM_package_option_set(p, key, val)
519         ZOOM_package    p
520         const char *    key
521         const char *    val
522
523 # This has to be called with a single argument which is a _reference_
524 # to an array -- rather than directly with an array, which is of
525 # course identical to passing arbitrarily many arguments.  This is
526 # because there doesn't seem to be a way to do varargs in an XS
527 # function.
528 #
529 int
530 ZOOM_event(conns)
531         SV* conns
532         INIT:
533                 SV *realconns;
534                 I32 n, i;
535                 int res;
536                 ZOOM_connection cs[100];
537         CODE:
538                 /*printf("* in ZOOM_event(%p)\n", conns);*/
539                 if (!SvROK(conns)) {
540                         /*printf("* argument is not a reference\n");*/
541                         XSRETURN_IV(-1);
542                 }
543                 realconns = SvRV(conns);
544                 /*printf("* realconns = %p\n", realconns);*/
545                 if (SvTYPE(realconns) != SVt_PVAV) {
546                         /*printf("* reference is not to an array\n");*/
547                         XSRETURN_IV(-2);
548                 }
549                 n = av_len((AV*) realconns);
550                 n++; /* The av_len() return-value is zero-based */
551                 if (n == 0) {
552                         /*printf("* No connections in referenced array\n");*/
553                         XSRETURN_IV(-3);
554                 } else if (n >= sizeof(cs)/sizeof(cs[0])) {
555                         /*printf("* Too many connections (%d)\n", (int) n);*/
556                         XSRETURN_IV(-4);
557                 }
558
559                 /*printf("* n = %d\n", n);*/
560                 for (i = 0; i < n; i++) {
561                     SV **connp = av_fetch((AV*) realconns, i, (I32) 0);
562                     SV *conn, *sv;
563                     /*printf("* %d of %d: connp = %p\n", (int) i, (int) n,connp);*/
564                     assert(connp != 0);
565                     conn = *connp;
566                     /*printf("* conn = %p\n", conn);*/
567                     /*
568                      * From here on, the tests and assertions seem to
569                      * be ignored: if I pass in a reference to
570                      * something other than a ZOOM_connection, or even
571                      * if I pass a non-reference, the assertions still
572                      * pass and everything seems to work until the
573                      * segmentation fault bites.
574                      */
575                     assert(sv_derived_from(conn, "ZOOM_connection"));
576                     /*printf("* passed assert(isa(ZOOM_connection))\n");*/
577                     assert(SvROK(conn));
578                     /*printf("* passed assert SvROK()\n");*/
579                     sv = (SV*) SvRV(conn);
580                     /*printf("* sv = %p\n", sv);*/
581                     cs[i] = INT2PTR(ZOOM_connection, SvIV(sv));
582                     /*printf("got cs[%d] of %d = %p\n", (int) i, (int) n, cs[i]);*/
583                 }
584                 RETVAL = ZOOM_event((int) n, cs);
585         OUTPUT:
586                 RETVAL
587
588 int
589 ZOOM_connection_last_event(cs)
590         ZOOM_connection cs
591
592 int
593 ZOOM_connection_is_idle(cs)
594         ZOOM_connection cs
595
596
597 # ----------------------------------------------------------------------------
598 # What follows is the YAZ logging API.  This is not strictly part of
599 # ZOOM, but it's so useful that it would be silly to omit.
600
601 int
602 yaz_log_mask_str(str)
603         const char *str
604
605 int
606 yaz_log_module_level(name)
607         const char *name
608
609 void
610 yaz_log_init(level, prefix, name)
611         int level
612         const char *prefix
613         const char *name
614
615 void
616 yaz_log_init_file(fname)
617         const char *fname
618
619 void
620 yaz_log_init_level(level)
621         int level
622
623 void
624 yaz_log_init_prefix(prefix)
625         const char *prefix
626
627 void
628 yaz_log_time_format(fmt)
629         const char *fmt
630
631 void
632 yaz_log_init_max_size(mx)
633         int mx
634
635 # <stdarg.h> interfaces are horrible to code for a Perl-C interface
636 # layer.  Instead, we expect Perl applications to construct the
637 # message themselves, and pass it in as an opaque lump.
638 void
639 yaz_log(level, str)
640         int level
641         const char *str
642         CODE:
643                 yaz_log(level, "%s", str);
644
645 # This is also not strictly part of ZOOM
646 unsigned long
647 yaz_version(version_str, sys_str)
648         char *version_str
649         char *sys_str
650         OUTPUT:
651                 RETVAL
652                 version_str
653                 sys_str
654