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