ZOOM: scan number is 20 by default
[yaz-moved-to-github.git] / src / zoom-c.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file zoom-c.c
7  * \brief Implements ZOOM C interface.
8  */
9
10 #include <assert.h>
11 #include <string.h>
12 #include <errno.h>
13 #include "zoom-p.h"
14
15 #include <yaz/yaz-util.h>
16 #include <yaz/xmalloc.h>
17 #include <yaz/otherinfo.h>
18 #include <yaz/log.h>
19 #include <yaz/pquery.h>
20 #include <yaz/marcdisp.h>
21 #include <yaz/diagbib1.h>
22 #include <yaz/charneg.h>
23 #include <yaz/ill.h>
24 #include <yaz/srw.h>
25 #include <yaz/cql.h>
26 #include <yaz/ccl.h>
27 #include <yaz/query-charset.h>
28 #include <yaz/copy_types.h>
29 #include <yaz/snprintf.h>
30
31 static int log_api = 0;
32 static int log_details = 0;
33
34 typedef enum {
35     zoom_pending,
36     zoom_complete
37 } zoom_ret;
38
39 static void resultset_destroy(ZOOM_resultset r);
40 static zoom_ret ZOOM_connection_send_init(ZOOM_connection c);
41 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out);
42 static char *cql2pqf(ZOOM_connection c, const char *cql);
43
44 ZOOM_API(const char *) ZOOM_get_event_str(int event)
45 {
46     static const char *ar[] = {
47         "NONE",
48         "CONNECT",
49         "SEND_DATA",
50         "RECV_DATA",
51         "TIMEOUT",
52         "UNKNOWN",
53         "SEND_APDU",
54         "RECV_APDU",
55         "RECV_RECORD",
56         "RECV_SEARCH",
57         "END"
58     };
59     return ar[event];
60 }
61
62 /*
63  * This wrapper is just for logging failed lookups.  It would be nicer
64  * if it could cause failure when a lookup fails, but that's hard.
65  */
66 static Odr_oid *zoom_yaz_str_to_z3950oid(ZOOM_connection c,
67                                      oid_class oid_class, const char *str) {
68     Odr_oid *res = yaz_string_to_oid_odr(yaz_oid_std(), oid_class, str,
69                                      c->odr_out);
70     if (res == 0)
71         yaz_log(YLOG_WARN, "%p OID lookup (%d, '%s') failed",
72                 c, (int) oid_class, str);
73     return res;
74 }
75
76
77 static void initlog(void)
78 {
79     static int log_level_initialized = 0;
80     if (!log_level_initialized)
81     {
82         log_api = yaz_log_module_level("zoom");
83         log_details = yaz_log_module_level("zoomdetails");
84         log_level_initialized = 1;
85     }
86 }
87
88 static ZOOM_Event ZOOM_Event_create(int kind)
89 {
90     ZOOM_Event event = (ZOOM_Event) xmalloc(sizeof(*event));
91     event->kind = kind;
92     event->next = 0;
93     event->prev = 0;
94     yaz_log(log_details, "ZOOM_Event_create(kind=%d)", kind);
95     return event;
96 }
97
98 static void ZOOM_Event_destroy(ZOOM_Event event)
99 {
100     xfree(event);
101 }
102
103 static void ZOOM_connection_put_event(ZOOM_connection c, ZOOM_Event event)
104 {
105     if (c->m_queue_back)
106     {
107         c->m_queue_back->prev = event;
108         assert(c->m_queue_front);
109     }
110     else
111     {
112         assert(!c->m_queue_front);
113         c->m_queue_front = event;
114     }
115     event->next = c->m_queue_back;
116     event->prev = 0;
117     c->m_queue_back = event;
118 }
119
120 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
121 {
122     ZOOM_Event event = c->m_queue_front;
123     if (!event)
124     {
125         c->last_event = ZOOM_EVENT_NONE;
126         return 0;
127     }
128     assert(c->m_queue_back);
129     c->m_queue_front = event->prev;
130     if (c->m_queue_front)
131     {
132         assert(c->m_queue_back);
133         c->m_queue_front->next = 0;
134     }
135     else
136         c->m_queue_back = 0;
137     c->last_event = event->kind;
138     return event;
139 }
140
141 static void ZOOM_connection_remove_events(ZOOM_connection c)
142 {
143     ZOOM_Event event;
144     while ((event = ZOOM_connection_get_event(c)))
145         ZOOM_Event_destroy(event);
146 }
147
148 ZOOM_API(int) ZOOM_connection_peek_event(ZOOM_connection c)
149 {
150     ZOOM_Event event = c->m_queue_front;
151
152     return event ? event->kind : ZOOM_EVENT_NONE;
153 }
154
155 void ZOOM_connection_remove_tasks(ZOOM_connection c);
156
157 static void set_dset_error(ZOOM_connection c, int error,
158                            const char *dset,
159                            const char *addinfo, const char *addinfo2)
160 {
161     char *cp;
162
163     xfree(c->addinfo);
164     c->addinfo = 0;
165     c->error = error;
166     if (!c->diagset || strcmp(dset, c->diagset))
167     {
168         xfree(c->diagset);
169         c->diagset = xstrdup(dset);
170         /* remove integer part from SRW diagset .. */
171         if ((cp = strrchr(c->diagset, '/')))
172             *cp = '\0';
173     }
174     if (addinfo && addinfo2)
175     {
176         c->addinfo = (char*) xmalloc(strlen(addinfo) + strlen(addinfo2) + 2);
177         strcpy(c->addinfo, addinfo);
178         strcat(c->addinfo, addinfo2);
179     }
180     else if (addinfo)
181         c->addinfo = xstrdup(addinfo);
182     if (error != ZOOM_ERROR_NONE)
183     {
184         yaz_log(log_api, "%p set_dset_error %s %s:%d %s %s",
185                 c, c->host_port ? c->host_port : "<>", dset, error,
186                 addinfo ? addinfo : "",
187                 addinfo2 ? addinfo2 : "");
188         ZOOM_connection_remove_tasks(c);
189     }
190 }
191
192 static int uri_to_code(const char *uri)
193 {
194     int code = 0;       
195     const char *cp;
196     if ((cp = strrchr(uri, '/')))
197         code = atoi(cp+1);
198     return code;
199 }
200
201 #if YAZ_HAVE_XML2
202 static void set_HTTP_error(ZOOM_connection c, int error,
203                            const char *addinfo, const char *addinfo2)
204 {
205     set_dset_error(c, error, "HTTP", addinfo, addinfo2);
206 }
207
208 static void set_SRU_error(ZOOM_connection c, Z_SRW_diagnostic *d)
209 {
210     const char *uri = d->uri;
211     if (uri)
212         set_dset_error(c, uri_to_code(uri), uri, d->details, 0);
213 }
214
215 #endif
216
217
218 static void set_ZOOM_error(ZOOM_connection c, int error,
219                            const char *addinfo)
220 {
221     set_dset_error(c, error, "ZOOM", addinfo, 0);
222 }
223
224 static void clear_error(ZOOM_connection c)
225 {
226     /*
227      * If an error is tied to an operation then it's ok to clear: for
228      * example, a diagnostic returned from a search is cleared by a
229      * subsequent search.  However, problems such as Connection Lost
230      * or Init Refused are not cleared, because they are not
231      * recoverable: doing another search doesn't help.
232      */
233
234     ZOOM_connection_remove_events(c);
235     switch (c->error)
236     {
237     case ZOOM_ERROR_CONNECT:
238     case ZOOM_ERROR_MEMORY:
239     case ZOOM_ERROR_DECODE:
240     case ZOOM_ERROR_CONNECTION_LOST:
241     case ZOOM_ERROR_INIT:
242     case ZOOM_ERROR_INTERNAL:
243     case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
244         break;
245     default:
246         set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
247     }
248 }
249
250 void ZOOM_connection_show_task(ZOOM_task task)
251 {
252     switch(task->which)
253     {
254     case ZOOM_TASK_SEARCH:
255         yaz_log(YLOG_LOG, "search p=%p", task);
256         break;
257     case ZOOM_TASK_RETRIEVE:
258         yaz_log(YLOG_LOG, "retrieve p=%p", task);
259         break;
260     case ZOOM_TASK_CONNECT:
261         yaz_log(YLOG_LOG, "connect p=%p", task);
262         break;
263     case ZOOM_TASK_SCAN:
264         yaz_log(YLOG_LOG, "scan p=%p", task);
265         break;
266     }
267 }
268
269 void ZOOM_connection_show_tasks(ZOOM_connection c)
270 {
271     ZOOM_task task;
272     yaz_log(YLOG_LOG, "connection p=%p tasks", c);
273     for (task = c->tasks; task; task = task->next)
274         ZOOM_connection_show_task(task);
275 }
276
277 ZOOM_task ZOOM_connection_add_task(ZOOM_connection c, int which)
278 {
279     ZOOM_task *taskp = &c->tasks;
280     while (*taskp)
281         taskp = &(*taskp)->next;
282     *taskp = (ZOOM_task) xmalloc(sizeof(**taskp));
283     (*taskp)->running = 0;
284     (*taskp)->which = which;
285     (*taskp)->next = 0;
286     clear_error(c);
287     return *taskp;
288 }
289
290 ZOOM_API(int) ZOOM_connection_is_idle(ZOOM_connection c)
291 {
292     return c->tasks ? 0 : 1;
293 }
294
295 ZOOM_task ZOOM_connection_insert_task(ZOOM_connection c, int which)
296 {
297     ZOOM_task task = (ZOOM_task) xmalloc(sizeof(*task));
298
299     task->next = c->tasks;
300     c->tasks = task;
301
302     task->running = 0;
303     task->which = which;
304     clear_error(c);
305     return task;
306 }
307
308 void ZOOM_connection_remove_task(ZOOM_connection c)
309 {
310     ZOOM_task task = c->tasks;
311
312     if (task)
313     {
314         c->tasks = task->next;
315         switch (task->which)
316         {
317         case ZOOM_TASK_SEARCH:
318             resultset_destroy(task->u.search.resultset);
319             xfree(task->u.search.syntax);
320             xfree(task->u.search.elementSetName);
321             break;
322         case ZOOM_TASK_RETRIEVE:
323             resultset_destroy(task->u.retrieve.resultset);
324             xfree(task->u.retrieve.syntax);
325             xfree(task->u.retrieve.elementSetName);
326             break;
327         case ZOOM_TASK_CONNECT:
328             break;
329         case ZOOM_TASK_SCAN:
330             ZOOM_scanset_destroy(task->u.scan.scan);
331             break;
332         case ZOOM_TASK_PACKAGE:
333             ZOOM_package_destroy(task->u.package);
334             break;
335         case ZOOM_TASK_SORT:
336             resultset_destroy(task->u.sort.resultset);
337             ZOOM_query_destroy(task->u.sort.q);
338             break;
339         default:
340             assert(0);
341         }
342         xfree(task);
343
344         if (!c->tasks)
345         {
346             ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_END);
347             ZOOM_connection_put_event(c, event);
348         }
349     }
350 }
351
352 void ZOOM_connection_remove_tasks(ZOOM_connection c)
353 {
354     while (c->tasks)
355         ZOOM_connection_remove_task(c);
356 }
357
358 static ZOOM_record record_cache_lookup(ZOOM_resultset r, int pos,
359                                        const char *syntax,
360                                        const char *elementSetName);
361
362 ZOOM_API(ZOOM_connection)
363     ZOOM_connection_create(ZOOM_options options)
364 {
365     ZOOM_connection c = (ZOOM_connection) xmalloc(sizeof(*c));
366
367     initlog();
368
369     yaz_log(log_api, "%p ZOOM_connection_create", c);
370
371     c->proto = PROTO_Z3950;
372     c->cs = 0;
373     ZOOM_connection_set_mask(c, 0);
374     c->reconnect_ok = 0;
375     c->state = STATE_IDLE;
376     c->addinfo = 0;
377     c->diagset = 0;
378     set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
379     c->buf_in = 0;
380     c->len_in = 0;
381     c->buf_out = 0;
382     c->len_out = 0;
383     c->resultsets = 0;
384
385     c->options = ZOOM_options_create_with_parent(options);
386
387     c->host_port = 0;
388     c->path = 0;
389     c->proxy = 0;
390     
391     c->charset = c->lang = 0;
392
393     c->cookie_out = 0;
394     c->cookie_in = 0;
395     c->client_IP = 0;
396     c->tasks = 0;
397
398     c->user = 0;
399     c->group = 0;
400     c->password = 0;
401
402     c->maximum_record_size = 0;
403     c->preferred_message_size = 0;
404
405     c->odr_in = odr_createmem(ODR_DECODE);
406     c->odr_out = odr_createmem(ODR_ENCODE);
407     c->odr_print = 0;
408
409     c->async = 0;
410     c->support_named_resultsets = 0;
411     c->last_event = ZOOM_EVENT_NONE;
412
413     c->m_queue_front = 0;
414     c->m_queue_back = 0;
415
416     c->sru_version = 0;
417     c->no_redirects = 0;
418     return c;
419 }
420
421
422 /* set database names. Take local databases (if set); otherwise
423    take databases given in ZURL (if set); otherwise use Default */
424 static char **set_DatabaseNames(ZOOM_connection con, ZOOM_options options,
425                                 int *num, ODR odr)
426 {
427     char **databaseNames;
428     const char *cp = ZOOM_options_get(options, "databaseName");
429     
430     if ((!cp || !*cp) && con->host_port)
431     {
432         if (strncmp(con->host_port, "unix:", 5) == 0)
433             cp = strchr(con->host_port+5, ':');
434         else
435             cp = strchr(con->host_port, '/');
436         if (cp)
437             cp++;
438     }
439     if (!cp)
440         cp = "Default";
441     nmem_strsplit(odr_getmem(odr), "+", cp,  &databaseNames, num);
442     return databaseNames;
443 }
444
445 ZOOM_API(ZOOM_connection)
446     ZOOM_connection_new(const char *host, int portnum)
447 {
448     ZOOM_connection c = ZOOM_connection_create(0);
449
450     ZOOM_connection_connect(c, host, portnum);
451     return c;
452 }
453
454 static zoom_sru_mode get_sru_mode_from_string(const char *s)
455 {
456     if (!s || !*s)
457         return zoom_sru_soap;
458     if (!yaz_matchstr(s, "soap"))
459         return zoom_sru_soap;
460     else if (!yaz_matchstr(s, "get"))
461         return zoom_sru_get;
462     else if (!yaz_matchstr(s, "post"))
463         return zoom_sru_post;
464     return zoom_sru_error;
465 }
466
467 ZOOM_API(void)
468     ZOOM_connection_connect(ZOOM_connection c,
469                             const char *host, int portnum)
470 {
471     const char *val;
472     ZOOM_task task;
473
474     initlog();
475
476     yaz_log(log_api, "%p ZOOM_connection_connect host=%s portnum=%d",
477             c, host ? host : "null", portnum);
478
479     set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
480     ZOOM_connection_remove_tasks(c);
481
482     if (c->odr_print)
483     {
484         odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
485         odr_destroy(c->odr_print);
486     }
487     if (ZOOM_options_get_bool(c->options, "apdulog", 0))
488     {
489         c->odr_print = odr_createmem(ODR_PRINT);
490         odr_setprint(c->odr_print, yaz_log_file());
491     }
492     else
493         c->odr_print = 0;
494
495     if (c->cs)
496     {
497         yaz_log(log_details, "%p ZOOM_connection_connect reconnect ok", c);
498         c->reconnect_ok = 1;
499         return;
500     }
501     yaz_log(log_details, "%p ZOOM_connection_connect connect", c);
502     xfree(c->proxy);
503     c->proxy = 0;
504     val = ZOOM_options_get(c->options, "proxy");
505     if (val && *val)
506     {
507         yaz_log(log_details, "%p ZOOM_connection_connect proxy=%s", c, val);
508         c->proxy = xstrdup(val);
509     }
510
511     xfree(c->charset);
512     c->charset = 0;
513     val = ZOOM_options_get(c->options, "charset");
514     if (val && *val)
515     {
516         yaz_log(log_details, "%p ZOOM_connection_connect charset=%s", c, val);
517         c->charset = xstrdup(val);
518     }
519
520     xfree(c->lang);
521     val = ZOOM_options_get(c->options, "lang");
522     if (val && *val)
523     {
524         yaz_log(log_details, "%p ZOOM_connection_connect lang=%s", c, val);
525         c->lang = xstrdup(val);
526     }
527     else
528         c->lang = 0;
529
530     if (host)
531     {
532         xfree(c->host_port);
533         if (portnum)
534         {
535             char hostn[128];
536             sprintf(hostn, "%.80s:%d", host, portnum);
537             c->host_port = xstrdup(hostn);
538         }
539         else
540             c->host_port = xstrdup(host);
541     }        
542
543     {
544         /*
545          * If the "<scheme>:" part of the host string is preceded by one
546          * or more comma-separated <name>=<value> pairs, these are taken
547          * to be options to be set on the connection object.  Among other
548          * applications, this facility can be used to embed authentication
549          * in a host string:
550          *          user=admin,password=secret,tcp:localhost:9999
551          */
552         char *remainder = c->host_port;
553         char *pcolon = strchr(remainder, ':');
554         char *pcomma;
555         char *pequals;
556         while ((pcomma = strchr(remainder, ',')) != 0 &&
557                (pcolon == 0 || pcomma < pcolon)) {
558             *pcomma = '\0';
559             if ((pequals = strchr(remainder, '=')) != 0) {
560                 *pequals = '\0';
561                 /*printf("# setting '%s'='%s'\n", remainder, pequals+1);*/
562                 ZOOM_connection_option_set(c, remainder, pequals+1);
563             }
564             remainder = pcomma+1;
565         }
566
567         if (remainder != c->host_port) {
568             xfree(c->host_port);
569             c->host_port = xstrdup(remainder);
570             /*printf("# reset hp='%s'\n", remainder);*/
571         }
572     }
573
574     val = ZOOM_options_get(c->options, "sru");
575     c->sru_mode = get_sru_mode_from_string(val);
576
577     xfree(c->sru_version);
578     val = ZOOM_options_get(c->options, "sru_version");
579     c->sru_version = xstrdup(val ? val : "1.2");
580
581     ZOOM_options_set(c->options, "host", c->host_port);
582
583     xfree(c->cookie_out);
584     c->cookie_out = 0;
585     val = ZOOM_options_get(c->options, "cookie");
586     if (val && *val)
587     { 
588         yaz_log(log_details, "%p ZOOM_connection_connect cookie=%s", c, val);
589         c->cookie_out = xstrdup(val);
590     }
591
592     xfree(c->client_IP);
593     c->client_IP = 0;
594     val = ZOOM_options_get(c->options, "clientIP");
595     if (val && *val)
596     {
597         yaz_log(log_details, "%p ZOOM_connection_connect clientIP=%s",
598                 c, val);
599         c->client_IP = xstrdup(val);
600     }
601
602     xfree(c->group);
603     c->group = 0;
604     val = ZOOM_options_get(c->options, "group");
605     if (val && *val)
606         c->group = xstrdup(val);
607
608     xfree(c->user);
609     c->user = 0;
610     val = ZOOM_options_get(c->options, "user");
611     if (val && *val)
612         c->user = xstrdup(val);
613
614     xfree(c->password);
615     c->password = 0;
616     val = ZOOM_options_get(c->options, "password");
617     if (!val)
618         val = ZOOM_options_get(c->options, "pass");
619
620     if (val && *val)
621         c->password = xstrdup(val);
622     
623     c->maximum_record_size =
624         ZOOM_options_get_int(c->options, "maximumRecordSize", 1024*1024);
625     c->preferred_message_size =
626         ZOOM_options_get_int(c->options, "preferredMessageSize", 1024*1024);
627
628     c->async = ZOOM_options_get_bool(c->options, "async", 0);
629     yaz_log(log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
630  
631     task = ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
632
633     if (!c->async)
634     {
635         while (ZOOM_event(1, &c))
636             ;
637     }
638 }
639
640 ZOOM_API(ZOOM_query)
641     ZOOM_query_create(void)
642 {
643     ZOOM_query s = (ZOOM_query) xmalloc(sizeof(*s));
644
645     yaz_log(log_details, "%p ZOOM_query_create", s);
646     s->refcount = 1;
647     s->z_query = 0;
648     s->sort_spec = 0;
649     s->odr = odr_createmem(ODR_ENCODE);
650     s->query_string = 0;
651
652     return s;
653 }
654
655 ZOOM_API(void)
656     ZOOM_query_destroy(ZOOM_query s)
657 {
658     if (!s)
659         return;
660
661     (s->refcount)--;
662     yaz_log(log_details, "%p ZOOM_query_destroy count=%d", s, s->refcount);
663     if (s->refcount == 0)
664     {
665         odr_destroy(s->odr);
666         xfree(s);
667     }
668 }
669
670 ZOOM_API(int)
671     ZOOM_query_prefix(ZOOM_query s, const char *str)
672 {
673     s->query_string = odr_strdup(s->odr, str);
674     s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
675     s->z_query->which = Z_Query_type_1;
676     s->z_query->u.type_1 =  p_query_rpn(s->odr, str);
677     if (!s->z_query->u.type_1)
678     {
679         yaz_log(log_details, "%p ZOOM_query_prefix str=%s failed", s, str);
680         s->z_query = 0;
681         return -1;
682     }
683     yaz_log(log_details, "%p ZOOM_query_prefix str=%s", s, str);
684     return 0;
685 }
686
687 ZOOM_API(int)
688     ZOOM_query_cql(ZOOM_query s, const char *str)
689 {
690     Z_External *ext;
691
692     s->query_string = odr_strdup(s->odr, str);
693
694     ext = (Z_External *) odr_malloc(s->odr, sizeof(*ext));
695     ext->direct_reference = odr_oiddup(s->odr, yaz_oid_userinfo_cql);
696     ext->indirect_reference = 0;
697     ext->descriptor = 0;
698     ext->which = Z_External_CQL;
699     ext->u.cql = s->query_string;
700     
701     s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
702     s->z_query->which = Z_Query_type_104;
703     s->z_query->u.type_104 =  ext;
704
705     yaz_log(log_details, "%p ZOOM_query_cql str=%s", s, str);
706
707     return 0;
708 }
709
710 /*
711  * Translate the CQL string client-side into RPN which is passed to
712  * the server.  This is useful for server's that don't themselves
713  * support CQL, for which ZOOM_query_cql() is useless.  `conn' is used
714  * only as a place to stash diagnostics if compilation fails; if this
715  * information is not needed, a null pointer may be used.
716  */
717 ZOOM_API(int)
718     ZOOM_query_cql2rpn(ZOOM_query s, const char *str, ZOOM_connection conn)
719 {
720     char *rpn;
721     int ret;
722     ZOOM_connection freeme = 0;
723
724     yaz_log(log_details, "%p ZOOM_query_cql2rpn str=%s conn=%p", s, str, conn);
725     if (conn == 0)
726         conn = freeme = ZOOM_connection_create(0);
727
728     rpn = cql2pqf(conn, str);
729     if (freeme != 0)
730         ZOOM_connection_destroy(freeme);
731     if (rpn == 0)
732         return -1;
733
734     ret = ZOOM_query_prefix(s, rpn);
735     xfree(rpn);
736     return ret;
737 }
738
739 /*
740  * Analogous in every way to ZOOM_query_cql2rpn(), except that there
741  * is no analogous ZOOM_query_ccl() that just sends uninterpreted CCL
742  * to the server, as the YAZ GFS doesn't know how to handle this.
743  */
744 ZOOM_API(int)
745     ZOOM_query_ccl2rpn(ZOOM_query s, const char *str, const char *config,
746                        int *ccl_error, const char **error_string,
747                        int *error_pos)
748 {
749     int ret;
750     struct ccl_rpn_node *rpn;
751     CCL_bibset bibset = ccl_qual_mk();
752
753     if (config)
754         ccl_qual_buf(bibset, config);
755
756     rpn = ccl_find_str(bibset, str, ccl_error, error_pos);
757     if (!rpn)
758     {
759         *error_string = ccl_err_msg(*ccl_error);
760         ret = -1;
761     }
762     else
763     {
764         WRBUF wr = wrbuf_alloc();
765         ccl_pquery(wr, rpn);
766         ccl_rpn_delete(rpn);
767         ret = ZOOM_query_prefix(s, wrbuf_cstr(wr));
768         wrbuf_destroy(wr);
769     }
770     ccl_qual_rm(&bibset);
771     return ret;
772 }
773
774 ZOOM_API(int)
775     ZOOM_query_sortby(ZOOM_query s, const char *criteria)
776 {
777     s->sort_spec = yaz_sort_spec(s->odr, criteria);
778     if (!s->sort_spec)
779     {
780         yaz_log(log_details, "%p ZOOM_query_sortby criteria=%s failed",
781                 s, criteria);
782         return -1;
783     }
784     yaz_log(log_details, "%p ZOOM_query_sortby criteria=%s", s, criteria);
785     return 0;
786 }
787
788 static zoom_ret do_write(ZOOM_connection c);
789
790 ZOOM_API(void)
791     ZOOM_connection_destroy(ZOOM_connection c)
792 {
793     ZOOM_resultsets list;
794     if (!c)
795         return;
796     yaz_log(log_api, "%p ZOOM_connection_destroy", c);
797     if (c->cs)
798         cs_close(c->cs);
799
800     // Remove the connection's usage of resultsets
801     list = c->resultsets;
802     while (list) {
803         ZOOM_resultsets removed = list;
804         ZOOM_resultset_destroy(list->resultset);
805         list = list->next;
806         xfree(removed);
807     }
808
809     xfree(c->buf_in);
810     xfree(c->addinfo);
811     xfree(c->diagset);
812     odr_destroy(c->odr_in);
813     odr_destroy(c->odr_out);
814     if (c->odr_print)
815     {
816         odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
817         odr_destroy(c->odr_print);
818     }
819     ZOOM_options_destroy(c->options);
820     ZOOM_connection_remove_tasks(c);
821     ZOOM_connection_remove_events(c);
822     xfree(c->host_port);
823     xfree(c->path);
824     xfree(c->proxy);
825     xfree(c->charset);
826     xfree(c->lang);
827     xfree(c->cookie_out);
828     xfree(c->cookie_in);
829     xfree(c->client_IP);
830     xfree(c->user);
831     xfree(c->group);
832     xfree(c->password);
833     xfree(c->sru_version);
834     xfree(c);
835 }
836
837 void ZOOM_resultset_addref(ZOOM_resultset r)
838 {
839     if (r)
840     {
841         yaz_mutex_enter(r->mutex);
842         (r->refcount)++;
843         yaz_log(log_details, "%p ZOOM_resultset_addref count=%d",
844                 r, r->refcount);
845         yaz_mutex_leave(r->mutex);
846     }
847 }
848
849 ZOOM_resultset ZOOM_resultset_create(void)
850 {
851     int i;
852     ZOOM_resultset r = (ZOOM_resultset) xmalloc(sizeof(*r));
853
854     initlog();
855
856     yaz_log(log_details, "%p ZOOM_resultset_create", r);
857     r->refcount = 1;
858     r->size = 0;
859     r->odr = odr_createmem(ODR_ENCODE);
860     r->piggyback = 1;
861     r->setname = 0;
862     r->schema = 0;
863     r->step = 0;
864     for (i = 0; i<RECORD_HASH_SIZE; i++)
865         r->record_hash[i] = 0;
866     r->r_sort_spec = 0;
867     r->query = 0;
868     r->connection = 0;
869     r->databaseNames = 0;
870     r->num_databaseNames = 0;
871     r->mutex = 0;
872     yaz_mutex_create(&r->mutex);
873     return r;
874 }
875
876 ZOOM_API(ZOOM_resultset)
877     ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
878 {
879     ZOOM_resultset r;
880     ZOOM_query s = ZOOM_query_create();
881
882     ZOOM_query_prefix(s, q);
883
884     r = ZOOM_connection_search(c, s);
885     ZOOM_query_destroy(s);
886     return r;
887 }
888
889 ZOOM_API(ZOOM_resultset)
890     ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
891 {
892     ZOOM_resultset r = ZOOM_resultset_create();
893     ZOOM_task task;
894     const char *cp;
895     int start, count;
896     const char *syntax, *elementSetName;
897     ZOOM_resultsets set;
898
899     yaz_log(log_api, "%p ZOOM_connection_search set %p query %p", c, r, q);
900     r->r_sort_spec = q->sort_spec;
901     r->query = q;
902
903     r->options = ZOOM_options_create_with_parent(c->options);
904     
905     start = ZOOM_options_get_int(r->options, "start", 0);
906     count = ZOOM_options_get_int(r->options, "count", 0);
907     {
908         /* If "presentChunk" is defined use that; otherwise "step" */
909         const char *cp = ZOOM_options_get(r->options, "presentChunk");
910         r->step = ZOOM_options_get_int(r->options,
911                                        (cp != 0 ? "presentChunk": "step"), 0);
912     }
913     r->piggyback = ZOOM_options_get_bool(r->options, "piggyback", 1);
914     cp = ZOOM_options_get(r->options, "setname");
915     if (cp)
916         r->setname = xstrdup(cp);
917     cp = ZOOM_options_get(r->options, "schema");
918     if (cp)
919         r->schema = xstrdup(cp);
920
921     r->databaseNames = set_DatabaseNames(c, c->options, &r->num_databaseNames,
922                                          r->odr);
923     
924     r->connection = c;
925
926     yaz_log(log_details, "%p ZOOM_connection_search: Adding new resultset (%p) to resultsets (%p) ", c, r, c->resultsets);
927     set = xmalloc(sizeof(*set));
928     ZOOM_resultset_addref(r);
929     set->resultset = r;
930     set->next = c->resultsets;
931     c->resultsets = set;
932
933     if (c->host_port && c->proto == PROTO_HTTP)
934     {
935         if (!c->cs)
936         {
937             yaz_log(log_details, "ZOOM_connection_search: no comstack");
938             ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
939         }
940         else
941         {
942             yaz_log(log_details, "ZOOM_connection_search: reconnect");
943             c->reconnect_ok = 1;
944         }
945     }
946
947     task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
948     task->u.search.resultset = r;
949     task->u.search.start = start;
950     task->u.search.count = count;
951     task->u.search.recv_search_fired = 0;
952
953     syntax = ZOOM_options_get(r->options, "preferredRecordSyntax"); 
954     task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
955     elementSetName = ZOOM_options_get(r->options, "elementSetName");
956     task->u.search.elementSetName = elementSetName 
957         ? xstrdup(elementSetName) : 0;
958    
959     ZOOM_resultset_addref(r);
960
961     (q->refcount)++;
962
963     if (!c->async)
964     {
965         while (ZOOM_event(1, &c))
966             ;
967     }
968     return r;
969 }
970
971 ZOOM_API(void)
972     ZOOM_resultset_sort(ZOOM_resultset r,
973                          const char *sort_type, const char *sort_spec)
974 {
975     (void) ZOOM_resultset_sort1(r, sort_type, sort_spec);
976 }
977
978 ZOOM_API(int)
979     ZOOM_resultset_sort1(ZOOM_resultset r,
980                          const char *sort_type, const char *sort_spec)
981 {
982     ZOOM_connection c = r->connection;
983     ZOOM_task task;
984     ZOOM_query newq;
985
986     newq = ZOOM_query_create();
987     if (ZOOM_query_sortby(newq, sort_spec) < 0)
988         return -1;
989
990     yaz_log(log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s",
991             r, r, sort_type, sort_spec);
992     if (!c)
993         return 0;
994
995     if (c->host_port && c->proto == PROTO_HTTP)
996     {
997         if (!c->cs)
998         {
999             yaz_log(log_details, "%p ZOOM_resultset_sort: no comstack", r);
1000             ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
1001         }
1002         else
1003         {
1004             yaz_log(log_details, "%p ZOOM_resultset_sort: prepare reconnect",
1005                     r);
1006             c->reconnect_ok = 1;
1007         }
1008     }
1009     
1010     ZOOM_resultset_cache_reset(r);
1011     task = ZOOM_connection_add_task(c, ZOOM_TASK_SORT);
1012     task->u.sort.resultset = r;
1013     task->u.sort.q = newq;
1014
1015     ZOOM_resultset_addref(r);  
1016
1017     if (!c->async)
1018     {
1019         while (ZOOM_event(1, &c))
1020             ;
1021     }
1022
1023     return 0;
1024 }
1025
1026 static void ZOOM_record_release(ZOOM_record rec)
1027 {
1028     if (!rec)
1029         return;
1030     if (rec->wrbuf)
1031         wrbuf_destroy(rec->wrbuf);
1032 #if YAZ_HAVE_XML2
1033     if (rec->xml_mem)
1034         xmlFree(rec->xml_mem);
1035 #endif
1036     if (rec->odr)
1037         odr_destroy(rec->odr);
1038 }
1039
1040 ZOOM_API(void)
1041     ZOOM_resultset_cache_reset(ZOOM_resultset r)
1042 {
1043     int i;
1044     for (i = 0; i<RECORD_HASH_SIZE; i++)
1045     {
1046         ZOOM_record_cache rc;
1047         for (rc = r->record_hash[i]; rc; rc = rc->next)
1048         {
1049             ZOOM_record_release(&rc->rec);
1050         }
1051         r->record_hash[i] = 0;
1052     }
1053 }
1054
1055 ZOOM_API(void)
1056     ZOOM_resultset_destroy(ZOOM_resultset r)
1057 {
1058     resultset_destroy(r);
1059 }
1060
1061 static void resultset_destroy(ZOOM_resultset r)
1062 {
1063     if (!r)
1064         return;
1065     yaz_mutex_enter(r->mutex);
1066     (r->refcount)--;
1067     yaz_log(log_details, "%p ZOOM_resultset_destroy r=%p count=%d",
1068             r, r, r->refcount);
1069     if (r->refcount == 0)
1070     {
1071         yaz_mutex_leave(r->mutex);
1072         yaz_log(log_details, "%p ZOOM_connection resultset_destroy: Deleting resultset (%p) ", r->connection, r);
1073         ZOOM_resultset_cache_reset(r);
1074         ZOOM_query_destroy(r->query);
1075         ZOOM_options_destroy(r->options);
1076         odr_destroy(r->odr);
1077         xfree(r->setname);
1078         xfree(r->schema);
1079         yaz_mutex_destroy(&r->mutex);
1080         xfree(r);
1081     }
1082     else
1083         yaz_mutex_leave(r->mutex);
1084 }
1085
1086 ZOOM_API(size_t)
1087     ZOOM_resultset_size(ZOOM_resultset r)
1088 {
1089     yaz_log(log_details, "ZOOM_resultset_size r=%p count=" ODR_INT_PRINTF,
1090             r, r->size);
1091     return r->size;
1092 }
1093
1094 static void do_close(ZOOM_connection c)
1095 {
1096     if (c->cs)
1097         cs_close(c->cs);
1098     c->cs = 0;
1099     ZOOM_connection_set_mask(c, 0);
1100     c->state = STATE_IDLE;
1101 }
1102
1103 static int ZOOM_test_reconnect(ZOOM_connection c)
1104 {
1105     ZOOM_Event event;
1106
1107     if (!c->reconnect_ok)
1108         return 0;
1109     do_close(c);
1110     c->reconnect_ok = 0;
1111     c->tasks->running = 0;
1112     ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
1113
1114     event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1115     ZOOM_connection_put_event(c, event);
1116
1117     return 1;
1118 }
1119
1120 static void ZOOM_resultset_retrieve(ZOOM_resultset r,
1121                                     int force_sync, int start, int count)
1122 {
1123     ZOOM_task task;
1124     ZOOM_connection c;
1125     const char *cp;
1126     const char *syntax, *elementSetName;
1127
1128     if (!r)
1129         return;
1130     yaz_log(log_details, "%p ZOOM_resultset_retrieve force_sync=%d start=%d"
1131             " count=%d", r, force_sync, start, count);
1132     c = r->connection;
1133     if (!c)
1134         return;
1135
1136     if (c->host_port && c->proto == PROTO_HTTP)
1137     {
1138         if (!c->cs)
1139         {
1140             yaz_log(log_details, "%p ZOOM_resultset_retrieve: no comstack", r);
1141             ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
1142         }
1143         else
1144         {
1145             yaz_log(log_details, "%p ZOOM_resultset_retrieve: prepare "
1146                     "reconnect", r);
1147             c->reconnect_ok = 1;
1148         }
1149     }
1150     task = ZOOM_connection_add_task(c, ZOOM_TASK_RETRIEVE);
1151     task->u.retrieve.resultset = r;
1152     task->u.retrieve.start = start;
1153     task->u.retrieve.count = count;
1154
1155     syntax = ZOOM_options_get(r->options, "preferredRecordSyntax"); 
1156     task->u.retrieve.syntax = syntax ? xstrdup(syntax) : 0;
1157     elementSetName = ZOOM_options_get(r->options, "elementSetName");
1158     task->u.retrieve.elementSetName = elementSetName 
1159         ? xstrdup(elementSetName) : 0;
1160
1161     cp = ZOOM_options_get(r->options, "schema");
1162     if (cp)
1163     {
1164         if (!r->schema || strcmp(r->schema, cp))
1165         {
1166             xfree(r->schema);
1167             r->schema = xstrdup(cp);
1168         }
1169     }
1170
1171     ZOOM_resultset_addref(r);
1172
1173     if (!r->connection->async || force_sync)
1174         while (r->connection && ZOOM_event(1, &r->connection))
1175             ;
1176 }
1177
1178 ZOOM_API(void)
1179     ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
1180                            size_t start, size_t count)
1181 {
1182     int force_present = 0;
1183
1184     if (!r)
1185         return ;
1186     yaz_log(log_api, "%p ZOOM_resultset_records r=%p start=%ld count=%ld",
1187             r, r, (long) start, (long) count);
1188     if (count && recs)
1189         force_present = 1;
1190     ZOOM_resultset_retrieve(r, force_present, start, count);
1191     if (force_present)
1192     {
1193         size_t i;
1194         for (i = 0; i< count; i++)
1195             recs[i] = ZOOM_resultset_record_immediate(r, i+start);
1196     }
1197 }
1198
1199 static void get_cert(ZOOM_connection c)
1200 {
1201     char *cert_buf;
1202     int cert_len;
1203     
1204     if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
1205     {
1206         ZOOM_connection_option_setl(c, "sslPeerCert",
1207                                     cert_buf, cert_len);
1208         xfree(cert_buf);
1209     }
1210 }
1211
1212 static zoom_ret do_connect_host(ZOOM_connection c,
1213                                 const char *effective_host,
1214                                 const char *logical_url);
1215
1216 static zoom_ret do_connect(ZOOM_connection c)
1217 {
1218     const char *effective_host;
1219
1220     if (c->proxy)
1221         effective_host = c->proxy;
1222     else
1223         effective_host = c->host_port;
1224     return do_connect_host(c, effective_host, c->host_port);
1225 }
1226
1227 static zoom_ret do_connect_host(ZOOM_connection c, const char *effective_host,
1228     const char *logical_url)
1229 {
1230     void *add;
1231
1232     yaz_log(log_details, "%p do_connect effective_host=%s", c, effective_host);
1233
1234     if (c->cs)
1235         cs_close(c->cs);
1236     c->cs = cs_create_host(effective_host, 0, &add);
1237
1238     if (c->cs && c->cs->protocol == PROTO_HTTP)
1239     {
1240 #if YAZ_HAVE_XML2
1241         if (logical_url)
1242         {
1243             const char *db = 0;
1244             
1245             c->proto = PROTO_HTTP;
1246             cs_get_host_args(logical_url, &db);
1247             xfree(c->path);
1248             
1249             c->path = xmalloc(strlen(db) * 3 + 2);
1250             yaz_encode_sru_dbpath_buf(c->path, db);
1251         }
1252 #else
1253         set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
1254         do_close(c);
1255         return zoom_complete;
1256 #endif
1257     }
1258     if (c->cs)
1259     {
1260         int ret = cs_connect(c->cs, add);
1261         if (ret == 0)
1262         {
1263             ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1264             ZOOM_connection_put_event(c, event);
1265             get_cert(c);
1266             if (c->proto == PROTO_Z3950)
1267                 ZOOM_connection_send_init(c);
1268             else
1269             {
1270                 /* no init request for SRW .. */
1271                 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1272                 ZOOM_connection_remove_task(c);
1273                 ZOOM_connection_set_mask(c, 0);
1274                 ZOOM_connection_exec_task(c);
1275             }
1276             c->state = STATE_ESTABLISHED;
1277             return zoom_pending;
1278         }
1279         else if (ret > 0)
1280         {
1281             int mask = ZOOM_SELECT_EXCEPT;
1282             if (c->cs->io_pending & CS_WANT_WRITE)
1283                 mask += ZOOM_SELECT_WRITE;
1284             if (c->cs->io_pending & CS_WANT_READ)
1285                 mask += ZOOM_SELECT_READ;
1286             ZOOM_connection_set_mask(c, mask);
1287             c->state = STATE_CONNECTING; 
1288             return zoom_pending;
1289         }
1290     }
1291     c->state = STATE_IDLE;
1292     set_ZOOM_error(c, ZOOM_ERROR_CONNECT, logical_url);
1293     return zoom_complete;
1294 }
1295
1296 static void otherInfo_attach(ZOOM_connection c, Z_APDU *a, ODR out)
1297 {
1298     int i;
1299     for (i = 0; i<200; i++)
1300     {
1301         size_t len;
1302         Odr_oid *oid;
1303         Z_OtherInformation **oi;
1304         char buf[80];
1305         const char *val;
1306         const char *cp;
1307
1308         sprintf(buf, "otherInfo%d", i);
1309         val = ZOOM_options_get(c->options, buf);
1310         if (!val)
1311             break;
1312         cp = strchr(val, ':');
1313         if (!cp)
1314             continue;
1315         len = cp - val;
1316         if (len >= sizeof(buf))
1317             len = sizeof(buf)-1;
1318         memcpy(buf, val, len);
1319         buf[len] = '\0';
1320         
1321         oid = yaz_string_to_oid_odr(yaz_oid_std(), CLASS_USERINFO,
1322                                     buf, out);
1323         if (!oid)
1324             continue;
1325         
1326         yaz_oi_APDU(a, &oi);
1327         yaz_oi_set_string_oid(oi, out, oid, 1, cp+1);
1328     }
1329 }
1330
1331 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
1332 {
1333     assert(a);
1334     if (c->cookie_out)
1335     {
1336         Z_OtherInformation **oi;
1337         yaz_oi_APDU(a, &oi);
1338         yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_cookie, 
1339                               1, c->cookie_out);
1340     }
1341     if (c->client_IP)
1342     {
1343         Z_OtherInformation **oi;
1344         yaz_oi_APDU(a, &oi);
1345         yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_client_ip, 
1346                               1, c->client_IP);
1347     }
1348     otherInfo_attach(c, a, out);
1349     if (!z_APDU(out, &a, 0, 0))
1350     {
1351         FILE *outf = fopen("/tmp/apdu.txt", "a");
1352         if (a && outf)
1353         {
1354             ODR odr_pr = odr_createmem(ODR_PRINT);
1355             fprintf(outf, "a=%p\n", a);
1356             odr_setprint(odr_pr, outf);
1357             z_APDU(odr_pr, &a, 0, 0);
1358             odr_destroy(odr_pr);
1359         }
1360         yaz_log(log_api, "%p encoding_APDU: encoding failed", c);
1361         set_ZOOM_error(c, ZOOM_ERROR_ENCODE, 0);
1362         odr_reset(out);
1363         return -1;
1364     }
1365     if (c->odr_print)
1366         z_APDU(c->odr_print, &a, 0, 0);
1367     yaz_log(log_details, "%p encoding_APDU encoding OK", c);
1368     return 0;
1369 }
1370
1371 static zoom_ret send_APDU(ZOOM_connection c, Z_APDU *a)
1372 {
1373     ZOOM_Event event;
1374     assert(a);
1375     if (encode_APDU(c, a, c->odr_out))
1376         return zoom_complete;
1377     yaz_log(log_details, "%p send APDU type=%d", c, a->which);
1378     c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1379     event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1380     ZOOM_connection_put_event(c, event);
1381     odr_reset(c->odr_out);
1382     return do_write(c);
1383 }
1384
1385 /* returns 1 if PDU was sent OK (still pending )
1386    0 if PDU was not sent OK (nothing to wait for) 
1387 */
1388
1389 static zoom_ret ZOOM_connection_send_init(ZOOM_connection c)
1390 {
1391     Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
1392     Z_InitRequest *ireq = apdu->u.initRequest;
1393     Z_IdAuthentication *auth = (Z_IdAuthentication *)
1394         odr_malloc(c->odr_out, sizeof(*auth));
1395
1396     ODR_MASK_SET(ireq->options, Z_Options_search);
1397     ODR_MASK_SET(ireq->options, Z_Options_present);
1398     ODR_MASK_SET(ireq->options, Z_Options_scan);
1399     ODR_MASK_SET(ireq->options, Z_Options_sort);
1400     ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
1401     ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
1402     
1403     ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
1404     ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
1405     ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
1406     
1407     ireq->implementationId =
1408         odr_prepend(c->odr_out,
1409                     ZOOM_options_get(c->options, "implementationId"),
1410                     ireq->implementationId);
1411     
1412     ireq->implementationName = 
1413         odr_prepend(c->odr_out,
1414                     ZOOM_options_get(c->options, "implementationName"),
1415                     odr_prepend(c->odr_out, "ZOOM-C",
1416                                 ireq->implementationName));
1417     
1418     ireq->implementationVersion = 
1419         odr_prepend(c->odr_out,
1420                     ZOOM_options_get(c->options, "implementationVersion"),
1421                                 ireq->implementationVersion);
1422     
1423     *ireq->maximumRecordSize = c->maximum_record_size;
1424     *ireq->preferredMessageSize = c->preferred_message_size;
1425     
1426     if (c->group || c->password)
1427     {
1428         Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
1429         pass->groupId = odr_strdup_null(c->odr_out, c->group);
1430         pass->userId = odr_strdup_null(c->odr_out, c->user);
1431         pass->password = odr_strdup_null(c->odr_out, c->password);
1432         auth->which = Z_IdAuthentication_idPass;
1433         auth->u.idPass = pass;
1434         ireq->idAuthentication = auth;
1435     }
1436     else if (c->user)
1437     {
1438         auth->which = Z_IdAuthentication_open;
1439         auth->u.open = odr_strdup(c->odr_out, c->user);
1440         ireq->idAuthentication = auth;
1441     }
1442     if (c->proxy)
1443     {
1444         yaz_oi_set_string_oid(&ireq->otherInfo, c->odr_out,
1445                               yaz_oid_userinfo_proxy, 1, c->host_port);
1446     }
1447     if (c->charset || c->lang)
1448     {
1449         Z_OtherInformation **oi;
1450         Z_OtherInformationUnit *oi_unit;
1451         
1452         yaz_oi_APDU(apdu, &oi);
1453         
1454         if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
1455         {
1456             ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
1457             oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
1458             oi_unit->information.externallyDefinedInfo =
1459                 yaz_set_proposal_charneg_list(c->odr_out, " ",
1460                                               c->charset, c->lang, 1);
1461         }
1462     }
1463     assert(apdu);
1464     return send_APDU(c, apdu);
1465 }
1466
1467 #if YAZ_HAVE_XML2
1468 static zoom_ret send_srw(ZOOM_connection c, Z_SRW_PDU *sr)
1469 {
1470     Z_GDU *gdu;
1471     ZOOM_Event event;
1472     const char *database =  ZOOM_options_get(c->options, "databaseName");
1473     char *fdatabase = 0;
1474     
1475     if (database)
1476         fdatabase = yaz_encode_sru_dbpath_odr(c->odr_out, database);
1477     gdu = z_get_HTTP_Request_host_path(c->odr_out, c->host_port,
1478                                        fdatabase ? fdatabase : c->path);
1479
1480     if (c->sru_mode == zoom_sru_get)
1481     {
1482         yaz_sru_get_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1483     }
1484     else if (c->sru_mode == zoom_sru_post)
1485     {
1486         yaz_sru_post_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1487     }
1488     else if (c->sru_mode == zoom_sru_soap)
1489     {
1490         yaz_sru_soap_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1491     }
1492     if (!z_GDU(c->odr_out, &gdu, 0, 0))
1493         return zoom_complete;
1494     if (c->odr_print)
1495         z_GDU(c->odr_print, &gdu, 0, 0);
1496     c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1497         
1498     event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1499     ZOOM_connection_put_event(c, event);
1500     odr_reset(c->odr_out);
1501     return do_write(c);
1502 }
1503 #endif
1504
1505 #if YAZ_HAVE_XML2
1506 static Z_SRW_PDU *ZOOM_srw_get_pdu(ZOOM_connection c, int type)
1507 {
1508     Z_SRW_PDU *sr = yaz_srw_get_pdu(c->odr_out, type, c->sru_version);
1509     sr->username = c->user;
1510     sr->password = c->password;
1511     return sr;
1512 }
1513 #endif
1514
1515 #if YAZ_HAVE_XML2
1516 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1517 {
1518     int i;
1519     int *start, *count;
1520     ZOOM_resultset resultset = 0;
1521     Z_SRW_PDU *sr = 0;
1522     const char *option_val = 0;
1523
1524     if (c->error)                  /* don't continue on error */
1525         return zoom_complete;
1526     assert(c->tasks);
1527     switch(c->tasks->which)
1528     {
1529     case ZOOM_TASK_SEARCH:
1530         resultset = c->tasks->u.search.resultset;
1531         if (!resultset->setname)
1532             resultset->setname = xstrdup("default");
1533         ZOOM_options_set(resultset->options, "setname", resultset->setname);
1534         start = &c->tasks->u.search.start;
1535         count = &c->tasks->u.search.count;
1536         break;
1537     case ZOOM_TASK_RETRIEVE:
1538         resultset = c->tasks->u.retrieve.resultset;
1539
1540         start = &c->tasks->u.retrieve.start;
1541         count = &c->tasks->u.retrieve.count;
1542
1543         if (*start >= resultset->size)
1544             return zoom_complete;
1545         if (*start + *count > resultset->size)
1546             *count = resultset->size - *start;
1547
1548         for (i = 0; i < *count; i++)
1549         {
1550             ZOOM_record rec =
1551                 record_cache_lookup(resultset, i + *start,
1552                                     c->tasks->u.retrieve.syntax,
1553                                     c->tasks->u.retrieve.elementSetName);
1554             if (!rec)
1555                 break;
1556             else
1557             {
1558                 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
1559                 ZOOM_connection_put_event(c, event);
1560             }
1561         }
1562         *start += i;
1563         *count -= i;
1564
1565         if (*count == 0)
1566             return zoom_complete;
1567         break;
1568     default:
1569         return zoom_complete;
1570     }
1571     assert(resultset->query);
1572         
1573     sr = ZOOM_srw_get_pdu(c, Z_SRW_searchRetrieve_request);
1574     if (resultset->query->z_query->which == Z_Query_type_104
1575         && resultset->query->z_query->u.type_104->which == Z_External_CQL)
1576     {
1577         sr->u.request->query_type = Z_SRW_query_type_cql;
1578         sr->u.request->query.cql =resultset->query->z_query->u.type_104->u.cql;
1579     }
1580     else if (resultset->query->z_query->which == Z_Query_type_1 &&
1581              resultset->query->z_query->u.type_1)
1582     {
1583         sr->u.request->query_type = Z_SRW_query_type_pqf;
1584         sr->u.request->query.pqf = resultset->query->query_string;
1585     }
1586     else
1587     {
1588         set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
1589         return zoom_complete;
1590     }
1591     sr->u.request->startRecord = odr_intdup(c->odr_out, *start + 1);
1592     sr->u.request->maximumRecords = odr_intdup(
1593         c->odr_out, (resultset->step > 0 && resultset->step < *count) ? 
1594         resultset->step : *count);
1595     sr->u.request->recordSchema = resultset->schema;
1596     
1597     option_val = ZOOM_resultset_option_get(resultset, "recordPacking");
1598     if (option_val)
1599         sr->u.request->recordPacking = odr_strdup(c->odr_out, option_val);
1600
1601     option_val = ZOOM_resultset_option_get(resultset, "extraArgs");
1602     yaz_encode_sru_extra(sr, c->odr_out, option_val);
1603     return send_srw(c, sr);
1604 }
1605 #else
1606 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1607 {
1608     return zoom_complete;
1609 }
1610 #endif
1611
1612 static zoom_ret ZOOM_connection_send_search(ZOOM_connection c)
1613 {
1614     ZOOM_resultset r;
1615     int lslb, ssub, mspn;
1616     const char *syntax;
1617     Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
1618     Z_SearchRequest *search_req = apdu->u.searchRequest;
1619     const char *elementSetName;
1620     const char *smallSetElementSetName;
1621     const char *mediumSetElementSetName;
1622
1623     assert(c->tasks);
1624     assert(c->tasks->which == ZOOM_TASK_SEARCH);
1625
1626     r = c->tasks->u.search.resultset;
1627
1628     yaz_log(log_details, "%p ZOOM_connection_send_search set=%p", c, r);
1629
1630     elementSetName =
1631         ZOOM_options_get(r->options, "elementSetName");
1632     smallSetElementSetName  =
1633         ZOOM_options_get(r->options, "smallSetElementSetName");
1634     mediumSetElementSetName =
1635         ZOOM_options_get(r->options, "mediumSetElementSetName");
1636
1637     if (!smallSetElementSetName)
1638         smallSetElementSetName = elementSetName;
1639
1640     if (!mediumSetElementSetName)
1641         mediumSetElementSetName = elementSetName;
1642
1643     assert(r);
1644     assert(r->query);
1645
1646     /* prepare query for the search request */
1647     search_req->query = r->query->z_query;
1648     if (!search_req->query)
1649     {
1650         set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
1651         return zoom_complete;
1652     }
1653     if (r->query->z_query->which == Z_Query_type_1 || 
1654         r->query->z_query->which == Z_Query_type_101)
1655     {
1656         const char *cp = ZOOM_options_get(r->options, "rpnCharset");
1657         if (cp)
1658         {
1659             yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
1660             if (cd)
1661             {
1662                 int r;
1663                 search_req->query = yaz_copy_Z_Query(search_req->query,
1664                                                      c->odr_out);
1665                 
1666                 r = yaz_query_charset_convert_rpnquery_check(
1667                     search_req->query->u.type_1,
1668                     c->odr_out, cd);
1669                 yaz_iconv_close(cd);
1670                 if (r)
1671                 {  /* query could not be char converted */
1672                     set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
1673                     return zoom_complete;
1674                 }
1675             }
1676         }
1677     }
1678     search_req->databaseNames = r->databaseNames;
1679     search_req->num_databaseNames = r->num_databaseNames;
1680
1681     /* get syntax (no need to provide unless piggyback is in effect) */
1682     syntax = c->tasks->u.search.syntax;
1683
1684     lslb = ZOOM_options_get_int(r->options, "largeSetLowerBound", -1);
1685     ssub = ZOOM_options_get_int(r->options, "smallSetUpperBound", -1);
1686     mspn = ZOOM_options_get_int(r->options, "mediumSetPresentNumber", -1);
1687     if (lslb != -1 && ssub != -1 && mspn != -1)
1688     {
1689         /* So're a Z39.50 expert? Let's hope you don't do sort */
1690         *search_req->largeSetLowerBound = lslb;
1691         *search_req->smallSetUpperBound = ssub;
1692         *search_req->mediumSetPresentNumber = mspn;
1693     }
1694     else if (c->tasks->u.search.start == 0 && c->tasks->u.search.count > 0
1695              && r->piggyback && !r->r_sort_spec && !r->schema)
1696     {
1697         /* Regular piggyback - do it unless we're going to do sort */
1698         *search_req->largeSetLowerBound = 2000000000;
1699         *search_req->smallSetUpperBound = 1;
1700         *search_req->mediumSetPresentNumber = 
1701             r->step>0 ? r->step : c->tasks->u.search.count;
1702     }
1703     else
1704     {
1705         /* non-piggyback. Need not provide elementsets or syntaxes .. */
1706         smallSetElementSetName = 0;
1707         mediumSetElementSetName = 0;
1708         syntax = 0;
1709     }
1710     if (smallSetElementSetName && *smallSetElementSetName)
1711     {
1712         Z_ElementSetNames *esn = (Z_ElementSetNames *)
1713             odr_malloc(c->odr_out, sizeof(*esn));
1714         
1715         esn->which = Z_ElementSetNames_generic;
1716         esn->u.generic = odr_strdup(c->odr_out, smallSetElementSetName);
1717         search_req->smallSetElementSetNames = esn;
1718     }
1719     if (mediumSetElementSetName && *mediumSetElementSetName)
1720     {
1721         Z_ElementSetNames *esn =(Z_ElementSetNames *)
1722             odr_malloc(c->odr_out, sizeof(*esn));
1723         
1724         esn->which = Z_ElementSetNames_generic;
1725         esn->u.generic = odr_strdup(c->odr_out, mediumSetElementSetName);
1726         search_req->mediumSetElementSetNames = esn;
1727     }
1728     if (syntax)
1729         search_req->preferredRecordSyntax =
1730             zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
1731     
1732     if (!r->setname)
1733     {
1734         if (c->support_named_resultsets)
1735         {
1736             char setname[14];
1737             int ord;
1738             /* find the lowest unused ordinal so that we re-use
1739                result sets on the server. */
1740             for (ord = 1; ; ord++)
1741             {
1742                 ZOOM_resultsets rsp;
1743                 sprintf(setname, "%d", ord);
1744                 for (rsp = c->resultsets; rsp; rsp = rsp->next)
1745                     if (rsp->resultset->setname && !strcmp(rsp->resultset->setname, setname))
1746                         break;
1747                 if (!rsp)
1748                     break;
1749             }
1750             r->setname = xstrdup(setname);
1751             yaz_log(log_details, "%p ZOOM_connection_send_search: allocating "
1752                     "set %s", c, r->setname);
1753         }
1754         else
1755         {
1756             yaz_log(log_details, "%p ZOOM_connection_send_search: using "
1757                     "default set", c);
1758             r->setname = xstrdup("default");
1759         }
1760         ZOOM_options_set(r->options, "setname", r->setname);
1761     }
1762     search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
1763     return send_APDU(c, apdu);
1764 }
1765
1766 static void response_default_diag(ZOOM_connection c, Z_DefaultDiagFormat *r)
1767 {
1768     char oid_name_buf[OID_STR_MAX];
1769     const char *oid_name;
1770     char *addinfo = 0;
1771
1772     oid_name = yaz_oid_to_string_buf(r->diagnosticSetId, 0, oid_name_buf);
1773     switch (r->which)
1774     {
1775     case Z_DefaultDiagFormat_v2Addinfo:
1776         addinfo = r->u.v2Addinfo;
1777         break;
1778     case Z_DefaultDiagFormat_v3Addinfo:
1779         addinfo = r->u.v3Addinfo;
1780         break;
1781     }
1782     xfree(c->addinfo);
1783     c->addinfo = 0;
1784     set_dset_error(c, *r->condition, oid_name, addinfo, 0);
1785 }
1786
1787 static void response_diag(ZOOM_connection c, Z_DiagRec *p)
1788 {
1789     if (p->which != Z_DiagRec_defaultFormat)
1790         set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1791     else
1792         response_default_diag(c, p->u.defaultFormat);
1793 }
1794
1795 ZOOM_API(ZOOM_record)
1796     ZOOM_record_clone(ZOOM_record srec)
1797 {
1798     char *buf;
1799     int size;
1800     ODR odr_enc;
1801     ZOOM_record nrec;
1802
1803     odr_enc = odr_createmem(ODR_ENCODE);
1804     if (!z_NamePlusRecord(odr_enc, &srec->npr, 0, 0))
1805         return 0;
1806     buf = odr_getbuf(odr_enc, &size, 0);
1807     
1808     nrec = (ZOOM_record) xmalloc(sizeof(*nrec));
1809     nrec->odr = odr_createmem(ODR_DECODE);
1810     nrec->wrbuf = 0;
1811 #if YAZ_HAVE_XML2
1812     nrec->xml_mem = 0;
1813     nrec->xml_size = 0;
1814 #endif
1815     odr_setbuf(nrec->odr, buf, size, 0);
1816     z_NamePlusRecord(nrec->odr, &nrec->npr, 0, 0);
1817     
1818     nrec->schema = odr_strdup_null(nrec->odr, srec->schema);
1819     nrec->diag_uri = odr_strdup_null(nrec->odr, srec->diag_uri);
1820     nrec->diag_message = odr_strdup_null(nrec->odr, srec->diag_message);
1821     nrec->diag_details = odr_strdup_null(nrec->odr, srec->diag_details);
1822     nrec->diag_set = odr_strdup_null(nrec->odr, srec->diag_set);
1823     odr_destroy(odr_enc);
1824     return nrec;
1825 }
1826
1827 ZOOM_API(ZOOM_record)
1828     ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
1829 {
1830     const char *syntax =
1831         ZOOM_options_get(s->options, "preferredRecordSyntax"); 
1832     const char *elementSetName =
1833         ZOOM_options_get(s->options, "elementSetName");
1834
1835     return record_cache_lookup(s, pos, syntax, elementSetName);
1836 }
1837
1838 ZOOM_API(ZOOM_record)
1839     ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
1840 {
1841     ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1842
1843     if (!rec)
1844     {
1845         /*
1846          * MIKE: I think force_sync should always be zero, but I don't
1847          * want to make this change until I get the go-ahead from
1848          * Adam, in case something depends on the old synchronous
1849          * behaviour.
1850          */
1851         int force_sync = 1;
1852         if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
1853         ZOOM_resultset_retrieve(r, force_sync, pos, 1);
1854         rec = ZOOM_resultset_record_immediate(r, pos);
1855     }
1856     return rec;
1857 }
1858
1859 ZOOM_API(void)
1860     ZOOM_record_destroy(ZOOM_record rec)
1861 {
1862     ZOOM_record_release(rec);
1863     xfree(rec);
1864 }
1865
1866
1867 static yaz_iconv_t iconv_create_charset(const char *record_charset)
1868 {
1869     char to[40];
1870     char from[40];
1871     yaz_iconv_t cd = 0;
1872
1873     *from = '\0';
1874     strcpy(to, "UTF-8");
1875     if (record_charset && *record_charset)
1876     {
1877         /* Use "from,to" or just "from" */
1878         const char *cp = strchr(record_charset, ',');
1879         size_t clen = strlen(record_charset);
1880         if (cp && cp[1])
1881         {
1882             strncpy( to, cp+1, sizeof(to)-1);
1883             to[sizeof(to)-1] = '\0';
1884             clen = cp - record_charset;
1885         }
1886         if (clen > sizeof(from)-1)
1887             clen = sizeof(from)-1;
1888         
1889         if (clen)
1890             strncpy(from, record_charset, clen);
1891         from[clen] = '\0';
1892     }
1893     if (*from && *to)
1894         cd = yaz_iconv_open(to, from);
1895     return cd;
1896 }
1897
1898 static const char *return_marc_record(ZOOM_record rec, int marc_type,
1899                                       int *len,
1900                                       const char *buf, int sz,
1901                                       const char *record_charset)
1902 {
1903     yaz_iconv_t cd = iconv_create_charset(record_charset);
1904     yaz_marc_t mt = yaz_marc_create();
1905     const char *ret_string = 0;
1906
1907     if (cd)
1908         yaz_marc_iconv(mt, cd);
1909     yaz_marc_xml(mt, marc_type);
1910     if (!rec->wrbuf)
1911         rec->wrbuf = wrbuf_alloc();
1912     wrbuf_rewind(rec->wrbuf);
1913     if (yaz_marc_decode_wrbuf(mt, buf, sz, rec->wrbuf) > 0)
1914     {
1915         if (len)
1916             *len = wrbuf_len(rec->wrbuf);
1917         ret_string = wrbuf_cstr(rec->wrbuf);
1918     }
1919     yaz_marc_destroy(mt);
1920     if (cd)
1921         yaz_iconv_close(cd);
1922     return ret_string;
1923 }
1924
1925 static const char *return_opac_record(ZOOM_record rec, int marc_type,
1926                                       int *len,
1927                                       Z_OPACRecord *opac_rec,
1928                                       const char *record_charset)
1929 {
1930     yaz_iconv_t cd = iconv_create_charset(record_charset);
1931     yaz_marc_t mt = yaz_marc_create();
1932
1933     if (cd)
1934         yaz_marc_iconv(mt, cd);
1935     yaz_marc_xml(mt, marc_type);
1936
1937     if (!rec->wrbuf)
1938         rec->wrbuf = wrbuf_alloc();
1939     wrbuf_rewind(rec->wrbuf);
1940
1941     yaz_opac_decode_wrbuf(mt, opac_rec, rec->wrbuf);
1942     yaz_marc_destroy(mt);
1943
1944     if (cd)
1945         yaz_iconv_close(cd);
1946     if (len)
1947         *len = wrbuf_len(rec->wrbuf);
1948     return wrbuf_cstr(rec->wrbuf);
1949 }
1950
1951 static const char *return_string_record(ZOOM_record rec, int *len,
1952                                         const char *buf, int sz,
1953                                         const char *record_charset)
1954 {
1955     yaz_iconv_t cd = iconv_create_charset(record_charset);
1956
1957     if (cd)
1958     {
1959         if (!rec->wrbuf)
1960             rec->wrbuf = wrbuf_alloc();
1961
1962         wrbuf_rewind(rec->wrbuf);
1963
1964         wrbuf_iconv_write(rec->wrbuf, cd, buf, sz);
1965         wrbuf_iconv_reset(rec->wrbuf, cd);
1966
1967         buf = wrbuf_cstr(rec->wrbuf);
1968         sz = wrbuf_len(rec->wrbuf);
1969         yaz_iconv_close(cd);
1970     }
1971     if (len)
1972         *len = sz;
1973     return buf;
1974 }
1975
1976 static const char *return_record(ZOOM_record rec, int *len,
1977                                  Z_NamePlusRecord *npr,
1978                                  int marctype, const char *charset)
1979 {
1980     Z_External *r = (Z_External *) npr->u.databaseRecord;
1981     const Odr_oid *oid = r->direct_reference;
1982     
1983     /* render bibliographic record .. */
1984     if (r->which == Z_External_OPAC)
1985     {
1986         return return_opac_record(rec, marctype, len,
1987                                   r->u.opac, charset);
1988     }
1989     if (r->which == Z_External_sutrs)
1990         return return_string_record(rec, len,
1991                                     (char*) r->u.sutrs->buf,
1992                                     r->u.sutrs->len,
1993                                     charset);
1994     else if (r->which == Z_External_octet)
1995     {
1996         if (yaz_oid_is_iso2709(oid))
1997         {
1998             const char *ret_buf = return_marc_record(
1999                 rec, marctype, len,
2000                 (const char *) r->u.octet_aligned->buf,
2001                 r->u.octet_aligned->len,
2002                 charset);
2003             if (ret_buf)
2004                 return ret_buf;
2005             /* bad ISO2709. Return fail unless raw (ISO2709) is wanted */
2006             if (marctype != YAZ_MARC_ISO2709)
2007                 return 0;
2008         }
2009         return return_string_record(rec, len,
2010                                     (const char *) r->u.octet_aligned->buf,
2011                                     r->u.octet_aligned->len,
2012                                     charset);
2013     }
2014     else if (r->which == Z_External_grs1)
2015     {
2016         if (!rec->wrbuf)
2017             rec->wrbuf = wrbuf_alloc();
2018         wrbuf_rewind(rec->wrbuf);
2019         yaz_display_grs1(rec->wrbuf, r->u.grs1, 0);
2020         return return_string_record(rec, len,
2021                                     wrbuf_buf(rec->wrbuf),
2022                                     wrbuf_len(rec->wrbuf),
2023                                     charset);
2024     }
2025     return 0;
2026 }
2027     
2028
2029 ZOOM_API(int)
2030     ZOOM_record_error(ZOOM_record rec, const char **cp,
2031                       const char **addinfo, const char **diagset)
2032 {
2033     Z_NamePlusRecord *npr;
2034     
2035     if (!rec)
2036         return 0;
2037
2038     npr = rec->npr;
2039     if (rec->diag_uri)
2040     {
2041         if (cp)
2042             *cp = rec->diag_message;
2043         if (addinfo)
2044             *addinfo = rec->diag_details;
2045         if (diagset)
2046             *diagset = rec->diag_set;
2047         return uri_to_code(rec->diag_uri);
2048     }
2049     if (npr && npr->which == Z_NamePlusRecord_surrogateDiagnostic)
2050     {
2051         Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic;
2052         int error = YAZ_BIB1_UNSPECIFIED_ERROR;
2053         const char *add = 0;
2054
2055         if (diag_rec->which == Z_DiagRec_defaultFormat)
2056         {
2057             Z_DefaultDiagFormat *ddf = diag_rec->u.defaultFormat;
2058             oid_class oclass;
2059     
2060             error = *ddf->condition;
2061             switch (ddf->which)
2062             {
2063             case Z_DefaultDiagFormat_v2Addinfo:
2064                 add = ddf->u.v2Addinfo;
2065                 break;
2066             case Z_DefaultDiagFormat_v3Addinfo:
2067                 add = ddf->u.v3Addinfo;
2068                 break;
2069             }
2070             if (diagset)
2071                 *diagset =
2072                     yaz_oid_to_string(yaz_oid_std(),
2073                                       ddf->diagnosticSetId, &oclass);
2074         }
2075         else
2076         {
2077             if (diagset)
2078                 *diagset = "Bib-1";
2079         }
2080         if (addinfo)
2081             *addinfo = add ? add : "";
2082         if (cp)
2083             *cp = diagbib1_str(error);
2084         return error;
2085     }
2086     return 0;
2087 }
2088
2089 static const char *get_record_format(ZOOM_record rec, int *len,
2090                                      Z_NamePlusRecord *npr,
2091                                      int marctype, const char *charset,
2092                                      const char *format)
2093 {
2094     const char *res = return_record(rec, len, npr, marctype, charset);
2095 #if YAZ_HAVE_XML2
2096     if (*format == '1' && len)
2097     {
2098         /* try to XML format res */
2099         xmlDocPtr doc;
2100         xmlKeepBlanksDefault(0); /* get get xmlDocFormatMemory to work! */
2101         doc = xmlParseMemory(res, *len);
2102         if (doc)
2103         {
2104             if (rec->xml_mem)
2105                 xmlFree(rec->xml_mem);
2106             xmlDocDumpFormatMemory(doc, &rec->xml_mem, &rec->xml_size, 1);
2107             xmlFreeDoc(doc);
2108             res = (char *) rec->xml_mem;
2109             *len = rec->xml_size;
2110         } 
2111     }
2112 #endif
2113     return res;
2114 }
2115
2116
2117 ZOOM_API(const char *)
2118     ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len)
2119 {
2120     char type[40];
2121     char charset[40];
2122     char format[3];
2123     const char *cp;
2124     size_t i;
2125     Z_NamePlusRecord *npr;
2126     
2127     if (len)
2128         *len = 0; /* default return */
2129         
2130     if (!rec)
2131         return 0;
2132     npr = rec->npr;
2133     if (!npr)
2134         return 0;
2135
2136     cp = type_spec;
2137     for (i = 0; cp[i] && cp[i] != ';' && cp[i] != ' ' && i < sizeof(type)-1;
2138          i++)
2139         type[i] = cp[i];
2140     type[i] = '\0';
2141     charset[0] = '\0';
2142     format[0] = '\0';
2143     while (1)
2144     {
2145         while (cp[i] == ' ')
2146             i++;
2147         if (cp[i] != ';')
2148             break;
2149         i++;
2150         while (cp[i] == ' ')
2151             i++;
2152         if (!strncmp(cp + i, "charset=", 8))
2153         {
2154             size_t j = 0;
2155             i = i + 8; /* skip charset= */
2156             for (j = 0; cp[i] && cp[i] != ';' && cp[i] != ' '; i++)
2157             {
2158                 if (j < sizeof(charset)-1)
2159                     charset[j++] = cp[i];
2160             }
2161             charset[j] = '\0';
2162         }
2163         else if (!strncmp(cp + i, "format=", 7))
2164         {
2165             size_t j = 0; 
2166             i = i + 7;
2167             for (j = 0; cp[i] && cp[i] != ';' && cp[i] != ' '; i++)
2168             {
2169                 if (j < sizeof(format)-1)
2170                     format[j++] = cp[i];
2171             }
2172             format[j] = '\0';
2173         } 
2174     }
2175     if (!strcmp(type, "database"))
2176     {
2177         if (len)
2178             *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
2179         return npr->databaseName;
2180     }
2181     else if (!strcmp(type, "schema"))
2182     {
2183         if (len)
2184             *len = rec->schema ? strlen(rec->schema) : 0;
2185         return rec->schema;
2186     }
2187     else if (!strcmp(type, "syntax"))
2188     {
2189         const char *desc = 0;   
2190         if (npr->which == Z_NamePlusRecord_databaseRecord)
2191         {
2192             Z_External *r = (Z_External *) npr->u.databaseRecord;
2193             desc = yaz_oid_to_string(yaz_oid_std(), r->direct_reference, 0);
2194         }
2195         if (!desc)
2196             desc = "none";
2197         if (len)
2198             *len = strlen(desc);
2199         return desc;
2200     }
2201     if (npr->which != Z_NamePlusRecord_databaseRecord)
2202         return 0;
2203
2204     /* from now on - we have a database record .. */
2205     if (!strcmp(type, "render"))
2206     {
2207         return get_record_format(rec, len, npr, YAZ_MARC_LINE, charset, format);
2208     }
2209     else if (!strcmp(type, "xml"))
2210     {
2211         return get_record_format(rec, len, npr, YAZ_MARC_MARCXML, charset,
2212                                  format);
2213     }
2214     else if (!strcmp(type, "txml"))
2215     {
2216         return get_record_format(rec, len, npr, YAZ_MARC_TURBOMARC, charset,
2217                                  format);
2218     }
2219     else if (!strcmp(type, "raw"))
2220     {
2221         return get_record_format(rec, len, npr, YAZ_MARC_ISO2709, charset,
2222             format);
2223     }
2224     else if (!strcmp(type, "ext"))
2225     {
2226         if (len) *len = -1;
2227         return (const char *) npr->u.databaseRecord;
2228     }
2229     else if (!strcmp(type, "opac"))
2230     {
2231         if (npr->u.databaseRecord->which == Z_External_OPAC)
2232             return get_record_format(rec, len, npr, YAZ_MARC_MARCXML, charset,
2233                 format);
2234     }
2235     return 0;
2236 }
2237
2238 static int strcmp_null(const char *v1, const char *v2)
2239 {
2240     if (!v1 && !v2)
2241         return 0;
2242     if (!v1 || !v2)
2243         return -1;
2244     return strcmp(v1, v2);
2245 }
2246
2247 static size_t record_hash(int pos)
2248 {
2249     if (pos < 0)
2250         pos = 0;
2251     return pos % RECORD_HASH_SIZE;
2252 }
2253
2254 static void record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr, 
2255                              int pos,
2256                              const char *syntax, const char *elementSetName,
2257                              const char *schema,
2258                              Z_SRW_diagnostic *diag)
2259 {
2260     ZOOM_record_cache rc = 0;
2261     
2262     ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
2263     ZOOM_connection_put_event(r->connection, event);
2264
2265     for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
2266     {
2267         if (pos == rc->pos 
2268             && strcmp_null(r->schema, rc->schema) == 0
2269             && strcmp_null(elementSetName,rc->elementSetName) == 0
2270             && strcmp_null(syntax, rc->syntax) == 0)
2271             break;
2272     }
2273     if (!rc)
2274     {
2275         rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc));
2276         rc->rec.odr = 0;
2277         rc->rec.wrbuf = 0;
2278 #if YAZ_HAVE_XML2
2279         rc->rec.xml_mem = 0;
2280 #endif
2281         rc->elementSetName = odr_strdup_null(r->odr, elementSetName);
2282         
2283         rc->syntax = odr_strdup_null(r->odr, syntax);
2284         
2285         rc->schema = odr_strdup_null(r->odr, r->schema);
2286
2287         rc->pos = pos;
2288         rc->next = r->record_hash[record_hash(pos)];
2289         r->record_hash[record_hash(pos)] = rc;
2290     }
2291     rc->rec.npr = npr;
2292     rc->rec.schema = odr_strdup_null(r->odr, schema);
2293     rc->rec.diag_set = 0;
2294     rc->rec.diag_uri = 0;
2295     rc->rec.diag_message = 0;
2296     rc->rec.diag_details = 0;
2297     if (diag)
2298     {
2299         if (diag->uri)
2300         {
2301             char *cp;
2302             rc->rec.diag_set = odr_strdup(r->odr, diag->uri);
2303             if ((cp = strrchr(rc->rec.diag_set, '/')))
2304                 *cp = '\0';
2305             rc->rec.diag_uri = odr_strdup(r->odr, diag->uri);
2306         }
2307         rc->rec.diag_message = odr_strdup_null(r->odr, diag->message);            
2308         rc->rec.diag_details = odr_strdup_null(r->odr, diag->details);
2309     }
2310 }
2311
2312 static ZOOM_record record_cache_lookup(ZOOM_resultset r, int pos,
2313                                        const char *syntax,
2314                                        const char *elementSetName)
2315 {
2316     ZOOM_record_cache rc;
2317     
2318     for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
2319     {
2320         if (pos == rc->pos)
2321         {
2322             if (strcmp_null(r->schema, rc->schema))
2323                 continue;
2324             if (strcmp_null(elementSetName,rc->elementSetName))
2325                 continue;
2326             if (strcmp_null(syntax, rc->syntax))
2327                 continue;
2328             return &rc->rec;
2329         }
2330     }
2331     return 0;
2332 }
2333                                              
2334 static void handle_records(ZOOM_connection c, Z_Records *sr,
2335                            int present_phase)
2336 {
2337     ZOOM_resultset resultset;
2338     int *start, *count;
2339     const char *syntax = 0, *elementSetName = 0;
2340
2341     if (!c->tasks)
2342         return ;
2343     switch (c->tasks->which)
2344     {
2345     case ZOOM_TASK_SEARCH:
2346         resultset = c->tasks->u.search.resultset;
2347         start = &c->tasks->u.search.start;
2348         count = &c->tasks->u.search.count;
2349         syntax = c->tasks->u.search.syntax;
2350         elementSetName = c->tasks->u.search.elementSetName;
2351         break;
2352     case ZOOM_TASK_RETRIEVE:
2353         resultset = c->tasks->u.retrieve.resultset;        
2354         start = &c->tasks->u.retrieve.start;
2355         count = &c->tasks->u.retrieve.count;
2356         syntax = c->tasks->u.retrieve.syntax;
2357         elementSetName = c->tasks->u.retrieve.elementSetName;
2358         break;
2359     default:
2360         return;
2361     }
2362     if (sr && sr->which == Z_Records_NSD)
2363         response_default_diag(c, sr->u.nonSurrogateDiagnostic);
2364     else if (sr && sr->which == Z_Records_multipleNSD)
2365     {
2366         if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
2367             response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
2368         else
2369             set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
2370     }
2371     else 
2372     {
2373         if (*count + *start > resultset->size)
2374             *count = resultset->size - *start;
2375         if (*count < 0)
2376             *count = 0;
2377         if (sr && sr->which == Z_Records_DBOSD)
2378         {
2379             int i;
2380             NMEM nmem = odr_extract_mem(c->odr_in);
2381             Z_NamePlusRecordList *p =
2382                 sr->u.databaseOrSurDiagnostics;
2383             for (i = 0; i<p->num_records; i++)
2384             {
2385                 record_cache_add(resultset, p->records[i], i + *start,
2386                                  syntax, elementSetName,
2387                                  elementSetName, 0);
2388             }
2389             *count -= i;
2390             if (*count < 0)
2391                 *count = 0;
2392             *start += i;
2393             yaz_log(log_details, 
2394                     "handle_records resultset=%p start=%d count=%d",
2395                     resultset, *start, *count);
2396
2397             /* transfer our response to search_nmem .. we need it later */
2398             nmem_transfer(odr_getmem(resultset->odr), nmem);
2399             nmem_destroy(nmem);
2400             if (present_phase && p->num_records == 0)
2401             {
2402                 /* present response and we didn't get any records! */
2403                 Z_NamePlusRecord *myrec = 
2404                     zget_surrogateDiagRec(
2405                         resultset->odr, 0, 
2406                         YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
2407                         "ZOOM C generated. Present phase and no records");
2408                 record_cache_add(resultset, myrec, *start,
2409                                  syntax, elementSetName, 0, 0);
2410             }
2411         }
2412         else if (present_phase)
2413         {
2414             /* present response and we didn't get any records! */
2415             Z_NamePlusRecord *myrec = 
2416                 zget_surrogateDiagRec(
2417                     resultset->odr, 0,
2418                     YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
2419                     "ZOOM C generated: Present response and no records");
2420             record_cache_add(resultset, myrec, *start, syntax, elementSetName,
2421                              0, 0);
2422         }
2423     }
2424 }
2425
2426 static void handle_present_response(ZOOM_connection c, Z_PresentResponse *pr)
2427 {
2428     handle_records(c, pr->records, 1);
2429 }
2430
2431 static void handle_queryExpressionTerm(ZOOM_options opt, const char *name,
2432                                        Z_Term *term)
2433 {
2434     switch (term->which)
2435     {
2436     case Z_Term_general:
2437         ZOOM_options_setl(opt, name,
2438                           (const char *)(term->u.general->buf), 
2439                           term->u.general->len);
2440         break;
2441     case Z_Term_characterString:
2442         ZOOM_options_set(opt, name, term->u.characterString);
2443         break;
2444     case Z_Term_numeric:
2445         ZOOM_options_set_int(opt, name, *term->u.numeric);
2446         break;
2447     }
2448 }
2449
2450 static void handle_queryExpression(ZOOM_options opt, const char *name,
2451                                    Z_QueryExpression *exp)
2452 {
2453     char opt_name[80];
2454     
2455     switch (exp->which)
2456     {
2457     case Z_QueryExpression_term:
2458         if (exp->u.term && exp->u.term->queryTerm)
2459         {
2460             sprintf(opt_name, "%s.term", name);
2461             handle_queryExpressionTerm(opt, opt_name, exp->u.term->queryTerm);
2462         }
2463         break;
2464     case Z_QueryExpression_query:
2465         break;
2466     }
2467 }
2468
2469 static void handle_searchResult(ZOOM_connection c, ZOOM_resultset resultset,
2470                                 Z_OtherInformation *o)
2471 {
2472     int i;
2473     for (i = 0; o && i < o->num_elements; i++)
2474     {
2475         if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
2476         {
2477             Z_External *ext = o->list[i]->information.externallyDefinedInfo;
2478             
2479             if (ext->which == Z_External_searchResult1)
2480             {
2481                 int j;
2482                 Z_SearchInfoReport *sr = ext->u.searchResult1;
2483                 
2484                 if (sr->num)
2485                     ZOOM_options_set_int(
2486                         resultset->options, "searchresult.size", sr->num);
2487
2488                 for (j = 0; j < sr->num; j++)
2489                 {
2490                     Z_SearchInfoReport_s *ent =
2491                         ext->u.searchResult1->elements[j];
2492                     char pref[80];
2493                     
2494                     sprintf(pref, "searchresult.%d", j);
2495
2496                     if (ent->subqueryId)
2497                     {
2498                         char opt_name[80];
2499                         sprintf(opt_name, "%s.id", pref);
2500                         ZOOM_options_set(resultset->options, opt_name,
2501                                          ent->subqueryId);
2502                     }
2503                     if (ent->subqueryExpression)
2504                     {
2505                         char opt_name[80];
2506                         sprintf(opt_name, "%s.subquery", pref);
2507                         handle_queryExpression(resultset->options, opt_name,
2508                                                ent->subqueryExpression);
2509                     }
2510                     if (ent->subqueryInterpretation)
2511                     {
2512                         char opt_name[80];
2513                         sprintf(opt_name, "%s.interpretation", pref);
2514                         handle_queryExpression(resultset->options, opt_name,
2515                                                ent->subqueryInterpretation);
2516                     }
2517                     if (ent->subqueryRecommendation)
2518                     {
2519                         char opt_name[80];
2520                         sprintf(opt_name, "%s.recommendation", pref);
2521                         handle_queryExpression(resultset->options, opt_name,
2522                                                ent->subqueryRecommendation);
2523                     }
2524                     if (ent->subqueryCount)
2525                     {
2526                         char opt_name[80];
2527                         sprintf(opt_name, "%s.count", pref);
2528                         ZOOM_options_set_int(resultset->options, opt_name,
2529                                              *ent->subqueryCount);
2530                     }                                             
2531                 }
2532             }
2533         }
2534     }
2535 }
2536
2537 static void handle_search_response(ZOOM_connection c, Z_SearchResponse *sr)
2538 {
2539     ZOOM_resultset resultset;
2540     ZOOM_Event event;
2541
2542     if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
2543         return ;
2544
2545     event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
2546     ZOOM_connection_put_event(c, event);
2547
2548     resultset = c->tasks->u.search.resultset;
2549
2550     if (sr->resultSetStatus)
2551     {
2552         ZOOM_options_set_int(resultset->options, "resultSetStatus",
2553                              *sr->resultSetStatus);
2554     }
2555     if (sr->presentStatus)
2556     {
2557         ZOOM_options_set_int(resultset->options, "presentStatus",
2558                              *sr->presentStatus);
2559     }
2560     handle_searchResult(c, resultset, sr->additionalSearchInfo);
2561
2562     resultset->size = *sr->resultCount;
2563     handle_records(c, sr->records, 0);
2564 }
2565
2566 static void sort_response(ZOOM_connection c, Z_SortResponse *res)
2567 {
2568     if (res->diagnostics && res->num_diagnostics > 0)
2569         response_diag(c, res->diagnostics[0]);
2570 }
2571
2572 static int scan_response(ZOOM_connection c, Z_ScanResponse *res)
2573 {
2574     NMEM nmem = odr_extract_mem(c->odr_in);
2575     ZOOM_scanset scan;
2576
2577     if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
2578         return 0;
2579     scan = c->tasks->u.scan.scan;
2580
2581     if (res->entries && res->entries->nonsurrogateDiagnostics)
2582         response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
2583     scan->scan_response = res;
2584     scan->srw_scan_response = 0;
2585     nmem_transfer(odr_getmem(scan->odr), nmem);
2586     if (res->stepSize)
2587         ZOOM_options_set_int(scan->options, "stepSize", *res->stepSize);
2588     if (res->positionOfTerm)
2589         ZOOM_options_set_int(scan->options, "position", *res->positionOfTerm);
2590     if (res->scanStatus)
2591         ZOOM_options_set_int(scan->options, "scanStatus", *res->scanStatus);
2592     if (res->numberOfEntriesReturned)
2593         ZOOM_options_set_int(scan->options, "number",
2594                              *res->numberOfEntriesReturned);
2595     nmem_destroy(nmem);
2596     return 1;
2597 }
2598
2599 static zoom_ret send_sort(ZOOM_connection c,
2600                           ZOOM_resultset resultset)
2601 {
2602     if (c->error)
2603         resultset->r_sort_spec = 0;
2604     if (resultset->r_sort_spec)
2605     {
2606         Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
2607         Z_SortRequest *req = apdu->u.sortRequest;
2608         
2609         req->num_inputResultSetNames = 1;
2610         req->inputResultSetNames = (Z_InternationalString **)
2611             odr_malloc(c->odr_out, sizeof(*req->inputResultSetNames));
2612         req->inputResultSetNames[0] =
2613             odr_strdup(c->odr_out, resultset->setname);
2614         req->sortedResultSetName = odr_strdup(c->odr_out, resultset->setname);
2615         req->sortSequence = resultset->r_sort_spec;
2616         resultset->r_sort_spec = 0;
2617         return send_APDU(c, apdu);
2618     }
2619     return zoom_complete;
2620 }
2621
2622 static zoom_ret send_present(ZOOM_connection c)
2623 {
2624     Z_APDU *apdu = 0;
2625     Z_PresentRequest *req = 0;
2626     int i = 0;
2627     const char *syntax = 0;
2628     const char *elementSetName = 0;
2629     ZOOM_resultset  resultset;
2630     int *start, *count;
2631
2632     if (!c->tasks)
2633     {
2634         yaz_log(log_details, "%p send_present no tasks", c);
2635         return zoom_complete;
2636     }
2637     
2638     switch (c->tasks->which)
2639     {
2640     case ZOOM_TASK_SEARCH:
2641         resultset = c->tasks->u.search.resultset;
2642         start = &c->tasks->u.search.start;
2643         count = &c->tasks->u.search.count;
2644         syntax = c->tasks->u.search.syntax;
2645         elementSetName = c->tasks->u.search.elementSetName;
2646         break;
2647     case ZOOM_TASK_RETRIEVE:
2648         resultset = c->tasks->u.retrieve.resultset;
2649         start = &c->tasks->u.retrieve.start;
2650         count = &c->tasks->u.retrieve.count;
2651         syntax = c->tasks->u.retrieve.syntax;
2652         elementSetName = c->tasks->u.retrieve.elementSetName;
2653         break;
2654     default:
2655         return zoom_complete;
2656     }
2657     yaz_log(log_details, "%p send_present start=%d count=%d",
2658             c, *start, *count);
2659
2660     if (*start < 0 || *count < 0 || *start + *count > resultset->size)
2661     {
2662         set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
2663                        "", 0);
2664     }
2665     if (c->error)                  /* don't continue on error */
2666         return zoom_complete;
2667     yaz_log(log_details, "send_present resultset=%p start=%d count=%d",
2668             resultset, *start, *count);
2669
2670     for (i = 0; i < *count; i++)
2671     {
2672         ZOOM_record rec =
2673             record_cache_lookup(resultset, i + *start, syntax, elementSetName);
2674         if (!rec)
2675             break;
2676         else
2677         {
2678             ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
2679             ZOOM_connection_put_event(c, event);
2680         }
2681     }
2682     *start += i;
2683     *count -= i;
2684
2685     if (*count == 0)
2686     {
2687         yaz_log(log_details, "%p send_present skip=%d no more to fetch", c, i);
2688         return zoom_complete;
2689     }
2690
2691     apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
2692     req = apdu->u.presentRequest;
2693
2694     if (i)
2695         yaz_log(log_details, "%p send_present skip=%d", c, i);
2696
2697     *req->resultSetStartPoint = *start + 1;
2698
2699     if (resultset->step > 0 && resultset->step < *count)
2700         *req->numberOfRecordsRequested = resultset->step;
2701     else
2702         *req->numberOfRecordsRequested = *count;
2703     
2704     if (*req->numberOfRecordsRequested + *start > resultset->size)
2705         *req->numberOfRecordsRequested = resultset->size - *start;
2706     assert(*req->numberOfRecordsRequested > 0);
2707
2708     if (syntax && *syntax)
2709         req->preferredRecordSyntax =
2710             zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
2711
2712     if (resultset->schema && *resultset->schema)
2713     {
2714         Z_RecordComposition *compo = (Z_RecordComposition *)
2715             odr_malloc(c->odr_out, sizeof(*compo));
2716
2717         req->recordComposition = compo;
2718         compo->which = Z_RecordComp_complex;
2719         compo->u.complex = (Z_CompSpec *)
2720             odr_malloc(c->odr_out, sizeof(*compo->u.complex));
2721         compo->u.complex->selectAlternativeSyntax = (bool_t *) 
2722             odr_malloc(c->odr_out, sizeof(bool_t));
2723         *compo->u.complex->selectAlternativeSyntax = 0;
2724
2725         compo->u.complex->generic = (Z_Specification *)
2726             odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
2727
2728         compo->u.complex->generic->which = Z_Schema_oid;
2729         compo->u.complex->generic->schema.oid = (Odr_oid *)
2730             zoom_yaz_str_to_z3950oid (c, CLASS_SCHEMA, resultset->schema);
2731
2732         if (!compo->u.complex->generic->schema.oid)
2733         {
2734             /* OID wasn't a schema! Try record syntax instead. */
2735
2736             compo->u.complex->generic->schema.oid = (Odr_oid *)
2737                 zoom_yaz_str_to_z3950oid (c, CLASS_RECSYN, resultset->schema);
2738         }
2739         if (elementSetName && *elementSetName)
2740         {
2741             compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
2742                 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
2743             compo->u.complex->generic->elementSpec->which =
2744                 Z_ElementSpec_elementSetName;
2745             compo->u.complex->generic->elementSpec->u.elementSetName =
2746                 odr_strdup(c->odr_out, elementSetName);
2747         }
2748         else
2749             compo->u.complex->generic->elementSpec = 0;
2750         compo->u.complex->num_dbSpecific = 0;
2751         compo->u.complex->dbSpecific = 0;
2752         compo->u.complex->num_recordSyntax = 0;
2753         compo->u.complex->recordSyntax = 0;
2754     }
2755     else if (elementSetName && *elementSetName)
2756     {
2757         Z_ElementSetNames *esn = (Z_ElementSetNames *)
2758             odr_malloc(c->odr_out, sizeof(*esn));
2759         Z_RecordComposition *compo = (Z_RecordComposition *)
2760             odr_malloc(c->odr_out, sizeof(*compo));
2761         
2762         esn->which = Z_ElementSetNames_generic;
2763         esn->u.generic = odr_strdup(c->odr_out, elementSetName);
2764         compo->which = Z_RecordComp_simple;
2765         compo->u.simple = esn;
2766         req->recordComposition = compo;
2767     }
2768     req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
2769     return send_APDU(c, apdu);
2770 }
2771
2772 ZOOM_API(ZOOM_scanset)
2773     ZOOM_connection_scan(ZOOM_connection c, const char *start)
2774 {
2775     ZOOM_scanset s;
2776     ZOOM_query q = ZOOM_query_create();
2777
2778     ZOOM_query_prefix(q, start);
2779
2780     s = ZOOM_connection_scan1(c, q);
2781     ZOOM_query_destroy(q);
2782     return s;
2783
2784 }
2785
2786 ZOOM_API(ZOOM_scanset)
2787     ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query q)
2788 {
2789     ZOOM_scanset scan = 0;
2790
2791     if (!q->z_query)
2792         return 0;
2793     scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
2794     scan->connection = c;
2795     scan->odr = odr_createmem(ODR_DECODE);
2796     scan->options = ZOOM_options_create_with_parent(c->options);
2797     scan->refcount = 1;
2798     scan->scan_response = 0;
2799     scan->srw_scan_response = 0;
2800
2801     scan->query = q;
2802     (q->refcount)++;
2803     scan->databaseNames = set_DatabaseNames(c, c->options,
2804                                             &scan->num_databaseNames,
2805                                             scan->odr);
2806
2807     if (1)
2808     {
2809         ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
2810         task->u.scan.scan = scan;
2811         
2812         (scan->refcount)++;
2813         if (!c->async)
2814         {
2815             while (ZOOM_event(1, &c))
2816                 ;
2817         }
2818     }
2819     return scan;
2820 }
2821
2822 ZOOM_API(void)
2823     ZOOM_scanset_destroy(ZOOM_scanset scan)
2824 {
2825     if (!scan)
2826         return;
2827     (scan->refcount)--;
2828     if (scan->refcount == 0)
2829     {
2830         ZOOM_query_destroy(scan->query);
2831
2832         odr_destroy(scan->odr);
2833         
2834         ZOOM_options_destroy(scan->options);
2835         xfree(scan);
2836     }
2837 }
2838
2839 static zoom_ret send_package(ZOOM_connection c)
2840 {
2841     ZOOM_Event event;
2842
2843     yaz_log(log_details, "%p send_package", c);
2844     if (!c->tasks)
2845         return zoom_complete;
2846     assert (c->tasks->which == ZOOM_TASK_PACKAGE);
2847     
2848     event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
2849     ZOOM_connection_put_event(c, event);
2850     
2851     c->buf_out = c->tasks->u.package->buf_out;
2852     c->len_out = c->tasks->u.package->len_out;
2853
2854     return do_write(c);
2855 }
2856
2857 static zoom_ret ZOOM_connection_send_scan(ZOOM_connection c)
2858 {
2859     ZOOM_scanset scan;
2860     Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
2861     Z_ScanRequest *req = apdu->u.scanRequest;
2862
2863     yaz_log(log_details, "%p send_scan", c);
2864     if (!c->tasks)
2865         return zoom_complete;
2866     assert (c->tasks->which == ZOOM_TASK_SCAN);
2867     scan = c->tasks->u.scan.scan;
2868
2869     /* Z39.50 scan can only carry RPN */
2870     if (scan->query->z_query->which == Z_Query_type_1 ||
2871         scan->query->z_query->which == Z_Query_type_101)
2872     {
2873         Z_RPNQuery *rpn = scan->query->z_query->u.type_1;
2874         const char *cp = ZOOM_options_get(scan->options, "rpnCharset");
2875         if (cp)
2876         {
2877             yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
2878             if (cd)
2879             {
2880                 rpn = yaz_copy_z_RPNQuery(rpn, c->odr_out);
2881
2882                 yaz_query_charset_convert_rpnquery(
2883                     rpn, c->odr_out, cd);
2884                 yaz_iconv_close(cd);
2885             }
2886         }
2887         req->attributeSet = rpn->attributeSetId;
2888         if (!req->attributeSet)
2889             req->attributeSet = odr_oiddup(c->odr_out, yaz_oid_attset_bib_1);
2890         if (rpn->RPNStructure->which == Z_RPNStructure_simple &&
2891             rpn->RPNStructure->u.simple->which == Z_Operand_APT)
2892         {
2893             req->termListAndStartPoint =
2894                 rpn->RPNStructure->u.simple->u.attributesPlusTerm;
2895         }
2896         else
2897         {
2898             set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
2899             return zoom_complete;
2900         }
2901     }
2902     else
2903     {
2904         set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
2905         return zoom_complete;
2906     }
2907
2908     *req->numberOfTermsRequested =
2909         ZOOM_options_get_int(scan->options, "number", 20);
2910
2911     req->preferredPositionInResponse =
2912         odr_intdup(c->odr_out,
2913                    ZOOM_options_get_int(scan->options, "position", 1));
2914
2915     req->stepSize =
2916         odr_intdup(c->odr_out,
2917                    ZOOM_options_get_int(scan->options, "stepSize", 0));
2918     
2919     req->databaseNames = scan->databaseNames;
2920     req->num_databaseNames = scan->num_databaseNames;
2921
2922     return send_APDU(c, apdu);
2923 }
2924
2925 #if YAZ_HAVE_XML2
2926 static zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
2927 {
2928     ZOOM_scanset scan;
2929     Z_SRW_PDU *sr = 0;
2930     const char *option_val = 0;
2931
2932     if (!c->tasks)
2933         return zoom_complete;
2934     assert (c->tasks->which == ZOOM_TASK_SCAN);
2935     scan = c->tasks->u.scan.scan;
2936         
2937     sr = ZOOM_srw_get_pdu(c, Z_SRW_scan_request);
2938
2939     /* SRU scan can only carry CQL and PQF */
2940     if (scan->query->z_query->which == Z_Query_type_104)
2941     {
2942         sr->u.scan_request->query_type = Z_SRW_query_type_cql;
2943         sr->u.scan_request->scanClause.cql = scan->query->query_string;
2944     }
2945     else if (scan->query->z_query->which == Z_Query_type_1
2946              || scan->query->z_query->which == Z_Query_type_101)
2947     {
2948         sr->u.scan_request->query_type = Z_SRW_query_type_pqf;
2949         sr->u.scan_request->scanClause.pqf = scan->query->query_string;
2950     }
2951     else
2952     {
2953         set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
2954         return zoom_complete;
2955     }
2956
2957     sr->u.scan_request->maximumTerms = odr_intdup(
2958         c->odr_out, ZOOM_options_get_int(scan->options, "number", 10));
2959     
2960     sr->u.scan_request->responsePosition = odr_intdup(
2961         c->odr_out, ZOOM_options_get_int(scan->options, "position", 1));
2962     
2963     option_val = ZOOM_options_get(scan->options, "extraArgs");
2964     yaz_encode_sru_extra(sr, c->odr_out, option_val);
2965     return send_srw(c, sr);
2966 }
2967 #else
2968 static zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
2969 {
2970     return zoom_complete;
2971 }
2972 #endif
2973
2974
2975 ZOOM_API(size_t)
2976     ZOOM_scanset_size(ZOOM_scanset scan)
2977 {
2978     if (!scan)
2979         return 0;
2980
2981     if (scan->scan_response && scan->scan_response->entries)
2982         return scan->scan_response->entries->num_entries;
2983     else if (scan->srw_scan_response)
2984         return scan->srw_scan_response->num_terms;
2985     return 0;
2986 }
2987
2988 static void ZOOM_scanset_term_x(ZOOM_scanset scan, size_t pos,
2989                                 size_t *occ,
2990                                 const char **value_term, size_t *value_len,
2991                                 const char **disp_term, size_t *disp_len)
2992 {
2993     size_t noent = ZOOM_scanset_size(scan);
2994     
2995     *value_term = 0;
2996     *value_len = 0;
2997
2998     *disp_term = 0;
2999     *disp_len = 0;
3000
3001     *occ = 0;
3002     if (pos >= noent)
3003         return;
3004     if (scan->scan_response)
3005     {
3006         Z_ScanResponse *res = scan->scan_response;
3007         if (res->entries->entries[pos]->which == Z_Entry_termInfo)
3008         {
3009             Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
3010             
3011             *value_term = (const char *) t->term->u.general->buf;
3012             *value_len = t->term->u.general->len;
3013             if (t->displayTerm)
3014             {
3015                 *disp_term = t->displayTerm;
3016                 *disp_len = strlen(*disp_term);
3017             }
3018             else if (t->term->which == Z_Term_general)
3019             {
3020                 *disp_term = (const char *) t->term->u.general->buf;
3021                 *disp_len = t->term->u.general->len;
3022             }
3023             *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
3024         }
3025     }
3026     if (scan->srw_scan_response)
3027     {
3028         Z_SRW_scanResponse *res = scan->srw_scan_response;
3029         Z_SRW_scanTerm *t = res->terms + pos;
3030         if (t)
3031         {
3032             *value_term = t->value;
3033             *value_len = strlen(*value_term);
3034
3035             if (t->displayTerm)
3036                 *disp_term = t->displayTerm;
3037             else
3038                 *disp_term = t->value;
3039             *disp_len = strlen(*disp_term);
3040             *occ = t->numberOfRecords ? *t->numberOfRecords : 0;
3041         }
3042     }
3043 }
3044
3045 ZOOM_API(const char *)
3046     ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
3047                       size_t *occ, size_t *len)
3048 {
3049     const char *value_term = 0;
3050     size_t value_len = 0;
3051     const char *disp_term = 0;
3052     size_t disp_len = 0;
3053
3054     ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
3055                         &disp_term, &disp_len);
3056     
3057     *len = value_len;
3058     return value_term;
3059 }
3060
3061 ZOOM_API(const char *)
3062     ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
3063                               size_t *occ, size_t *len)
3064 {
3065     const char *value_term = 0;
3066     size_t value_len = 0;
3067     const char *disp_term = 0;
3068     size_t disp_len = 0;
3069
3070     ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
3071                         &disp_term, &disp_len);
3072     
3073     *len = disp_len;
3074     return disp_term;
3075 }
3076
3077 ZOOM_API(const char *)
3078     ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
3079 {
3080     return ZOOM_options_get(scan->options, key);
3081 }
3082
3083 ZOOM_API(void)
3084     ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
3085                             const char *val)
3086 {
3087     ZOOM_options_set(scan->options, key, val);
3088 }
3089
3090 static Z_APDU *create_es_package(ZOOM_package p, const Odr_oid *oid)
3091 {
3092     const char *str;
3093     Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
3094     Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
3095     
3096     str = ZOOM_options_get(p->options, "package-name");
3097     if (str && *str)
3098         req->packageName = odr_strdup(p->odr_out, str);
3099     
3100     str = ZOOM_options_get(p->options, "user-id");
3101     if (str)
3102         req->userId = odr_strdup_null(p->odr_out, str);
3103     
3104     req->packageType = odr_oiddup(p->odr_out, oid);
3105
3106     str = ZOOM_options_get(p->options, "function");
3107     if (str)
3108     {
3109         if (!strcmp (str, "create"))
3110             *req->function = Z_ExtendedServicesRequest_create;
3111         if (!strcmp (str, "delete"))
3112             *req->function = Z_ExtendedServicesRequest_delete;
3113         if (!strcmp (str, "modify"))
3114             *req->function = Z_ExtendedServicesRequest_modify;
3115     }
3116
3117     str = ZOOM_options_get(p->options, "waitAction");
3118     if (str)
3119     {
3120         if (!strcmp (str, "wait"))
3121             *req->waitAction = Z_ExtendedServicesRequest_wait;
3122         if (!strcmp (str, "waitIfPossible"))
3123             *req->waitAction = Z_ExtendedServicesRequest_waitIfPossible;
3124         if (!strcmp (str, "dontWait"))
3125             *req->waitAction = Z_ExtendedServicesRequest_dontWait;
3126         if (!strcmp (str, "dontReturnPackage"))
3127             *req->waitAction = Z_ExtendedServicesRequest_dontReturnPackage;
3128     }
3129     return apdu;
3130 }
3131
3132 static const char *ill_array_lookup(void *clientData, const char *idx)
3133 {
3134     ZOOM_package p = (ZOOM_package) clientData;
3135     return ZOOM_options_get(p->options, idx+4);
3136 }
3137
3138 static Z_External *encode_ill_request(ZOOM_package p)
3139 {
3140     ODR out = p->odr_out;
3141     ILL_Request *req;
3142     Z_External *r = 0;
3143     struct ill_get_ctl ctl;
3144         
3145     ctl.odr = p->odr_out;
3146     ctl.clientData = p;
3147     ctl.f = ill_array_lookup;
3148         
3149     req = ill_get_ILLRequest(&ctl, "ill", 0);
3150         
3151     if (!ill_Request(out, &req, 0, 0))
3152     {
3153         int ill_request_size;
3154         char *ill_request_buf = odr_getbuf(out, &ill_request_size, 0);
3155         if (ill_request_buf)
3156             odr_setbuf(out, ill_request_buf, ill_request_size, 1);
3157         return 0;
3158     }
3159     else
3160     {
3161         int illRequest_size = 0;
3162         char *illRequest_buf = odr_getbuf(out, &illRequest_size, 0);
3163                 
3164         r = (Z_External *) odr_malloc(out, sizeof(*r));
3165         r->direct_reference = odr_oiddup(out, yaz_oid_general_isoill_1);
3166         r->indirect_reference = 0;
3167         r->descriptor = 0;
3168         r->which = Z_External_single;
3169                 
3170         r->u.single_ASN1_type =
3171             odr_create_Odr_oct(out,
3172                                (unsigned char *)illRequest_buf,
3173                                illRequest_size);
3174     }
3175     return r;
3176 }
3177
3178 static Z_ItemOrder *encode_item_order(ZOOM_package p)
3179 {
3180     Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc(p->odr_out, sizeof(*req));
3181     const char *str;
3182     int len;
3183     
3184     req->which = Z_IOItemOrder_esRequest;
3185     req->u.esRequest = (Z_IORequest *) 
3186         odr_malloc(p->odr_out,sizeof(Z_IORequest));
3187
3188     /* to keep part ... */
3189     req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
3190         odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
3191     req->u.esRequest->toKeep->supplDescription = 0;
3192     req->u.esRequest->toKeep->contact = (Z_IOContact *)
3193         odr_malloc(p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
3194         
3195     str = ZOOM_options_get(p->options, "contact-name");
3196     req->u.esRequest->toKeep->contact->name =
3197         odr_strdup_null(p->odr_out, str);
3198         
3199     str = ZOOM_options_get(p->options, "contact-phone");
3200     req->u.esRequest->toKeep->contact->phone =
3201         odr_strdup_null(p->odr_out, str);
3202         
3203     str = ZOOM_options_get(p->options, "contact-email");
3204     req->u.esRequest->toKeep->contact->email =
3205         odr_strdup_null(p->odr_out, str);
3206         
3207     req->u.esRequest->toKeep->addlBilling = 0;
3208         
3209     /* not to keep part ... */
3210     req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
3211         odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
3212         
3213     str = ZOOM_options_get(p->options, "itemorder-setname");
3214     if (!str)
3215         str = "default";
3216
3217     if (!*str) 
3218         req->u.esRequest->notToKeep->resultSetItem = 0;
3219     else
3220     {
3221         req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
3222             odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
3223
3224         req->u.esRequest->notToKeep->resultSetItem->resultSetId =
3225             odr_strdup(p->odr_out, str);
3226         req->u.esRequest->notToKeep->resultSetItem->item =
3227             odr_intdup(p->odr_out, 0);
3228         
3229         str = ZOOM_options_get(p->options, "itemorder-item");
3230         *req->u.esRequest->notToKeep->resultSetItem->item =
3231             (str ? atoi(str) : 1);
3232     }
3233
3234     str = ZOOM_options_getl(p->options, "doc", &len);
3235     if (str)
3236     {
3237         req->u.esRequest->notToKeep->itemRequest =
3238             z_ext_record_xml(p->odr_out, str, len);
3239     }
3240     else
3241         req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
3242     
3243     return req;
3244 }
3245
3246 Z_APDU *create_admin_package(ZOOM_package p, int type, 
3247                              Z_ESAdminOriginPartToKeep **toKeepP,
3248                              Z_ESAdminOriginPartNotToKeep **notToKeepP)
3249 {
3250     Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_admin);
3251     if (apdu)
3252     {
3253         Z_ESAdminOriginPartToKeep  *toKeep;
3254         Z_ESAdminOriginPartNotToKeep  *notToKeep;
3255         Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
3256         const char *first_db = "Default";
3257         int num_db;
3258         char **db = set_DatabaseNames(p->connection, p->options, &num_db,
3259                                       p->odr_out);
3260         if (num_db > 0)
3261             first_db = db[0];
3262             
3263         r->direct_reference = odr_oiddup(p->odr_out, yaz_oid_extserv_admin);
3264         r->descriptor = 0;
3265         r->indirect_reference = 0;
3266         r->which = Z_External_ESAdmin;
3267         
3268         r->u.adminService = (Z_Admin *)
3269             odr_malloc(p->odr_out, sizeof(*r->u.adminService));
3270         r->u.adminService->which = Z_Admin_esRequest;
3271         r->u.adminService->u.esRequest = (Z_AdminEsRequest *)
3272             odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest));
3273         
3274         toKeep = r->u.adminService->u.esRequest->toKeep =
3275             (Z_ESAdminOriginPartToKeep *) 
3276             odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest->toKeep));
3277         toKeep->which = type;
3278         toKeep->databaseName = odr_strdup(p->odr_out, first_db);
3279         toKeep->u.create = odr_nullval();
3280         apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3281         
3282         r->u.adminService->u.esRequest->notToKeep = notToKeep =
3283             (Z_ESAdminOriginPartNotToKeep *)
3284             odr_malloc(p->odr_out,
3285                        sizeof(*r->u.adminService->u.esRequest->notToKeep));
3286         notToKeep->which = Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
3287         notToKeep->u.recordsWillFollow = odr_nullval();
3288         if (toKeepP)
3289             *toKeepP = toKeep;
3290         if (notToKeepP)
3291             *notToKeepP = notToKeep;
3292     }
3293     return apdu;
3294 }
3295
3296 static Z_APDU *create_xmlupdate_package(ZOOM_package p)
3297 {
3298     Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_xml_es);
3299     Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
3300     Z_External *ext = (Z_External *) odr_malloc(p->odr_out, sizeof(*ext));
3301     int len;
3302     const char *doc = ZOOM_options_getl(p->options, "doc", &len);
3303
3304     if (!doc)
3305     {
3306         doc = "";
3307         len = 0;
3308     }
3309
3310     req->taskSpecificParameters = ext;
3311     ext->direct_reference = req->packageType;
3312     ext->descriptor = 0;
3313     ext->indirect_reference = 0;
3314     
3315     ext->which = Z_External_octet;
3316     ext->u.single_ASN1_type =
3317         odr_create_Odr_oct(p->odr_out, (const unsigned char *) doc, len);
3318     return apdu;
3319 }
3320
3321 static Z_APDU *create_update_package(ZOOM_package p)
3322 {
3323     Z_APDU *apdu = 0;
3324     const char *first_db = "Default";
3325     int num_db;
3326     char **db = set_DatabaseNames(p->connection, p->options, &num_db, p->odr_out);
3327     const char *action = ZOOM_options_get(p->options, "action");
3328     int recordIdOpaque_len;
3329     const char *recordIdOpaque = ZOOM_options_getl(p->options, "recordIdOpaque",
3330         &recordIdOpaque_len);
3331     const char *recordIdNumber = ZOOM_options_get(p->options, "recordIdNumber");
3332     int record_len;
3333     const char *record_buf = ZOOM_options_getl(p->options, "record",
3334         &record_len);
3335     int recordOpaque_len;
3336     const char *recordOpaque_buf = ZOOM_options_getl(p->options, "recordOpaque",
3337         &recordOpaque_len);
3338     const char *syntax_str = ZOOM_options_get(p->options, "syntax");
3339     const char *version = ZOOM_options_get(p->options, "updateVersion");
3340
3341     const char *correlationInfo_note =
3342         ZOOM_options_get(p->options, "correlationInfo.note");
3343     const char *correlationInfo_id =
3344         ZOOM_options_get(p->options, "correlationInfo.id");
3345     int action_no = -1;
3346     Odr_oid *syntax_oid = 0;
3347     const Odr_oid *package_oid = yaz_oid_extserv_database_update;
3348
3349     if (!version)
3350         version = "3";
3351     if (!syntax_str)
3352         syntax_str = "xml";
3353     if (!record_buf && !recordOpaque_buf)
3354     {
3355         record_buf = "void";
3356         record_len = 4;
3357         syntax_str = "SUTRS";
3358     }
3359
3360     if (syntax_str)
3361     {
3362         syntax_oid = yaz_string_to_oid_odr(yaz_oid_std(),
3363                                            CLASS_RECSYN, syntax_str,
3364                                            p->odr_out);
3365     }
3366     if (!syntax_oid)
3367         return 0;
3368
3369     if (num_db > 0)
3370         first_db = db[0];
3371     
3372     switch(*version)
3373     {
3374     case '1':
3375         package_oid = yaz_oid_extserv_database_update_first_version;
3376         /* old update does not support specialUpdate */
3377         if (!action)
3378             action = "recordInsert";
3379         break;
3380     case '2':
3381         if (!action)
3382             action = "specialUpdate";
3383         package_oid = yaz_oid_extserv_database_update_second_version;
3384         break;
3385     case '3':
3386         if (!action)
3387             action = "specialUpdate";
3388         package_oid = yaz_oid_extserv_database_update;
3389         break;
3390     default:
3391         return 0;
3392     }
3393     
3394     if (!strcmp(action, "recordInsert"))
3395         action_no = Z_IUOriginPartToKeep_recordInsert;
3396     else if (!strcmp(action, "recordReplace"))
3397         action_no = Z_IUOriginPartToKeep_recordReplace;
3398     else if (!strcmp(action, "recordDelete"))
3399         action_no = Z_IUOriginPartToKeep_recordDelete;
3400     else if (!strcmp(action, "elementUpdate"))
3401         action_no = Z_IUOriginPartToKeep_elementUpdate;
3402     else if (!strcmp(action, "specialUpdate"))
3403         action_no = Z_IUOriginPartToKeep_specialUpdate;
3404     else
3405         return 0;
3406
3407     apdu = create_es_package(p, package_oid);
3408     if (apdu)
3409     {
3410         Z_IUOriginPartToKeep *toKeep;
3411         Z_IUSuppliedRecords *notToKeep;
3412         Z_External *r = (Z_External *)
3413             odr_malloc(p->odr_out, sizeof(*r));
3414         const char *elementSetName =
3415             ZOOM_options_get(p->options, "elementSetName");
3416         
3417         apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3418         
3419         r->direct_reference = odr_oiddup(p->odr_out, package_oid);
3420         r->descriptor = 0;
3421         r->which = Z_External_update;
3422         r->indirect_reference = 0;
3423         r->u.update = (Z_IUUpdate *)
3424             odr_malloc(p->odr_out, sizeof(*r->u.update));
3425         
3426         r->u.update->which = Z_IUUpdate_esRequest;
3427         r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
3428             odr_malloc(p->odr_out, sizeof(*r->u.update->u.esRequest));
3429         toKeep = r->u.update->u.esRequest->toKeep = 
3430             (Z_IUOriginPartToKeep *)
3431             odr_malloc(p->odr_out, sizeof(*toKeep));
3432         
3433         toKeep->databaseName = odr_strdup(p->odr_out, first_db);
3434         toKeep->schema = 0;
3435         
3436         toKeep->elementSetName = odr_strdup_null(p->odr_out, elementSetName);
3437             
3438         toKeep->actionQualifier = 0;
3439         toKeep->action = odr_intdup(p->odr_out, action_no);
3440         
3441         notToKeep = r->u.update->u.esRequest->notToKeep = 
3442             (Z_IUSuppliedRecords *)
3443             odr_malloc(p->odr_out, sizeof(*notToKeep));
3444         notToKeep->num = 1;
3445         notToKeep->elements = (Z_IUSuppliedRecords_elem **)
3446             odr_malloc(p->odr_out, sizeof(*notToKeep->elements));
3447         notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
3448             odr_malloc(p->odr_out, sizeof(**notToKeep->elements));
3449         notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
3450         if (recordIdOpaque)
3451         {
3452             notToKeep->elements[0]->u.opaque = 
3453                 odr_create_Odr_oct(p->odr_out,
3454                                    (const unsigned char *) recordIdOpaque,
3455                                    recordIdOpaque_len);
3456         }
3457         else if (recordIdNumber)
3458         {
3459             notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_number;
3460             
3461             notToKeep->elements[0]->u.number =
3462                 odr_intdup(p->odr_out, atoi(recordIdNumber));
3463         }
3464         else
3465             notToKeep->elements[0]->u.opaque = 0;
3466         notToKeep->elements[0]->supplementalId = 0;
3467         if (correlationInfo_note || correlationInfo_id)
3468         {
3469             Z_IUCorrelationInfo *ci;
3470             ci = notToKeep->elements[0]->correlationInfo =
3471                 (Z_IUCorrelationInfo *) odr_malloc(p->odr_out, sizeof(*ci));
3472             ci->note = odr_strdup_null(p->odr_out, correlationInfo_note);
3473             ci->id = correlationInfo_id ?
3474                 odr_intdup(p->odr_out, atoi(correlationInfo_id)) : 0;
3475         }
3476         else
3477             notToKeep->elements[0]->correlationInfo = 0;
3478         if (recordOpaque_buf)
3479         {
3480             notToKeep->elements[0]->record =
3481                 z_ext_record_oid_any(p->odr_out, syntax_oid,
3482                                  recordOpaque_buf, recordOpaque_len);
3483         }
3484         else
3485         {
3486             notToKeep->elements[0]->record =
3487                 z_ext_record_oid(p->odr_out, syntax_oid,
3488                                  record_buf, record_len);
3489         }
3490     }
3491     if (0 && apdu)
3492     {
3493         ODR print = odr_createmem(ODR_PRINT);
3494
3495         z_APDU(print, &apdu, 0, 0);
3496         odr_destroy(print);
3497     }
3498     return apdu;
3499 }
3500
3501 ZOOM_API(void)
3502     ZOOM_package_send(ZOOM_package p, const char *type)
3503 {
3504     Z_APDU *apdu = 0;
3505     ZOOM_connection c;
3506     if (!p)
3507         return;
3508     c = p->connection;
3509     odr_reset(p->odr_out);
3510     xfree(p->buf_out);
3511     p->buf_out = 0;
3512     if (!strcmp(type, "itemorder"))
3513     {
3514         apdu = create_es_package(p, yaz_oid_extserv_item_order);
3515         if (apdu)
3516         {
3517             Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
3518             
3519             r->direct_reference = 
3520                 odr_oiddup(p->odr_out, yaz_oid_extserv_item_order);
3521             r->descriptor = 0;
3522             r->which = Z_External_itemOrder;
3523             r->indirect_reference = 0;
3524             r->u.itemOrder = encode_item_order(p);
3525
3526             apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3527         }
3528     }
3529     else if (!strcmp(type, "create"))  /* create database */
3530     {
3531         apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_create,
3532                                     0, 0);
3533     }   
3534     else if (!strcmp(type, "drop"))  /* drop database */
3535     {
3536         apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_drop,
3537                                     0, 0);
3538     }
3539     else if (!strcmp(type, "commit"))  /* commit changes */
3540     {
3541         apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_commit,
3542                                     0, 0);
3543     }
3544     else if (!strcmp(type, "update")) /* update record(s) */
3545     {
3546         apdu = create_update_package(p);
3547     }
3548     else if (!strcmp(type, "xmlupdate"))
3549     {
3550         apdu = create_xmlupdate_package(p);
3551     }
3552     if (apdu)
3553     {
3554         if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
3555         {
3556             char *buf;
3557
3558             ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_PACKAGE);
3559             task->u.package = p;
3560             buf = odr_getbuf(p->odr_out, &p->len_out, 0);
3561             p->buf_out = (char *) xmalloc(p->len_out);
3562             memcpy(p->buf_out, buf, p->len_out);
3563             
3564             (p->refcount)++;
3565             if (!c->async)
3566             {
3567                 while (ZOOM_event(1, &c))
3568                     ;
3569             }
3570         }
3571     }
3572 }
3573
3574 ZOOM_API(ZOOM_package)
3575     ZOOM_connection_package(ZOOM_connection c, ZOOM_options options)
3576 {
3577     ZOOM_package p = (ZOOM_package) xmalloc(sizeof(*p));
3578
3579     p->connection = c;
3580     p->odr_out = odr_createmem(ODR_ENCODE);
3581     p->options = ZOOM_options_create_with_parent2(options, c->options);
3582     p->refcount = 1;
3583     p->buf_out = 0;
3584     p->len_out = 0;
3585     return p;
3586 }
3587
3588 ZOOM_API(void)
3589     ZOOM_package_destroy(ZOOM_package p)
3590 {
3591     if (!p)
3592         return;
3593     (p->refcount)--;
3594     if (p->refcount == 0)
3595     {
3596         odr_destroy(p->odr_out);
3597         xfree(p->buf_out);
3598         
3599         ZOOM_options_destroy(p->options);
3600         xfree(p);
3601     }
3602 }
3603
3604 ZOOM_API(const char *)
3605     ZOOM_package_option_get(ZOOM_package p, const char *key)
3606 {
3607     return ZOOM_options_get(p->options, key);
3608 }
3609
3610 ZOOM_API(const char *)
3611     ZOOM_package_option_getl(ZOOM_package p, const char *key, int *lenp)
3612 {
3613     return ZOOM_options_getl(p->options, key, lenp);
3614 }
3615
3616 ZOOM_API(void)
3617     ZOOM_package_option_set(ZOOM_package p, const char *key,
3618                             const char *val)
3619 {
3620     ZOOM_options_set(p->options, key, val);
3621 }
3622
3623 ZOOM_API(void)
3624     ZOOM_package_option_setl(ZOOM_package p, const char *key,
3625                              const char *val, int len)
3626 {
3627     ZOOM_options_setl(p->options, key, val, len);
3628 }
3629
3630 ZOOM_API(int)
3631     ZOOM_connection_exec_task(ZOOM_connection c)
3632 {
3633     ZOOM_task task = c->tasks;
3634     zoom_ret ret = zoom_complete;
3635
3636     if (!task)
3637         return 0;
3638     yaz_log(log_details, "%p ZOOM_connection_exec_task type=%d run=%d",
3639             c, task->which, task->running);
3640     if (c->error != ZOOM_ERROR_NONE)
3641     {
3642         yaz_log(log_details, "%p ZOOM_connection_exec_task "
3643                 "removing tasks because of error = %d", c, c->error);
3644         ZOOM_connection_remove_tasks(c);
3645         return 0;
3646     }
3647     if (task->running)
3648     {
3649         yaz_log(log_details, "%p ZOOM_connection_exec_task "
3650                 "task already running", c);
3651         return 0;
3652     }
3653     task->running = 1;
3654     ret = zoom_complete;
3655     if (c->cs || task->which == ZOOM_TASK_CONNECT)
3656     {
3657         switch (task->which)
3658         {
3659         case ZOOM_TASK_SEARCH:
3660             if (c->proto == PROTO_HTTP)
3661                 ret = ZOOM_connection_srw_send_search(c);
3662             else
3663                 ret = ZOOM_connection_send_search(c);
3664             break;
3665         case ZOOM_TASK_RETRIEVE:
3666             if (c->proto == PROTO_HTTP)
3667                 ret = ZOOM_connection_srw_send_search(c);
3668             else
3669                 ret = send_present(c);
3670             break;
3671         case ZOOM_TASK_CONNECT:
3672             ret = do_connect(c);
3673             break;
3674         case ZOOM_TASK_SCAN:
3675             if (c->proto == PROTO_HTTP)
3676                 ret = ZOOM_connection_srw_send_scan(c);
3677             else
3678                 ret = ZOOM_connection_send_scan(c);
3679             break;
3680         case ZOOM_TASK_PACKAGE:
3681             ret = send_package(c);
3682             break;
3683         case ZOOM_TASK_SORT:
3684             c->tasks->u.sort.resultset->r_sort_spec = 
3685                 c->tasks->u.sort.q->sort_spec;
3686             ret = send_sort(c, c->tasks->u.sort.resultset);
3687             break;
3688         }
3689     }
3690     else
3691     {
3692         yaz_log(log_details, "%p ZOOM_connection_exec_task "
3693                 "remove tasks because no connection exist", c);
3694         ZOOM_connection_remove_tasks(c);
3695     }
3696     if (ret == zoom_complete)
3697     {
3698         yaz_log(log_details, "%p ZOOM_connection_exec_task "
3699                 "task removed (complete)", c);
3700         ZOOM_connection_remove_task(c);
3701         return 0;
3702     }
3703     yaz_log(log_details, "%p ZOOM_connection_exec_task "
3704             "task pending", c);
3705     return 1;
3706 }
3707
3708 static zoom_ret send_sort_present(ZOOM_connection c)
3709 {
3710     zoom_ret r = zoom_complete;
3711
3712     if (c->tasks && c->tasks->which == ZOOM_TASK_SEARCH)
3713         r = send_sort(c, c->tasks->u.search.resultset);
3714     if (r == zoom_complete)
3715         r = send_present(c);
3716     return r;
3717 }
3718
3719 static int es_response_taskpackage_update(ZOOM_connection c,
3720                 Z_IUUpdateTaskPackage *utp)
3721 {
3722         if (utp && utp->targetPart)
3723         {
3724                 Z_IUTargetPart *targetPart = utp->targetPart;
3725                 switch ( *targetPart->updateStatus ) {
3726                         case Z_IUTargetPart_success:
3727                                 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "success");
3728                                 break;
3729                         case Z_IUTargetPart_partial:
3730                                 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "partial");
3731                                 break;
3732                         case Z_IUTargetPart_failure:
3733                                 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "failure");
3734                                 if (targetPart->globalDiagnostics && targetPart->num_globalDiagnostics > 0)
3735                                         response_diag(c, targetPart->globalDiagnostics[0]);
3736                                 break;
3737                 }
3738                 // NOTE: Individual record status, surrogate diagnostics, and supplemental diagnostics ARE NOT REPORTED.
3739         }
3740     return 1;
3741 }
3742
3743 static int es_response_taskpackage(ZOOM_connection c,
3744                                    Z_TaskPackage *taskPackage)
3745 {
3746         // targetReference
3747         Odr_oct *id = taskPackage->targetReference;
3748         if (id)
3749                 ZOOM_options_setl(c->tasks->u.package->options,
3750                                                         "targetReference", (char*) id->buf, id->len);
3751         
3752         // taskStatus
3753         switch ( *taskPackage->taskStatus ) {
3754                 case Z_TaskPackage_pending:
3755                         ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "pending");
3756                         break;
3757                 case Z_TaskPackage_active:
3758                         ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "active");
3759                         break;
3760                 case Z_TaskPackage_complete:
3761                         ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "complete");
3762                         break;
3763                 case Z_TaskPackage_aborted:
3764                         ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "aborted");
3765                         if ( taskPackage->num_packageDiagnostics && taskPackage->packageDiagnostics )
3766                                 response_diag(c, taskPackage->packageDiagnostics[0]);
3767                         break;
3768         }
3769         
3770         // taskSpecificParameters
3771         // NOTE: Only Update implemented, no others.
3772         if ( taskPackage->taskSpecificParameters->which == Z_External_update ) {
3773                         Z_IUUpdateTaskPackage *utp = taskPackage->taskSpecificParameters->u.update->u.taskPackage;
3774                         es_response_taskpackage_update(c, utp);
3775         }
3776         return 1;
3777 }
3778
3779
3780 static int es_response(ZOOM_connection c,
3781                        Z_ExtendedServicesResponse *res)
3782 {
3783     if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
3784         return 0;
3785     switch (*res->operationStatus) {
3786         case Z_ExtendedServicesResponse_done:
3787             ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "done");
3788             break;
3789         case Z_ExtendedServicesResponse_accepted:
3790             ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "accepted");
3791             break;
3792         case Z_ExtendedServicesResponse_failure:
3793             ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "failure");
3794             if (res->diagnostics && res->num_diagnostics > 0)
3795                 response_diag(c, res->diagnostics[0]);
3796             break;
3797     }
3798     if (res->taskPackage &&
3799         res->taskPackage->which == Z_External_extendedService)
3800     {
3801         Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
3802         es_response_taskpackage(c, taskPackage);
3803     }
3804     if (res->taskPackage && 
3805         res->taskPackage->which == Z_External_octet)
3806     {
3807         Odr_oct *doc = res->taskPackage->u.octet_aligned;
3808         ZOOM_options_setl(c->tasks->u.package->options,
3809                           "xmlUpdateDoc", (char*) doc->buf, doc->len);
3810     }
3811     return 1;
3812 }
3813
3814 static void interpret_init_diag(ZOOM_connection c,
3815                                 Z_DiagnosticFormat *diag)
3816 {
3817     if (diag->num > 0)
3818     {
3819         Z_DiagnosticFormat_s *ds = diag->elements[0];
3820         if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
3821             response_default_diag(c, ds->u.defaultDiagRec);
3822     }
3823 }
3824
3825
3826 static void interpret_otherinformation_field(ZOOM_connection c,
3827                                              Z_OtherInformation *ui)
3828 {
3829     int i;
3830     for (i = 0; i < ui->num_elements; i++)
3831     {
3832         Z_OtherInformationUnit *unit = ui->list[i];
3833         if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
3834             unit->information.externallyDefinedInfo &&
3835             unit->information.externallyDefinedInfo->which ==
3836             Z_External_diag1) 
3837         {
3838             interpret_init_diag(c, unit->information.externallyDefinedInfo->u.diag1);
3839         } 
3840     }
3841 }
3842
3843
3844 static void set_init_option(const char *name, void *clientData) {
3845     ZOOM_connection c = (ZOOM_connection) clientData;
3846     char buf[80];
3847
3848     sprintf(buf, "init_opt_%.70s", name);
3849     ZOOM_connection_option_set(c, buf, "1");
3850 }
3851
3852
3853 static void recv_apdu(ZOOM_connection c, Z_APDU *apdu)
3854 {
3855     Z_InitResponse *initrs;
3856     
3857     ZOOM_connection_set_mask(c, 0);
3858     yaz_log(log_details, "%p recv_apdu apdu->which=%d", c, apdu->which);
3859     switch(apdu->which)
3860     {
3861     case Z_APDU_initResponse:
3862         yaz_log(log_api, "%p recv_apdu: Received Init response", c);
3863         initrs = apdu->u.initResponse;
3864         ZOOM_connection_option_set(c, "serverImplementationId",
3865                                    initrs->implementationId ?
3866                                    initrs->implementationId : "");
3867         ZOOM_connection_option_set(c, "serverImplementationName",
3868                                    initrs->implementationName ?
3869                                    initrs->implementationName : "");
3870         ZOOM_connection_option_set(c, "serverImplementationVersion",
3871                                    initrs->implementationVersion ?
3872                                    initrs->implementationVersion : "");
3873         /* Set the three old options too, for old applications */
3874         ZOOM_connection_option_set(c, "targetImplementationId",
3875                                    initrs->implementationId ?
3876                                    initrs->implementationId : "");
3877         ZOOM_connection_option_set(c, "targetImplementationName",
3878                                    initrs->implementationName ?
3879                                    initrs->implementationName : "");
3880         ZOOM_connection_option_set(c, "targetImplementationVersion",
3881                                    initrs->implementationVersion ?
3882                                    initrs->implementationVersion : "");
3883
3884         /* Make initrs->options available as ZOOM-level options */
3885         yaz_init_opt_decode(initrs->options, set_init_option, (void*) c);
3886
3887         if (!*initrs->result)
3888         {
3889             Z_External *uif = initrs->userInformationField;
3890
3891             set_ZOOM_error(c, ZOOM_ERROR_INIT, 0); /* default error */
3892
3893             if (uif && uif->which == Z_External_userInfo1)
3894                 interpret_otherinformation_field(c, uif->u.userInfo1);
3895         }
3896         else
3897         {
3898             char *cookie =
3899                 yaz_oi_get_string_oid(&apdu->u.initResponse->otherInfo,
3900                                       yaz_oid_userinfo_cookie, 1, 0);
3901             xfree(c->cookie_in);
3902             c->cookie_in = 0;
3903             if (cookie)
3904                 c->cookie_in = xstrdup(cookie);
3905             if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
3906                 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
3907                 c->support_named_resultsets = 1;
3908             if (c->tasks)
3909             {
3910                 assert(c->tasks->which == ZOOM_TASK_CONNECT);
3911                 ZOOM_connection_remove_task(c);
3912             }
3913             ZOOM_connection_exec_task(c);
3914         }
3915         if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
3916         {
3917             NMEM tmpmem = nmem_create();
3918             Z_CharSetandLanguageNegotiation *p =
3919                 yaz_get_charneg_record(initrs->otherInfo);
3920             
3921             if (p)
3922             {
3923                 char *charset = NULL, *lang = NULL;
3924                 int sel;
3925                 
3926                 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
3927                 yaz_log(log_details, "%p recv_apdu target accepted: "
3928                         "charset %s, language %s, select %d",
3929                         c,
3930                         charset ? charset : "none", lang ? lang : "none", sel);
3931                 if (charset)
3932                     ZOOM_connection_option_set(c, "negotiation-charset",
3933                                                charset);
3934                 if (lang)
3935                     ZOOM_connection_option_set(c, "negotiation-lang",
3936                                                lang);
3937
3938                 ZOOM_connection_option_set(
3939                     c,  "negotiation-charset-in-effect-for-records",
3940                     (sel != 0) ? "1" : "0");
3941                 nmem_destroy(tmpmem);
3942             }
3943         }       
3944         break;
3945     case Z_APDU_searchResponse:
3946         yaz_log(log_api, "%p recv_apdu Search response", c);
3947         handle_search_response(c, apdu->u.searchResponse);
3948         if (send_sort_present(c) == zoom_complete)
3949             ZOOM_connection_remove_task(c);
3950         break;
3951     case Z_APDU_presentResponse:
3952         yaz_log(log_api, "%p recv_apdu Present response", c);
3953         handle_present_response(c, apdu->u.presentResponse);
3954         if (send_present(c) == zoom_complete)
3955             ZOOM_connection_remove_task(c);
3956         break;
3957     case Z_APDU_sortResponse:
3958         yaz_log(log_api, "%p recv_apdu Sort response", c);
3959         sort_response(c, apdu->u.sortResponse);
3960         if (send_present(c) == zoom_complete)
3961             ZOOM_connection_remove_task(c);
3962         break;
3963     case Z_APDU_scanResponse:
3964         yaz_log(log_api, "%p recv_apdu Scan response", c);
3965         scan_response(c, apdu->u.scanResponse);
3966         ZOOM_connection_remove_task(c);
3967         break;
3968     case Z_APDU_extendedServicesResponse:
3969         yaz_log(log_api, "%p recv_apdu Extended Services response", c);
3970         es_response(c, apdu->u.extendedServicesResponse);
3971         ZOOM_connection_remove_task(c);
3972         break;
3973     case Z_APDU_close:
3974         yaz_log(log_api, "%p recv_apdu Close PDU", c);
3975         if (!ZOOM_test_reconnect(c))
3976         {
3977             set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
3978             do_close(c);
3979         }
3980         break;
3981     default:
3982         yaz_log(log_api, "%p Received unknown PDU", c);
3983         set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
3984         do_close(c);
3985     }
3986 }
3987
3988 #if YAZ_HAVE_XML2
3989 static zoom_ret handle_srw_response(ZOOM_connection c,
3990                                     Z_SRW_searchRetrieveResponse *res)
3991 {
3992     ZOOM_resultset resultset = 0;
3993     int i;
3994     NMEM nmem;
3995     ZOOM_Event event;
3996     int *start, *count;
3997     const char *syntax, *elementSetName;
3998
3999     if (!c->tasks)
4000         return zoom_complete;
4001
4002     switch(c->tasks->which)
4003     {
4004     case ZOOM_TASK_SEARCH:
4005         resultset = c->tasks->u.search.resultset;
4006         start = &c->tasks->u.search.start;
4007         count = &c->tasks->u.search.count;
4008         syntax = c->tasks->u.search.syntax;
4009         elementSetName = c->tasks->u.search.elementSetName;        
4010
4011         if (!c->tasks->u.search.recv_search_fired)
4012         {
4013             event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
4014             ZOOM_connection_put_event(c, event);
4015             c->tasks->u.search.recv_search_fired = 1;
4016         }
4017         break;
4018     case ZOOM_TASK_RETRIEVE:
4019         resultset = c->tasks->u.retrieve.resultset;
4020         start = &c->tasks->u.retrieve.start;
4021         count = &c->tasks->u.retrieve.count;
4022         syntax = c->tasks->u.retrieve.syntax;
4023         elementSetName = c->tasks->u.retrieve.elementSetName;
4024         break;
4025     default:
4026         return zoom_complete;
4027     }
4028
4029     resultset->size = 0;
4030
4031     if (res->resultSetId)
4032         ZOOM_resultset_option_set(resultset, "resultSetId", res->resultSetId);
4033
4034     yaz_log(log_details, "%p handle_srw_response got SRW response OK", c);
4035
4036     if (res->num_diagnostics > 0)
4037     {
4038         set_SRU_error(c, &res->diagnostics[0]);
4039     }
4040     else
4041     {
4042         if (res->numberOfRecords)
4043             resultset->size = *res->numberOfRecords;
4044         for (i = 0; i<res->num_records; i++)
4045         {
4046             int pos;
4047             Z_SRW_record *sru_rec;
4048             Z_SRW_diagnostic *diag = 0;
4049             int num_diag;
4050             
4051             Z_NamePlusRecord *npr = (Z_NamePlusRecord *)
4052                 odr_malloc(c->odr_in, sizeof(Z_NamePlusRecord));
4053             
4054             if (res->records[i].recordPosition && 
4055                 *res->records[i].recordPosition > 0)
4056                 pos = *res->records[i].recordPosition - 1;
4057             else
4058                 pos = *start + i;
4059             
4060             sru_rec = &res->records[i];
4061             
4062             npr->databaseName = 0;
4063             npr->which = Z_NamePlusRecord_databaseRecord;
4064             npr->u.databaseRecord = (Z_External *)
4065                 odr_malloc(c->odr_in, sizeof(Z_External));
4066             npr->u.databaseRecord->descriptor = 0;
4067             npr->u.databaseRecord->direct_reference =
4068                 odr_oiddup(c->odr_in, yaz_oid_recsyn_xml);
4069             npr->u.databaseRecord->which = Z_External_octet;
4070             
4071             npr->u.databaseRecord->u.octet_aligned = (Odr_oct *)
4072                 odr_malloc(c->odr_in, sizeof(Odr_oct));
4073             npr->u.databaseRecord->u.octet_aligned->buf = (unsigned char*)
4074                 sru_rec->recordData_buf;
4075             npr->u.databaseRecord->u.octet_aligned->len = 
4076                 npr->u.databaseRecord->u.octet_aligned->size = 
4077                 sru_rec->recordData_len;
4078             
4079             if (sru_rec->recordSchema 
4080                 && !strcmp(sru_rec->recordSchema,
4081                            "info:srw/schema/1/diagnostics-v1.1"))
4082             {
4083                 sru_decode_surrogate_diagnostics(sru_rec->recordData_buf,
4084                                                  sru_rec->recordData_len,
4085                                                  &diag, &num_diag,
4086                                                  resultset->odr);
4087             }
4088             record_cache_add(resultset, npr, pos, syntax, elementSetName,
4089                              sru_rec->recordSchema, diag);
4090         }
4091         *count -= i;
4092         *start += i;
4093         if (*count + *start > resultset->size)
4094             *count = resultset->size - *start;
4095         if (*count < 0)
4096             *count = 0;
4097         
4098         nmem = odr_extract_mem(c->odr_in);
4099         nmem_transfer(odr_getmem(resultset->odr), nmem);
4100         nmem_destroy(nmem);
4101
4102         if (*count > 0)
4103             return ZOOM_connection_srw_send_search(c);
4104     }
4105     return zoom_complete;
4106 }
4107 #endif
4108
4109 #if YAZ_HAVE_XML2
4110 static void handle_srw_scan_response(ZOOM_connection c,
4111                                      Z_SRW_scanResponse *res)
4112 {
4113     NMEM nmem = odr_extract_mem(c->odr_in);
4114     ZOOM_scanset scan;
4115
4116     if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
4117         return;
4118     scan = c->tasks->u.scan.scan;
4119
4120     if (res->num_diagnostics > 0)
4121         set_SRU_error(c, &res->diagnostics[0]);
4122
4123     scan->scan_response = 0;
4124     scan->srw_scan_response = res;
4125     nmem_transfer(odr_getmem(scan->odr), nmem);
4126
4127     ZOOM_options_set_int(scan->options, "number", res->num_terms);
4128     nmem_destroy(nmem);
4129 }
4130 #endif
4131
4132 #if YAZ_HAVE_XML2
4133 static Z_GDU *get_HTTP_Request_url(ODR odr, const char *url)
4134 {
4135     Z_GDU *p = z_get_HTTP_Request(odr);
4136     const char *host = url;
4137     const char *cp0 = strstr(host, "://");
4138     const char *cp1 = 0;
4139     if (cp0)
4140         cp0 = cp0+3;
4141     else
4142         cp0 = host;
4143     
4144     cp1 = strchr(cp0, '/');
4145     if (!cp1)
4146         cp1 = cp0 + strlen(cp0);
4147     
4148     if (cp0 && cp1)
4149     {
4150         char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1);
4151         memcpy (h, cp0, cp1 - cp0);
4152         h[cp1-cp0] = '\0';
4153         z_HTTP_header_add(odr, &p->u.HTTP_Request->headers, "Host", h);
4154     }
4155     p->u.HTTP_Request->path = odr_strdup(odr, *cp1 ? cp1 : "/");
4156     return p;
4157 }
4158
4159 static zoom_ret send_SRW_redirect(ZOOM_connection c, const char *uri,
4160                                   Z_HTTP_Response *cookie_hres)
4161 {
4162     struct Z_HTTP_Header *h;
4163     Z_GDU *gdu = get_HTTP_Request_url(c->odr_out, uri);
4164
4165     gdu->u.HTTP_Request->method = odr_strdup(c->odr_out, "GET");
4166     z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, "Accept",
4167                       "text/xml");
4168     
4169     for (h = cookie_hres->headers; h; h = h->next)
4170     {
4171         if (!strcmp(h->name, "Set-Cookie"))
4172             z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers,
4173                               "Cookie", h->value);
4174     }
4175     if (c->user && c->password)
4176     {
4177         z_HTTP_header_add_basic_auth(c->odr_out, &gdu->u.HTTP_Request->headers,
4178                                      c->user, c->password);
4179     }
4180     if (!z_GDU(c->odr_out, &gdu, 0, 0))
4181         return zoom_complete;
4182     if (c->odr_print)
4183         z_GDU(c->odr_print, &gdu, 0, 0);
4184     c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
4185
4186     odr_reset(c->odr_out);
4187     return do_write(c);
4188 }
4189
4190 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
4191 {
4192     zoom_ret cret = zoom_complete;
4193     int ret = -1;
4194     const char *addinfo = 0;
4195     const char *connection_head = z_HTTP_header_lookup(hres->headers,
4196                                                        "Connection");
4197     const char *location;
4198
4199     ZOOM_connection_set_mask(c, 0);
4200     yaz_log(log_details, "%p handle_http", c);
4201     
4202     if ((hres->code == 301 || hres->code == 302) && c->sru_mode == zoom_sru_get
4203         && (location = z_HTTP_header_lookup(hres->headers, "Location")))
4204     {
4205         c->no_redirects++;
4206         if (c->no_redirects > 10)
4207         {
4208             set_HTTP_error(c, hres->code, 0, 0);
4209             c->no_redirects = 0;
4210             do_close(c);
4211         }
4212         else
4213         {
4214             /* since redirect may change host we just reconnect. A smarter
4215                implementation might check whether it's the same server */
4216             do_connect_host(c, location, 0);
4217             send_SRW_redirect(c, location, hres);
4218             /* we're OK for now. Operation is not really complete */
4219             ret = 0;
4220             cret = zoom_pending;
4221         }
4222     }
4223     else 
4224     {   /* not redirect (normal response) */
4225         if (!yaz_srw_check_content_type(hres))
4226             addinfo = "content-type";
4227         else
4228         {
4229             Z_SOAP *soap_package = 0;
4230             ODR o = c->odr_in;
4231             Z_SOAP_Handler soap_handlers[2] = {
4232                 {YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec},
4233                 {0, 0, 0}
4234             };
4235             ret = z_soap_codec(o, &soap_package,
4236                                &hres->content_buf, &hres->content_len,
4237                                soap_handlers);
4238             if (!ret && soap_package->which == Z_SOAP_generic &&
4239                 soap_package->u.generic->no == 0)
4240             {
4241                 Z_SRW_PDU *sr = (Z_SRW_PDU*) soap_package->u.generic->p;
4242                 
4243                 ZOOM_options_set(c->options, "sru_version", sr->srw_version);
4244                 ZOOM_options_setl(c->options, "sru_extra_response_data",
4245                                   sr->extraResponseData_buf, sr->extraResponseData_len);
4246                 if (sr->which == Z_SRW_searchRetrieve_response)
4247                     cret = handle_srw_response(c, sr->u.response);
4248                 else if (sr->which == Z_SRW_scan_response)
4249                     handle_srw_scan_response(c, sr->u.scan_response);
4250                 else
4251                     ret = -1;
4252             }
4253             else if (!ret && (soap_package->which == Z_SOAP_fault
4254                               || soap_package->which == Z_SOAP_error))
4255             {
4256                 set_HTTP_error(c, hres->code,
4257                                soap_package->u.fault->fault_code,
4258                                soap_package->u.fault->fault_string);
4259             }
4260             else
4261                 ret = -1;
4262         }   
4263         if (ret == 0)
4264         {
4265             if (c->no_redirects) /* end of redirect. change hosts again */
4266                 do_close(c);
4267         }
4268         c->no_redirects = 0;
4269     }
4270     if (ret)
4271     {
4272         if (hres->code != 200)
4273             set_HTTP_error(c, hres->code, 0, 0);
4274         else
4275             set_ZOOM_error(c, ZOOM_ERROR_DECODE, addinfo);
4276         do_close(c);
4277     }
4278     if (cret == zoom_complete)
4279         ZOOM_connection_remove_task(c);
4280     if (!strcmp(hres->version, "1.0"))
4281     {
4282         /* HTTP 1.0: only if Keep-Alive we stay alive.. */
4283         if (!connection_head || strcmp(connection_head, "Keep-Alive"))
4284             do_close(c);
4285     }
4286     else 
4287     {
4288         /* HTTP 1.1: only if no close we stay alive .. */
4289         if (connection_head && !strcmp(connection_head, "close"))
4290             do_close(c);
4291     }
4292 }
4293 #endif
4294
4295 static int do_read(ZOOM_connection c)
4296 {
4297     int r, more;
4298     ZOOM_Event event;
4299     
4300     event = ZOOM_Event_create(ZOOM_EVENT_RECV_DATA);
4301     ZOOM_connection_put_event(c, event);
4302     
4303     r = cs_get(c->cs, &c->buf_in, &c->len_in);
4304     more = cs_more(c->cs);
4305     yaz_log(log_details, "%p do_read len=%d more=%d", c, r, more);
4306     if (r == 1)
4307         return 0;
4308     if (r <= 0)
4309     {
4310         if (!ZOOM_test_reconnect(c))
4311         {
4312             set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4313             do_close(c);
4314         }
4315     }
4316     else
4317     {
4318         Z_GDU *gdu;
4319         ZOOM_Event event;
4320
4321         odr_reset(c->odr_in);
4322         odr_setbuf(c->odr_in, c->buf_in, r, 0);
4323         event = ZOOM_Event_create(ZOOM_EVENT_RECV_APDU);
4324         ZOOM_connection_put_event(c, event);
4325
4326         if (!z_GDU(c->odr_in, &gdu, 0, 0))
4327         {
4328             int x;
4329             int err = odr_geterrorx(c->odr_in, &x);
4330             char msg[100];
4331             const char *element = odr_getelement(c->odr_in);
4332             yaz_snprintf(msg, sizeof(msg),
4333                     "ODR code %d:%d element=%s offset=%d",
4334                     err, x, element ? element : "<unknown>",
4335                     odr_offset(c->odr_in));
4336             set_ZOOM_error(c, ZOOM_ERROR_DECODE, msg);
4337             if (log_api)
4338             {
4339                 FILE *ber_file = yaz_log_file();
4340                 if (ber_file)
4341                     odr_dumpBER(ber_file, c->buf_in, r);
4342             }
4343             do_close(c);
4344         }
4345         else
4346         {
4347             if (c->odr_print)
4348                 z_GDU(c->odr_print, &gdu, 0, 0);
4349             if (gdu->which == Z_GDU_Z3950)
4350                 recv_apdu(c, gdu->u.z3950);
4351             else if (gdu->which == Z_GDU_HTTP_Response)
4352             {
4353 #if YAZ_HAVE_XML2
4354                 handle_http(c, gdu->u.HTTP_Response);
4355 #else
4356                 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
4357                 do_close(c);
4358 #endif
4359             }
4360         }
4361         c->reconnect_ok = 0;
4362     }
4363     return 1;
4364 }
4365
4366 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
4367 {
4368     int r;
4369     ZOOM_Event event;
4370     
4371     event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
4372     ZOOM_connection_put_event(c, event);
4373
4374     yaz_log(log_details, "%p do_write_ex len=%d", c, len_out);
4375     if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
4376     {
4377         yaz_log(log_details, "%p do_write_ex write failed", c);
4378         if (ZOOM_test_reconnect(c))
4379         {
4380             return zoom_pending;
4381         }
4382         if (c->state == STATE_CONNECTING)
4383             set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4384         else
4385             set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4386         do_close(c);
4387         return zoom_complete;
4388     }
4389     else if (r == 1)
4390     {    
4391         int mask = ZOOM_SELECT_EXCEPT;
4392         if (c->cs->io_pending & CS_WANT_WRITE)
4393             mask += ZOOM_SELECT_WRITE;
4394         if (c->cs->io_pending & CS_WANT_READ)
4395             mask += ZOOM_SELECT_READ;
4396         ZOOM_connection_set_mask(c, mask);
4397         yaz_log(log_details, "%p do_write_ex write incomplete mask=%d",
4398                 c, c->mask);
4399     }
4400     else
4401     {
4402         ZOOM_connection_set_mask(c, ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT);
4403         yaz_log(log_details, "%p do_write_ex write complete mask=%d",
4404                 c, c->mask);
4405     }
4406     return zoom_pending;
4407 }
4408
4409 static zoom_ret do_write(ZOOM_connection c)
4410 {
4411     return do_write_ex(c, c->buf_out, c->len_out);
4412 }
4413
4414
4415 ZOOM_API(const char *)
4416     ZOOM_connection_option_get(ZOOM_connection c, const char *key)
4417 {
4418     return ZOOM_options_get(c->options, key);
4419 }
4420
4421 ZOOM_API(const char *)
4422     ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp)
4423 {
4424     return ZOOM_options_getl(c->options, key, lenp);
4425 }
4426
4427 ZOOM_API(void)
4428     ZOOM_connection_option_set(ZOOM_connection c, const char *key,
4429                                const char *val)
4430 {
4431     ZOOM_options_set(c->options, key, val);
4432 }
4433
4434 ZOOM_API(void)
4435     ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
4436                                 const char *val, int len)
4437 {
4438     ZOOM_options_setl(c->options, key, val, len);
4439 }
4440
4441 ZOOM_API(const char *)
4442     ZOOM_resultset_option_get(ZOOM_resultset r, const char *key)
4443 {
4444     return ZOOM_options_get(r->options, key);
4445 }
4446
4447 ZOOM_API(void)
4448     ZOOM_resultset_option_set(ZOOM_resultset r, const char *key,
4449                               const char *val)
4450 {
4451     ZOOM_options_set(r->options, key, val);
4452 }
4453
4454
4455 ZOOM_API(int)
4456     ZOOM_connection_errcode(ZOOM_connection c)
4457 {
4458     return ZOOM_connection_error(c, 0, 0);
4459 }
4460
4461 ZOOM_API(const char *)
4462     ZOOM_connection_errmsg(ZOOM_connection c)
4463 {
4464     const char *msg;
4465     ZOOM_connection_error(c, &msg, 0);
4466     return msg;
4467 }
4468
4469 ZOOM_API(const char *)
4470     ZOOM_connection_addinfo(ZOOM_connection c)
4471 {
4472     const char *addinfo;
4473     ZOOM_connection_error(c, 0, &addinfo);
4474     return addinfo;
4475 }
4476
4477 ZOOM_API(const char *)
4478     ZOOM_connection_diagset(ZOOM_connection c)
4479 {
4480     const char *diagset;
4481     ZOOM_connection_error_x(c, 0, 0, &diagset);
4482     return diagset;
4483 }
4484
4485 ZOOM_API(const char *)
4486     ZOOM_diag_str(int error)
4487 {
4488     switch (error)
4489     {
4490     case ZOOM_ERROR_NONE:
4491         return "No error";
4492     case ZOOM_ERROR_CONNECT:
4493         return "Connect failed";
4494     case ZOOM_ERROR_MEMORY:
4495         return "Out of memory";
4496     case ZOOM_ERROR_ENCODE:
4497         return "Encoding failed";
4498     case ZOOM_ERROR_DECODE:
4499         return "Decoding failed";
4500     case ZOOM_ERROR_CONNECTION_LOST:
4501         return "Connection lost";
4502     case ZOOM_ERROR_INIT:
4503         return "Init rejected";
4504     case ZOOM_ERROR_INTERNAL:
4505         return "Internal failure";
4506     case ZOOM_ERROR_TIMEOUT:
4507         return "Timeout";
4508     case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
4509         return "Unsupported protocol";
4510     case ZOOM_ERROR_UNSUPPORTED_QUERY:
4511         return "Unsupported query type";
4512     case ZOOM_ERROR_INVALID_QUERY:
4513         return "Invalid query";
4514     case ZOOM_ERROR_CQL_PARSE:
4515         return "CQL parsing error";
4516     case ZOOM_ERROR_CQL_TRANSFORM:
4517         return "CQL transformation error";
4518     case ZOOM_ERROR_CCL_CONFIG:
4519         return "CCL configuration error";
4520     case ZOOM_ERROR_CCL_PARSE:
4521         return "CCL parsing error";
4522     default:
4523         return diagbib1_str(error);
4524     }
4525 }
4526
4527 ZOOM_API(int)
4528     ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
4529                             const char **addinfo, const char **diagset)
4530 {
4531     int error = c->error;
4532     if (cp)
4533     {
4534         if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
4535             *cp = ZOOM_diag_str(error);
4536         else if (!strcmp(c->diagset, "HTTP"))
4537             *cp = z_HTTP_errmsg(c->error);
4538         else if (!strcmp(c->diagset, "Bib-1"))
4539             *cp = ZOOM_diag_str(error);
4540         else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
4541             *cp = yaz_diag_srw_str(c->error);
4542         else
4543             *cp = "Unknown error and diagnostic set";
4544     }
4545     if (addinfo)
4546         *addinfo = c->addinfo ? c->addinfo : "";
4547     if (diagset)
4548         *diagset = c->diagset ? c->diagset : "";
4549     return c->error;
4550 }
4551
4552 ZOOM_API(int)
4553     ZOOM_connection_error(ZOOM_connection c, const char **cp,
4554                           const char **addinfo)
4555 {
4556     return ZOOM_connection_error_x(c, cp, addinfo, 0);
4557 }
4558
4559 static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
4560 {
4561     ZOOM_Event event = 0;
4562     int r = cs_look(c->cs);
4563     yaz_log(log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
4564             c, mask, r);
4565     
4566     if (r == CS_NONE)
4567     {
4568         event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
4569         set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4570         do_close(c);
4571         ZOOM_connection_put_event(c, event);
4572     }
4573     else if (r == CS_CONNECT)
4574     {
4575         int ret = ret = cs_rcvconnect(c->cs);
4576         yaz_log(log_details, "%p ZOOM_connection_do_io "
4577                 "cs_rcvconnect returned %d", c, ret);
4578         if (ret == 1)
4579         {
4580             int mask = ZOOM_SELECT_EXCEPT;
4581             if (c->cs->io_pending & CS_WANT_WRITE)
4582                 mask += ZOOM_SELECT_WRITE;
4583             if (c->cs->io_pending & CS_WANT_READ)
4584                 mask += ZOOM_SELECT_READ;
4585             ZOOM_connection_set_mask(c, mask);
4586             event = ZOOM_Event_create(ZOOM_EVENT_NONE);
4587             ZOOM_connection_put_event(c, event);
4588         }
4589         else if (ret == 0)
4590         {
4591             event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
4592             ZOOM_connection_put_event(c, event);
4593             get_cert(c);
4594             if (c->proto == PROTO_Z3950)
4595                 ZOOM_connection_send_init(c);
4596             else
4597             {
4598                 /* no init request for SRW .. */
4599                 assert(c->tasks->which == ZOOM_TASK_CONNECT);
4600                 ZOOM_connection_remove_task(c);
4601                 ZOOM_connection_set_mask(c, 0);
4602                 ZOOM_connection_exec_task(c);
4603             }
4604             c->state = STATE_ESTABLISHED;
4605         }
4606         else
4607         {
4608             set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4609             do_close(c);
4610         }
4611     }
4612     else
4613     {
4614         if (mask & ZOOM_SELECT_EXCEPT)
4615         {
4616             if (!ZOOM_test_reconnect(c))
4617             {
4618                 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4619                 do_close(c);
4620             }
4621             return;
4622         }
4623         if (mask & ZOOM_SELECT_READ)
4624             do_read(c);
4625         if (c->cs && (mask & ZOOM_SELECT_WRITE))
4626             do_write(c);
4627     }
4628 }
4629
4630 ZOOM_API(int)
4631     ZOOM_connection_last_event(ZOOM_connection cs)
4632 {
4633     if (!cs)
4634         return ZOOM_EVENT_NONE;
4635     return cs->last_event;
4636 }
4637
4638
4639 static void cql2pqf_wrbuf_puts(const char *buf, void *client_data)
4640 {
4641     WRBUF wrbuf = (WRBUF) client_data;
4642     wrbuf_puts(wrbuf, buf);
4643 }
4644
4645 /*
4646  * Returns an xmalloc()d string containing RPN that corresponds to the
4647  * CQL passed in.  On error, sets the Connection object's error state
4648  * and returns a null pointer.
4649  * ### We could cache CQL parser and/or transformer in Connection.
4650  */
4651 static char *cql2pqf(ZOOM_connection c, const char *cql)
4652 {
4653     CQL_parser parser;
4654     int error;
4655     const char *cqlfile;
4656     cql_transform_t trans;
4657     char *result = 0;
4658
4659     parser = cql_parser_create();
4660     if ((error = cql_parser_string(parser, cql)) != 0) {
4661         cql_parser_destroy(parser);
4662         set_ZOOM_error(c, ZOOM_ERROR_CQL_PARSE, cql);
4663         return 0;
4664     }
4665
4666     cqlfile = ZOOM_connection_option_get(c, "cqlfile");
4667     if (cqlfile == 0) 
4668     {
4669         set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, "no CQL transform file");
4670     }
4671     else if ((trans = cql_transform_open_fname(cqlfile)) == 0) 
4672     {
4673         char buf[512];        
4674         sprintf(buf, "can't open CQL transform file '%.200s': %.200s",
4675                 cqlfile, strerror(errno));
4676         set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
4677     }
4678     else 
4679     {
4680         WRBUF wrbuf_result = wrbuf_alloc();
4681         error = cql_transform(trans, cql_parser_result(parser),
4682                               cql2pqf_wrbuf_puts, wrbuf_result);
4683         if (error != 0) {
4684             char buf[512];
4685             const char *addinfo;
4686             error = cql_transform_error(trans, &addinfo);
4687             sprintf(buf, "%.200s (addinfo=%.200s)", 
4688                     cql_strerror(error), addinfo);
4689             set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
4690         }
4691         else
4692         {
4693             result = xstrdup(wrbuf_cstr(wrbuf_result));
4694         }
4695         cql_transform_close(trans);
4696         wrbuf_destroy(wrbuf_result);
4697     }
4698     cql_parser_destroy(parser);
4699     return result;
4700 }
4701
4702 ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
4703 {
4704     if (c->mask)
4705     {
4706         ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
4707         /* timeout and this connection was waiting */
4708         set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
4709         do_close(c);
4710         ZOOM_connection_put_event(c, event);
4711     }
4712     return 0;
4713 }
4714
4715 ZOOM_API(int)
4716     ZOOM_connection_process(ZOOM_connection c)
4717 {
4718     ZOOM_Event event;
4719     if (!c)
4720         return 0;
4721
4722     event = ZOOM_connection_get_event(c);
4723     if (event)
4724     {
4725         ZOOM_Event_destroy(event);
4726         return 1;
4727     }
4728     ZOOM_connection_exec_task(c);
4729     event = ZOOM_connection_get_event(c);
4730     if (event)
4731     {
4732         ZOOM_Event_destroy(event);
4733         return 1;
4734     }
4735     return 0;
4736 }
4737
4738 ZOOM_API(int)
4739     ZOOM_event_nonblock(int no, ZOOM_connection *cs)
4740 {
4741     int i;
4742
4743     yaz_log(log_details, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
4744     
4745     for (i = 0; i<no; i++)
4746     {
4747         ZOOM_connection c = cs[i];
4748
4749         if (c && ZOOM_connection_process(c))
4750             return i+1;
4751     }
4752     return 0;
4753 }
4754
4755 ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
4756 {
4757     if (c->mask && mask)
4758         ZOOM_connection_do_io(c, mask);
4759     return 0;
4760 }
4761
4762 ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
4763 {
4764     if (c->cs)
4765         return cs_fileno(c->cs);
4766     return -1;
4767 }
4768
4769 ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
4770 {
4771     c->mask = mask;
4772     if (!c->cs)
4773         return -1; 
4774     return 0;
4775 }
4776
4777 ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
4778 {
4779     if (c->cs)
4780         return c->mask;
4781     return 0;
4782 }
4783
4784 ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
4785 {
4786     return ZOOM_options_get_int(c->options, "timeout", 30);
4787 }
4788
4789 ZOOM_API(void) ZOOM_connection_close(ZOOM_connection c)
4790 {
4791     do_close(c);
4792 }
4793
4794 /*
4795  * Local variables:
4796  * c-basic-offset: 4
4797  * c-file-style: "Stroustrup"
4798  * indent-tabs-mode: nil
4799  * End:
4800  * vim: shiftwidth=4 tabstop=8 expandtab
4801  */
4802