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