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