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