Fix memory leak for MARC record management.
[yaz-moved-to-github.git] / zoom / zoom-c.c
1 /*
2  * $Id: zoom-c.c,v 1.22 2002-01-24 19:33:09 adam Exp $
3  *
4  * ZOOM layer for C, connections, result sets, queries.
5  */
6 #include <assert.h>
7 #include <yaz/xmalloc.h>
8 #include <yaz/otherinfo.h>
9 #include <yaz/log.h>
10 #include <yaz/pquery.h>
11 #include <yaz/diagbib1.h>
12
13 #include "zoom-p.h"
14
15 #if HAVE_SYS_POLL_H
16 #include <sys/poll.h>
17 #endif
18
19 static ZOOM_Event ZOOM_Event_create (int kind)
20 {
21     ZOOM_Event event = xmalloc (sizeof(*event));
22     event->kind = kind;
23     event->next = 0;
24     event->prev = 0;
25     return event;
26 }
27
28 static void ZOOM_Event_destroy (ZOOM_Event event)
29 {
30     xfree (event);
31 }
32
33 static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event)
34 {
35     if (c->m_queue_back)
36     {
37         c->m_queue_back->prev = event;
38         assert (c->m_queue_front);
39     }
40     else
41     {
42         assert (!c->m_queue_front);
43         c->m_queue_front = event;
44     }
45     event->next = c->m_queue_back;
46     event->prev = 0;
47     c->m_queue_back = event;
48 }
49
50 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
51 {
52     ZOOM_Event event = c->m_queue_front;
53     if (!event)
54         return 0;
55     assert (c->m_queue_back);
56     c->m_queue_front = event->prev;
57     if (c->m_queue_front)
58     {
59         assert (c->m_queue_back);
60         c->m_queue_front->next = 0;
61     }
62     else
63         c->m_queue_back = 0;
64     c->last_event = event->kind;
65     return event;
66 }
67
68 static void clear_error (ZOOM_connection c)
69 {
70
71     switch (c->error)
72     {
73     case ZOOM_ERROR_CONNECT:
74     case ZOOM_ERROR_MEMORY:
75     case ZOOM_ERROR_DECODE:
76     case ZOOM_ERROR_CONNECTION_LOST:
77     case ZOOM_ERROR_INIT:
78     case ZOOM_ERROR_INTERNAL:
79         break;
80     default:
81         c->error = ZOOM_ERROR_NONE;
82         xfree (c->addinfo);
83         c->addinfo = 0;
84     }
85 }
86
87 ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which)
88 {
89     ZOOM_task *taskp = &c->tasks;
90     while (*taskp)
91         taskp = &(*taskp)->next;
92     *taskp = xmalloc (sizeof(**taskp));
93     (*taskp)->running = 0;
94     (*taskp)->which = which;
95     (*taskp)->next = 0;
96     clear_error (c);
97     return *taskp;
98 }
99
100 void ZOOM_connection_remove_task (ZOOM_connection c)
101 {
102     ZOOM_task task = c->tasks;
103
104     if (task)
105     {
106         c->tasks = task->next;
107         switch (task->which)
108         {
109         case ZOOM_TASK_SEARCH:
110             ZOOM_resultset_destroy (task->u.search.resultset);
111             break;
112         case ZOOM_TASK_RETRIEVE:
113             ZOOM_resultset_destroy (task->u.retrieve.resultset);
114             break;
115         case ZOOM_TASK_CONNECT:
116             break;
117         case ZOOM_TASK_SCAN:
118             ZOOM_scanset_destroy (task->u.scan.scan);
119             break;
120         default:
121             assert (0);
122         }
123         xfree (task);
124     }
125 }
126
127 void ZOOM_connection_remove_tasks (ZOOM_connection c)
128 {
129     while (c->tasks)
130         ZOOM_connection_remove_task(c);
131 }
132
133 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
134                                          int pos,
135                                          const char *elementSetName);
136
137 ZOOM_connection ZOOM_connection_create (ZOOM_options options)
138 {
139     ZOOM_connection c = xmalloc (sizeof(*c));
140
141     c->cs = 0;
142     c->mask = 0;
143     c->state = STATE_IDLE;
144     c->error = ZOOM_ERROR_NONE;
145     c->addinfo = 0;
146     c->buf_in = 0;
147     c->len_in = 0;
148     c->buf_out = 0;
149     c->len_out = 0;
150     c->resultsets = 0;
151
152     c->options = ZOOM_options_create_with_parent(options);
153
154     c->host_port = 0;
155     c->proxy = 0;
156
157     c->cookie_out = 0;
158     c->cookie_in = 0;
159     c->tasks = 0;
160
161     c->odr_in = odr_createmem (ODR_DECODE);
162     c->odr_out = odr_createmem (ODR_ENCODE);
163
164     c->async = 0;
165     c->support_named_resultsets = 0;
166     c->last_event = ZOOM_EVENT_NONE;
167
168     c->m_queue_front = 0;
169     c->m_queue_back = 0;
170     return c;
171 }
172
173 /* set database names. Take local databases (if set); otherwise
174    take databases given in ZURL (if set); otherwise use Default */
175 static char **set_DatabaseNames (ZOOM_connection con, ZOOM_options options,
176                                  int *num)
177 {
178     char **databaseNames;
179     const char *c;
180     int no = 2;
181     const char *cp = ZOOM_options_get (options, "databaseName");
182     
183     if (!cp || !*cp)
184     {
185         cp = strchr (con->host_port, '/');
186         if (cp)
187             cp++;
188         }
189     if (cp)
190     {
191         c = cp;
192         while ((c = strchr(c, '+')))
193         {
194             c++;
195             no++;
196         }
197     }
198     else
199         cp = "Default";
200     databaseNames = odr_malloc (con->odr_out, no * sizeof(*databaseNames));
201     no = 0;
202     while (*cp)
203     {
204         c = strchr (cp, '+');
205         if (!c)
206             c = cp + strlen(cp);
207         else if (c == cp)
208         {
209             cp++;
210             continue;
211         }
212         /* cp ptr to first char of db name, c is char
213            following db name */
214         databaseNames[no] = odr_malloc (con->odr_out, 1+c-cp);
215         memcpy (databaseNames[no], cp, c-cp);
216         databaseNames[no++][c-cp] = '\0';
217         cp = c;
218         if (*cp)
219             cp++;
220     }
221     databaseNames[no] = NULL;
222     *num = no;
223     return databaseNames;
224 }
225
226 ZOOM_connection ZOOM_connection_new (const char *host, int portnum)
227 {
228     ZOOM_connection c = ZOOM_connection_create (0);
229
230     ZOOM_connection_connect (c, host, portnum);
231     return c;
232 }
233
234 void ZOOM_connection_connect(ZOOM_connection c,
235                               const char *host, int portnum)
236 {
237     const char *val;
238     ZOOM_task task;
239
240     val = ZOOM_options_get (c->options, "proxy");
241     if (val && *val)
242         c->proxy = xstrdup (val);
243     else
244         c->proxy = 0;
245
246     if (portnum)
247     {
248         char hostn[128];
249         sprintf (hostn, "%.80s:%d", host, portnum);
250         c->host_port = xstrdup(hostn);
251     }
252     else
253         c->host_port = xstrdup(host);
254
255     ZOOM_options_set(c->options, "host", c->host_port);
256
257     c->async = ZOOM_options_get_bool (c->options, "async", 0);
258  
259     c->error = ZOOM_ERROR_NONE;
260
261     task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
262
263     if (!c->async)
264     {
265         while (ZOOM_event (1, &c))
266             ;
267     }
268 }
269
270 ZOOM_query ZOOM_query_create(void)
271 {
272     ZOOM_query s = xmalloc (sizeof(*s));
273
274     s->refcount = 1;
275     s->query = 0;
276     s->sort_spec = 0;
277     s->odr = odr_createmem (ODR_ENCODE);
278
279     return s;
280 }
281
282 void ZOOM_query_destroy(ZOOM_query s)
283 {
284     if (!s)
285         return;
286
287     (s->refcount)--;
288     yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount);
289     if (s->refcount == 0)
290     {
291         odr_destroy (s->odr);
292         xfree (s);
293     }
294 }
295
296 int ZOOM_query_prefix(ZOOM_query s, const char *str)
297 {
298     s->query = odr_malloc (s->odr, sizeof(*s->query));
299     s->query->which = Z_Query_type_1;
300     s->query->u.type_1 =  p_query_rpn(s->odr, PROTO_Z3950, str);
301     if (!s->query->u.type_1)
302         return -1;
303     return 0;
304 }
305
306 int ZOOM_query_sortby(ZOOM_query s, const char *criteria)
307 {
308     s->sort_spec = yaz_sort_spec (s->odr, criteria);
309     if (!s->sort_spec)
310         return -1;
311     return 0;
312 }
313
314 static int do_write(ZOOM_connection c);
315
316 void ZOOM_connection_destroy(ZOOM_connection c)
317 {
318     ZOOM_resultset r;
319     if (!c)
320         return;
321     if (c->cs)
322         cs_close (c->cs);
323     for (r = c->resultsets; r; r = r->next)
324         r->connection = 0;
325
326     xfree (c->buf_in);
327     xfree (c->addinfo);
328     odr_destroy (c->odr_in);
329     odr_destroy (c->odr_out);
330     ZOOM_options_destroy (c->options);
331     ZOOM_connection_remove_tasks (c);
332     xfree (c->host_port);
333     xfree (c);
334 }
335
336 void ZOOM_resultset_addref (ZOOM_resultset r)
337 {
338     if (r)
339         (r->refcount)++;
340 }
341 ZOOM_resultset ZOOM_resultset_create ()
342 {
343     ZOOM_resultset r = xmalloc (sizeof(*r));
344
345     r->refcount = 1;
346     r->size = 0;
347     r->odr = odr_createmem (ODR_ENCODE);
348     r->start = 0;
349     r->piggyback = 1;
350     r->setname = 0;
351     r->count = 0;
352     r->record_cache = 0;
353     r->r_sort_spec = 0;
354     r->r_query = 0;
355     r->search = 0;
356     r->connection = 0;
357     r->next = 0;
358     return r;
359 }
360
361 ZOOM_resultset ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
362 {
363     ZOOM_resultset r;
364     ZOOM_query s = ZOOM_query_create();
365
366     ZOOM_query_prefix (s, q);
367
368     r = ZOOM_connection_search (c, s);
369     ZOOM_query_destroy (s);
370     return r;
371 }
372
373 ZOOM_resultset ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
374 {
375     ZOOM_resultset r = ZOOM_resultset_create ();
376     ZOOM_task task;
377     const char *cp;
378
379     r->r_sort_spec = q->sort_spec;
380     r->r_query = q->query;
381     r->search = q;
382
383     r->options = ZOOM_options_create_with_parent(c->options);
384
385     r->start = ZOOM_options_get_int(r->options, "start", 0);
386     r->count = ZOOM_options_get_int(r->options, "count", 0);
387     r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
388     cp = ZOOM_options_get (r->options, "setname");
389     if (cp)
390         r->setname = xstrdup (cp);
391     
392     r->connection = c;
393
394     r->next = c->resultsets;
395     c->resultsets = r;
396
397     task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
398     task->u.search.resultset = r;
399     ZOOM_resultset_addref (r);  
400
401     (q->refcount)++;
402
403     if (!c->async)
404     {
405         while (ZOOM_event (1, &c))
406             ;
407     }
408     return r;
409 }
410
411 void ZOOM_resultset_destroy(ZOOM_resultset r)
412 {
413     if (!r)
414         return;
415     (r->refcount)--;
416     yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount);
417     if (r->refcount == 0)
418     {
419         ZOOM_record_cache rc;
420
421         for (rc = r->record_cache; rc; rc = rc->next)
422             if (rc->rec.wrbuf_marc)
423                 wrbuf_free (rc->rec.wrbuf_marc, 1);
424         if (r->connection)
425         {
426             /* remove ourselves from the resultsets in connection */
427             ZOOM_resultset *rp = &r->connection->resultsets;
428             while (1)
429             {
430                 assert (*rp);   /* we must be in this list!! */
431                 if (*rp == r)
432                 {   /* OK, we're here - take us out of it */
433                     *rp = (*rp)->next;
434                     break;
435                 }
436                 rp = &(*rp)->next;
437             }
438         }
439         ZOOM_query_destroy (r->search);
440         ZOOM_options_destroy (r->options);
441         odr_destroy (r->odr);
442         xfree (r->setname);
443         xfree (r);
444     }
445 }
446
447 size_t ZOOM_resultset_size (ZOOM_resultset r)
448 {
449     return r->size;
450 }
451
452 static void do_close (ZOOM_connection c)
453 {
454     if (c->cs)
455         cs_close(c->cs);
456     c->cs = 0;
457     c->mask = 0;
458     c->state = STATE_IDLE;
459 }
460
461 static void ZOOM_resultset_retrieve (ZOOM_resultset r,
462                                       int force_sync, int start, int count)
463 {
464     ZOOM_task task;
465     ZOOM_connection c;
466
467     if (!r)
468         return;
469     c = r->connection;
470     if (!c)
471         return;
472     task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
473     task->u.retrieve.resultset = r;
474     task->u.retrieve.start = start;
475     task->u.retrieve.count = count;
476
477     ZOOM_resultset_addref (r);
478
479     if (!r->connection->async || force_sync)
480         while (r->connection && ZOOM_event (1, &r->connection))
481             ;
482 }
483
484 void ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
485                               size_t start, size_t count)
486 {
487     int force_present = 0;
488
489     if (!r)
490         return ;
491     if (count && recs)
492         force_present = 1;
493     ZOOM_resultset_retrieve (r, force_present, start, count);
494     if (force_present)
495     {
496         size_t i;
497         for (i = 0; i< count; i++)
498             recs[i] = ZOOM_resultset_record_immediate (r, i+start);
499     }
500 }
501
502 static int do_connect (ZOOM_connection c)
503 {
504     void *add;
505     const char *effective_host;
506
507     if (c->proxy)
508         effective_host = c->proxy;
509     else
510         effective_host = c->host_port;
511
512     yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
513
514     assert (!c->cs);
515     c->cs = cs_create_host (effective_host, 0, &add);
516
517     if (c->cs)
518     {
519         int ret = cs_connect (c->cs, add);
520         yaz_log (LOG_DEBUG, "cs_connect returned %d", ret);
521         if (ret >= 0)
522         {
523             c->state = STATE_CONNECTING; 
524             c->mask = ZOOM_SELECT_EXCEPT;
525             if (c->cs->io_pending & CS_WANT_WRITE)
526                 c->mask += ZOOM_SELECT_WRITE;
527             if (c->cs->io_pending & CS_WANT_READ)
528                 c->mask += ZOOM_SELECT_READ;
529             return 1;
530         }
531     }
532     c->state = STATE_IDLE;
533     c->error = ZOOM_ERROR_CONNECT;
534     return 0;
535 }
536
537 int z3950_connection_socket(ZOOM_connection c)
538 {
539     if (c->cs)
540         return cs_fileno(c->cs);
541     return -1;
542 }
543
544 int z3950_connection_mask(ZOOM_connection c)
545 {
546     if (c->cs)
547         return c->mask;
548     return 0;
549 }
550
551 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
552 {
553     char str[120];
554
555     assert (a);
556     sprintf (str, "send_APDU t=%p type=%d", c, a->which);
557     if (c->cookie_out)
558     {
559         Z_OtherInformation **oi;
560         yaz_oi_APDU(a, &oi);
561         yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
562     }
563     if (!z_APDU(out, &a, 0, 0))
564     {
565         FILE *outf = fopen("/tmp/apdu.txt", "w");
566         if (outf)
567         {
568             ODR odr_pr = odr_createmem(ODR_PRINT);
569             fprintf (outf, "a=%p\n", a);
570             odr_setprint(odr_pr, outf);
571             z_APDU(odr_pr, &a, 0, 0);
572             odr_destroy(odr_pr);
573             fclose (outf);
574         }
575         c->error = ZOOM_ERROR_ENCODE;
576         do_close (c);
577         return -1;
578     }
579     
580     return 0;
581 }
582
583 static int send_APDU (ZOOM_connection c, Z_APDU *a)
584 {
585     ZOOM_Event event;
586     assert (a);
587     if (encode_APDU(c, a, c->odr_out))
588         return -1;
589     c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
590     event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
591     ZOOM_connection_put_event (c, event);
592     odr_reset(c->odr_out);
593     do_write (c);
594     return 0;   
595 }
596
597 static int ZOOM_connection_send_init (ZOOM_connection c)
598 {
599     const char *impname;
600     Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
601     Z_InitRequest *ireq = apdu->u.initRequest;
602     Z_IdAuthentication *auth = odr_malloc(c->odr_out, sizeof(*auth));
603     const char *auth_groupId = ZOOM_options_get (c->options, "group");
604     const char *auth_userId = ZOOM_options_get (c->options, "user");
605     const char *auth_password = ZOOM_options_get (c->options, "pass");
606     
607     ODR_MASK_SET(ireq->options, Z_Options_search);
608     ODR_MASK_SET(ireq->options, Z_Options_present);
609     ODR_MASK_SET(ireq->options, Z_Options_scan);
610     ODR_MASK_SET(ireq->options, Z_Options_sort);
611     ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
612     ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
613     
614     ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
615     ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
616     ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
617     
618     impname = ZOOM_options_get (c->options, "implementationName");
619     ireq->implementationName =
620         odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
621     strcpy (ireq->implementationName, "");
622     if (impname)
623     {
624         strcat (ireq->implementationName, impname);
625         strcat (ireq->implementationName, "/");
626     }                                          
627     strcat (ireq->implementationName, "ZOOM-C/YAZ");
628     
629     *ireq->maximumRecordSize =
630         ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024);
631     *ireq->preferredMessageSize =
632         ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024);
633     
634     if (auth_groupId || auth_password)
635     {
636         Z_IdPass *pass = odr_malloc(c->odr_out, sizeof(*pass));
637         int i = 0;
638         pass->groupId = 0;
639         if (auth_groupId && *auth_groupId)
640         {
641             pass->groupId = odr_malloc(c->odr_out, strlen(auth_groupId)+1);
642             strcpy(pass->groupId, auth_groupId);
643             i++;
644         }
645         pass->userId = 0;
646         if (auth_userId && *auth_userId)
647         {
648             pass->userId = odr_malloc(c->odr_out, strlen(auth_userId)+1);
649             strcpy(pass->userId, auth_userId);
650             i++;
651         }
652         pass->password = 0;
653         if (auth_password && *auth_password)
654         {
655             pass->password = odr_malloc(c->odr_out, strlen(auth_password)+1);
656             strcpy(pass->password, auth_password);
657             i++;
658         }
659         if (i)
660         {
661             auth->which = Z_IdAuthentication_idPass;
662             auth->u.idPass = pass;
663             ireq->idAuthentication = auth;
664         }
665     }
666     else if (auth_userId)
667     {
668         auth->which = Z_IdAuthentication_open;
669         auth->u.open = odr_malloc(c->odr_out, strlen(auth_userId)+1);
670         strcpy(auth->u.open, auth_userId);
671         ireq->idAuthentication = auth;
672     }
673     if (c->proxy)
674         yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
675                                  VAL_PROXY, 1, c->host_port);
676     assert (apdu);
677     send_APDU (c, apdu);
678     
679     return 0;
680 }
681
682 static int ZOOM_connection_send_search (ZOOM_connection c)
683 {
684     ZOOM_resultset r;
685     int lslb, ssub, mspn;
686     const char *syntax;
687     Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
688     Z_SearchRequest *search_req = apdu->u.searchRequest;
689     const char *elementSetName;
690     const char *smallSetElementSetName;
691     const char *mediumSetElementSetName;
692     const char *schema;
693
694     assert (c->tasks);
695     assert (c->tasks->which == ZOOM_TASK_SEARCH);
696
697     r = c->tasks->u.search.resultset;
698
699     elementSetName =
700         ZOOM_options_get (r->options, "elementSetName");
701     smallSetElementSetName  =
702         ZOOM_options_get (r->options, "smallSetElementSetName");
703     mediumSetElementSetName =
704         ZOOM_options_get (r->options, "mediumSetElementSetName");
705     schema =
706         ZOOM_options_get (r->options, "schema");
707
708     if (!smallSetElementSetName)
709         smallSetElementSetName = elementSetName;
710
711     if (!mediumSetElementSetName)
712         mediumSetElementSetName = elementSetName;
713
714     assert (r);
715     assert (r->r_query);
716
717     /* prepare query for the search request */
718     search_req->query = r->r_query;
719
720     search_req->databaseNames =
721         set_DatabaseNames (c, r->options, &search_req->num_databaseNames);
722
723     /* get syntax (no need to provide unless piggyback is in effect) */
724     syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
725
726     lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
727     ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
728     mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
729     if (lslb != -1 && ssub != -1 && mspn != -1)
730     {
731         /* So're a Z39.50 expert? Let's hope you don't do sort */
732         *search_req->largeSetLowerBound = lslb;
733         *search_req->smallSetUpperBound = ssub;
734         *search_req->mediumSetPresentNumber = mspn;
735     }
736     else if (r->start == 0 && r->count > 0
737              && r->piggyback && !r->r_sort_spec && !schema)
738     {
739         /* Regular piggyback - do it unless we're going to do sort */
740         *search_req->largeSetLowerBound = 2000000000;
741         *search_req->smallSetUpperBound = r->count;
742         *search_req->mediumSetPresentNumber = r->count;
743         smallSetElementSetName = 0;  /* no need to provide this */
744     }
745     else
746     {
747         /* non-piggyback. Need not provide elementsets or syntaxes .. */
748         smallSetElementSetName = 0;
749         mediumSetElementSetName = 0;
750         syntax = 0;
751     }
752     if (smallSetElementSetName && *smallSetElementSetName)
753     {
754         Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
755         
756         esn->which = Z_ElementSetNames_generic;
757         esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
758         search_req->smallSetElementSetNames = esn;
759     }
760     if (mediumSetElementSetName && *mediumSetElementSetName)
761     {
762         Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
763         
764         esn->which = Z_ElementSetNames_generic;
765         esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
766         search_req->mediumSetElementSetNames = esn;
767     }
768     if (syntax)
769         search_req->preferredRecordSyntax =
770             yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
771     
772     if (!r->setname)
773     {
774         if (c->support_named_resultsets)
775         {
776             char setname[14];
777             int ord;
778             /* find the lowest unused ordinal so that we re-use
779                result sets on the server. */
780             for (ord = 1; ; ord++)
781             {
782                 ZOOM_resultset rp;
783                 sprintf (setname, "%d", ord);
784                 for (rp = c->resultsets; rp; rp = rp->next)
785                     if (rp->setname && !strcmp (rp->setname, setname))
786                         break;
787                 if (!rp)
788                     break;
789             }
790             r->setname = xstrdup (setname);
791             yaz_log (LOG_DEBUG, "allocating %s", r->setname);
792         }
793         else
794             r->setname = xstrdup ("default");
795         ZOOM_options_set (r->options, "setname", r->setname);
796     }
797     search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
798     /* send search request */
799     send_APDU (c, apdu);
800     r->r_query = 0;
801     return 1;
802 }
803
804 static void response_diag (ZOOM_connection c, Z_DiagRec *p)
805 {
806     Z_DefaultDiagFormat *r;
807     char *addinfo = 0;
808     
809     xfree (c->addinfo);
810     c->addinfo = 0;
811     if (p->which != Z_DiagRec_defaultFormat)
812     {
813         c->error = ZOOM_ERROR_DECODE;
814         return;
815     }
816     r = p->u.defaultFormat;
817     switch (r->which)
818     {
819     case Z_DefaultDiagFormat_v2Addinfo:
820         addinfo = r->u.v2Addinfo;
821         break;
822     case Z_DefaultDiagFormat_v3Addinfo:
823         addinfo = r->u.v3Addinfo;
824         break;
825     }
826     if (addinfo)
827         c->addinfo = xstrdup (addinfo);
828     c->error = *r->condition;
829 }
830
831 ZOOM_record ZOOM_record_clone (ZOOM_record srec)
832 {
833     char *buf;
834     int size;
835     ODR odr_enc;
836     ZOOM_record nrec;
837
838     odr_enc = odr_createmem(ODR_ENCODE);
839     if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
840         return 0;
841     buf = odr_getbuf (odr_enc, &size, 0);
842     
843     nrec = xmalloc (sizeof(*nrec));
844     nrec->odr = odr_createmem(ODR_DECODE);
845     nrec->wrbuf_marc = 0;
846     odr_setbuf (nrec->odr, buf, size, 0);
847     z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
848     
849     odr_destroy (odr_enc);
850     return nrec;
851 }
852
853 ZOOM_record ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
854 {
855     return record_cache_lookup (s, pos, 0);
856 }
857
858 ZOOM_record ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
859 {
860     ZOOM_resultset_retrieve (r, 1, pos, 1);
861     return ZOOM_resultset_record_immediate (r, pos);
862 }
863
864 void ZOOM_record_destroy (ZOOM_record rec)
865 {
866     if (!rec)
867         return;
868     if (rec->wrbuf_marc)
869         wrbuf_free (rec->wrbuf_marc, 1);
870     odr_destroy (rec->odr);
871     xfree (rec);
872 }
873
874 void *ZOOM_record_get (ZOOM_record rec, const char *type, size_t *len)
875 {
876     Z_NamePlusRecord *npr;
877     if (!rec)
878         return 0;
879     npr = rec->npr;
880     if (!npr)
881         return 0;
882     if (!strcmp (type, "database"))
883     {
884         return npr->databaseName;
885     }
886     else if (!strcmp (type, "syntax"))
887     {
888         if (npr->which == Z_NamePlusRecord_databaseRecord)
889         {
890             Z_External *r = (Z_External *) npr->u.databaseRecord;
891             oident *ent = oid_getentbyoid(r->direct_reference);
892             if (ent)
893                 return ent->desc;
894         }
895         return "none";
896     }
897     else if (!strcmp (type, "render"))
898     {
899         if (npr->which == Z_NamePlusRecord_databaseRecord)
900         {
901             Z_External *r = (Z_External *) npr->u.databaseRecord;
902             oident *ent = oid_getentbyoid(r->direct_reference);
903             
904             if (r->which == Z_External_sutrs)
905             {
906                 *len = r->u.sutrs->len;
907                 return r->u.sutrs->buf;
908             }
909             else if (r->which == Z_External_octet)
910             {
911                 switch (ent->value)
912                 {
913                 case VAL_SOIF:
914                 case VAL_HTML:
915                 case VAL_SUTRS:
916                     break;
917                 case VAL_TEXT_XML:
918                 case VAL_APPLICATION_XML:
919                     break;
920                 default:
921                     if (!rec->wrbuf_marc)
922                         rec->wrbuf_marc = wrbuf_alloc();
923                     wrbuf_rewind (rec->wrbuf_marc);
924                     if (marc_display_wrbuf (r->u.octet_aligned->buf,
925                                         rec->wrbuf_marc, 0,
926                                             r->u.octet_aligned->len) > 0)
927                     {
928                         *len = wrbuf_len(rec->wrbuf_marc);
929                         return wrbuf_buf(rec->wrbuf_marc);
930                     }
931                 }
932                 *len = r->u.octet_aligned->len;
933                 return r->u.octet_aligned->buf;
934             }
935             else if (r->which == Z_External_grs1)
936             {
937                 *len = 5;
938                 return "GRS-1";
939             }
940         }
941         return 0;
942     }
943     else if (!strcmp (type, "raw"))
944     {
945         if (npr->which == Z_NamePlusRecord_databaseRecord)
946         {
947             Z_External *r = (Z_External *) npr->u.databaseRecord;
948             
949             if (r->which == Z_External_sutrs)
950             {
951                 *len = r->u.sutrs->len;
952                 return r->u.sutrs->buf;
953             }
954             else if (r->which == Z_External_octet)
955             {
956                 *len = r->u.octet_aligned->len;
957                 return r->u.octet_aligned->buf;
958             }
959             else /* grs-1, explain, ... */
960             {
961                 *len = -1;
962                 return (Z_External *) npr->u.databaseRecord;
963             }
964         }
965         return 0;
966     }
967     return 0;
968 }
969
970 static void record_cache_add (ZOOM_resultset r,
971                               Z_NamePlusRecord *npr,
972                               int pos,
973                               const char *elementSetName)
974 {
975     ZOOM_record_cache rc;
976
977     for (rc = r->record_cache; rc; rc = rc->next)
978     {
979         if (pos == rc->pos)
980         {
981             if ((!elementSetName && !rc->elementSetName)
982                 || (elementSetName && rc->elementSetName &&
983                     !strcmp (elementSetName, rc->elementSetName)))
984             {
985                 /* not destroying rc->npr (it's handled by nmem )*/
986                 rc->rec.npr = npr;
987                 /* keeping wrbuf_marc too */
988                 return;
989             }
990         }
991     }
992     rc = odr_malloc (r->odr, sizeof(*rc));
993     rc->rec.npr = npr; 
994     rc->rec.odr = 0;
995     rc->rec.wrbuf_marc = 0;
996     if (elementSetName)
997         rc->elementSetName = odr_strdup (r->odr, elementSetName);
998     else
999         rc->elementSetName = 0;
1000     rc->pos = pos;
1001     rc->next = r->record_cache;
1002     r->record_cache = rc;
1003 }
1004
1005 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
1006                                          int pos,
1007                                          const char *elementSetName)
1008 {
1009     ZOOM_record_cache rc;
1010
1011     for (rc = r->record_cache; rc; rc = rc->next)
1012     {
1013         if (pos == rc->pos)
1014         {
1015             if ((!elementSetName && !rc->elementSetName)
1016                 || (elementSetName && rc->elementSetName &&
1017                     !strcmp (elementSetName, rc->elementSetName)))
1018                 return &rc->rec;
1019         }
1020     }
1021     return 0;
1022 }
1023                                              
1024 static void handle_records (ZOOM_connection c, Z_Records *sr,
1025                             int present_phase)
1026 {
1027     ZOOM_resultset resultset;
1028
1029     if (!c->tasks)
1030         return ;
1031     switch (c->tasks->which)
1032     {
1033     case ZOOM_TASK_SEARCH:
1034         resultset = c->tasks->u.search.resultset;
1035         break;
1036     case ZOOM_TASK_RETRIEVE:
1037         resultset = c->tasks->u.retrieve.resultset;        
1038         break;
1039     default:
1040         return;
1041     }
1042     if (sr && sr->which == Z_Records_NSD)
1043     {
1044         Z_DiagRec dr, *dr_p = &dr;
1045         dr.which = Z_DiagRec_defaultFormat;
1046         dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
1047         
1048         response_diag (c, dr_p);
1049     }
1050     else if (sr && sr->which == Z_Records_multipleNSD)
1051     {
1052         if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1053             response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1054         else
1055             c->error = ZOOM_ERROR_DECODE;
1056     }
1057     else 
1058     {
1059         if (resultset->count + resultset->start > resultset->size)
1060             resultset->count = resultset->size - resultset->start;
1061         if (resultset->count < 0)
1062             resultset->count = 0;
1063         if (sr && sr->which == Z_Records_DBOSD)
1064         {
1065             int i;
1066             NMEM nmem = odr_extract_mem (c->odr_in);
1067             Z_NamePlusRecordList *p =
1068                 sr->u.databaseOrSurDiagnostics;
1069             for (i = 0; i<p->num_records; i++)
1070             {
1071                 record_cache_add (resultset, p->records[i],
1072                                   i+ resultset->start, 0);
1073             }
1074             /* transfer our response to search_nmem .. we need it later */
1075             nmem_transfer (resultset->odr->mem, nmem);
1076             nmem_destroy (nmem);
1077             if (present_phase && p->num_records == 0)
1078             {
1079                 /* present response and we didn't get any records! */
1080                 c->error = ZOOM_ERROR_DECODE;
1081             }
1082         }
1083         else if (present_phase)
1084         {
1085             /* present response and we didn't get any records! */
1086             c->error = ZOOM_ERROR_DECODE;
1087         }
1088     }
1089 }
1090
1091 static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
1092 {
1093     handle_records (c, pr->records, 1);
1094 }
1095
1096 static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
1097 {
1098     ZOOM_resultset resultset;
1099
1100     yaz_log (LOG_DEBUG, "got search response");
1101
1102     if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1103         return ;
1104
1105     resultset = c->tasks->u.search.resultset;
1106
1107     resultset->size = *sr->resultCount;
1108     handle_records (c, sr->records, 0);
1109 }
1110
1111 static void sort_response (ZOOM_connection c, Z_SortResponse *res)
1112 {
1113     if (res->diagnostics && res->num_diagnostics > 0)
1114         response_diag (c, res->diagnostics[0]);
1115 }
1116
1117 static int scan_response (ZOOM_connection c, Z_ScanResponse *res)
1118 {
1119     NMEM nmem = odr_extract_mem (c->odr_in);
1120     ZOOM_scanset scan;
1121
1122     if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1123         return 0;
1124     scan = c->tasks->u.scan.scan;
1125
1126     if (res->entries && res->entries->nonsurrogateDiagnostics)
1127         response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1128     scan->scan_response = res;
1129     nmem_transfer (scan->odr->mem, nmem);
1130     if (res->stepSize)
1131         ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize);
1132     if (res->positionOfTerm)
1133         ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm);
1134     if (res->scanStatus)
1135         ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus);
1136     if (res->numberOfEntriesReturned)
1137         ZOOM_options_set_int (scan->options, "number",
1138                               *res->numberOfEntriesReturned);
1139     nmem_destroy (nmem);
1140     return 1;
1141 }
1142
1143 static int send_sort (ZOOM_connection c)
1144 {
1145     ZOOM_resultset  resultset;
1146
1147     if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1148         return 0;
1149
1150     resultset = c->tasks->u.search.resultset;
1151
1152     if (c->error)
1153     {
1154         resultset->r_sort_spec = 0;
1155         return 0;
1156     }
1157     if (resultset->r_sort_spec)
1158     {
1159         Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1160         Z_SortRequest *req = apdu->u.sortRequest;
1161         
1162         req->num_inputResultSetNames = 1;
1163         req->inputResultSetNames = (Z_InternationalString **)
1164             odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1165         req->inputResultSetNames[0] =
1166             odr_strdup (c->odr_out, resultset->setname);
1167         req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname);
1168         req->sortSequence = resultset->r_sort_spec;
1169         resultset->r_sort_spec = 0;
1170         send_APDU (c, apdu);
1171         return 1;
1172     }
1173     return 0;
1174 }
1175
1176 static int send_present (ZOOM_connection c)
1177 {
1178     Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1179     Z_PresentRequest *req = apdu->u.presentRequest;
1180     int i = 0;
1181     const char *syntax = 
1182         ZOOM_options_get (c->options, "preferredRecordSyntax");
1183     const char *element =
1184         ZOOM_options_get (c->options, "elementSetName");
1185     const char *schema =
1186         ZOOM_options_get (c->options, "schema");
1187     ZOOM_resultset  resultset;
1188
1189     if (!c->tasks)
1190         return 0;
1191
1192     switch (c->tasks->which)
1193     {
1194     case ZOOM_TASK_SEARCH:
1195         resultset = c->tasks->u.search.resultset;
1196         break;
1197     case ZOOM_TASK_RETRIEVE:
1198         resultset = c->tasks->u.retrieve.resultset;
1199         resultset->start = c->tasks->u.retrieve.start;
1200         resultset->count = c->tasks->u.retrieve.count;
1201
1202         if (resultset->start >= resultset->size)
1203             return 0;
1204         if (resultset->start + resultset->count > resultset->size)
1205             resultset->count = resultset->size - resultset->start;
1206         break;
1207     default:
1208         return 0;
1209     }
1210
1211     if (c->error)                  /* don't continue on error */
1212         return 0;
1213     if (resultset->start < 0)
1214         return 0;
1215     for (i = 0; i<resultset->count; i++)
1216     {
1217         ZOOM_record rec =
1218             record_cache_lookup (resultset, i + resultset->start, 0);
1219         if (!rec)
1220             break;
1221     }
1222     if (i == resultset->count)
1223         return 0;
1224
1225     resultset->start += i;
1226     resultset->count -= i;
1227     *req->resultSetStartPoint = resultset->start + 1;
1228     *req->numberOfRecordsRequested = resultset->count;
1229     assert (*req->numberOfRecordsRequested > 0);
1230
1231     if (syntax && *syntax)
1232         req->preferredRecordSyntax =
1233             yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1234
1235     if (schema && *schema)
1236     {
1237         Z_RecordComposition *compo = odr_malloc (c->odr_out, sizeof(*compo));
1238
1239         req->recordComposition = compo;
1240         compo->which = Z_RecordComp_complex;
1241         compo->u.complex = (Z_CompSpec *)
1242             odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1243         compo->u.complex->selectAlternativeSyntax = (bool_t *) 
1244             odr_malloc(c->odr_out, sizeof(bool_t));
1245         *compo->u.complex->selectAlternativeSyntax = 0;
1246
1247         compo->u.complex->generic = (Z_Specification *)
1248             odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1249
1250         compo->u.complex->generic->schema = (Odr_oid *)
1251             yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema);
1252
1253         if (!compo->u.complex->generic->schema)
1254         {
1255             /* OID wasn't a schema! Try record syntax instead. */
1256
1257             compo->u.complex->generic->schema = (Odr_oid *)
1258                 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema);
1259         }
1260         if (element && *element)
1261         {
1262             compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1263                 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1264             compo->u.complex->generic->elementSpec->which =
1265                 Z_ElementSpec_elementSetName;
1266             compo->u.complex->generic->elementSpec->u.elementSetName =
1267                 odr_strdup (c->odr_out, element);
1268         }
1269         else
1270             compo->u.complex->generic->elementSpec = 0;
1271         compo->u.complex->num_dbSpecific = 0;
1272         compo->u.complex->dbSpecific = 0;
1273         compo->u.complex->num_recordSyntax = 0;
1274         compo->u.complex->recordSyntax = 0;
1275     }
1276     else if (element && *element)
1277     {
1278         Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
1279         Z_RecordComposition *compo = odr_malloc (c->odr_out, sizeof(*compo));
1280         
1281         esn->which = Z_ElementSetNames_generic;
1282         esn->u.generic = odr_strdup (c->odr_out, element);
1283         compo->which = Z_RecordComp_simple;
1284         compo->u.simple = esn;
1285         req->recordComposition = compo;
1286     }
1287     req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1288     send_APDU (c, apdu);
1289     return 1;
1290 }
1291
1292 ZOOM_scanset ZOOM_connection_scan (ZOOM_connection c, const char *start)
1293 {
1294     ZOOM_scanset scan = xmalloc (sizeof(*scan));
1295
1296     scan->connection = c;
1297     scan->odr = odr_createmem (ODR_DECODE);
1298     scan->options = ZOOM_options_create_with_parent (c->options);
1299     scan->refcount = 1;
1300     scan->scan_response = 0;
1301
1302     if ((scan->termListAndStartPoint =
1303          p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet,
1304                       start)))
1305     {
1306         ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN);
1307         task->u.scan.scan = scan;
1308         
1309         (scan->refcount)++;
1310         if (!c->async)
1311         {
1312             while (ZOOM_event (1, &c))
1313                 ;
1314         }
1315     }
1316     return scan;
1317 }
1318
1319 void ZOOM_scanset_destroy (ZOOM_scanset scan)
1320 {
1321     if (!scan)
1322         return;
1323     (scan->refcount)--;
1324     if (scan->refcount == 0)
1325     {
1326         odr_destroy (scan->odr);
1327         
1328         ZOOM_options_destroy (scan->options);
1329         xfree (scan);
1330     }
1331 }
1332
1333 int send_scan (ZOOM_connection c)
1334 {
1335     ZOOM_scanset scan;
1336     Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
1337     Z_ScanRequest *req = apdu->u.scanRequest;
1338     if (!c->tasks)
1339         return 0;
1340     assert (c->tasks->which == ZOOM_TASK_SCAN);
1341     scan = c->tasks->u.scan.scan;
1342
1343     req->termListAndStartPoint = scan->termListAndStartPoint;
1344     req->attributeSet = scan->attributeSet;
1345
1346     *req->numberOfTermsRequested =
1347         ZOOM_options_get_int(scan->options, "number", 10);
1348
1349     req->preferredPositionInResponse =
1350         odr_intdup (c->odr_out,
1351                     ZOOM_options_get_int(scan->options, "position", 1));
1352
1353     req->stepSize =
1354         odr_intdup (c->odr_out,
1355                     ZOOM_options_get_int(scan->options, "stepSize", 0));
1356     
1357     req->databaseNames = set_DatabaseNames (c, scan->options, 
1358                                             &req->num_databaseNames);
1359
1360     send_APDU (c, apdu);
1361
1362     return 1;
1363 }
1364
1365 size_t ZOOM_scanset_size (ZOOM_scanset scan)
1366 {
1367     if (!scan || !scan->scan_response || !scan->scan_response->entries)
1368         return 0;
1369     return scan->scan_response->entries->num_entries;
1370 }
1371
1372 const char *ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
1373                                int *occ, size_t *len)
1374 {
1375     const char *term = 0;
1376     size_t noent = ZOOM_scanset_size (scan);
1377     Z_ScanResponse *res = scan->scan_response;
1378     
1379     *len = 0;
1380     *occ = 0;
1381     if (pos >= noent)
1382         return 0;
1383     if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1384     {
1385         Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1386         
1387         if (t->term->which == Z_Term_general)
1388         {
1389             term = t->term->u.general->buf;
1390             *len = t->term->u.general->len;
1391         }
1392         *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1393     }
1394     return term;
1395 }
1396
1397 const char *ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key)
1398 {
1399     return ZOOM_options_get (scan->options, key);
1400 }
1401
1402 void ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
1403                               const char *val)
1404 {
1405     ZOOM_options_set (scan->options, key, val);
1406 }
1407
1408 static int ZOOM_connection_exec_task (ZOOM_connection c)
1409 {
1410     ZOOM_task task = c->tasks;
1411
1412     yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task");
1413     if (!task)
1414         return 0;
1415     if (c->error != ZOOM_ERROR_NONE ||
1416         (!c->cs && task->which != ZOOM_TASK_CONNECT))
1417     {
1418         ZOOM_connection_remove_tasks (c);
1419         return 0;
1420     }
1421     yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d", task->which);
1422     if (task->running)
1423         return 0;
1424     task->running = 1;
1425     switch (task->which)
1426     {
1427     case ZOOM_TASK_SEARCH:
1428         /* see if search hasn't been sent yet. */
1429         if (ZOOM_connection_send_search (c))
1430             return 1;
1431         break;
1432     case ZOOM_TASK_RETRIEVE:
1433         if (send_present (c))
1434             return 1;
1435         break;
1436     case ZOOM_TASK_CONNECT:
1437         if (do_connect(c))
1438             return 1;
1439         break;
1440     case ZOOM_TASK_SCAN:
1441         if (send_scan(c))
1442             return 1;
1443     }
1444     ZOOM_connection_remove_task (c);
1445     return 0;
1446 }
1447
1448 static int send_sort_present (ZOOM_connection c)
1449 {
1450     int r = send_sort (c);
1451     if (!r)
1452         r = send_present (c);
1453     return r;
1454 }
1455
1456 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
1457 {
1458     Z_InitResponse *initrs;
1459     
1460     yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1461     c->mask = 0;
1462     switch(apdu->which)
1463     {
1464     case Z_APDU_initResponse:
1465         initrs = apdu->u.initResponse;
1466         if (!*initrs->result)
1467         {
1468             c->error = ZOOM_ERROR_INIT;
1469         }
1470         else
1471         {
1472             char *cookie =
1473                 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1474                                           VAL_COOKIE, 1, 0);
1475             xfree (c->cookie_in);
1476             c->cookie_in = 0;
1477             if (cookie)
1478                 c->cookie_in = xstrdup(cookie);
1479             if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
1480                 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
1481                 c->support_named_resultsets = 1;
1482             if (c->tasks)
1483             {
1484                 assert (c->tasks->which == ZOOM_TASK_CONNECT);
1485                 ZOOM_connection_remove_task (c);
1486             }
1487             ZOOM_connection_exec_task (c);
1488         }
1489         break;
1490     case Z_APDU_searchResponse:
1491         handle_search_response (c, apdu->u.searchResponse);
1492         if (!send_sort_present (c))
1493             ZOOM_connection_remove_task (c);
1494         break;
1495     case Z_APDU_presentResponse:
1496         handle_present_response (c, apdu->u.presentResponse);
1497         if (!send_present (c))
1498             ZOOM_connection_remove_task (c);
1499         break;
1500     case Z_APDU_sortResponse:
1501         sort_response (c, apdu->u.sortResponse);
1502         if (!send_present (c))
1503             ZOOM_connection_remove_task (c);
1504         break;
1505     case Z_APDU_scanResponse:
1506         scan_response (c, apdu->u.scanResponse);
1507         ZOOM_connection_remove_task (c);
1508     }
1509 }
1510
1511 static int do_read (ZOOM_connection c)
1512 {
1513     int r;
1514     Z_APDU *apdu;
1515     ZOOM_Event event;
1516     
1517     event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA);
1518     ZOOM_connection_put_event (c, event);
1519     
1520     r = cs_get (c->cs, &c->buf_in, &c->len_in);
1521     if (r == 1)
1522         return 0;
1523     if (r <= 0)
1524     {
1525         c->error= ZOOM_ERROR_CONNECTION_LOST;
1526         do_close (c);
1527     }
1528     else
1529     {
1530         ZOOM_Event event;
1531         odr_reset (c->odr_in);
1532         odr_setbuf (c->odr_in, c->buf_in, r, 0);
1533         event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU);
1534         ZOOM_connection_put_event (c, event);
1535         if (!z_APDU (c->odr_in, &apdu, 0, 0))
1536         {
1537             c->error = ZOOM_ERROR_DECODE;
1538             do_close (c);
1539         }
1540         else
1541         {
1542             handle_apdu (c, apdu);
1543         }
1544     }
1545     return 1;
1546 }
1547
1548 static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
1549 {
1550     int r;
1551     ZOOM_Event event;
1552     
1553     event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
1554     ZOOM_connection_put_event (c, event);
1555
1556     if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
1557     {
1558         if (c->state == STATE_CONNECTING)
1559             c->error = ZOOM_ERROR_CONNECT;
1560         else
1561             c->error = ZOOM_ERROR_CONNECTION_LOST;
1562         do_close (c);
1563         return 1;
1564     }
1565     else if (r == 1)
1566     {    
1567         c->mask = ZOOM_SELECT_EXCEPT;
1568         if (c->cs->io_pending & CS_WANT_WRITE)
1569             c->mask += ZOOM_SELECT_WRITE;
1570         if (c->cs->io_pending & CS_WANT_READ)
1571             c->mask += ZOOM_SELECT_READ;
1572         yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
1573     }
1574     else
1575     {
1576         c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
1577         yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
1578     }
1579     return 0;
1580 }
1581
1582 static int do_write(ZOOM_connection c)
1583 {
1584     return do_write_ex (c, c->buf_out, c->len_out);
1585 }
1586
1587
1588 const char *ZOOM_connection_option_get (ZOOM_connection c, const char *key)
1589 {
1590     return ZOOM_options_get (c->options, key);
1591 }
1592
1593 void ZOOM_connection_option_set (ZOOM_connection c, const char *key,
1594                                   const char *val)
1595 {
1596     ZOOM_options_set (c->options, key, val);
1597 }
1598
1599 const char *ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
1600 {
1601     return ZOOM_options_get (r->options, key);
1602 }
1603
1604 void ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
1605                                   const char *val)
1606 {
1607     ZOOM_options_set (r->options, key, val);
1608 }
1609
1610
1611 int ZOOM_connection_errcode (ZOOM_connection c)
1612 {
1613     return ZOOM_connection_error (c, 0, 0);
1614 }
1615
1616 const char *ZOOM_connection_errmsg (ZOOM_connection c)
1617 {
1618     const char *msg;
1619     ZOOM_connection_error (c, &msg, 0);
1620     return msg;
1621 }
1622
1623 const char *ZOOM_connection_addinfo (ZOOM_connection c)
1624 {
1625     const char *addinfo;
1626     ZOOM_connection_error (c, 0, &addinfo);
1627     return addinfo;
1628 }
1629
1630 int ZOOM_connection_error (ZOOM_connection c, const char **cp,
1631                             const char **addinfo)
1632 {
1633     int error = c->error;
1634     if (cp)
1635     {
1636         switch (error)
1637         {
1638         case ZOOM_ERROR_NONE:
1639             *cp = "No error"; break;
1640         case ZOOM_ERROR_CONNECT:
1641             *cp = "Connect failed"; break;
1642         case ZOOM_ERROR_MEMORY:
1643             *cp = "Out of memory"; break;
1644         case ZOOM_ERROR_ENCODE:
1645             *cp = "Encoding failed"; break;
1646         case ZOOM_ERROR_DECODE:
1647             *cp = "Decoding failed"; break;
1648         case ZOOM_ERROR_CONNECTION_LOST:
1649             *cp = "Connection lost"; break;
1650         case ZOOM_ERROR_INIT:
1651             *cp = "Init rejected"; break;
1652         case ZOOM_ERROR_INTERNAL:
1653             *cp = "Internal failure"; break;
1654         case ZOOM_ERROR_TIMEOUT:
1655             *cp = "Timeout"; break;
1656         default:
1657             *cp = diagbib1_str (error);
1658         }
1659     }
1660     if (addinfo)
1661     {
1662         if (c->addinfo)
1663             *addinfo = c->addinfo;
1664         else
1665             *addinfo = "";
1666     }
1667     return c->error;
1668 }
1669
1670 int ZOOM_connection_do_io(ZOOM_connection c, int mask)
1671 {
1672     ZOOM_Event event = 0;
1673     int r = cs_look(c->cs);
1674     yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
1675              c, mask, r);
1676     
1677     if (r == CS_NONE)
1678     {
1679         event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
1680         c->error = ZOOM_ERROR_CONNECT;
1681         do_close (c);
1682         ZOOM_connection_put_event (c, event);
1683     }
1684     else if (r == CS_CONNECT)
1685     {
1686         int ret;
1687         event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
1688
1689         ret = cs_rcvconnect (c->cs);
1690         yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
1691         if (ret == 1)
1692         {
1693             c->mask = ZOOM_SELECT_EXCEPT;
1694             if (c->cs->io_pending & CS_WANT_WRITE)
1695                 c->mask += ZOOM_SELECT_WRITE;
1696             if (c->cs->io_pending & CS_WANT_READ)
1697                 c->mask += ZOOM_SELECT_READ;
1698             ZOOM_connection_put_event (c, event);
1699         }
1700         else if (ret == 0)
1701         {
1702             ZOOM_connection_put_event (c, event);
1703             ZOOM_connection_send_init (c);
1704             c->state = STATE_ESTABLISHED;
1705         }
1706         else
1707         {
1708             c->error = ZOOM_ERROR_CONNECT;
1709             do_close (c);
1710             ZOOM_connection_put_event (c, event);
1711         }
1712     }
1713     else
1714     {
1715         if (mask & ZOOM_SELECT_READ)
1716             do_read (c);
1717         if (c->cs && (mask & ZOOM_SELECT_WRITE))
1718             do_write (c);
1719     }
1720     return 1;
1721 }
1722
1723 int ZOOM_connection_last_event(ZOOM_connection cs)
1724 {
1725     if (!cs)
1726         return ZOOM_EVENT_NONE;
1727     return cs->last_event;
1728 }
1729
1730 int ZOOM_event (int no, ZOOM_connection *cs)
1731 {
1732 #if HAVE_SYS_POLL_H
1733     struct pollfd pollfds[1024];
1734     ZOOM_connection poll_cs[1024];
1735 #else
1736     struct timeval tv;
1737     fd_set input, output, except;
1738 #endif
1739     int i, r, nfds;
1740     int max_fd = 0;
1741
1742     for (i = 0; i<no; i++)
1743     {
1744         ZOOM_connection c = cs[i];
1745         ZOOM_Event event;
1746         if (c && (event = ZOOM_connection_get_event(c)))
1747         {
1748             ZOOM_Event_destroy (event);
1749             return i+1;
1750         }
1751     }
1752     for (i = 0; i<no; i++)
1753     {
1754         ZOOM_connection c = cs[i];
1755         ZOOM_Event event;
1756         if (c && ZOOM_connection_exec_task (c))
1757         {
1758             if ((event = ZOOM_connection_get_event(c)))
1759             {
1760                 ZOOM_Event_destroy (event);
1761                 return i+1;
1762             }
1763         }
1764     }
1765 #if HAVE_SYS_POLL_H
1766
1767 #else
1768     tv.tv_sec = 15;
1769     tv.tv_usec = 0;
1770     
1771     FD_ZERO (&input);
1772     FD_ZERO (&output);
1773     FD_ZERO (&except);
1774 #endif
1775     nfds = 0;
1776     for (i = 0; i<no; i++)
1777     {
1778         ZOOM_connection c = cs[i];
1779         int fd, mask;
1780         
1781         if (!c)
1782             continue;
1783         fd = z3950_connection_socket(c);
1784         mask = z3950_connection_mask(c);
1785
1786         if (fd == -1)
1787             continue;
1788         if (max_fd < fd)
1789             max_fd = fd;
1790
1791 #if HAVE_SYS_POLL_H
1792         if (mask)
1793         {
1794             short poll_events = 0;
1795
1796             if (mask & ZOOM_SELECT_READ)
1797                 poll_events += POLLIN;
1798             if (mask & ZOOM_SELECT_WRITE)
1799                 poll_events += POLLOUT;
1800             if (mask & ZOOM_SELECT_EXCEPT)
1801                 poll_events += POLLERR;
1802             pollfds[nfds].fd = fd;
1803             pollfds[nfds].events = poll_events;
1804             pollfds[nfds].revents = 0;
1805             poll_cs[nfds] = c;
1806             nfds++;
1807         }
1808 #else
1809         if (mask & ZOOM_SELECT_READ)
1810         {
1811             FD_SET (fd, &input);
1812             nfds++;
1813         }
1814         if (mask & ZOOM_SELECT_WRITE)
1815         {
1816             FD_SET (fd, &output);
1817             nfds++;
1818         }
1819         if (mask & ZOOM_SELECT_EXCEPT)
1820         {
1821             FD_SET (fd, &except);
1822             nfds++;
1823         }
1824 #endif
1825     }
1826     if (!nfds)
1827         return 0;
1828 #if HAVE_SYS_POLL_H
1829     r = poll (pollfds, nfds, 15000);
1830     for (i = 0; i<nfds; i++)
1831     {
1832         ZOOM_connection c = poll_cs[i];
1833         if (r && c->mask)
1834         {
1835             int mask = 0;
1836             if (pollfds[i].revents & POLLIN)
1837                 mask += ZOOM_SELECT_READ;
1838             if (pollfds[i].revents & POLLOUT)
1839                 mask += ZOOM_SELECT_WRITE;
1840             if (pollfds[i].revents & POLLERR)
1841                 mask += ZOOM_SELECT_EXCEPT;
1842             if (mask)
1843                 ZOOM_connection_do_io(c, mask);
1844         }
1845         else if (r == 0 && c->mask)
1846         {
1847             ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
1848             /* timeout and this connection was waiting */
1849             c->error = ZOOM_ERROR_TIMEOUT;
1850             do_close (c);
1851             ZOOM_connection_put_event(c, event);
1852         }
1853     }
1854 #else
1855     yaz_log (LOG_DEBUG, "select start");
1856     r = select (max_fd+1, &input, &output, &except, &tv);
1857     yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
1858     for (i = 0; i<no; i++)
1859     {
1860         ZOOM_connection c = cs[i];
1861         int fd, mask;
1862
1863         if (!c)
1864             continue;
1865         fd = z3950_connection_socket(c);
1866         mask = 0;
1867         if (r && c->mask)
1868         {
1869             /* no timeout and real socket */
1870             if (FD_ISSET(fd, &input))
1871                 mask += ZOOM_SELECT_READ;
1872             if (FD_ISSET(fd, &output))
1873                 mask += ZOOM_SELECT_WRITE;
1874             if (FD_ISSET(fd, &except))
1875                 mask += ZOOM_SELECT_EXCEPT;
1876             if (mask)
1877                 ZOOM_connection_do_io(c, mask);
1878         }
1879         if (r == 0 && c->mask)
1880         {
1881             ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
1882             /* timeout and this connection was waiting */
1883             c->error = ZOOM_ERROR_TIMEOUT;
1884             do_close (c);
1885             yaz_log (LOG_DEBUG, "timeout");
1886             ZOOM_connection_put_event(c, event);
1887         }
1888     }
1889 #endif
1890     for (i = 0; i<no; i++)
1891     {
1892         ZOOM_connection c = cs[i];
1893         ZOOM_Event event;
1894         if (c && (event = ZOOM_connection_get_event(c)))
1895         {
1896             ZOOM_Event_destroy (event);
1897             return i+1;
1898         }
1899     }
1900     return 0;
1901 }