Debian dependency on libmarc-record-perl
[ZOOM-Perl-moved-to-github.git] / ZOOM.xs
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #include <yaz/zoom.h>
6 #include <yaz/diagsrw.h>
7 #include <yaz/xmalloc.h>
8 #include <yaz/log.h>
9 #include <yaz/yaz-version.h>
10
11 /* Used by the *_setl() functions */
12 typedef char opaquechar;
13
14 /* Used as the return value of the *_getl() functions */
15 struct datachunk {
16         char *data;
17         int len;
18 };
19
20 /* Used to package Perl function-pointer and user-data together */
21 struct callback_block {
22         SV *function;
23         SV *handle;
24 };
25
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
30  * Work, More Or Less.
31  */
32 const char *__ZOOM_option_callback (void *handle, const char *name)
33 {
34         struct callback_block *cb = (struct callback_block*) handle;
35         int count;
36         SV *ret;
37         char *s;
38         char *res;
39
40         dSP;
41
42         ENTER;
43         SAVETMPS;
44
45         PUSHMARK(SP);
46         XPUSHs(cb->handle);
47         XPUSHs(sv_2mortal(newSVpv(name, 0)));
48         PUTBACK;
49         /* Perl_sv_dump(0, cb->function); */
50
51         count = call_sv(cb->function, G_SCALAR);
52
53         SPAGAIN;
54
55         if (count != 1)
56                 croak("callback function for ZOOM_options_get() returned %d values: should have returned exactly one", count);
57
58         ret = POPs;
59         if (SvPOK(ret)) {
60                 s = SvPV_nolen(ret);
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
64                  * inadequate. */
65                 res = xstrdup(s);
66         } else {
67                 res = 0;
68         }
69
70         PUTBACK;
71         FREETMPS;
72         LEAVE;
73
74         return res;
75 }
76
77
78 MODULE = Net::Z3950::ZOOM               PACKAGE = Net::Z3950::ZOOM              PREFIX=ZOOM_
79
80 PROTOTYPES: ENABLE
81
82
83 ZOOM_connection
84 ZOOM_connection_new(host, portnum)
85         const char* host
86         int portnum
87
88 ZOOM_connection
89 ZOOM_connection_create(options)
90         ZOOM_options options
91
92 void
93 ZOOM_connection_connect(c, host, portnum)
94         ZOOM_connection c
95         const char* host
96         int portnum
97
98 void
99 ZOOM_connection_destroy(c)
100         ZOOM_connection c
101
102 const char *
103 ZOOM_connection_option_get(c, key)
104         ZOOM_connection c
105         const char *key
106
107 struct datachunk
108 ZOOM_connection_option_getl(c, key, len)
109         ZOOM_connection c
110         const char* key
111         int &len
112         CODE:
113                 RETVAL.data = (char*) ZOOM_connection_option_getl(c, key, &len);
114                 RETVAL.len = len;
115         OUTPUT:
116                 RETVAL
117                 len
118
119 void
120 ZOOM_connection_option_set(c, key, val)
121         ZOOM_connection c
122         const char *key
123         const char *val
124
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.
130 #
131 void
132 ZOOM_connection_option_setl(c, key, val, len)
133         ZOOM_connection c
134         const char* key
135         opaquechar* val
136         int len
137
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.
145 #
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.
149 #
150 int
151 ZOOM_connection_error(c, cp, addinfo)
152         ZOOM_connection c
153         char* &cp
154         char* &addinfo
155         CODE:
156                 {
157                 const char *ccp, *caddinfo;
158                 RETVAL = ZOOM_connection_error(c, &ccp, &caddinfo);
159                 cp = (char*) ccp;
160                 addinfo = (char*) caddinfo;
161                 }
162         OUTPUT:
163                 RETVAL
164                 cp
165                 addinfo
166
167 # See comments for ZOOM_connection_error() above
168 int
169 ZOOM_connection_error_x(c, cp, addinfo, diagset)
170         ZOOM_connection c
171         const char * &cp
172         const char * &addinfo
173         const char * &diagset
174         CODE:
175                 {
176                 const char *ccp, *caddinfo, *cdset;
177                 RETVAL = ZOOM_connection_error_x(c, &ccp, &caddinfo, &cdset);
178                 cp = (char*) ccp;
179                 addinfo = (char*) caddinfo;
180                 diagset = (char*) cdset;
181                 }
182         OUTPUT:
183                 RETVAL
184                 cp
185                 addinfo
186                 diagset
187
188 int
189 ZOOM_connection_errcode(c)
190         ZOOM_connection c
191
192 const char *
193 ZOOM_connection_errmsg(c)
194         ZOOM_connection c
195
196 const char *
197 ZOOM_connection_addinfo(c)
198         ZOOM_connection c
199
200 const char *
201 ZOOM_connection_diagset(c)
202         ZOOM_connection c
203
204 const char *
205 ZOOM_diag_str(error)
206         int error
207
208 const char *
209 ZOOM_diag_srw_str(error)
210         int error
211         CODE:
212                 RETVAL = yaz_diag_srw_str(error);
213         OUTPUT:
214                 RETVAL
215
216 ZOOM_resultset
217 ZOOM_connection_search(arg0, q)
218         ZOOM_connection arg0
219         ZOOM_query q
220
221 ZOOM_resultset
222 ZOOM_connection_search_pqf(c, q)
223         ZOOM_connection c
224         const char *q
225
226 void
227 ZOOM_resultset_destroy(r)
228         ZOOM_resultset r
229
230 const char *
231 ZOOM_resultset_option_get(r, key)
232         ZOOM_resultset r
233         const char* key
234
235 void
236 ZOOM_resultset_option_set(r, key, val)
237         ZOOM_resultset r
238         const char* key
239         const char* val
240
241 size_t
242 ZOOM_resultset_size(r)
243         ZOOM_resultset r
244
245 SV *
246 ZOOM_resultset_records(r, start, count, return_records)
247         ZOOM_resultset r
248         size_t start
249         size_t count
250         int return_records
251         CODE:
252                 {
253                 ZOOM_record *recs = 0;
254                 if (return_records)
255                         recs = (ZOOM_record*) xmalloc(count * sizeof *recs);
256                 ZOOM_resultset_records(r, recs, start, count);
257                 if (return_records) {
258                         AV *av = newAV();
259                         int i;
260                         for (i = 0; i < count; i++) {
261                                 SV *tmp = newSV(0);
262                                 sv_setref_pv(tmp, "ZOOM_record", (void*) recs[i]);
263                                 av_push(av, tmp);
264                         }
265                         RETVAL = newRV((SV*) av);
266                 } else {
267                         RETVAL = &PL_sv_undef;
268                 }
269                 }
270         OUTPUT:
271                 RETVAL
272
273 ZOOM_record
274 ZOOM_resultset_record(s, pos)
275         ZOOM_resultset s
276         size_t pos
277
278 ZOOM_record
279 ZOOM_resultset_record_immediate(s, pos)
280         ZOOM_resultset s
281         size_t pos
282
283 void
284 ZOOM_resultset_cache_reset(r)
285         ZOOM_resultset r
286
287 # TESTED (but deprecated)
288 void
289 ZOOM_resultset_sort(r, sort_type, sort_spec)
290         ZOOM_resultset r
291         const char* sort_type
292         const char* sort_spec
293
294 int
295 ZOOM_resultset_sort1(r, sort_type, sort_spec)
296         ZOOM_resultset r
297         const char* sort_type
298         const char* sort_spec
299
300 # See comments for ZOOM_connection_error() above
301 int
302 ZOOM_record_error(rec, cp, addinfo, diagset)
303         ZOOM_record rec
304         const char* &cp
305         const char* &addinfo
306         const char* &diagset
307         CODE:
308                 {
309                 const char *ccp = "", *caddinfo = "", *cdset = "";
310                 RETVAL = ZOOM_record_error(rec, &ccp, &caddinfo, &cdset);
311                 cp = (char*) ccp;
312                 addinfo = (char*) caddinfo;
313                 diagset = (char*) cdset;
314                 }
315         OUTPUT:
316                 RETVAL
317                 cp
318                 addinfo
319                 diagset
320
321 # See "typemap" for discussion of the "const char *" return-type.
322 const char *
323 ZOOM_record_get_string(rec, type)
324         ZOOM_record rec
325         const char* type
326         INIT:
327                 int len;
328         CODE:
329                 RETVAL = ZOOM_record_get(rec, type, &len);
330         OUTPUT:
331                 RETVAL
332
333 struct datachunk
334 ZOOM_record_get_binary(rec, type)
335         ZOOM_record rec
336         const char* type
337         CODE:
338                 RETVAL.data = (char*) ZOOM_record_get(rec, type, &RETVAL.len);
339         OUTPUT:
340                 RETVAL
341
342 void
343 ZOOM_record_destroy(rec)
344         ZOOM_record rec
345
346 ZOOM_record
347 ZOOM_record_clone(srec)
348         ZOOM_record srec
349
350 ZOOM_query
351 ZOOM_query_create()
352
353 void
354 ZOOM_query_destroy(s)
355         ZOOM_query s
356
357 int
358 ZOOM_query_cql(s, str)
359         ZOOM_query s
360         const char* str
361
362 int
363 ZOOM_query_cql2rpn(s, str, conn)
364         ZOOM_query s
365         const char* str
366         ZOOM_connection conn
367
368 int
369 ZOOM_query_ccl2rpn(s, query_str, config, errcode, errstr, errpos)
370         ZOOM_query s
371         const char* query_str
372         const char* config
373         int &errcode
374         const char* &errstr
375         int &errpos
376         OUTPUT:
377                 RETVAL
378                 errcode
379                 errstr
380                 errpos
381
382 int
383 ZOOM_query_prefix(s, str)
384         ZOOM_query s
385         const char* str
386
387 int
388 ZOOM_query_sortby(s, criteria)
389         ZOOM_query      s
390         const char *    criteria
391
392 ZOOM_scanset
393 ZOOM_connection_scan(c, startterm)
394         ZOOM_connection c
395         const char* startterm
396
397 ZOOM_scanset
398 ZOOM_connection_scan1(c, startterm)
399         ZOOM_connection c
400         ZOOM_query startterm
401
402 const char *
403 ZOOM_scanset_term(scan, pos, occ, len)
404         ZOOM_scanset scan
405         size_t pos
406         size_t& occ
407         size_t& len
408         OUTPUT:
409                 RETVAL
410                 occ
411                 len
412
413 const char *
414 ZOOM_scanset_display_term(scan, pos, occ, len)
415         ZOOM_scanset scan
416         size_t pos
417         size_t& occ
418         size_t& len
419         OUTPUT:
420                 RETVAL
421                 occ
422                 len
423
424 size_t
425 ZOOM_scanset_size(scan)
426         ZOOM_scanset scan
427
428 void
429 ZOOM_scanset_destroy(scan)
430         ZOOM_scanset scan
431
432 const char *
433 ZOOM_scanset_option_get(scan, key)
434         ZOOM_scanset    scan
435         const char *    key
436
437 void
438 ZOOM_scanset_option_set(scan, key, val)
439         ZOOM_scanset    scan
440         const char *    key
441         const char *    val
442
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.
448 #
449 void
450 ZOOM_options_set_callback(opt, function, handle)
451         ZOOM_options opt
452         SV* function;
453         SV* handle;
454         CODE:
455                 {
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!
462                  */
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,
470                                           (void*) block);
471                 }
472
473 ZOOM_options
474 ZOOM_options_create()
475
476 ZOOM_options
477 ZOOM_options_create_with_parent(parent)
478         ZOOM_options parent
479
480 ZOOM_options
481 ZOOM_options_create_with_parent2(parent1, parent2)
482         ZOOM_options parent1
483         ZOOM_options parent2
484
485 const char *
486 ZOOM_options_get(opt, name)
487         ZOOM_options opt
488         const char* name
489
490 struct datachunk
491 ZOOM_options_getl(opt, name, len)
492         ZOOM_options opt
493         const char* name
494         int &len
495         CODE:
496                 RETVAL.data = (char*) ZOOM_options_getl(opt, name, &len);
497                 RETVAL.len = len;
498         OUTPUT:
499                 RETVAL
500                 len
501
502 void
503 ZOOM_options_set(opt, name, v)
504         ZOOM_options opt
505         const char* name
506         const char* v
507
508 void
509 ZOOM_options_setl(opt, name, value, len)
510         ZOOM_options opt
511         const char* name
512         opaquechar* value
513         int len
514
515 void
516 ZOOM_options_destroy(opt)
517         ZOOM_options opt
518
519 int
520 ZOOM_options_get_bool(opt, name, defa)
521         ZOOM_options opt
522         const char* name
523         int defa
524
525 int
526 ZOOM_options_get_int(opt, name, defa)
527         ZOOM_options opt
528         const char* name
529         int defa
530
531 void
532 ZOOM_options_set_int(opt, name, value)
533         ZOOM_options opt
534         const char* name
535         int value
536
537 ZOOM_package
538 ZOOM_connection_package(c, options)
539         ZOOM_connection c
540         ZOOM_options    options
541
542 void
543 ZOOM_package_destroy(p)
544         ZOOM_package    p
545
546 void
547 ZOOM_package_send(p, type)
548         ZOOM_package    p
549         const char *    type
550
551 const char *
552 ZOOM_package_option_get(p, key)
553         ZOOM_package    p
554         const char *    key
555
556 void
557 ZOOM_package_option_set(p, key, val)
558         ZOOM_package    p
559         const char *    key
560         const char *    val
561
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
566 # function.
567 #
568 int
569 ZOOM_event(conns)
570         SV* conns
571         INIT:
572                 SV *realconns;
573                 I32 n, i;
574                 ZOOM_connection *cs;
575         CODE:
576                 /*printf("* in ZOOM_event(%p)\n", conns);*/
577                 if (!SvROK(conns)) {
578                         /*printf("* argument is not a reference\n");*/
579                         XSRETURN_IV(-1);
580                 }
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");*/
585                         XSRETURN_IV(-2);
586                 }
587                 n = av_len((AV*) realconns);
588                 n++; /* The av_len() return-value is zero-based */
589                 if (n == 0) {
590                         /*printf("* No connections in referenced array\n");*/
591                         XSRETURN_IV(-3);
592                 }
593
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);*/
597                         XSRETURN_IV(-4);
598                 }
599
600                 for (i = 0; i < n; i++) {
601                     SV **connp = av_fetch((AV*) realconns, i, (I32) 0);
602                     SV *conn, *sv;
603                     /*printf("* %d of %d: connp = %p\n", (int) i, (int) n,connp);*/
604                     assert(connp != 0);
605                     conn = *connp;
606                     /*printf("* conn = %p\n", conn);*/
607                     /*
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.
614                      */
615                     assert(sv_derived_from(conn, "ZOOM_connection"));
616                     /*printf("* passed assert(isa(ZOOM_connection))\n");*/
617                     assert(SvROK(conn));
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]);*/
623                 }
624                 RETVAL = ZOOM_event((int) n, cs);
625                 free(cs);
626         OUTPUT:
627                 RETVAL
628
629 int
630 ZOOM_connection_last_event(cs)
631         ZOOM_connection cs
632
633 int
634 ZOOM_connection_is_idle(cs)
635         ZOOM_connection cs
636
637 int
638 ZOOM_connection_peek_event(cs)
639         ZOOM_connection cs
640
641
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.
645
646 int
647 yaz_log_mask_str(str)
648         const char *str
649
650 int
651 yaz_log_module_level(name)
652         const char *name
653
654 void
655 yaz_log_init(level, prefix, name)
656         int level
657         const char *prefix
658         const char *name
659
660 void
661 yaz_log_init_file(fname)
662         const char *fname
663
664 void
665 yaz_log_init_level(level)
666         int level
667
668 void
669 yaz_log_init_prefix(prefix)
670         const char *prefix
671
672 void
673 yaz_log_time_format(fmt)
674         const char *fmt
675
676 void
677 yaz_log_init_max_size(mx)
678         int mx
679
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.
683 void
684 yaz_log(level, str)
685         int level
686         const char *str
687         CODE:
688                 yaz_log(level, "%s", str);
689
690 # This is also not strictly part of ZOOM
691 unsigned long
692 yaz_version(version_str, sys_str)
693         char *version_str
694         char *sys_str
695         OUTPUT:
696                 RETVAL
697                 version_str
698                 sys_str
699