From: Adam Dickmeiss Date: Mon, 16 Sep 2002 18:45:13 +0000 (+0000) Subject: Moved zoom implementation to zutil. Man page for zoomsh X-Git-Tag: YAZ.1.9.1~34 X-Git-Url: http://sru.miketaylor.org.uk/cgi-bin?a=commitdiff_plain;h=141269ce5f566fcd7ed188ebe9bb878bf4b32018;p=yaz-moved-to-github.git Moved zoom implementation to zutil. Man page for zoomsh --- diff --git a/Makefile.am b/Makefile.am index 7c8b583..86477e2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,10 +1,10 @@ -## $Id: Makefile.am,v 1.14 2002-06-02 21:34:45 adam Exp $ +## $Id: Makefile.am,v 1.15 2002-09-16 18:45:13 adam Exp $ AUTOMAKE_OPTIONS = foreign -DIST_SUBDIRS=util odr z39.50 ill zutil comstack ccl tab retrieval server lib zoom include client ztest doc +DIST_SUBDIRS=util odr z39.50 ill zutil comstack ccl tab retrieval server lib include client ztest zoom doc -SUBDIRS = util odr comstack z39.50 ill zutil ccl zoom tab retrieval server include lib client ztest +SUBDIRS = util odr comstack z39.50 ill zutil ccl tab retrieval server include lib client ztest zoom aclocaldir=$(datadir)/aclocal diff --git a/configure.in b/configure.in index cf5c14d..c5d35b0 100644 --- a/configure.in +++ b/configure.in @@ -1,6 +1,6 @@ dnl YAZ Toolkit, Index Data 1994-2002 dnl See the file LICENSE for details. -dnl $Id: configure.in,v 1.88 2002-09-16 14:16:31 adam Exp $ +dnl $Id: configure.in,v 1.89 2002-09-16 18:45:13 adam Exp $ AC_INIT(include/yaz/yaz-version.h) AM_INIT_AUTOMAKE(yaz, 1.9.1) dnl @@ -328,9 +328,9 @@ server/Makefile include/Makefile include/yaz/Makefile lib/Makefile -zoom/Makefile client/Makefile ztest/Makefile +zoom/Makefile doc/Makefile doc/yaz.xml doc/yazhtml.dsl diff --git a/debian/rules b/debian/rules index b8c2d8c..90573eb 100755 --- a/debian/rules +++ b/debian/rules @@ -84,7 +84,7 @@ stamp-install: stamp-build binary-indep: build install dh_testdir dh_testroot - dh_installdocs -p yaz-doc doc/ + dh_installdocs -p yaz-doc doc/*.html doc/*.pdf dh_installchangelogs -p yaz-doc dh_link -p yaz-doc dh_strip -p yaz-doc @@ -118,8 +118,8 @@ endif dh_installdeb -p yaz-runtime dh_installdeb -p yaz-devel - dh_installman -p yaz-runtime doc/yaz-client.1 doc/yaz-ztest.8 doc/yaz-config.1 doc/yaz.7 - dh_undocumented -p yaz-runtime yaz-comp.1 zoomsh.1 + dh_installman -p yaz-runtime doc/yaz-client.1 doc/yaz-ztest.8 doc/yaz-config.1 doc/yaz.7 doc/zoomsh.1 + dh_undocumented -p yaz-runtime yaz-comp.1 echo "libyaz 1 yaz-runtime" > debian/yaz-runtime/DEBIAN/shlibs echo "libyazthread 1 yaz-runtime" >> debian/yaz-runtime/DEBIAN/shlibs echo "libyazmalloc 1 yaz-runtime" >> debian/yaz-runtime/DEBIAN/shlibs diff --git a/doc/Makefile.am b/doc/Makefile.am index f1a3a19..15a2f59 100644 --- a/doc/Makefile.am +++ b/doc/Makefile.am @@ -1,4 +1,4 @@ -## $Id: Makefile.am,v 1.32 2002-09-16 14:16:31 adam Exp $ +## $Id: Makefile.am,v 1.33 2002-09-16 18:45:13 adam Exp $ docdir=$(datadir)/doc/@PACKAGE@ @@ -27,8 +27,9 @@ HTMLFILES = asn.external.html asn.html asn.oid.html asn.pdu.html \ DOCFILES=$(HTMLFILES) yaz.pdf MANFILES=yaz-client.1 yaz-client-ssl.1 yaz-ztest.8 \ - yaz-ztest-ssl.8 yaz-config.1 yaz.7 -REFFILES=yaz-client.sgml yaz-ztest.sgml yaz-config.sgml yaz-man.sgml + yaz-ztest-ssl.8 yaz-config.1 yaz.7 zoomsh.1 +REFFILES=yaz-client.sgml yaz-ztest.sgml yaz-config.sgml \ + yaz-man.sgml zoomsh.sgml SUPPORTFILES=yazhtml.dsl yazphp.dsl yazprint.dsl xml.dcl id.eps id.png @@ -49,6 +50,9 @@ yaz-config.1: yaz-config.sgml yaz.7: yaz-man.sgml cd $(srcdir); docbook2man yaz-man.sgml +zoomsh.1: zoomsh.sgml + cd $(srcdir); docbook2man zoomsh.sgml + yaz-ztest-ssl.8: yaz-ztest.8 ln -s yaz-ztest.8 yaz-ztest-ssl.8 diff --git a/doc/yaz-man.sgml b/doc/yaz-man.sgml index 8f5bdce..0bb8517 100644 --- a/doc/yaz-man.sgml +++ b/doc/yaz-man.sgml @@ -1,5 +1,5 @@ - + yaz @@ -64,7 +64,8 @@ SEE ALSO yaz-client(1), yaz-ztest(8), - yaz-config(1) + yaz-config(1), + zoomsh(1) YAZ manual ( prefix/share/yaz/doc) diff --git a/doc/zoomsh.sgml b/doc/zoomsh.sgml new file mode 100644 index 0000000..4d56535 --- /dev/null +++ b/doc/zoomsh.sgml @@ -0,0 +1,152 @@ + +]> + + + + + zoomsh + 1 + + + + zoomsh + ZOOM shell + + + + + zoomsh + commands + + + DESCRIPTION + + zoomsh is a ZOOM client with a simple + command line interface. The client demonstrates the ZOOM + API and is useful for testing targets. + + + You may parse one or more commands to zoomsh. + These commands are invoked first. + + + EXAMPLES + + If you start the yaz-ztest in one console you + can use the ZOOM shell as follows: + connect localhost:9999 +ZOOM>search computer +localhost:9999: 7 hits +ZOOM>show 0 1 +1 Default USmarc +001 11224466 +003 DLC +005 00000000000000.0 +008 910710c19910701nju 00010 eng +010 $a 11224466 +040 $a DLC $c DLC +050 00 $a 123-xyz +100 10 $a Jack Collins +245 10 $a How to program a computer +260 1 $a Penguin +263 $a 8710 +300 $a p. cm. +ZOOM>quit +]]> + + + + You can also achieve the same result by passing the commands + as arguments on a single command line: + + + $ zoomsh "connect localhost:9999" "search computer" + "show 0 1" quit + + + COMMANDS + + + connect + zurl + + Connects to the target given by zurl. + + + close + [zurl] + + Closes connection to target given by + zurl or all targets if + zurl was omitted. + + + show + [start + [count]] + + + Displays count records starting at offset given + by start. First records has offset 0 + (unlike the Z39.50 protocol). + + + quit + + + Quits zoomsh. + + + set + name + [value] + + + Sets option name to + value. + + + get + name + + + Prints value of option name. + + + help + + + Prints list of available commands. + + + + + + SEE ALSO + yaz(7), yaz-ztest(8) + + Section "Building clients with ZOOM" in the YAZ manual. + + + ZOOM home page. + + + + + diff --git a/lib/Makefile.am b/lib/Makefile.am index 94c371e..1965843 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -1,4 +1,4 @@ -## $Id: Makefile.am,v 1.22 2002-06-02 21:34:45 adam Exp $ +## $Id: Makefile.am,v 1.23 2002-09-16 18:45:13 adam Exp $ if ISTHR extra=libyazthread.la @@ -76,5 +76,5 @@ libyaz_la_LIBADD=../odr/libodr.la \ ../comstack/libcomstack.la ../server/libserver.la \ ../util/libutil.la ../retrieval/libret.la \ ../ccl/libccl.la ../zutil/libzutil.la \ - ../zoom/libzoom.la ../ill/libill.la ../z39.50/libz39.50.la + ../ill/libill.la ../z39.50/libz39.50.la diff --git a/win/yaz.nsi b/win/yaz.nsi index 98939e8..b5ef142 100644 --- a/win/yaz.nsi +++ b/win/yaz.nsi @@ -1,4 +1,4 @@ -; $Id: yaz.nsi,v 1.12 2002-09-10 18:43:03 adam Exp $ +; $Id: yaz.nsi,v 1.13 2002-09-16 18:45:14 adam Exp $ !define VERSION "1.9.1" @@ -107,11 +107,11 @@ Section "YAZ Source" File ..\ill\*.asn SetOutPath $INSTDIR\zutil File ..\zutil\*.c + File ..\zutil\*.h SetOutPath $INSTDIR\ccl File ..\ccl\*.c SetOutPath $INSTDIR\zoom File ..\zoom\*.c - File ..\zoom\*.h SetOutPath $INSTDIR\comstack File ..\comstack\*.c SetOutPath $INSTDIR\server diff --git a/zoom/Makefile.am b/zoom/Makefile.am index 24e7f4c..d656356 100644 --- a/zoom/Makefile.am +++ b/zoom/Makefile.am @@ -1,25 +1,21 @@ -## $Id: Makefile.am,v 1.4 2002-06-02 21:27:17 adam Exp $ +## $Id: Makefile.am,v 1.5 2002-09-16 18:45:14 adam Exp $ ## Copyright (C) 2001, Index Data AM_CPPFLAGS = -I$(top_srcdir)/include -noinst_LTLIBRARIES = libzoom.la -libzoom_la_SOURCES = zoom-opt.c zoom-c.c zoom-p.h - -zoomlibs = libzoom.la ../comstack/libcomstack.la ../z39.50/libz39.50.la ../ill/libill.la ../zutil/libzutil.la ../odr/libodr.la ../util/libutil.la $(READLINE_LIBS) - -zoomsh_LDADD = $(zoomlibs) -zoomtst1_LDADD = $(zoomlibs) -zoomtst2_LDADD = $(zoomlibs) -zoomtst3_LDADD = $(zoomlibs) -zoomtst4_LDADD = $(zoomlibs) -zoomtst5_LDADD = $(zoomlibs) -zoomtst6_LDADD = $(zoomlibs) -zoomtst7_LDADD = $(zoomlibs) -zoomtst8_LDADD = $(zoomlibs) noinst_PROGRAMS = zoomtst1 zoomtst2 zoomtst3 zoomtst4 zoomtst5 zoomtst6 zoomtst7 zoomtst8 bin_PROGRAMS = zoomsh +zoomtst1_LDADD = ../lib/libyaz.la +zoomtst2_LDADD = ../lib/libyaz.la +zoomtst3_LDADD = ../lib/libyaz.la +zoomtst4_LDADD = ../lib/libyaz.la +zoomtst5_LDADD = ../lib/libyaz.la +zoomtst6_LDADD = ../lib/libyaz.la +zoomtst7_LDADD = ../lib/libyaz.la +zoomtst8_LDADD = ../lib/libyaz.la +zoomsh_LDADD = ../lib/libyaz.la $(READLINE_LIBS) + zoomtst1_SOURCES = zoomtst1.c zoomtst2_SOURCES = zoomtst2.c zoomtst3_SOURCES = zoomtst3.c diff --git a/zoom/zoom-c.c b/zoom/zoom-c.c deleted file mode 100644 index e081199..0000000 --- a/zoom/zoom-c.c +++ /dev/null @@ -1,2504 +0,0 @@ -/* - * $Id: zoom-c.c,v 1.44 2002-09-13 08:57:03 adam Exp $ - * - * ZOOM layer for C, connections, result sets, queries. - */ -#include -#include -#include -#include -#include -#include -#include -#include - -#include "zoom-p.h" - -#if HAVE_SYS_POLL_H -#include -#endif - -static int ZOOM_connection_send_init (ZOOM_connection c); -static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out); - -static ZOOM_Event ZOOM_Event_create (int kind) -{ - ZOOM_Event event = (ZOOM_Event) xmalloc (sizeof(*event)); - event->kind = kind; - event->next = 0; - event->prev = 0; - return event; -} - -static void ZOOM_Event_destroy (ZOOM_Event event) -{ - xfree (event); -} - -static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event) -{ - if (c->m_queue_back) - { - c->m_queue_back->prev = event; - assert (c->m_queue_front); - } - else - { - assert (!c->m_queue_front); - c->m_queue_front = event; - } - event->next = c->m_queue_back; - event->prev = 0; - c->m_queue_back = event; -} - -static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c) -{ - ZOOM_Event event = c->m_queue_front; - if (!event) - return 0; - assert (c->m_queue_back); - c->m_queue_front = event->prev; - if (c->m_queue_front) - { - assert (c->m_queue_back); - c->m_queue_front->next = 0; - } - else - c->m_queue_back = 0; - c->last_event = event->kind; - return event; -} - -static void clear_error (ZOOM_connection c) -{ - - switch (c->error) - { - case ZOOM_ERROR_CONNECT: - case ZOOM_ERROR_MEMORY: - case ZOOM_ERROR_DECODE: - case ZOOM_ERROR_CONNECTION_LOST: - case ZOOM_ERROR_INIT: - case ZOOM_ERROR_INTERNAL: - break; - default: - c->error = ZOOM_ERROR_NONE; - xfree (c->addinfo); - c->addinfo = 0; - } -} - -ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which) -{ - ZOOM_task *taskp = &c->tasks; - while (*taskp) - taskp = &(*taskp)->next; - *taskp = (ZOOM_task) xmalloc (sizeof(**taskp)); - (*taskp)->running = 0; - (*taskp)->which = which; - (*taskp)->next = 0; - clear_error (c); - return *taskp; -} - -ZOOM_task ZOOM_connection_insert_task (ZOOM_connection c, int which) -{ - ZOOM_task task = (ZOOM_task) xmalloc (sizeof(*task)); - - task->next = c->tasks; - c->tasks = task; - - task->running = 0; - task->which = which; - clear_error (c); - return task; -} - -void ZOOM_connection_remove_task (ZOOM_connection c) -{ - ZOOM_task task = c->tasks; - - if (task) - { - c->tasks = task->next; - switch (task->which) - { - case ZOOM_TASK_SEARCH: - ZOOM_resultset_destroy (task->u.search.resultset); - break; - case ZOOM_TASK_RETRIEVE: - ZOOM_resultset_destroy (task->u.retrieve.resultset); - break; - case ZOOM_TASK_CONNECT: - break; - case ZOOM_TASK_SCAN: - ZOOM_scanset_destroy (task->u.scan.scan); - break; - case ZOOM_TASK_PACKAGE: - ZOOM_package_destroy (task->u.package); - break; - default: - assert (0); - } - xfree (task); - } -} - -void ZOOM_connection_remove_tasks (ZOOM_connection c) -{ - while (c->tasks) - ZOOM_connection_remove_task(c); -} - -static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos); - -ZOOM_API(ZOOM_connection) -ZOOM_connection_create (ZOOM_options options) -{ - ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c)); - - c->cs = 0; - c->mask = 0; - c->reconnect_ok = 0; - c->state = STATE_IDLE; - c->error = ZOOM_ERROR_NONE; - c->addinfo = 0; - c->buf_in = 0; - c->len_in = 0; - c->buf_out = 0; - c->len_out = 0; - c->resultsets = 0; - - c->options = ZOOM_options_create_with_parent(options); - - c->host_port = 0; - c->proxy = 0; - - c->charset = c->lang = 0; - - c->cookie_out = 0; - c->cookie_in = 0; - c->client_IP = 0; - c->tasks = 0; - - c->odr_in = odr_createmem (ODR_DECODE); - c->odr_out = odr_createmem (ODR_ENCODE); - - c->async = 0; - c->support_named_resultsets = 0; - c->last_event = ZOOM_EVENT_NONE; - - c->m_queue_front = 0; - c->m_queue_back = 0; - return c; -} - -/* set database names. Take local databases (if set); otherwise - take databases given in ZURL (if set); otherwise use Default */ -static char **set_DatabaseNames (ZOOM_connection con, ZOOM_options options, - int *num) -{ - char **databaseNames; - const char *c; - int no = 2; - const char *cp = ZOOM_options_get (options, "databaseName"); - - if (!cp || !*cp) - { - if (strncmp (con->host_port, "unix:", 5) == 0) - cp = strchr (con->host_port+5, ':'); - else - cp = strchr (con->host_port, '/'); - if (cp) - cp++; - } - if (cp) - { - c = cp; - while ((c = strchr(c, '+'))) - { - c++; - no++; - } - } - else - cp = "Default"; - databaseNames = (char**) - odr_malloc (con->odr_out, no * sizeof(*databaseNames)); - no = 0; - while (*cp) - { - c = strchr (cp, '+'); - if (!c) - c = cp + strlen(cp); - else if (c == cp) - { - cp++; - continue; - } - /* cp ptr to first char of db name, c is char - following db name */ - databaseNames[no] = (char*) odr_malloc (con->odr_out, 1+c-cp); - memcpy (databaseNames[no], cp, c-cp); - databaseNames[no++][c-cp] = '\0'; - cp = c; - if (*cp) - cp++; - } - databaseNames[no] = NULL; - *num = no; - return databaseNames; -} - -ZOOM_API(ZOOM_connection) -ZOOM_connection_new (const char *host, int portnum) -{ - ZOOM_connection c = ZOOM_connection_create (0); - - ZOOM_connection_connect (c, host, portnum); - return c; -} - -ZOOM_API(void) -ZOOM_connection_connect(ZOOM_connection c, - const char *host, int portnum) -{ - const char *val; - ZOOM_task task; - - if (c->cs) - { - yaz_log (LOG_DEBUG, "reconnect"); - c->reconnect_ok = 1; - return; - } - yaz_log(LOG_DEBUG, "connect"); - xfree (c->proxy); - val = ZOOM_options_get (c->options, "proxy"); - if (val && *val) - c->proxy = xstrdup (val); - else - c->proxy = 0; - - xfree (c->charset); - val = ZOOM_options_get (c->options, "charset"); - if (val && *val) - c->charset = xstrdup (val); - else - c->charset = 0; - - xfree (c->lang); - val = ZOOM_options_get (c->options, "lang"); - if (val && *val) - c->lang = xstrdup (val); - else - c->lang = 0; - - xfree (c->host_port); - if (portnum) - { - char hostn[128]; - sprintf (hostn, "%.80s:%d", host, portnum); - c->host_port = xstrdup(hostn); - } - else - c->host_port = xstrdup(host); - - ZOOM_options_set(c->options, "host", c->host_port); - - val = ZOOM_options_get (c->options, "cookie"); - if (val && *val) - c->cookie_out = xstrdup (val); - - val = ZOOM_options_get (c->options, "clientIP"); - if (val && *val) - c->client_IP = xstrdup (val); - - c->async = ZOOM_options_get_bool (c->options, "async", 0); - - c->error = ZOOM_ERROR_NONE; - - task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT); - - if (!c->async) - { - while (ZOOM_event (1, &c)) - ; - } -} - -ZOOM_API(ZOOM_query) -ZOOM_query_create(void) -{ - ZOOM_query s = (ZOOM_query) xmalloc (sizeof(*s)); - - s->refcount = 1; - s->query = 0; - s->sort_spec = 0; - s->odr = odr_createmem (ODR_ENCODE); - - return s; -} - -ZOOM_API(void) -ZOOM_query_destroy(ZOOM_query s) -{ - if (!s) - return; - - (s->refcount)--; - yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount); - if (s->refcount == 0) - { - odr_destroy (s->odr); - xfree (s); - } -} - -ZOOM_API(int) -ZOOM_query_prefix(ZOOM_query s, const char *str) -{ - s->query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->query)); - s->query->which = Z_Query_type_1; - s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str); - if (!s->query->u.type_1) - return -1; - return 0; -} - -ZOOM_API(int) -ZOOM_query_sortby(ZOOM_query s, const char *criteria) -{ - s->sort_spec = yaz_sort_spec (s->odr, criteria); - if (!s->sort_spec) - return -1; - return 0; -} - -static int do_write(ZOOM_connection c); - -ZOOM_API(void) -ZOOM_connection_destroy(ZOOM_connection c) -{ - ZOOM_resultset r; - if (!c) - return; - if (c->cs) - cs_close (c->cs); - for (r = c->resultsets; r; r = r->next) - r->connection = 0; - - xfree (c->buf_in); - xfree (c->addinfo); - odr_destroy (c->odr_in); - odr_destroy (c->odr_out); - ZOOM_options_destroy (c->options); - ZOOM_connection_remove_tasks (c); - xfree (c->host_port); - xfree (c->proxy); - xfree (c->charset); - xfree (c->lang); - xfree (c->cookie_out); - xfree (c->cookie_in); - xfree (c->client_IP); - xfree (c); -} - -void ZOOM_resultset_addref (ZOOM_resultset r) -{ - if (r) - (r->refcount)++; -} -ZOOM_resultset ZOOM_resultset_create () -{ - ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r)); - - r->refcount = 1; - r->size = 0; - r->odr = odr_createmem (ODR_ENCODE); - r->start = 0; - r->piggyback = 1; - r->setname = 0; - r->count = 0; - r->record_cache = 0; - r->r_sort_spec = 0; - r->r_query = 0; - r->search = 0; - r->connection = 0; - r->next = 0; - return r; -} - -ZOOM_API(ZOOM_resultset) -ZOOM_connection_search_pqf(ZOOM_connection c, const char *q) -{ - ZOOM_resultset r; - ZOOM_query s = ZOOM_query_create(); - - ZOOM_query_prefix (s, q); - - r = ZOOM_connection_search (c, s); - ZOOM_query_destroy (s); - return r; -} - -ZOOM_API(ZOOM_resultset) -ZOOM_connection_search(ZOOM_connection c, ZOOM_query q) -{ - ZOOM_resultset r = ZOOM_resultset_create (); - ZOOM_task task; - const char *cp; - - r->r_sort_spec = q->sort_spec; - r->r_query = q->query; - r->search = q; - - r->options = ZOOM_options_create_with_parent(c->options); - - r->start = ZOOM_options_get_int(r->options, "start", 0); - r->count = ZOOM_options_get_int(r->options, "count", 0); - r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1); - cp = ZOOM_options_get (r->options, "setname"); - if (cp) - r->setname = xstrdup (cp); - - r->connection = c; - - r->next = c->resultsets; - c->resultsets = r; - - task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH); - task->u.search.resultset = r; - ZOOM_resultset_addref (r); - - (q->refcount)++; - - if (!c->async) - { - while (ZOOM_event (1, &c)) - ; - } - return r; -} - -ZOOM_API(void) -ZOOM_resultset_destroy(ZOOM_resultset r) -{ - if (!r) - return; - (r->refcount)--; - yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount); - if (r->refcount == 0) - { - ZOOM_record_cache rc; - - for (rc = r->record_cache; rc; rc = rc->next) - if (rc->rec.wrbuf_marc) - wrbuf_free (rc->rec.wrbuf_marc, 1); - if (r->connection) - { - /* remove ourselves from the resultsets in connection */ - ZOOM_resultset *rp = &r->connection->resultsets; - while (1) - { - assert (*rp); /* we must be in this list!! */ - if (*rp == r) - { /* OK, we're here - take us out of it */ - *rp = (*rp)->next; - break; - } - rp = &(*rp)->next; - } - } - ZOOM_query_destroy (r->search); - ZOOM_options_destroy (r->options); - odr_destroy (r->odr); - xfree (r->setname); - xfree (r); - } -} - -ZOOM_API(size_t) -ZOOM_resultset_size (ZOOM_resultset r) -{ - return r->size; -} - -static void do_close (ZOOM_connection c) -{ - if (c->cs) - cs_close(c->cs); - c->cs = 0; - c->mask = 0; - c->state = STATE_IDLE; -} - -static void ZOOM_resultset_retrieve (ZOOM_resultset r, - int force_sync, int start, int count) -{ - ZOOM_task task; - ZOOM_connection c; - - if (!r) - return; - c = r->connection; - if (!c) - return; - task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE); - task->u.retrieve.resultset = r; - task->u.retrieve.start = start; - task->u.retrieve.count = count; - - ZOOM_resultset_addref (r); - - if (!r->connection->async || force_sync) - while (r->connection && ZOOM_event (1, &r->connection)) - ; -} - -ZOOM_API(void) -ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs, - size_t start, size_t count) -{ - int force_present = 0; - - if (!r) - return ; - if (count && recs) - force_present = 1; - ZOOM_resultset_retrieve (r, force_present, start, count); - if (force_present) - { - size_t i; - for (i = 0; i< count; i++) - recs[i] = ZOOM_resultset_record_immediate (r, i+start); - } -} - -static int do_connect (ZOOM_connection c) -{ - void *add; - const char *effective_host; - - if (c->proxy) - effective_host = c->proxy; - else - effective_host = c->host_port; - - yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host); - - assert (!c->cs); - c->cs = cs_create_host (effective_host, 0, &add); - - if (c->cs) - { - int ret = cs_connect (c->cs, add); - if (ret == 0) - { - ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT); - ZOOM_connection_put_event(c, event); - ZOOM_connection_send_init(c); - c->state = STATE_ESTABLISHED; - return 1; - } - else if (ret > 0) - { - c->state = STATE_CONNECTING; - c->mask = ZOOM_SELECT_EXCEPT; - if (c->cs->io_pending & CS_WANT_WRITE) - c->mask += ZOOM_SELECT_WRITE; - if (c->cs->io_pending & CS_WANT_READ) - c->mask += ZOOM_SELECT_READ; - return 1; - } - } - c->state = STATE_IDLE; - c->error = ZOOM_ERROR_CONNECT; - return 0; -} - -int z3950_connection_socket(ZOOM_connection c) -{ - if (c->cs) - return cs_fileno(c->cs); - return -1; -} - -int z3950_connection_mask(ZOOM_connection c) -{ - if (c->cs) - return c->mask; - return 0; -} - -static void otherInfo_attach (ZOOM_connection c, Z_APDU *a, ODR out) -{ - int i; - for (i = 0; i<200; i++) - { - size_t len; - Z_OtherInformation **oi; - char buf[80]; - const char *val; - const char *cp; - int oidval; - - sprintf (buf, "otherInfo%d", i); - val = ZOOM_options_get (c->options, buf); - if (!val) - break; - cp = strchr (val, ':'); - if (!cp) - continue; - len = cp - val; - if (len >= sizeof(buf)) - len = sizeof(buf)-1; - memcpy (buf, val, len); - buf[len] = '\0'; - oidval = oid_getvalbyname (buf); - if (oidval == VAL_NONE) - continue; - - yaz_oi_APDU(a, &oi); - yaz_oi_set_string_oidval(oi, out, oidval, 1, cp+1); - } -} - -static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out) -{ - assert (a); - if (c->cookie_out) - { - Z_OtherInformation **oi; - yaz_oi_APDU(a, &oi); - yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out); - } - if (c->client_IP) - { - Z_OtherInformation **oi; - yaz_oi_APDU(a, &oi); - yaz_oi_set_string_oidval(oi, out, VAL_CLIENT_IP, 1, c->client_IP); - } - otherInfo_attach (c, a, out); - if (!z_APDU(out, &a, 0, 0)) - { - FILE *outf = fopen("/tmp/apdu.txt", "a"); - if (a && outf) - { - ODR odr_pr = odr_createmem(ODR_PRINT); - fprintf (outf, "a=%p\n", a); - odr_setprint(odr_pr, outf); - z_APDU(odr_pr, &a, 0, 0); - odr_destroy(odr_pr); - } - c->error = ZOOM_ERROR_ENCODE; - do_close (c); - return -1; - } - - return 0; -} - -static int send_APDU (ZOOM_connection c, Z_APDU *a) -{ - ZOOM_Event event; - assert (a); - if (encode_APDU(c, a, c->odr_out)) - return -1; - c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0); - event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU); - ZOOM_connection_put_event (c, event); - odr_reset(c->odr_out); - do_write (c); - return 0; -} - -static int ZOOM_connection_send_init (ZOOM_connection c) -{ - const char *impname; - Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest); - Z_InitRequest *ireq = apdu->u.initRequest; - Z_IdAuthentication *auth = (Z_IdAuthentication *) - odr_malloc(c->odr_out, sizeof(*auth)); - const char *auth_groupId = ZOOM_options_get (c->options, "group"); - const char *auth_userId = ZOOM_options_get (c->options, "user"); - const char *auth_password = ZOOM_options_get (c->options, "pass"); - - ODR_MASK_SET(ireq->options, Z_Options_search); - ODR_MASK_SET(ireq->options, Z_Options_present); - ODR_MASK_SET(ireq->options, Z_Options_scan); - ODR_MASK_SET(ireq->options, Z_Options_sort); - ODR_MASK_SET(ireq->options, Z_Options_extendedServices); - ODR_MASK_SET(ireq->options, Z_Options_namedResultSets); - - ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1); - ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2); - ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3); - - impname = ZOOM_options_get (c->options, "implementationName"); - ireq->implementationName = - (char *) odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0)); - strcpy (ireq->implementationName, ""); - if (impname) - { - strcat (ireq->implementationName, impname); - strcat (ireq->implementationName, "/"); - } - strcat (ireq->implementationName, "ZOOM-C/YAZ"); - - *ireq->maximumRecordSize = - ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024); - *ireq->preferredMessageSize = - ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024); - - if (auth_groupId || auth_password) - { - Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass)); - int i = 0; - pass->groupId = 0; - if (auth_groupId && *auth_groupId) - { - pass->groupId = (char *) - odr_malloc(c->odr_out, strlen(auth_groupId)+1); - strcpy(pass->groupId, auth_groupId); - i++; - } - pass->userId = 0; - if (auth_userId && *auth_userId) - { - pass->userId = (char *) - odr_malloc(c->odr_out, strlen(auth_userId)+1); - strcpy(pass->userId, auth_userId); - i++; - } - pass->password = 0; - if (auth_password && *auth_password) - { - pass->password = (char *) - odr_malloc(c->odr_out, strlen(auth_password)+1); - strcpy(pass->password, auth_password); - i++; - } - if (i) - { - auth->which = Z_IdAuthentication_idPass; - auth->u.idPass = pass; - ireq->idAuthentication = auth; - } - } - else if (auth_userId) - { - auth->which = Z_IdAuthentication_open; - auth->u.open = (char *) - odr_malloc(c->odr_out, strlen(auth_userId)+1); - strcpy(auth->u.open, auth_userId); - ireq->idAuthentication = auth; - } - if (c->proxy) - yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out, - VAL_PROXY, 1, c->host_port); - if (c->charset||c->lang) - { - Z_OtherInformation **oi; - Z_OtherInformationUnit *oi_unit; - - yaz_oi_APDU(apdu, &oi); - - if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0))) - { - ODR_MASK_SET(ireq->options, Z_Options_negotiationModel); - - oi_unit->which = Z_OtherInfo_externallyDefinedInfo; - oi_unit->information.externallyDefinedInfo = - yaz_set_proposal_charneg - (c->odr_out, - (const char **)&c->charset, (c->charset) ? 1:0, - (const char **)&c->lang, (c->lang) ? 1:0, 1); - } - } - assert (apdu); - send_APDU (c, apdu); - - return 0; -} - -static int ZOOM_connection_send_search (ZOOM_connection c) -{ - ZOOM_resultset r; - int lslb, ssub, mspn; - const char *syntax; - Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest); - Z_SearchRequest *search_req = apdu->u.searchRequest; - const char *elementSetName; - const char *smallSetElementSetName; - const char *mediumSetElementSetName; - const char *schema; - - assert (c->tasks); - assert (c->tasks->which == ZOOM_TASK_SEARCH); - - r = c->tasks->u.search.resultset; - - elementSetName = - ZOOM_options_get (r->options, "elementSetName"); - smallSetElementSetName = - ZOOM_options_get (r->options, "smallSetElementSetName"); - mediumSetElementSetName = - ZOOM_options_get (r->options, "mediumSetElementSetName"); - schema = - ZOOM_options_get (r->options, "schema"); - - if (!smallSetElementSetName) - smallSetElementSetName = elementSetName; - - if (!mediumSetElementSetName) - mediumSetElementSetName = elementSetName; - - assert (r); - assert (r->r_query); - - /* prepare query for the search request */ - search_req->query = r->r_query; - - search_req->databaseNames = - set_DatabaseNames (c, r->options, &search_req->num_databaseNames); - - /* get syntax (no need to provide unless piggyback is in effect) */ - syntax = ZOOM_options_get (r->options, "preferredRecordSyntax"); - - lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1); - ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1); - mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1); - if (lslb != -1 && ssub != -1 && mspn != -1) - { - /* So're a Z39.50 expert? Let's hope you don't do sort */ - *search_req->largeSetLowerBound = lslb; - *search_req->smallSetUpperBound = ssub; - *search_req->mediumSetPresentNumber = mspn; - } - else if (r->start == 0 && r->count > 0 - && r->piggyback && !r->r_sort_spec && !schema) - { - /* Regular piggyback - do it unless we're going to do sort */ - *search_req->largeSetLowerBound = 2000000000; - *search_req->smallSetUpperBound = r->count; - *search_req->mediumSetPresentNumber = r->count; - smallSetElementSetName = 0; /* no need to provide this */ - } - else - { - /* non-piggyback. Need not provide elementsets or syntaxes .. */ - smallSetElementSetName = 0; - mediumSetElementSetName = 0; - syntax = 0; - } - if (smallSetElementSetName && *smallSetElementSetName) - { - Z_ElementSetNames *esn = (Z_ElementSetNames *) - odr_malloc (c->odr_out, sizeof(*esn)); - - esn->which = Z_ElementSetNames_generic; - esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName); - search_req->smallSetElementSetNames = esn; - } - if (mediumSetElementSetName && *mediumSetElementSetName) - { - Z_ElementSetNames *esn = (Z_ElementSetNames *) - odr_malloc (c->odr_out, sizeof(*esn)); - - esn->which = Z_ElementSetNames_generic; - esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName); - search_req->mediumSetElementSetNames = esn; - } - if (syntax) - search_req->preferredRecordSyntax = - yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax); - - if (!r->setname) - { - if (c->support_named_resultsets) - { - char setname[14]; - int ord; - /* find the lowest unused ordinal so that we re-use - result sets on the server. */ - for (ord = 1; ; ord++) - { - ZOOM_resultset rp; - sprintf (setname, "%d", ord); - for (rp = c->resultsets; rp; rp = rp->next) - if (rp->setname && !strcmp (rp->setname, setname)) - break; - if (!rp) - break; - } - r->setname = xstrdup (setname); - yaz_log (LOG_DEBUG, "allocating %s", r->setname); - } - else - r->setname = xstrdup ("default"); - ZOOM_options_set (r->options, "setname", r->setname); - } - search_req->resultSetName = odr_strdup(c->odr_out, r->setname); - /* send search request */ - send_APDU (c, apdu); - return 1; -} - -static void response_diag (ZOOM_connection c, Z_DiagRec *p) -{ - Z_DefaultDiagFormat *r; - char *addinfo = 0; - - xfree (c->addinfo); - c->addinfo = 0; - if (p->which != Z_DiagRec_defaultFormat) - { - c->error = ZOOM_ERROR_DECODE; - return; - } - r = p->u.defaultFormat; - switch (r->which) - { - case Z_DefaultDiagFormat_v2Addinfo: - addinfo = r->u.v2Addinfo; - break; - case Z_DefaultDiagFormat_v3Addinfo: - addinfo = r->u.v3Addinfo; - break; - } - if (addinfo) - c->addinfo = xstrdup (addinfo); - c->error = *r->condition; -} - -ZOOM_API(ZOOM_record) -ZOOM_record_clone (ZOOM_record srec) -{ - char *buf; - int size; - ODR odr_enc; - ZOOM_record nrec; - - odr_enc = odr_createmem(ODR_ENCODE); - if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0)) - return 0; - buf = odr_getbuf (odr_enc, &size, 0); - - nrec = (ZOOM_record) xmalloc (sizeof(*nrec)); - nrec->odr = odr_createmem(ODR_DECODE); - nrec->wrbuf_marc = 0; - odr_setbuf (nrec->odr, buf, size, 0); - z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0); - - odr_destroy (odr_enc); - return nrec; -} - -ZOOM_API(ZOOM_record) -ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos) -{ - return record_cache_lookup (s, pos); -} - -ZOOM_API(ZOOM_record) -ZOOM_resultset_record (ZOOM_resultset r, size_t pos) -{ - ZOOM_resultset_retrieve (r, 1, pos, 1); - return ZOOM_resultset_record_immediate (r, pos); -} - -ZOOM_API(void) -ZOOM_record_destroy (ZOOM_record rec) -{ - if (!rec) - return; - if (rec->wrbuf_marc) - wrbuf_free (rec->wrbuf_marc, 1); - odr_destroy (rec->odr); - xfree (rec); -} - -ZOOM_API(const char *) -ZOOM_record_get (ZOOM_record rec, const char *type, int *len) -{ - Z_NamePlusRecord *npr; - - if (len) - *len = 0; /* default return */ - - if (!rec) - return 0; - npr = rec->npr; - if (!npr) - return 0; - if (!strcmp (type, "database")) - { - if (len) - *len = (npr->databaseName ? strlen(npr->databaseName) : 0); - return npr->databaseName; - } - else if (!strcmp (type, "syntax")) - { - if (npr->which == Z_NamePlusRecord_databaseRecord) - { - Z_External *r = (Z_External *) npr->u.databaseRecord; - oident *ent = oid_getentbyoid(r->direct_reference); - if (ent) - { - if (len) - *len = strlen(ent->desc); - return ent->desc; - } - } - return "none"; - } - else if (!strcmp (type, "render") && - npr->which == Z_NamePlusRecord_databaseRecord) - { - Z_External *r = (Z_External *) npr->u.databaseRecord; - oident *ent = oid_getentbyoid(r->direct_reference); - - if (r->which == Z_External_sutrs) - { - if (len) *len = r->u.sutrs->len; - return (const char *) r->u.sutrs->buf; - } - else if (r->which == Z_External_octet) - { - switch (ent->value) - { - case VAL_SOIF: - case VAL_HTML: - case VAL_SUTRS: - break; - case VAL_TEXT_XML: - case VAL_APPLICATION_XML: - break; - default: - if (!rec->wrbuf_marc) - rec->wrbuf_marc = wrbuf_alloc(); - wrbuf_rewind (rec->wrbuf_marc); - if (yaz_marc_decode ((const char *) - r->u.octet_aligned->buf, - rec->wrbuf_marc, 0, - r->u.octet_aligned->len, - 0) > 0) - { - if (len) *len = wrbuf_len(rec->wrbuf_marc); - return wrbuf_buf(rec->wrbuf_marc); - } - } - if (len) *len = r->u.octet_aligned->len; - return (const char *) r->u.octet_aligned->buf; - } - else if (r->which == Z_External_grs1) - { - if (len) *len = 5; - return "GRS-1"; - } - return 0; - } - else if (!strcmp (type, "xml") && - npr->which == Z_NamePlusRecord_databaseRecord) - { - Z_External *r = (Z_External *) npr->u.databaseRecord; - oident *ent = oid_getentbyoid(r->direct_reference); - - if (r->which == Z_External_sutrs) - { - if (len) *len = r->u.sutrs->len; - return (const char *) r->u.sutrs->buf; - } - else if (r->which == Z_External_octet) - { - switch (ent->value) - { - case VAL_SOIF: - case VAL_HTML: - case VAL_SUTRS: - break; - case VAL_TEXT_XML: - case VAL_APPLICATION_XML: - break; - default: - if (!rec->wrbuf_marc) - rec->wrbuf_marc = wrbuf_alloc(); - wrbuf_rewind (rec->wrbuf_marc); - if (yaz_marc_decode ((const char *) - r->u.octet_aligned->buf, - rec->wrbuf_marc, 0, - r->u.octet_aligned->len, - 1) > 0) - { - if (len) *len = wrbuf_len(rec->wrbuf_marc); - return wrbuf_buf(rec->wrbuf_marc); - } - } - if (len) *len = r->u.octet_aligned->len; - return (const char *) r->u.octet_aligned->buf; - } - else if (r->which == Z_External_grs1) - { - if (len) *len = 5; - return "GRS-1"; - } - return 0; - } - else if (!strcmp (type, "raw")) - { - if (npr->which == Z_NamePlusRecord_databaseRecord) - { - Z_External *r = (Z_External *) npr->u.databaseRecord; - - if (r->which == Z_External_sutrs) - { - if (len) *len = r->u.sutrs->len; - return (const char *) r->u.sutrs->buf; - } - else if (r->which == Z_External_octet) - { - if (len) *len = r->u.octet_aligned->len; - return (const char *) r->u.octet_aligned->buf; - } - else /* grs-1, explain, ... */ - { - if (len) *len = -1; - return (const char *) npr->u.databaseRecord; - } - } - return 0; - } - else if (!strcmp (type, "ext")) - { - if (npr->which == Z_NamePlusRecord_databaseRecord) - return (const char *) npr->u.databaseRecord; - return 0; - } - return 0; -} - -static void record_cache_add (ZOOM_resultset r, Z_NamePlusRecord *npr, int pos) -{ - ZOOM_record_cache rc; - const char *elementSetName = - ZOOM_resultset_option_get (r, "elementSetName"); - const char *syntax = - ZOOM_resultset_option_get (r, "preferredRecordSyntax"); - - - for (rc = r->record_cache; rc; rc = rc->next) - { - if (pos == rc->pos) - { - if ((!elementSetName && !rc->elementSetName) - || (elementSetName && rc->elementSetName && - !strcmp (elementSetName, rc->elementSetName))) - { - if ((!syntax && !rc->syntax) - || (syntax && rc->syntax && - !strcmp (syntax, rc->syntax))) - { - /* not destroying rc->npr (it's handled by nmem )*/ - rc->rec.npr = npr; - /* keeping wrbuf_marc too */ - return; - } - } - } - } - rc = (ZOOM_record_cache) odr_malloc (r->odr, sizeof(*rc)); - rc->rec.npr = npr; - rc->rec.odr = 0; - rc->rec.wrbuf_marc = 0; - if (elementSetName) - rc->elementSetName = odr_strdup (r->odr, elementSetName); - else - rc->elementSetName = 0; - - if (syntax) - rc->syntax = odr_strdup (r->odr, syntax); - else - rc->syntax = 0; - - rc->pos = pos; - rc->next = r->record_cache; - r->record_cache = rc; -} - -static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos) -{ - ZOOM_record_cache rc; - const char *elementSetName = - ZOOM_resultset_option_get (r, "elementSetName"); - const char *syntax = - ZOOM_resultset_option_get (r, "preferredRecordSyntax"); - - for (rc = r->record_cache; rc; rc = rc->next) - { - if (pos == rc->pos) - { - if ((!elementSetName && !rc->elementSetName) - || (elementSetName && rc->elementSetName && - !strcmp (elementSetName, rc->elementSetName))) - { - if ((!syntax && !rc->syntax) - || (syntax && rc->syntax && - !strcmp (syntax, rc->syntax))) - return &rc->rec; - } - } - } - return 0; -} - -static void handle_records (ZOOM_connection c, Z_Records *sr, - int present_phase) -{ - ZOOM_resultset resultset; - - if (!c->tasks) - return ; - switch (c->tasks->which) - { - case ZOOM_TASK_SEARCH: - resultset = c->tasks->u.search.resultset; - break; - case ZOOM_TASK_RETRIEVE: - resultset = c->tasks->u.retrieve.resultset; - break; - default: - return; - } - if (sr && sr->which == Z_Records_NSD) - { - Z_DiagRec dr, *dr_p = &dr; - dr.which = Z_DiagRec_defaultFormat; - dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic; - - response_diag (c, dr_p); - } - else if (sr && sr->which == Z_Records_multipleNSD) - { - if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1) - response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]); - else - c->error = ZOOM_ERROR_DECODE; - } - else - { - if (resultset->count + resultset->start > resultset->size) - resultset->count = resultset->size - resultset->start; - if (resultset->count < 0) - resultset->count = 0; - if (sr && sr->which == Z_Records_DBOSD) - { - int i; - NMEM nmem = odr_extract_mem (c->odr_in); - Z_NamePlusRecordList *p = - sr->u.databaseOrSurDiagnostics; - for (i = 0; inum_records; i++) - { - record_cache_add (resultset, p->records[i], - i+ resultset->start); - } - /* transfer our response to search_nmem .. we need it later */ - nmem_transfer (resultset->odr->mem, nmem); - nmem_destroy (nmem); - if (present_phase && p->num_records == 0) - { - /* present response and we didn't get any records! */ - c->error = ZOOM_ERROR_DECODE; - } - } - else if (present_phase) - { - /* present response and we didn't get any records! */ - c->error = ZOOM_ERROR_DECODE; - } - } -} - -static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr) -{ - handle_records (c, pr->records, 1); -} - -static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr) -{ - ZOOM_resultset resultset; - - yaz_log (LOG_DEBUG, "got search response"); - - if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH) - return ; - - resultset = c->tasks->u.search.resultset; - - resultset->size = *sr->resultCount; - handle_records (c, sr->records, 0); -} - -static void sort_response (ZOOM_connection c, Z_SortResponse *res) -{ - if (res->diagnostics && res->num_diagnostics > 0) - response_diag (c, res->diagnostics[0]); -} - -static int scan_response (ZOOM_connection c, Z_ScanResponse *res) -{ - NMEM nmem = odr_extract_mem (c->odr_in); - ZOOM_scanset scan; - - if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN) - return 0; - scan = c->tasks->u.scan.scan; - - if (res->entries && res->entries->nonsurrogateDiagnostics) - response_diag(c, res->entries->nonsurrogateDiagnostics[0]); - scan->scan_response = res; - nmem_transfer (scan->odr->mem, nmem); - if (res->stepSize) - ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize); - if (res->positionOfTerm) - ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm); - if (res->scanStatus) - ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus); - if (res->numberOfEntriesReturned) - ZOOM_options_set_int (scan->options, "number", - *res->numberOfEntriesReturned); - nmem_destroy (nmem); - return 1; -} - -static int send_sort (ZOOM_connection c) -{ - ZOOM_resultset resultset; - - if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH) - return 0; - - resultset = c->tasks->u.search.resultset; - - if (c->error) - { - resultset->r_sort_spec = 0; - return 0; - } - if (resultset->r_sort_spec) - { - Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest); - Z_SortRequest *req = apdu->u.sortRequest; - - req->num_inputResultSetNames = 1; - req->inputResultSetNames = (Z_InternationalString **) - odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames)); - req->inputResultSetNames[0] = - odr_strdup (c->odr_out, resultset->setname); - req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname); - req->sortSequence = resultset->r_sort_spec; - resultset->r_sort_spec = 0; - send_APDU (c, apdu); - return 1; - } - return 0; -} - -static int send_present (ZOOM_connection c) -{ - Z_APDU *apdu = 0; - Z_PresentRequest *req = 0; - int i = 0; - const char *syntax = 0; - const char *elementSetName = 0; - const char *schema = 0; - ZOOM_resultset resultset; - - if (!c->tasks) - return 0; - - switch (c->tasks->which) - { - case ZOOM_TASK_SEARCH: - resultset = c->tasks->u.search.resultset; - break; - case ZOOM_TASK_RETRIEVE: - resultset = c->tasks->u.retrieve.resultset; - resultset->start = c->tasks->u.retrieve.start; - resultset->count = c->tasks->u.retrieve.count; - - if (resultset->start >= resultset->size) - return 0; - if (resultset->start + resultset->count > resultset->size) - resultset->count = resultset->size - resultset->start; - break; - default: - return 0; - } - - syntax = ZOOM_resultset_option_get (resultset, "preferredRecordSyntax"); - elementSetName = ZOOM_resultset_option_get (resultset, "elementSetName"); - schema = ZOOM_resultset_option_get (resultset, "schema"); - - if (c->error) /* don't continue on error */ - return 0; - if (resultset->start < 0) - return 0; - for (i = 0; icount; i++) - { - ZOOM_record rec = - record_cache_lookup (resultset, i + resultset->start); - if (!rec) - break; - } - if (i == resultset->count) - return 0; - - apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest); - req = apdu->u.presentRequest; - - resultset->start += i; - resultset->count -= i; - *req->resultSetStartPoint = resultset->start + 1; - *req->numberOfRecordsRequested = resultset->count; - assert (*req->numberOfRecordsRequested > 0); - - if (syntax && *syntax) - req->preferredRecordSyntax = - yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax); - - if (schema && *schema) - { - Z_RecordComposition *compo = (Z_RecordComposition *) - odr_malloc (c->odr_out, sizeof(*compo)); - - req->recordComposition = compo; - compo->which = Z_RecordComp_complex; - compo->u.complex = (Z_CompSpec *) - odr_malloc(c->odr_out, sizeof(*compo->u.complex)); - compo->u.complex->selectAlternativeSyntax = (bool_t *) - odr_malloc(c->odr_out, sizeof(bool_t)); - *compo->u.complex->selectAlternativeSyntax = 0; - - compo->u.complex->generic = (Z_Specification *) - odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic)); - - compo->u.complex->generic->schema = (Odr_oid *) - yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema); - - if (!compo->u.complex->generic->schema) - { - /* OID wasn't a schema! Try record syntax instead. */ - - compo->u.complex->generic->schema = (Odr_oid *) - yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema); - } - if (elementSetName && *elementSetName) - { - compo->u.complex->generic->elementSpec = (Z_ElementSpec *) - odr_malloc(c->odr_out, sizeof(Z_ElementSpec)); - compo->u.complex->generic->elementSpec->which = - Z_ElementSpec_elementSetName; - compo->u.complex->generic->elementSpec->u.elementSetName = - odr_strdup (c->odr_out, elementSetName); - } - else - compo->u.complex->generic->elementSpec = 0; - compo->u.complex->num_dbSpecific = 0; - compo->u.complex->dbSpecific = 0; - compo->u.complex->num_recordSyntax = 0; - compo->u.complex->recordSyntax = 0; - } - else if (elementSetName && *elementSetName) - { - Z_ElementSetNames *esn = (Z_ElementSetNames *) - odr_malloc (c->odr_out, sizeof(*esn)); - Z_RecordComposition *compo = (Z_RecordComposition *) - odr_malloc (c->odr_out, sizeof(*compo)); - - esn->which = Z_ElementSetNames_generic; - esn->u.generic = odr_strdup (c->odr_out, elementSetName); - compo->which = Z_RecordComp_simple; - compo->u.simple = esn; - req->recordComposition = compo; - } - req->resultSetId = odr_strdup(c->odr_out, resultset->setname); - send_APDU (c, apdu); - return 1; -} - -ZOOM_API(ZOOM_scanset) -ZOOM_connection_scan (ZOOM_connection c, const char *start) -{ - ZOOM_scanset scan = (ZOOM_scanset) xmalloc (sizeof(*scan)); - - scan->connection = c; - scan->odr = odr_createmem (ODR_DECODE); - scan->options = ZOOM_options_create_with_parent (c->options); - scan->refcount = 1; - scan->scan_response = 0; - - if ((scan->termListAndStartPoint = - p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet, - start))) - { - ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN); - task->u.scan.scan = scan; - - (scan->refcount)++; - if (!c->async) - { - while (ZOOM_event (1, &c)) - ; - } - } - return scan; -} - -ZOOM_API(void) -ZOOM_scanset_destroy (ZOOM_scanset scan) -{ - if (!scan) - return; - (scan->refcount)--; - if (scan->refcount == 0) - { - odr_destroy (scan->odr); - - ZOOM_options_destroy (scan->options); - xfree (scan); - } -} - -static int send_package (ZOOM_connection c) -{ - ZOOM_Event event; - if (!c->tasks) - return 0; - assert (c->tasks->which == ZOOM_TASK_PACKAGE); - - event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU); - ZOOM_connection_put_event (c, event); - - do_write_ex (c, c->tasks->u.package->buf_out, - c->tasks->u.package->len_out); - return 1; -} - -static int send_scan (ZOOM_connection c) -{ - ZOOM_scanset scan; - Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest); - Z_ScanRequest *req = apdu->u.scanRequest; - if (!c->tasks) - return 0; - assert (c->tasks->which == ZOOM_TASK_SCAN); - scan = c->tasks->u.scan.scan; - - req->termListAndStartPoint = scan->termListAndStartPoint; - req->attributeSet = scan->attributeSet; - - *req->numberOfTermsRequested = - ZOOM_options_get_int(scan->options, "number", 10); - - req->preferredPositionInResponse = - odr_intdup (c->odr_out, - ZOOM_options_get_int(scan->options, "position", 1)); - - req->stepSize = - odr_intdup (c->odr_out, - ZOOM_options_get_int(scan->options, "stepSize", 0)); - - req->databaseNames = set_DatabaseNames (c, scan->options, - &req->num_databaseNames); - - send_APDU (c, apdu); - - return 1; -} - -ZOOM_API(size_t) -ZOOM_scanset_size (ZOOM_scanset scan) -{ - if (!scan || !scan->scan_response || !scan->scan_response->entries) - return 0; - return scan->scan_response->entries->num_entries; -} - -ZOOM_API(const char *) -ZOOM_scanset_term (ZOOM_scanset scan, size_t pos, - int *occ, int *len) -{ - const char *term = 0; - size_t noent = ZOOM_scanset_size (scan); - Z_ScanResponse *res = scan->scan_response; - - *len = 0; - *occ = 0; - if (pos >= noent) - return 0; - if (res->entries->entries[pos]->which == Z_Entry_termInfo) - { - Z_TermInfo *t = res->entries->entries[pos]->u.termInfo; - - if (t->term->which == Z_Term_general) - { - term = (const char *) t->term->u.general->buf; - *len = t->term->u.general->len; - } - *occ = t->globalOccurrences ? *t->globalOccurrences : 0; - } - return term; -} - -ZOOM_API(const char *) -ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key) -{ - return ZOOM_options_get (scan->options, key); -} - -ZOOM_API(void) -ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key, - const char *val) -{ - ZOOM_options_set (scan->options, key, val); -} - - - -static Z_APDU *create_es_package (ZOOM_package p, int type) -{ - const char *str; - Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest); - Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest; - - *req->function = Z_ExtendedServicesRequest_create; - - str = ZOOM_options_get(p->options, "package-name"); - if (str && *str) - req->packageName = nmem_strdup (p->odr_out->mem, str); - - str = ZOOM_options_get(p->options, "user-id"); - if (str) - req->userId = nmem_strdup (p->odr_out->mem, str); - - req->packageType = yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV, - type); - - str = ZOOM_options_get(p->options, "function"); - if (str) - { - if (!strcmp (str, "create")) - *req->function = 1; - if (!strcmp (str, "delete")) - *req->function = 2; - if (!strcmp (str, "modify")) - *req->function = 3; - } - return apdu; -} - -static const char *ill_array_lookup (void *clientData, const char *idx) -{ - ZOOM_package p = (ZOOM_package) clientData; - return ZOOM_options_get (p->options, idx+4); -} - -static Z_External *encode_ill_request (ZOOM_package p) -{ - ODR out = p->odr_out; - ILL_Request *req; - Z_External *r = 0; - struct ill_get_ctl ctl; - - ctl.odr = p->odr_out; - ctl.clientData = p; - ctl.f = ill_array_lookup; - - req = ill_get_ILLRequest(&ctl, "ill", 0); - - if (!ill_Request (out, &req, 0, 0)) - { - int ill_request_size; - char *ill_request_buf = odr_getbuf (out, &ill_request_size, 0); - if (ill_request_buf) - odr_setbuf (out, ill_request_buf, ill_request_size, 1); - return 0; - } - else - { - oident oid; - int illRequest_size = 0; - char *illRequest_buf = odr_getbuf (out, &illRequest_size, 0); - - oid.proto = PROTO_GENERAL; - oid.oclass = CLASS_GENERAL; - oid.value = VAL_ISO_ILL_1; - - r = (Z_External *) odr_malloc (out, sizeof(*r)); - r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid)); - r->indirect_reference = 0; - r->descriptor = 0; - r->which = Z_External_single; - - r->u.single_ASN1_type = (Odr_oct *) - odr_malloc (out, sizeof(*r->u.single_ASN1_type)); - r->u.single_ASN1_type->buf = odr_malloc (out, illRequest_size); - r->u.single_ASN1_type->len = illRequest_size; - r->u.single_ASN1_type->size = illRequest_size; - memcpy (r->u.single_ASN1_type->buf, illRequest_buf, illRequest_size); - } - return r; -} - -static Z_ItemOrder *encode_item_order(ZOOM_package p) -{ - Z_ItemOrder *req = odr_malloc (p->odr_out, sizeof(*req)); - const char *str; - - req->which=Z_IOItemOrder_esRequest; - req->u.esRequest = (Z_IORequest *) - odr_malloc(p->odr_out,sizeof(Z_IORequest)); - - /* to keep part ... */ - req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *) - odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep)); - req->u.esRequest->toKeep->supplDescription = 0; - req->u.esRequest->toKeep->contact = - odr_malloc (p->odr_out, sizeof(*req->u.esRequest->toKeep->contact)); - - str = ZOOM_options_get(p->options, "contact-name"); - req->u.esRequest->toKeep->contact->name = str ? - nmem_strdup (p->odr_out->mem, str) : 0; - - str = ZOOM_options_get(p->options, "contact-phone"); - req->u.esRequest->toKeep->contact->phone = str ? - nmem_strdup (p->odr_out->mem, str) : 0; - - str = ZOOM_options_get(p->options, "contact-email"); - req->u.esRequest->toKeep->contact->email = str ? - nmem_strdup (p->odr_out->mem, str) : 0; - - req->u.esRequest->toKeep->addlBilling = 0; - - /* not to keep part ... */ - req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *) - odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep)); - - req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *) - odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem)); - - str = ZOOM_options_get(p->options, "itemorder-setname"); - if (!str) - str = "default"; - req->u.esRequest->notToKeep->resultSetItem->resultSetId = - nmem_strdup (p->odr_out->mem, str); - req->u.esRequest->notToKeep->resultSetItem->item = - (int *) odr_malloc(p->odr_out, sizeof(int)); - - str = ZOOM_options_get(p->options, "itemorder-item"); - *req->u.esRequest->notToKeep->resultSetItem->item = - (str ? atoi(str) : 1); - - req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p); - - return req; -} - -ZOOM_API(void) - ZOOM_package_send (ZOOM_package p, const char *type) -{ - Z_APDU *apdu = 0; - ZOOM_connection c; - if (!p) - return; - c = p->connection; - odr_reset (p->odr_out); - xfree (p->buf_out); - p->buf_out = 0; - if (!strcmp(type, "itemorder")) - { - Z_External *r; - apdu = create_es_package (p, VAL_ITEMORDER); - if (apdu) - { - r = odr_malloc (p->odr_out, sizeof(*r)); - - r->direct_reference = - yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV, - VAL_ITEMORDER); - r->descriptor = 0; - r->which = Z_External_itemOrder; - r->indirect_reference = 0; - r->u.itemOrder = encode_item_order (p); - - apdu->u.extendedServicesRequest->taskSpecificParameters = r; - } - } - if (apdu) - { - if (encode_APDU(p->connection, apdu, p->odr_out) == 0) - { - char *buf; - - ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_PACKAGE); - task->u.package = p; - buf = odr_getbuf(p->odr_out, &p->len_out, 0); - p->buf_out = xmalloc (p->len_out); - memcpy (p->buf_out, buf, p->len_out); - - (p->refcount)++; - if (!c->async) - { - while (ZOOM_event (1, &c)) - ; - } - } - } -} - -ZOOM_API(ZOOM_package) - ZOOM_connection_package (ZOOM_connection c, ZOOM_options options) -{ - ZOOM_package p = (ZOOM_package) xmalloc (sizeof(*p)); - - p->connection = c; - p->odr_out = odr_createmem (ODR_ENCODE); - p->options = ZOOM_options_create_with_parent2 (options, c->options); - p->refcount = 1; - p->buf_out = 0; - p->len_out = 0; - return p; -} - -ZOOM_API(void) - ZOOM_package_destroy(ZOOM_package p) -{ - if (!p) - return; - (p->refcount)--; - if (p->refcount == 0) - { - odr_destroy (p->odr_out); - xfree (p->buf_out); - - ZOOM_options_destroy (p->options); - xfree (p); - } -} - -ZOOM_API(const char *) -ZOOM_package_option_get (ZOOM_package p, const char *key) -{ - return ZOOM_options_get (p->options, key); -} - -ZOOM_API(void) -ZOOM_package_option_set (ZOOM_package p, const char *key, - const char *val) -{ - ZOOM_options_set (p->options, key, val); -} - -static int ZOOM_connection_exec_task (ZOOM_connection c) -{ - ZOOM_task task = c->tasks; - - yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task"); - if (!task) - return 0; - if (c->error != ZOOM_ERROR_NONE || - (!c->cs && task->which != ZOOM_TASK_CONNECT)) - { - ZOOM_connection_remove_tasks (c); - return 0; - } - yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d run=%d", - task->which, task->running); - if (task->running) - return 0; - task->running = 1; - switch (task->which) - { - case ZOOM_TASK_SEARCH: - /* see if search hasn't been sent yet. */ - if (ZOOM_connection_send_search (c)) - return 1; - break; - case ZOOM_TASK_RETRIEVE: - if (send_present (c)) - return 1; - break; - case ZOOM_TASK_CONNECT: - if (do_connect(c)) - return 1; - break; - case ZOOM_TASK_SCAN: - if (send_scan(c)) - return 1; - break; - case ZOOM_TASK_PACKAGE: - if (send_package(c)) - return 1; - } - ZOOM_connection_remove_task (c); - return 0; -} - -static int send_sort_present (ZOOM_connection c) -{ - int r = send_sort (c); - if (!r) - r = send_present (c); - return r; -} - -static int es_response (ZOOM_connection c, - Z_ExtendedServicesResponse *res) -{ - if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE) - return 0; - if (res->diagnostics && res->num_diagnostics > 0) - response_diag(c, res->diagnostics[0]); - if (res->taskPackage && - res->taskPackage->which == Z_External_extendedService) - { - Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService; - Odr_oct *id = taskPackage->targetReference; - - if (id) - ZOOM_options_setl (c->tasks->u.package->options, - "targetReference", id->buf, id->len); - } - return 1; -} - - -static void handle_apdu (ZOOM_connection c, Z_APDU *apdu) -{ - Z_InitResponse *initrs; - - c->mask = 0; - yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which); - switch(apdu->which) - { - case Z_APDU_initResponse: - initrs = apdu->u.initResponse; - if (!*initrs->result) - { - c->error = ZOOM_ERROR_INIT; - } - else - { - char *cookie = - yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo, - VAL_COOKIE, 1, 0); - xfree (c->cookie_in); - c->cookie_in = 0; - if (cookie) - c->cookie_in = xstrdup(cookie); - if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) && - ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3)) - c->support_named_resultsets = 1; - if (c->tasks) - { - assert (c->tasks->which == ZOOM_TASK_CONNECT); - ZOOM_connection_remove_task (c); - } - ZOOM_connection_exec_task (c); - } - if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel)) - { - NMEM tmpmem = nmem_create(); - Z_CharSetandLanguageNegotiation *p = - yaz_get_charneg_record(initrs->otherInfo); - - if (p) - { - char *charset=NULL, *lang=NULL; - int selected; - - yaz_get_response_charneg(tmpmem, p, &charset, &lang, &selected); - yaz_log(LOG_DEBUG, "Target accepted: charset - %s," - "language - %s, select - %d", - charset, lang, selected); - - nmem_destroy(tmpmem); - } - } - break; - case Z_APDU_searchResponse: - handle_search_response (c, apdu->u.searchResponse); - if (!send_sort_present (c)) - ZOOM_connection_remove_task (c); - break; - case Z_APDU_presentResponse: - handle_present_response (c, apdu->u.presentResponse); - if (!send_present (c)) - ZOOM_connection_remove_task (c); - break; - case Z_APDU_sortResponse: - sort_response (c, apdu->u.sortResponse); - if (!send_present (c)) - ZOOM_connection_remove_task (c); - break; - case Z_APDU_scanResponse: - scan_response (c, apdu->u.scanResponse); - ZOOM_connection_remove_task (c); - break; - case Z_APDU_extendedServicesResponse: - es_response (c, apdu->u.extendedServicesResponse); - ZOOM_connection_remove_task (c); - break; - case Z_APDU_close: - if (c->reconnect_ok) - { - do_close(c); - c->tasks->running = 0; - ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT); - } - else - { - c->error = ZOOM_ERROR_CONNECTION_LOST; - do_close(c); - } - break; - default: - c->error = ZOOM_ERROR_DECODE; - do_close(c); - } -} - -static int do_read (ZOOM_connection c) -{ - int r; - Z_APDU *apdu; - ZOOM_Event event; - - event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA); - ZOOM_connection_put_event (c, event); - - yaz_log (LOG_DEBUG, "do_read len=%d", c->len_in); - - r = cs_get (c->cs, &c->buf_in, &c->len_in); - if (r == 1) - return 0; - if (r <= 0) - { - if (c->reconnect_ok) - { - do_close (c); - c->reconnect_ok = 0; - yaz_log (LOG_DEBUG, "reconnect read"); - c->tasks->running = 0; - ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT); - } - else - { - c->error= ZOOM_ERROR_CONNECTION_LOST; - do_close (c); - } - } - else - { - ZOOM_Event event; - odr_reset (c->odr_in); - odr_setbuf (c->odr_in, c->buf_in, r, 0); - event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU); - ZOOM_connection_put_event (c, event); - if (!z_APDU (c->odr_in, &apdu, 0, 0)) - { - c->error = ZOOM_ERROR_DECODE; - do_close (c); - } - else - handle_apdu (c, apdu); - c->reconnect_ok = 0; - } - return 1; -} - -static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out) -{ - int r; - ZOOM_Event event; - - event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA); - ZOOM_connection_put_event (c, event); - - yaz_log (LOG_DEBUG, "do_write_ex len=%d", len_out); - if ((r=cs_put (c->cs, buf_out, len_out)) < 0) - { - if (c->reconnect_ok) - { - do_close (c); - c->reconnect_ok = 0; - yaz_log (LOG_DEBUG, "reconnect write"); - c->tasks->running = 0; - ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT); - return 0; - } - if (c->state == STATE_CONNECTING) - c->error = ZOOM_ERROR_CONNECT; - else - c->error = ZOOM_ERROR_CONNECTION_LOST; - do_close (c); - return 1; - } - else if (r == 1) - { - c->mask = ZOOM_SELECT_EXCEPT; - if (c->cs->io_pending & CS_WANT_WRITE) - c->mask += ZOOM_SELECT_WRITE; - if (c->cs->io_pending & CS_WANT_READ) - c->mask += ZOOM_SELECT_READ; - yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask); - } - else - { - // c->reconnect_ok = 0; - c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT; - yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask); - } - return 0; -} - -static int do_write(ZOOM_connection c) -{ - return do_write_ex (c, c->buf_out, c->len_out); -} - - -ZOOM_API(const char *) -ZOOM_connection_option_get (ZOOM_connection c, const char *key) -{ - return ZOOM_options_get (c->options, key); -} - -ZOOM_API(void) -ZOOM_connection_option_set (ZOOM_connection c, const char *key, - const char *val) -{ - ZOOM_options_set (c->options, key, val); -} - -ZOOM_API(const char *) -ZOOM_resultset_option_get (ZOOM_resultset r, const char *key) -{ - return ZOOM_options_get (r->options, key); -} - -ZOOM_API(void) -ZOOM_resultset_option_set (ZOOM_resultset r, const char *key, - const char *val) -{ - ZOOM_options_set (r->options, key, val); -} - - -ZOOM_API(int) -ZOOM_connection_errcode (ZOOM_connection c) -{ - return ZOOM_connection_error (c, 0, 0); -} - -ZOOM_API(const char *) -ZOOM_connection_errmsg (ZOOM_connection c) -{ - const char *msg; - ZOOM_connection_error (c, &msg, 0); - return msg; -} - -ZOOM_API(const char *) -ZOOM_connection_addinfo (ZOOM_connection c) -{ - const char *addinfo; - ZOOM_connection_error (c, 0, &addinfo); - return addinfo; -} - -ZOOM_API(int) -ZOOM_connection_error (ZOOM_connection c, const char **cp, - const char **addinfo) -{ - int error = c->error; - if (cp) - { - switch (error) - { - case ZOOM_ERROR_NONE: - *cp = "No error"; break; - case ZOOM_ERROR_CONNECT: - *cp = "Connect failed"; break; - case ZOOM_ERROR_MEMORY: - *cp = "Out of memory"; break; - case ZOOM_ERROR_ENCODE: - *cp = "Encoding failed"; break; - case ZOOM_ERROR_DECODE: - *cp = "Decoding failed"; break; - case ZOOM_ERROR_CONNECTION_LOST: - *cp = "Connection lost"; break; - case ZOOM_ERROR_INIT: - *cp = "Init rejected"; break; - case ZOOM_ERROR_INTERNAL: - *cp = "Internal failure"; break; - case ZOOM_ERROR_TIMEOUT: - *cp = "Timeout"; break; - default: - *cp = diagbib1_str (error); - } - } - if (addinfo) - { - if (c->addinfo) - *addinfo = c->addinfo; - else - *addinfo = ""; - } - return c->error; -} - -static int ZOOM_connection_do_io(ZOOM_connection c, int mask) -{ - ZOOM_Event event = 0; - int r = cs_look(c->cs); - yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d", - c, mask, r); - - if (r == CS_NONE) - { - event = ZOOM_Event_create (ZOOM_EVENT_CONNECT); - c->error = ZOOM_ERROR_CONNECT; - do_close (c); - ZOOM_connection_put_event (c, event); - } - else if (r == CS_CONNECT) - { - int ret; - event = ZOOM_Event_create (ZOOM_EVENT_CONNECT); - - ret = cs_rcvconnect (c->cs); - yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret); - if (ret == 1) - { - c->mask = ZOOM_SELECT_EXCEPT; - if (c->cs->io_pending & CS_WANT_WRITE) - c->mask += ZOOM_SELECT_WRITE; - if (c->cs->io_pending & CS_WANT_READ) - c->mask += ZOOM_SELECT_READ; - ZOOM_connection_put_event (c, event); - } - else if (ret == 0) - { - ZOOM_connection_put_event (c, event); - ZOOM_connection_send_init (c); - c->state = STATE_ESTABLISHED; - } - else - { - c->error = ZOOM_ERROR_CONNECT; - do_close (c); - ZOOM_connection_put_event (c, event); - } - } - else - { - if (mask & ZOOM_SELECT_READ) - do_read (c); - if (c->cs && (mask & ZOOM_SELECT_WRITE)) - do_write (c); - } - return 1; -} - -ZOOM_API(int) -ZOOM_connection_last_event(ZOOM_connection cs) -{ - if (!cs) - return ZOOM_EVENT_NONE; - return cs->last_event; -} - -ZOOM_API(int) -ZOOM_event (int no, ZOOM_connection *cs) -{ - int timeout = 5000; -#if HAVE_SYS_POLL_H - struct pollfd pollfds[1024]; - ZOOM_connection poll_cs[1024]; -#else - struct timeval tv; - fd_set input, output, except; -#endif - int i, r, nfds; - int max_fd = 0; - - for (i = 0; ioptions, "timeout", -1); - if (this_timeout != -1 && this_timeout < timeout) - timeout = this_timeout; -#if HAVE_SYS_POLL_H - if (mask) - { - short poll_events = 0; - - if (mask & ZOOM_SELECT_READ) - poll_events += POLLIN; - if (mask & ZOOM_SELECT_WRITE) - poll_events += POLLOUT; - if (mask & ZOOM_SELECT_EXCEPT) - poll_events += POLLERR; - pollfds[nfds].fd = fd; - pollfds[nfds].events = poll_events; - pollfds[nfds].revents = 0; - poll_cs[nfds] = c; - nfds++; - } -#else - if (mask & ZOOM_SELECT_READ) - { - FD_SET (fd, &input); - nfds++; - } - if (mask & ZOOM_SELECT_WRITE) - { - FD_SET (fd, &output); - nfds++; - } - if (mask & ZOOM_SELECT_EXCEPT) - { - FD_SET (fd, &except); - nfds++; - } -#endif - } - if (timeout >= 5000) - timeout = 30; - - if (!nfds) - return 0; - -#if HAVE_SYS_POLL_H - r = poll (pollfds, nfds, timeout * 1000); - for (i = 0; imask) - { - int mask = 0; - if (pollfds[i].revents & POLLIN) - mask += ZOOM_SELECT_READ; - if (pollfds[i].revents & POLLOUT) - mask += ZOOM_SELECT_WRITE; - if (pollfds[i].revents & POLLERR) - mask += ZOOM_SELECT_EXCEPT; - if (mask) - ZOOM_connection_do_io(c, mask); - } - else if (r == 0 && c->mask) - { - ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT); - /* timeout and this connection was waiting */ - c->error = ZOOM_ERROR_TIMEOUT; - do_close (c); - ZOOM_connection_put_event(c, event); - } - } -#else - tv.tv_sec = timeout; - tv.tv_usec = 0; - yaz_log (LOG_DEBUG, "select start"); - r = select (max_fd+1, &input, &output, &except, &tv); - yaz_log (LOG_DEBUG, "select stop, returned r=%d", r); - for (i = 0; imask) - { - /* no timeout and real socket */ - if (FD_ISSET(fd, &input)) - mask += ZOOM_SELECT_READ; - if (FD_ISSET(fd, &output)) - mask += ZOOM_SELECT_WRITE; - if (FD_ISSET(fd, &except)) - mask += ZOOM_SELECT_EXCEPT; - if (mask) - ZOOM_connection_do_io(c, mask); - } - if (r == 0 && c->mask) - { - ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT); - /* timeout and this connection was waiting */ - c->error = ZOOM_ERROR_TIMEOUT; - do_close (c); - yaz_log (LOG_DEBUG, "timeout"); - ZOOM_connection_put_event(c, event); - } - } -#endif - for (i = 0; i -#include -#include - -#include "zoom-p.h" - -ZOOM_API(ZOOM_options) -ZOOM_options_create_with_parent (ZOOM_options parent) -{ - return ZOOM_options_create_with_parent2(parent, 0); -} - -ZOOM_API(ZOOM_options) -ZOOM_options_create (void) -{ - return ZOOM_options_create_with_parent (0); -} - - -ZOOM_API(ZOOM_options) -ZOOM_options_create_with_parent2 (ZOOM_options parent1, ZOOM_options parent2) -{ - ZOOM_options opt = (ZOOM_options) xmalloc (sizeof(*opt)); - - opt->refcount = 1; - opt->callback_func = 0; - opt->callback_handle = 0; - opt->entries = 0; - opt->parent1= parent1; - if (parent1) - (parent1->refcount)++; - opt->parent2= parent2; - if (parent2) - (parent2->refcount)++; - return opt; -} - - -void ZOOM_options_addref (ZOOM_options opt) -{ - (opt->refcount)++; -} - -ZOOM_API(ZOOM_options_callback) -ZOOM_options_set_callback ( - ZOOM_options opt, - ZOOM_options_callback callback_func, - void *callback_handle) -{ - ZOOM_options_callback callback_old; - - assert (opt); - callback_old = opt->callback_func; - opt->callback_func = callback_func; - opt->callback_handle = callback_handle; - return callback_old; -} - -ZOOM_API(void) -ZOOM_options_destroy (ZOOM_options opt) -{ - if (!opt) - return; - (opt->refcount)--; - if (opt->refcount == 0) - { - struct ZOOM_options_entry *e; - - ZOOM_options_destroy (opt->parent1); - ZOOM_options_destroy (opt->parent2); - e = opt->entries; - while (e) - { - struct ZOOM_options_entry *e0 = e; - xfree (e->name); - xfree (e->value); - e = e->next; - xfree (e0); - } - xfree (opt); - } -} - -ZOOM_API(void) -ZOOM_options_setl (ZOOM_options opt, const char *name, const char *value, - int len) -{ - struct ZOOM_options_entry **e; - - e = &opt->entries; - while (*e) - { - if (!strcmp((*e)->name, name)) - { - xfree ((*e)->value); - (*e)->value = 0; - if (value) - { - (*e)->value = xmalloc (len+1); - memcpy ((*e)->value, value, len); - (*e)->value[len] = '\0'; - } - return; - } - e = &(*e)->next; - } - *e = (struct ZOOM_options_entry *) xmalloc (sizeof(**e)); - (*e)->name = xstrdup (name); - (*e)->value = 0; - if (value) - { - (*e)->value = xmalloc (len+1); - memcpy ((*e)->value, value, len); - (*e)->value[len] = '\0'; - } - (*e)->next = 0; -} - -ZOOM_API(void) -ZOOM_options_set (ZOOM_options opt, const char *name, const char *value) -{ - ZOOM_options_setl (opt, name, value, value ? strlen(value): 0); -} - -ZOOM_API(const char *) -ZOOM_options_get (ZOOM_options opt, const char *name) -{ - const char *v = 0; - if (!opt) - return 0; - if (opt->callback_func) - v = (*opt->callback_func)(opt->callback_handle, name); - if (!v) - { - struct ZOOM_options_entry *e; - for (e = opt->entries; e; e = e->next) - if (!strcmp(e->name, name)) - { - v = e->value; - break; - } - } - if (!v) - v = ZOOM_options_get(opt->parent1, name); - if (!v) - v = ZOOM_options_get(opt->parent2, name); - return v; -} - -ZOOM_API(int) -ZOOM_options_get_bool (ZOOM_options opt, const char *name, int defa) -{ - const char *v = ZOOM_options_get (opt, name); - - if (!v) - return defa; - if (!strcmp (v, "1") || !strcmp(v, "T")) - return 1; - return 0; -} - -ZOOM_API(int) -ZOOM_options_get_int (ZOOM_options opt, const char *name, int defa) -{ - const char *v = ZOOM_options_get (opt, name); - - if (!v || !*v) - return defa; - return atoi(v); -} - -ZOOM_API(void) -ZOOM_options_set_int(ZOOM_options opt, const char *name, int value) -{ - char s[40]; - - sprintf (s, "%d", value); - ZOOM_options_set (opt, name, s); -} diff --git a/zoom/zoom-p.h b/zoom/zoom-p.h deleted file mode 100644 index 2d7d95d..0000000 --- a/zoom/zoom-p.h +++ /dev/null @@ -1,157 +0,0 @@ -/* - * Private C header for ZOOM C. - * $Id: zoom-p.h,v 1.12 2002-08-20 08:19:40 adam Exp $ - */ -#include -#include -#include -#include -#include - -typedef struct ZOOM_Event_p *ZOOM_Event; - -struct ZOOM_query_p { - Z_Query *query; - Z_SortKeySpecList *sort_spec; - int refcount; - ODR odr; -}; - -#define STATE_IDLE 0 -#define STATE_CONNECTING 1 -#define STATE_ESTABLISHED 2 - -#define ZOOM_SELECT_READ 1 -#define ZOOM_SELECT_WRITE 2 -#define ZOOM_SELECT_EXCEPT 4 - -struct ZOOM_connection_p { - COMSTACK cs; - char *host_port; - int error; - char *addinfo; - int state; - int mask; - int reconnect_ok; - ODR odr_in; - ODR odr_out; - char *buf_in; - int len_in; - char *buf_out; - int len_out; - char *proxy; - char *charset; - char *lang; - char *cookie_out; - char *cookie_in; - char *client_IP; - int async; - int support_named_resultsets; - int last_event; - ZOOM_task tasks; - ZOOM_options options; - ZOOM_resultset resultsets; - ZOOM_Event m_queue_front; - ZOOM_Event m_queue_back; -}; - - -struct ZOOM_options_entry { - char *name; - char *value; - struct ZOOM_options_entry *next; -}; - -struct ZOOM_options_p { - int refcount; - void *callback_handle; - ZOOM_options_callback callback_func; - struct ZOOM_options_entry *entries; - ZOOM_options parent1; - ZOOM_options parent2; -}; - -typedef struct ZOOM_record_cache_p *ZOOM_record_cache; - -struct ZOOM_resultset_p { - Z_Query *r_query; - Z_SortKeySpecList *r_sort_spec; - ZOOM_query search; - int refcount; - int size; - int start; - int count; - int piggyback; - char *setname; - ODR odr; - ZOOM_record_cache record_cache; - ZOOM_options options; - ZOOM_connection connection; - ZOOM_resultset next; -}; - -struct ZOOM_record_p { - ODR odr; - WRBUF wrbuf_marc; - Z_NamePlusRecord *npr; -}; - -struct ZOOM_record_cache_p { - struct ZOOM_record_p rec; - char *elementSetName; - char *syntax; - int pos; - ZOOM_record_cache next; -}; - -struct ZOOM_scanset_p { - int refcount; - ODR odr; - ZOOM_options options; - ZOOM_connection connection; - Z_AttributesPlusTerm *termListAndStartPoint; - Z_AttributeSetId *attributeSet; - Z_ScanResponse *scan_response; -}; - -struct ZOOM_package_p { - int refcount; - ODR odr_out; - ZOOM_options options; - ZOOM_connection connection; - char *buf_out; - int len_out; -}; - -struct ZOOM_task_p { - int running; - int which; - union { -#define ZOOM_TASK_SEARCH 1 - struct { - ZOOM_resultset resultset; - } search; -#define ZOOM_TASK_RETRIEVE 2 - struct { - int start; - ZOOM_resultset resultset; - int count; - } retrieve; -#define ZOOM_TASK_CONNECT 3 -#define ZOOM_TASK_SCAN 4 - struct { - ZOOM_scanset scan; - } scan; -#define ZOOM_TASK_PACKAGE 5 - ZOOM_package package; - } u; - ZOOM_task next; -}; - -struct ZOOM_Event_p { - int kind; - ZOOM_Event next; - ZOOM_Event prev; -}; - -void ZOOM_options_addref (ZOOM_options opt); diff --git a/zutil/Makefile.am b/zutil/Makefile.am index 3cc9b75..574a9e8 100644 --- a/zutil/Makefile.am +++ b/zutil/Makefile.am @@ -1,7 +1,8 @@ -## $Id: Makefile.am,v 1.11 2002-09-11 21:25:57 adam Exp $ +## $Id: Makefile.am,v 1.12 2002-09-16 18:45:14 adam Exp $ noinst_LTLIBRARIES = libzutil.la AM_CPPFLAGS=-I$(top_srcdir)/include libzutil_la_SOURCES = zget.c yaz-ccl.c diagbib1.c logrpn.c \ - otherinfo.c pquery.c sortspec.c z3950oid.c charneg.c + otherinfo.c pquery.c sortspec.c z3950oid.c charneg.c \ + zoom-c.c zoom-opt.c zoom-p.h diff --git a/zutil/zoom-c.c b/zutil/zoom-c.c new file mode 100644 index 0000000..58a51bf --- /dev/null +++ b/zutil/zoom-c.c @@ -0,0 +1,2504 @@ +/* + * $Id: zoom-c.c,v 1.1 2002-09-16 18:45:14 adam Exp $ + * + * ZOOM layer for C, connections, result sets, queries. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "zoom-p.h" + +#if HAVE_SYS_POLL_H +#include +#endif + +static int ZOOM_connection_send_init (ZOOM_connection c); +static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out); + +static ZOOM_Event ZOOM_Event_create (int kind) +{ + ZOOM_Event event = (ZOOM_Event) xmalloc (sizeof(*event)); + event->kind = kind; + event->next = 0; + event->prev = 0; + return event; +} + +static void ZOOM_Event_destroy (ZOOM_Event event) +{ + xfree (event); +} + +static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event) +{ + if (c->m_queue_back) + { + c->m_queue_back->prev = event; + assert (c->m_queue_front); + } + else + { + assert (!c->m_queue_front); + c->m_queue_front = event; + } + event->next = c->m_queue_back; + event->prev = 0; + c->m_queue_back = event; +} + +static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c) +{ + ZOOM_Event event = c->m_queue_front; + if (!event) + return 0; + assert (c->m_queue_back); + c->m_queue_front = event->prev; + if (c->m_queue_front) + { + assert (c->m_queue_back); + c->m_queue_front->next = 0; + } + else + c->m_queue_back = 0; + c->last_event = event->kind; + return event; +} + +static void clear_error (ZOOM_connection c) +{ + + switch (c->error) + { + case ZOOM_ERROR_CONNECT: + case ZOOM_ERROR_MEMORY: + case ZOOM_ERROR_DECODE: + case ZOOM_ERROR_CONNECTION_LOST: + case ZOOM_ERROR_INIT: + case ZOOM_ERROR_INTERNAL: + break; + default: + c->error = ZOOM_ERROR_NONE; + xfree (c->addinfo); + c->addinfo = 0; + } +} + +ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which) +{ + ZOOM_task *taskp = &c->tasks; + while (*taskp) + taskp = &(*taskp)->next; + *taskp = (ZOOM_task) xmalloc (sizeof(**taskp)); + (*taskp)->running = 0; + (*taskp)->which = which; + (*taskp)->next = 0; + clear_error (c); + return *taskp; +} + +ZOOM_task ZOOM_connection_insert_task (ZOOM_connection c, int which) +{ + ZOOM_task task = (ZOOM_task) xmalloc (sizeof(*task)); + + task->next = c->tasks; + c->tasks = task; + + task->running = 0; + task->which = which; + clear_error (c); + return task; +} + +void ZOOM_connection_remove_task (ZOOM_connection c) +{ + ZOOM_task task = c->tasks; + + if (task) + { + c->tasks = task->next; + switch (task->which) + { + case ZOOM_TASK_SEARCH: + ZOOM_resultset_destroy (task->u.search.resultset); + break; + case ZOOM_TASK_RETRIEVE: + ZOOM_resultset_destroy (task->u.retrieve.resultset); + break; + case ZOOM_TASK_CONNECT: + break; + case ZOOM_TASK_SCAN: + ZOOM_scanset_destroy (task->u.scan.scan); + break; + case ZOOM_TASK_PACKAGE: + ZOOM_package_destroy (task->u.package); + break; + default: + assert (0); + } + xfree (task); + } +} + +void ZOOM_connection_remove_tasks (ZOOM_connection c) +{ + while (c->tasks) + ZOOM_connection_remove_task(c); +} + +static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos); + +ZOOM_API(ZOOM_connection) +ZOOM_connection_create (ZOOM_options options) +{ + ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c)); + + c->cs = 0; + c->mask = 0; + c->reconnect_ok = 0; + c->state = STATE_IDLE; + c->error = ZOOM_ERROR_NONE; + c->addinfo = 0; + c->buf_in = 0; + c->len_in = 0; + c->buf_out = 0; + c->len_out = 0; + c->resultsets = 0; + + c->options = ZOOM_options_create_with_parent(options); + + c->host_port = 0; + c->proxy = 0; + + c->charset = c->lang = 0; + + c->cookie_out = 0; + c->cookie_in = 0; + c->client_IP = 0; + c->tasks = 0; + + c->odr_in = odr_createmem (ODR_DECODE); + c->odr_out = odr_createmem (ODR_ENCODE); + + c->async = 0; + c->support_named_resultsets = 0; + c->last_event = ZOOM_EVENT_NONE; + + c->m_queue_front = 0; + c->m_queue_back = 0; + return c; +} + +/* set database names. Take local databases (if set); otherwise + take databases given in ZURL (if set); otherwise use Default */ +static char **set_DatabaseNames (ZOOM_connection con, ZOOM_options options, + int *num) +{ + char **databaseNames; + const char *c; + int no = 2; + const char *cp = ZOOM_options_get (options, "databaseName"); + + if (!cp || !*cp) + { + if (strncmp (con->host_port, "unix:", 5) == 0) + cp = strchr (con->host_port+5, ':'); + else + cp = strchr (con->host_port, '/'); + if (cp) + cp++; + } + if (cp) + { + c = cp; + while ((c = strchr(c, '+'))) + { + c++; + no++; + } + } + else + cp = "Default"; + databaseNames = (char**) + odr_malloc (con->odr_out, no * sizeof(*databaseNames)); + no = 0; + while (*cp) + { + c = strchr (cp, '+'); + if (!c) + c = cp + strlen(cp); + else if (c == cp) + { + cp++; + continue; + } + /* cp ptr to first char of db name, c is char + following db name */ + databaseNames[no] = (char*) odr_malloc (con->odr_out, 1+c-cp); + memcpy (databaseNames[no], cp, c-cp); + databaseNames[no++][c-cp] = '\0'; + cp = c; + if (*cp) + cp++; + } + databaseNames[no] = NULL; + *num = no; + return databaseNames; +} + +ZOOM_API(ZOOM_connection) +ZOOM_connection_new (const char *host, int portnum) +{ + ZOOM_connection c = ZOOM_connection_create (0); + + ZOOM_connection_connect (c, host, portnum); + return c; +} + +ZOOM_API(void) +ZOOM_connection_connect(ZOOM_connection c, + const char *host, int portnum) +{ + const char *val; + ZOOM_task task; + + if (c->cs) + { + yaz_log (LOG_DEBUG, "reconnect"); + c->reconnect_ok = 1; + return; + } + yaz_log(LOG_DEBUG, "connect"); + xfree (c->proxy); + val = ZOOM_options_get (c->options, "proxy"); + if (val && *val) + c->proxy = xstrdup (val); + else + c->proxy = 0; + + xfree (c->charset); + val = ZOOM_options_get (c->options, "charset"); + if (val && *val) + c->charset = xstrdup (val); + else + c->charset = 0; + + xfree (c->lang); + val = ZOOM_options_get (c->options, "lang"); + if (val && *val) + c->lang = xstrdup (val); + else + c->lang = 0; + + xfree (c->host_port); + if (portnum) + { + char hostn[128]; + sprintf (hostn, "%.80s:%d", host, portnum); + c->host_port = xstrdup(hostn); + } + else + c->host_port = xstrdup(host); + + ZOOM_options_set(c->options, "host", c->host_port); + + val = ZOOM_options_get (c->options, "cookie"); + if (val && *val) + c->cookie_out = xstrdup (val); + + val = ZOOM_options_get (c->options, "clientIP"); + if (val && *val) + c->client_IP = xstrdup (val); + + c->async = ZOOM_options_get_bool (c->options, "async", 0); + + c->error = ZOOM_ERROR_NONE; + + task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT); + + if (!c->async) + { + while (ZOOM_event (1, &c)) + ; + } +} + +ZOOM_API(ZOOM_query) +ZOOM_query_create(void) +{ + ZOOM_query s = (ZOOM_query) xmalloc (sizeof(*s)); + + s->refcount = 1; + s->query = 0; + s->sort_spec = 0; + s->odr = odr_createmem (ODR_ENCODE); + + return s; +} + +ZOOM_API(void) +ZOOM_query_destroy(ZOOM_query s) +{ + if (!s) + return; + + (s->refcount)--; + yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount); + if (s->refcount == 0) + { + odr_destroy (s->odr); + xfree (s); + } +} + +ZOOM_API(int) +ZOOM_query_prefix(ZOOM_query s, const char *str) +{ + s->query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->query)); + s->query->which = Z_Query_type_1; + s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str); + if (!s->query->u.type_1) + return -1; + return 0; +} + +ZOOM_API(int) +ZOOM_query_sortby(ZOOM_query s, const char *criteria) +{ + s->sort_spec = yaz_sort_spec (s->odr, criteria); + if (!s->sort_spec) + return -1; + return 0; +} + +static int do_write(ZOOM_connection c); + +ZOOM_API(void) +ZOOM_connection_destroy(ZOOM_connection c) +{ + ZOOM_resultset r; + if (!c) + return; + if (c->cs) + cs_close (c->cs); + for (r = c->resultsets; r; r = r->next) + r->connection = 0; + + xfree (c->buf_in); + xfree (c->addinfo); + odr_destroy (c->odr_in); + odr_destroy (c->odr_out); + ZOOM_options_destroy (c->options); + ZOOM_connection_remove_tasks (c); + xfree (c->host_port); + xfree (c->proxy); + xfree (c->charset); + xfree (c->lang); + xfree (c->cookie_out); + xfree (c->cookie_in); + xfree (c->client_IP); + xfree (c); +} + +void ZOOM_resultset_addref (ZOOM_resultset r) +{ + if (r) + (r->refcount)++; +} +ZOOM_resultset ZOOM_resultset_create () +{ + ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r)); + + r->refcount = 1; + r->size = 0; + r->odr = odr_createmem (ODR_ENCODE); + r->start = 0; + r->piggyback = 1; + r->setname = 0; + r->count = 0; + r->record_cache = 0; + r->r_sort_spec = 0; + r->r_query = 0; + r->search = 0; + r->connection = 0; + r->next = 0; + return r; +} + +ZOOM_API(ZOOM_resultset) +ZOOM_connection_search_pqf(ZOOM_connection c, const char *q) +{ + ZOOM_resultset r; + ZOOM_query s = ZOOM_query_create(); + + ZOOM_query_prefix (s, q); + + r = ZOOM_connection_search (c, s); + ZOOM_query_destroy (s); + return r; +} + +ZOOM_API(ZOOM_resultset) +ZOOM_connection_search(ZOOM_connection c, ZOOM_query q) +{ + ZOOM_resultset r = ZOOM_resultset_create (); + ZOOM_task task; + const char *cp; + + r->r_sort_spec = q->sort_spec; + r->r_query = q->query; + r->search = q; + + r->options = ZOOM_options_create_with_parent(c->options); + + r->start = ZOOM_options_get_int(r->options, "start", 0); + r->count = ZOOM_options_get_int(r->options, "count", 0); + r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1); + cp = ZOOM_options_get (r->options, "setname"); + if (cp) + r->setname = xstrdup (cp); + + r->connection = c; + + r->next = c->resultsets; + c->resultsets = r; + + task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH); + task->u.search.resultset = r; + ZOOM_resultset_addref (r); + + (q->refcount)++; + + if (!c->async) + { + while (ZOOM_event (1, &c)) + ; + } + return r; +} + +ZOOM_API(void) +ZOOM_resultset_destroy(ZOOM_resultset r) +{ + if (!r) + return; + (r->refcount)--; + yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount); + if (r->refcount == 0) + { + ZOOM_record_cache rc; + + for (rc = r->record_cache; rc; rc = rc->next) + if (rc->rec.wrbuf_marc) + wrbuf_free (rc->rec.wrbuf_marc, 1); + if (r->connection) + { + /* remove ourselves from the resultsets in connection */ + ZOOM_resultset *rp = &r->connection->resultsets; + while (1) + { + assert (*rp); /* we must be in this list!! */ + if (*rp == r) + { /* OK, we're here - take us out of it */ + *rp = (*rp)->next; + break; + } + rp = &(*rp)->next; + } + } + ZOOM_query_destroy (r->search); + ZOOM_options_destroy (r->options); + odr_destroy (r->odr); + xfree (r->setname); + xfree (r); + } +} + +ZOOM_API(size_t) +ZOOM_resultset_size (ZOOM_resultset r) +{ + return r->size; +} + +static void do_close (ZOOM_connection c) +{ + if (c->cs) + cs_close(c->cs); + c->cs = 0; + c->mask = 0; + c->state = STATE_IDLE; +} + +static void ZOOM_resultset_retrieve (ZOOM_resultset r, + int force_sync, int start, int count) +{ + ZOOM_task task; + ZOOM_connection c; + + if (!r) + return; + c = r->connection; + if (!c) + return; + task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE); + task->u.retrieve.resultset = r; + task->u.retrieve.start = start; + task->u.retrieve.count = count; + + ZOOM_resultset_addref (r); + + if (!r->connection->async || force_sync) + while (r->connection && ZOOM_event (1, &r->connection)) + ; +} + +ZOOM_API(void) +ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs, + size_t start, size_t count) +{ + int force_present = 0; + + if (!r) + return ; + if (count && recs) + force_present = 1; + ZOOM_resultset_retrieve (r, force_present, start, count); + if (force_present) + { + size_t i; + for (i = 0; i< count; i++) + recs[i] = ZOOM_resultset_record_immediate (r, i+start); + } +} + +static int do_connect (ZOOM_connection c) +{ + void *add; + const char *effective_host; + + if (c->proxy) + effective_host = c->proxy; + else + effective_host = c->host_port; + + yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host); + + assert (!c->cs); + c->cs = cs_create_host (effective_host, 0, &add); + + if (c->cs) + { + int ret = cs_connect (c->cs, add); + if (ret == 0) + { + ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT); + ZOOM_connection_put_event(c, event); + ZOOM_connection_send_init(c); + c->state = STATE_ESTABLISHED; + return 1; + } + else if (ret > 0) + { + c->state = STATE_CONNECTING; + c->mask = ZOOM_SELECT_EXCEPT; + if (c->cs->io_pending & CS_WANT_WRITE) + c->mask += ZOOM_SELECT_WRITE; + if (c->cs->io_pending & CS_WANT_READ) + c->mask += ZOOM_SELECT_READ; + return 1; + } + } + c->state = STATE_IDLE; + c->error = ZOOM_ERROR_CONNECT; + return 0; +} + +int z3950_connection_socket(ZOOM_connection c) +{ + if (c->cs) + return cs_fileno(c->cs); + return -1; +} + +int z3950_connection_mask(ZOOM_connection c) +{ + if (c->cs) + return c->mask; + return 0; +} + +static void otherInfo_attach (ZOOM_connection c, Z_APDU *a, ODR out) +{ + int i; + for (i = 0; i<200; i++) + { + size_t len; + Z_OtherInformation **oi; + char buf[80]; + const char *val; + const char *cp; + int oidval; + + sprintf (buf, "otherInfo%d", i); + val = ZOOM_options_get (c->options, buf); + if (!val) + break; + cp = strchr (val, ':'); + if (!cp) + continue; + len = cp - val; + if (len >= sizeof(buf)) + len = sizeof(buf)-1; + memcpy (buf, val, len); + buf[len] = '\0'; + oidval = oid_getvalbyname (buf); + if (oidval == VAL_NONE) + continue; + + yaz_oi_APDU(a, &oi); + yaz_oi_set_string_oidval(oi, out, oidval, 1, cp+1); + } +} + +static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out) +{ + assert (a); + if (c->cookie_out) + { + Z_OtherInformation **oi; + yaz_oi_APDU(a, &oi); + yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out); + } + if (c->client_IP) + { + Z_OtherInformation **oi; + yaz_oi_APDU(a, &oi); + yaz_oi_set_string_oidval(oi, out, VAL_CLIENT_IP, 1, c->client_IP); + } + otherInfo_attach (c, a, out); + if (!z_APDU(out, &a, 0, 0)) + { + FILE *outf = fopen("/tmp/apdu.txt", "a"); + if (a && outf) + { + ODR odr_pr = odr_createmem(ODR_PRINT); + fprintf (outf, "a=%p\n", a); + odr_setprint(odr_pr, outf); + z_APDU(odr_pr, &a, 0, 0); + odr_destroy(odr_pr); + } + c->error = ZOOM_ERROR_ENCODE; + do_close (c); + return -1; + } + + return 0; +} + +static int send_APDU (ZOOM_connection c, Z_APDU *a) +{ + ZOOM_Event event; + assert (a); + if (encode_APDU(c, a, c->odr_out)) + return -1; + c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0); + event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU); + ZOOM_connection_put_event (c, event); + odr_reset(c->odr_out); + do_write (c); + return 0; +} + +static int ZOOM_connection_send_init (ZOOM_connection c) +{ + const char *impname; + Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest); + Z_InitRequest *ireq = apdu->u.initRequest; + Z_IdAuthentication *auth = (Z_IdAuthentication *) + odr_malloc(c->odr_out, sizeof(*auth)); + const char *auth_groupId = ZOOM_options_get (c->options, "group"); + const char *auth_userId = ZOOM_options_get (c->options, "user"); + const char *auth_password = ZOOM_options_get (c->options, "pass"); + + ODR_MASK_SET(ireq->options, Z_Options_search); + ODR_MASK_SET(ireq->options, Z_Options_present); + ODR_MASK_SET(ireq->options, Z_Options_scan); + ODR_MASK_SET(ireq->options, Z_Options_sort); + ODR_MASK_SET(ireq->options, Z_Options_extendedServices); + ODR_MASK_SET(ireq->options, Z_Options_namedResultSets); + + ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1); + ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2); + ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3); + + impname = ZOOM_options_get (c->options, "implementationName"); + ireq->implementationName = + (char *) odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0)); + strcpy (ireq->implementationName, ""); + if (impname) + { + strcat (ireq->implementationName, impname); + strcat (ireq->implementationName, "/"); + } + strcat (ireq->implementationName, "ZOOM-C/YAZ"); + + *ireq->maximumRecordSize = + ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024); + *ireq->preferredMessageSize = + ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024); + + if (auth_groupId || auth_password) + { + Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass)); + int i = 0; + pass->groupId = 0; + if (auth_groupId && *auth_groupId) + { + pass->groupId = (char *) + odr_malloc(c->odr_out, strlen(auth_groupId)+1); + strcpy(pass->groupId, auth_groupId); + i++; + } + pass->userId = 0; + if (auth_userId && *auth_userId) + { + pass->userId = (char *) + odr_malloc(c->odr_out, strlen(auth_userId)+1); + strcpy(pass->userId, auth_userId); + i++; + } + pass->password = 0; + if (auth_password && *auth_password) + { + pass->password = (char *) + odr_malloc(c->odr_out, strlen(auth_password)+1); + strcpy(pass->password, auth_password); + i++; + } + if (i) + { + auth->which = Z_IdAuthentication_idPass; + auth->u.idPass = pass; + ireq->idAuthentication = auth; + } + } + else if (auth_userId) + { + auth->which = Z_IdAuthentication_open; + auth->u.open = (char *) + odr_malloc(c->odr_out, strlen(auth_userId)+1); + strcpy(auth->u.open, auth_userId); + ireq->idAuthentication = auth; + } + if (c->proxy) + yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out, + VAL_PROXY, 1, c->host_port); + if (c->charset||c->lang) + { + Z_OtherInformation **oi; + Z_OtherInformationUnit *oi_unit; + + yaz_oi_APDU(apdu, &oi); + + if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0))) + { + ODR_MASK_SET(ireq->options, Z_Options_negotiationModel); + + oi_unit->which = Z_OtherInfo_externallyDefinedInfo; + oi_unit->information.externallyDefinedInfo = + yaz_set_proposal_charneg + (c->odr_out, + (const char **)&c->charset, (c->charset) ? 1:0, + (const char **)&c->lang, (c->lang) ? 1:0, 1); + } + } + assert (apdu); + send_APDU (c, apdu); + + return 0; +} + +static int ZOOM_connection_send_search (ZOOM_connection c) +{ + ZOOM_resultset r; + int lslb, ssub, mspn; + const char *syntax; + Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest); + Z_SearchRequest *search_req = apdu->u.searchRequest; + const char *elementSetName; + const char *smallSetElementSetName; + const char *mediumSetElementSetName; + const char *schema; + + assert (c->tasks); + assert (c->tasks->which == ZOOM_TASK_SEARCH); + + r = c->tasks->u.search.resultset; + + elementSetName = + ZOOM_options_get (r->options, "elementSetName"); + smallSetElementSetName = + ZOOM_options_get (r->options, "smallSetElementSetName"); + mediumSetElementSetName = + ZOOM_options_get (r->options, "mediumSetElementSetName"); + schema = + ZOOM_options_get (r->options, "schema"); + + if (!smallSetElementSetName) + smallSetElementSetName = elementSetName; + + if (!mediumSetElementSetName) + mediumSetElementSetName = elementSetName; + + assert (r); + assert (r->r_query); + + /* prepare query for the search request */ + search_req->query = r->r_query; + + search_req->databaseNames = + set_DatabaseNames (c, r->options, &search_req->num_databaseNames); + + /* get syntax (no need to provide unless piggyback is in effect) */ + syntax = ZOOM_options_get (r->options, "preferredRecordSyntax"); + + lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1); + ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1); + mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1); + if (lslb != -1 && ssub != -1 && mspn != -1) + { + /* So're a Z39.50 expert? Let's hope you don't do sort */ + *search_req->largeSetLowerBound = lslb; + *search_req->smallSetUpperBound = ssub; + *search_req->mediumSetPresentNumber = mspn; + } + else if (r->start == 0 && r->count > 0 + && r->piggyback && !r->r_sort_spec && !schema) + { + /* Regular piggyback - do it unless we're going to do sort */ + *search_req->largeSetLowerBound = 2000000000; + *search_req->smallSetUpperBound = r->count; + *search_req->mediumSetPresentNumber = r->count; + smallSetElementSetName = 0; /* no need to provide this */ + } + else + { + /* non-piggyback. Need not provide elementsets or syntaxes .. */ + smallSetElementSetName = 0; + mediumSetElementSetName = 0; + syntax = 0; + } + if (smallSetElementSetName && *smallSetElementSetName) + { + Z_ElementSetNames *esn = (Z_ElementSetNames *) + odr_malloc (c->odr_out, sizeof(*esn)); + + esn->which = Z_ElementSetNames_generic; + esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName); + search_req->smallSetElementSetNames = esn; + } + if (mediumSetElementSetName && *mediumSetElementSetName) + { + Z_ElementSetNames *esn = (Z_ElementSetNames *) + odr_malloc (c->odr_out, sizeof(*esn)); + + esn->which = Z_ElementSetNames_generic; + esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName); + search_req->mediumSetElementSetNames = esn; + } + if (syntax) + search_req->preferredRecordSyntax = + yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax); + + if (!r->setname) + { + if (c->support_named_resultsets) + { + char setname[14]; + int ord; + /* find the lowest unused ordinal so that we re-use + result sets on the server. */ + for (ord = 1; ; ord++) + { + ZOOM_resultset rp; + sprintf (setname, "%d", ord); + for (rp = c->resultsets; rp; rp = rp->next) + if (rp->setname && !strcmp (rp->setname, setname)) + break; + if (!rp) + break; + } + r->setname = xstrdup (setname); + yaz_log (LOG_DEBUG, "allocating %s", r->setname); + } + else + r->setname = xstrdup ("default"); + ZOOM_options_set (r->options, "setname", r->setname); + } + search_req->resultSetName = odr_strdup(c->odr_out, r->setname); + /* send search request */ + send_APDU (c, apdu); + return 1; +} + +static void response_diag (ZOOM_connection c, Z_DiagRec *p) +{ + Z_DefaultDiagFormat *r; + char *addinfo = 0; + + xfree (c->addinfo); + c->addinfo = 0; + if (p->which != Z_DiagRec_defaultFormat) + { + c->error = ZOOM_ERROR_DECODE; + return; + } + r = p->u.defaultFormat; + switch (r->which) + { + case Z_DefaultDiagFormat_v2Addinfo: + addinfo = r->u.v2Addinfo; + break; + case Z_DefaultDiagFormat_v3Addinfo: + addinfo = r->u.v3Addinfo; + break; + } + if (addinfo) + c->addinfo = xstrdup (addinfo); + c->error = *r->condition; +} + +ZOOM_API(ZOOM_record) +ZOOM_record_clone (ZOOM_record srec) +{ + char *buf; + int size; + ODR odr_enc; + ZOOM_record nrec; + + odr_enc = odr_createmem(ODR_ENCODE); + if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0)) + return 0; + buf = odr_getbuf (odr_enc, &size, 0); + + nrec = (ZOOM_record) xmalloc (sizeof(*nrec)); + nrec->odr = odr_createmem(ODR_DECODE); + nrec->wrbuf_marc = 0; + odr_setbuf (nrec->odr, buf, size, 0); + z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0); + + odr_destroy (odr_enc); + return nrec; +} + +ZOOM_API(ZOOM_record) +ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos) +{ + return record_cache_lookup (s, pos); +} + +ZOOM_API(ZOOM_record) +ZOOM_resultset_record (ZOOM_resultset r, size_t pos) +{ + ZOOM_resultset_retrieve (r, 1, pos, 1); + return ZOOM_resultset_record_immediate (r, pos); +} + +ZOOM_API(void) +ZOOM_record_destroy (ZOOM_record rec) +{ + if (!rec) + return; + if (rec->wrbuf_marc) + wrbuf_free (rec->wrbuf_marc, 1); + odr_destroy (rec->odr); + xfree (rec); +} + +ZOOM_API(const char *) +ZOOM_record_get (ZOOM_record rec, const char *type, int *len) +{ + Z_NamePlusRecord *npr; + + if (len) + *len = 0; /* default return */ + + if (!rec) + return 0; + npr = rec->npr; + if (!npr) + return 0; + if (!strcmp (type, "database")) + { + if (len) + *len = (npr->databaseName ? strlen(npr->databaseName) : 0); + return npr->databaseName; + } + else if (!strcmp (type, "syntax")) + { + if (npr->which == Z_NamePlusRecord_databaseRecord) + { + Z_External *r = (Z_External *) npr->u.databaseRecord; + oident *ent = oid_getentbyoid(r->direct_reference); + if (ent) + { + if (len) + *len = strlen(ent->desc); + return ent->desc; + } + } + return "none"; + } + else if (!strcmp (type, "render") && + npr->which == Z_NamePlusRecord_databaseRecord) + { + Z_External *r = (Z_External *) npr->u.databaseRecord; + oident *ent = oid_getentbyoid(r->direct_reference); + + if (r->which == Z_External_sutrs) + { + if (len) *len = r->u.sutrs->len; + return (const char *) r->u.sutrs->buf; + } + else if (r->which == Z_External_octet) + { + switch (ent->value) + { + case VAL_SOIF: + case VAL_HTML: + case VAL_SUTRS: + break; + case VAL_TEXT_XML: + case VAL_APPLICATION_XML: + break; + default: + if (!rec->wrbuf_marc) + rec->wrbuf_marc = wrbuf_alloc(); + wrbuf_rewind (rec->wrbuf_marc); + if (yaz_marc_decode ((const char *) + r->u.octet_aligned->buf, + rec->wrbuf_marc, 0, + r->u.octet_aligned->len, + 0) > 0) + { + if (len) *len = wrbuf_len(rec->wrbuf_marc); + return wrbuf_buf(rec->wrbuf_marc); + } + } + if (len) *len = r->u.octet_aligned->len; + return (const char *) r->u.octet_aligned->buf; + } + else if (r->which == Z_External_grs1) + { + if (len) *len = 5; + return "GRS-1"; + } + return 0; + } + else if (!strcmp (type, "xml") && + npr->which == Z_NamePlusRecord_databaseRecord) + { + Z_External *r = (Z_External *) npr->u.databaseRecord; + oident *ent = oid_getentbyoid(r->direct_reference); + + if (r->which == Z_External_sutrs) + { + if (len) *len = r->u.sutrs->len; + return (const char *) r->u.sutrs->buf; + } + else if (r->which == Z_External_octet) + { + switch (ent->value) + { + case VAL_SOIF: + case VAL_HTML: + case VAL_SUTRS: + break; + case VAL_TEXT_XML: + case VAL_APPLICATION_XML: + break; + default: + if (!rec->wrbuf_marc) + rec->wrbuf_marc = wrbuf_alloc(); + wrbuf_rewind (rec->wrbuf_marc); + if (yaz_marc_decode ((const char *) + r->u.octet_aligned->buf, + rec->wrbuf_marc, 0, + r->u.octet_aligned->len, + 1) > 0) + { + if (len) *len = wrbuf_len(rec->wrbuf_marc); + return wrbuf_buf(rec->wrbuf_marc); + } + } + if (len) *len = r->u.octet_aligned->len; + return (const char *) r->u.octet_aligned->buf; + } + else if (r->which == Z_External_grs1) + { + if (len) *len = 5; + return "GRS-1"; + } + return 0; + } + else if (!strcmp (type, "raw")) + { + if (npr->which == Z_NamePlusRecord_databaseRecord) + { + Z_External *r = (Z_External *) npr->u.databaseRecord; + + if (r->which == Z_External_sutrs) + { + if (len) *len = r->u.sutrs->len; + return (const char *) r->u.sutrs->buf; + } + else if (r->which == Z_External_octet) + { + if (len) *len = r->u.octet_aligned->len; + return (const char *) r->u.octet_aligned->buf; + } + else /* grs-1, explain, ... */ + { + if (len) *len = -1; + return (const char *) npr->u.databaseRecord; + } + } + return 0; + } + else if (!strcmp (type, "ext")) + { + if (npr->which == Z_NamePlusRecord_databaseRecord) + return (const char *) npr->u.databaseRecord; + return 0; + } + return 0; +} + +static void record_cache_add (ZOOM_resultset r, Z_NamePlusRecord *npr, int pos) +{ + ZOOM_record_cache rc; + const char *elementSetName = + ZOOM_resultset_option_get (r, "elementSetName"); + const char *syntax = + ZOOM_resultset_option_get (r, "preferredRecordSyntax"); + + + for (rc = r->record_cache; rc; rc = rc->next) + { + if (pos == rc->pos) + { + if ((!elementSetName && !rc->elementSetName) + || (elementSetName && rc->elementSetName && + !strcmp (elementSetName, rc->elementSetName))) + { + if ((!syntax && !rc->syntax) + || (syntax && rc->syntax && + !strcmp (syntax, rc->syntax))) + { + /* not destroying rc->npr (it's handled by nmem )*/ + rc->rec.npr = npr; + /* keeping wrbuf_marc too */ + return; + } + } + } + } + rc = (ZOOM_record_cache) odr_malloc (r->odr, sizeof(*rc)); + rc->rec.npr = npr; + rc->rec.odr = 0; + rc->rec.wrbuf_marc = 0; + if (elementSetName) + rc->elementSetName = odr_strdup (r->odr, elementSetName); + else + rc->elementSetName = 0; + + if (syntax) + rc->syntax = odr_strdup (r->odr, syntax); + else + rc->syntax = 0; + + rc->pos = pos; + rc->next = r->record_cache; + r->record_cache = rc; +} + +static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos) +{ + ZOOM_record_cache rc; + const char *elementSetName = + ZOOM_resultset_option_get (r, "elementSetName"); + const char *syntax = + ZOOM_resultset_option_get (r, "preferredRecordSyntax"); + + for (rc = r->record_cache; rc; rc = rc->next) + { + if (pos == rc->pos) + { + if ((!elementSetName && !rc->elementSetName) + || (elementSetName && rc->elementSetName && + !strcmp (elementSetName, rc->elementSetName))) + { + if ((!syntax && !rc->syntax) + || (syntax && rc->syntax && + !strcmp (syntax, rc->syntax))) + return &rc->rec; + } + } + } + return 0; +} + +static void handle_records (ZOOM_connection c, Z_Records *sr, + int present_phase) +{ + ZOOM_resultset resultset; + + if (!c->tasks) + return ; + switch (c->tasks->which) + { + case ZOOM_TASK_SEARCH: + resultset = c->tasks->u.search.resultset; + break; + case ZOOM_TASK_RETRIEVE: + resultset = c->tasks->u.retrieve.resultset; + break; + default: + return; + } + if (sr && sr->which == Z_Records_NSD) + { + Z_DiagRec dr, *dr_p = &dr; + dr.which = Z_DiagRec_defaultFormat; + dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic; + + response_diag (c, dr_p); + } + else if (sr && sr->which == Z_Records_multipleNSD) + { + if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1) + response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]); + else + c->error = ZOOM_ERROR_DECODE; + } + else + { + if (resultset->count + resultset->start > resultset->size) + resultset->count = resultset->size - resultset->start; + if (resultset->count < 0) + resultset->count = 0; + if (sr && sr->which == Z_Records_DBOSD) + { + int i; + NMEM nmem = odr_extract_mem (c->odr_in); + Z_NamePlusRecordList *p = + sr->u.databaseOrSurDiagnostics; + for (i = 0; inum_records; i++) + { + record_cache_add (resultset, p->records[i], + i+ resultset->start); + } + /* transfer our response to search_nmem .. we need it later */ + nmem_transfer (resultset->odr->mem, nmem); + nmem_destroy (nmem); + if (present_phase && p->num_records == 0) + { + /* present response and we didn't get any records! */ + c->error = ZOOM_ERROR_DECODE; + } + } + else if (present_phase) + { + /* present response and we didn't get any records! */ + c->error = ZOOM_ERROR_DECODE; + } + } +} + +static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr) +{ + handle_records (c, pr->records, 1); +} + +static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr) +{ + ZOOM_resultset resultset; + + yaz_log (LOG_DEBUG, "got search response"); + + if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH) + return ; + + resultset = c->tasks->u.search.resultset; + + resultset->size = *sr->resultCount; + handle_records (c, sr->records, 0); +} + +static void sort_response (ZOOM_connection c, Z_SortResponse *res) +{ + if (res->diagnostics && res->num_diagnostics > 0) + response_diag (c, res->diagnostics[0]); +} + +static int scan_response (ZOOM_connection c, Z_ScanResponse *res) +{ + NMEM nmem = odr_extract_mem (c->odr_in); + ZOOM_scanset scan; + + if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN) + return 0; + scan = c->tasks->u.scan.scan; + + if (res->entries && res->entries->nonsurrogateDiagnostics) + response_diag(c, res->entries->nonsurrogateDiagnostics[0]); + scan->scan_response = res; + nmem_transfer (scan->odr->mem, nmem); + if (res->stepSize) + ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize); + if (res->positionOfTerm) + ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm); + if (res->scanStatus) + ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus); + if (res->numberOfEntriesReturned) + ZOOM_options_set_int (scan->options, "number", + *res->numberOfEntriesReturned); + nmem_destroy (nmem); + return 1; +} + +static int send_sort (ZOOM_connection c) +{ + ZOOM_resultset resultset; + + if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH) + return 0; + + resultset = c->tasks->u.search.resultset; + + if (c->error) + { + resultset->r_sort_spec = 0; + return 0; + } + if (resultset->r_sort_spec) + { + Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest); + Z_SortRequest *req = apdu->u.sortRequest; + + req->num_inputResultSetNames = 1; + req->inputResultSetNames = (Z_InternationalString **) + odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames)); + req->inputResultSetNames[0] = + odr_strdup (c->odr_out, resultset->setname); + req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname); + req->sortSequence = resultset->r_sort_spec; + resultset->r_sort_spec = 0; + send_APDU (c, apdu); + return 1; + } + return 0; +} + +static int send_present (ZOOM_connection c) +{ + Z_APDU *apdu = 0; + Z_PresentRequest *req = 0; + int i = 0; + const char *syntax = 0; + const char *elementSetName = 0; + const char *schema = 0; + ZOOM_resultset resultset; + + if (!c->tasks) + return 0; + + switch (c->tasks->which) + { + case ZOOM_TASK_SEARCH: + resultset = c->tasks->u.search.resultset; + break; + case ZOOM_TASK_RETRIEVE: + resultset = c->tasks->u.retrieve.resultset; + resultset->start = c->tasks->u.retrieve.start; + resultset->count = c->tasks->u.retrieve.count; + + if (resultset->start >= resultset->size) + return 0; + if (resultset->start + resultset->count > resultset->size) + resultset->count = resultset->size - resultset->start; + break; + default: + return 0; + } + + syntax = ZOOM_resultset_option_get (resultset, "preferredRecordSyntax"); + elementSetName = ZOOM_resultset_option_get (resultset, "elementSetName"); + schema = ZOOM_resultset_option_get (resultset, "schema"); + + if (c->error) /* don't continue on error */ + return 0; + if (resultset->start < 0) + return 0; + for (i = 0; icount; i++) + { + ZOOM_record rec = + record_cache_lookup (resultset, i + resultset->start); + if (!rec) + break; + } + if (i == resultset->count) + return 0; + + apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest); + req = apdu->u.presentRequest; + + resultset->start += i; + resultset->count -= i; + *req->resultSetStartPoint = resultset->start + 1; + *req->numberOfRecordsRequested = resultset->count; + assert (*req->numberOfRecordsRequested > 0); + + if (syntax && *syntax) + req->preferredRecordSyntax = + yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax); + + if (schema && *schema) + { + Z_RecordComposition *compo = (Z_RecordComposition *) + odr_malloc (c->odr_out, sizeof(*compo)); + + req->recordComposition = compo; + compo->which = Z_RecordComp_complex; + compo->u.complex = (Z_CompSpec *) + odr_malloc(c->odr_out, sizeof(*compo->u.complex)); + compo->u.complex->selectAlternativeSyntax = (bool_t *) + odr_malloc(c->odr_out, sizeof(bool_t)); + *compo->u.complex->selectAlternativeSyntax = 0; + + compo->u.complex->generic = (Z_Specification *) + odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic)); + + compo->u.complex->generic->schema = (Odr_oid *) + yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema); + + if (!compo->u.complex->generic->schema) + { + /* OID wasn't a schema! Try record syntax instead. */ + + compo->u.complex->generic->schema = (Odr_oid *) + yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema); + } + if (elementSetName && *elementSetName) + { + compo->u.complex->generic->elementSpec = (Z_ElementSpec *) + odr_malloc(c->odr_out, sizeof(Z_ElementSpec)); + compo->u.complex->generic->elementSpec->which = + Z_ElementSpec_elementSetName; + compo->u.complex->generic->elementSpec->u.elementSetName = + odr_strdup (c->odr_out, elementSetName); + } + else + compo->u.complex->generic->elementSpec = 0; + compo->u.complex->num_dbSpecific = 0; + compo->u.complex->dbSpecific = 0; + compo->u.complex->num_recordSyntax = 0; + compo->u.complex->recordSyntax = 0; + } + else if (elementSetName && *elementSetName) + { + Z_ElementSetNames *esn = (Z_ElementSetNames *) + odr_malloc (c->odr_out, sizeof(*esn)); + Z_RecordComposition *compo = (Z_RecordComposition *) + odr_malloc (c->odr_out, sizeof(*compo)); + + esn->which = Z_ElementSetNames_generic; + esn->u.generic = odr_strdup (c->odr_out, elementSetName); + compo->which = Z_RecordComp_simple; + compo->u.simple = esn; + req->recordComposition = compo; + } + req->resultSetId = odr_strdup(c->odr_out, resultset->setname); + send_APDU (c, apdu); + return 1; +} + +ZOOM_API(ZOOM_scanset) +ZOOM_connection_scan (ZOOM_connection c, const char *start) +{ + ZOOM_scanset scan = (ZOOM_scanset) xmalloc (sizeof(*scan)); + + scan->connection = c; + scan->odr = odr_createmem (ODR_DECODE); + scan->options = ZOOM_options_create_with_parent (c->options); + scan->refcount = 1; + scan->scan_response = 0; + + if ((scan->termListAndStartPoint = + p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet, + start))) + { + ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN); + task->u.scan.scan = scan; + + (scan->refcount)++; + if (!c->async) + { + while (ZOOM_event (1, &c)) + ; + } + } + return scan; +} + +ZOOM_API(void) +ZOOM_scanset_destroy (ZOOM_scanset scan) +{ + if (!scan) + return; + (scan->refcount)--; + if (scan->refcount == 0) + { + odr_destroy (scan->odr); + + ZOOM_options_destroy (scan->options); + xfree (scan); + } +} + +static int send_package (ZOOM_connection c) +{ + ZOOM_Event event; + if (!c->tasks) + return 0; + assert (c->tasks->which == ZOOM_TASK_PACKAGE); + + event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU); + ZOOM_connection_put_event (c, event); + + do_write_ex (c, c->tasks->u.package->buf_out, + c->tasks->u.package->len_out); + return 1; +} + +static int send_scan (ZOOM_connection c) +{ + ZOOM_scanset scan; + Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest); + Z_ScanRequest *req = apdu->u.scanRequest; + if (!c->tasks) + return 0; + assert (c->tasks->which == ZOOM_TASK_SCAN); + scan = c->tasks->u.scan.scan; + + req->termListAndStartPoint = scan->termListAndStartPoint; + req->attributeSet = scan->attributeSet; + + *req->numberOfTermsRequested = + ZOOM_options_get_int(scan->options, "number", 10); + + req->preferredPositionInResponse = + odr_intdup (c->odr_out, + ZOOM_options_get_int(scan->options, "position", 1)); + + req->stepSize = + odr_intdup (c->odr_out, + ZOOM_options_get_int(scan->options, "stepSize", 0)); + + req->databaseNames = set_DatabaseNames (c, scan->options, + &req->num_databaseNames); + + send_APDU (c, apdu); + + return 1; +} + +ZOOM_API(size_t) +ZOOM_scanset_size (ZOOM_scanset scan) +{ + if (!scan || !scan->scan_response || !scan->scan_response->entries) + return 0; + return scan->scan_response->entries->num_entries; +} + +ZOOM_API(const char *) +ZOOM_scanset_term (ZOOM_scanset scan, size_t pos, + int *occ, int *len) +{ + const char *term = 0; + size_t noent = ZOOM_scanset_size (scan); + Z_ScanResponse *res = scan->scan_response; + + *len = 0; + *occ = 0; + if (pos >= noent) + return 0; + if (res->entries->entries[pos]->which == Z_Entry_termInfo) + { + Z_TermInfo *t = res->entries->entries[pos]->u.termInfo; + + if (t->term->which == Z_Term_general) + { + term = (const char *) t->term->u.general->buf; + *len = t->term->u.general->len; + } + *occ = t->globalOccurrences ? *t->globalOccurrences : 0; + } + return term; +} + +ZOOM_API(const char *) +ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key) +{ + return ZOOM_options_get (scan->options, key); +} + +ZOOM_API(void) +ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key, + const char *val) +{ + ZOOM_options_set (scan->options, key, val); +} + + + +static Z_APDU *create_es_package (ZOOM_package p, int type) +{ + const char *str; + Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest); + Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest; + + *req->function = Z_ExtendedServicesRequest_create; + + str = ZOOM_options_get(p->options, "package-name"); + if (str && *str) + req->packageName = nmem_strdup (p->odr_out->mem, str); + + str = ZOOM_options_get(p->options, "user-id"); + if (str) + req->userId = nmem_strdup (p->odr_out->mem, str); + + req->packageType = yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV, + type); + + str = ZOOM_options_get(p->options, "function"); + if (str) + { + if (!strcmp (str, "create")) + *req->function = 1; + if (!strcmp (str, "delete")) + *req->function = 2; + if (!strcmp (str, "modify")) + *req->function = 3; + } + return apdu; +} + +static const char *ill_array_lookup (void *clientData, const char *idx) +{ + ZOOM_package p = (ZOOM_package) clientData; + return ZOOM_options_get (p->options, idx+4); +} + +static Z_External *encode_ill_request (ZOOM_package p) +{ + ODR out = p->odr_out; + ILL_Request *req; + Z_External *r = 0; + struct ill_get_ctl ctl; + + ctl.odr = p->odr_out; + ctl.clientData = p; + ctl.f = ill_array_lookup; + + req = ill_get_ILLRequest(&ctl, "ill", 0); + + if (!ill_Request (out, &req, 0, 0)) + { + int ill_request_size; + char *ill_request_buf = odr_getbuf (out, &ill_request_size, 0); + if (ill_request_buf) + odr_setbuf (out, ill_request_buf, ill_request_size, 1); + return 0; + } + else + { + oident oid; + int illRequest_size = 0; + char *illRequest_buf = odr_getbuf (out, &illRequest_size, 0); + + oid.proto = PROTO_GENERAL; + oid.oclass = CLASS_GENERAL; + oid.value = VAL_ISO_ILL_1; + + r = (Z_External *) odr_malloc (out, sizeof(*r)); + r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid)); + r->indirect_reference = 0; + r->descriptor = 0; + r->which = Z_External_single; + + r->u.single_ASN1_type = (Odr_oct *) + odr_malloc (out, sizeof(*r->u.single_ASN1_type)); + r->u.single_ASN1_type->buf = odr_malloc (out, illRequest_size); + r->u.single_ASN1_type->len = illRequest_size; + r->u.single_ASN1_type->size = illRequest_size; + memcpy (r->u.single_ASN1_type->buf, illRequest_buf, illRequest_size); + } + return r; +} + +static Z_ItemOrder *encode_item_order(ZOOM_package p) +{ + Z_ItemOrder *req = odr_malloc (p->odr_out, sizeof(*req)); + const char *str; + + req->which=Z_IOItemOrder_esRequest; + req->u.esRequest = (Z_IORequest *) + odr_malloc(p->odr_out,sizeof(Z_IORequest)); + + /* to keep part ... */ + req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *) + odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep)); + req->u.esRequest->toKeep->supplDescription = 0; + req->u.esRequest->toKeep->contact = + odr_malloc (p->odr_out, sizeof(*req->u.esRequest->toKeep->contact)); + + str = ZOOM_options_get(p->options, "contact-name"); + req->u.esRequest->toKeep->contact->name = str ? + nmem_strdup (p->odr_out->mem, str) : 0; + + str = ZOOM_options_get(p->options, "contact-phone"); + req->u.esRequest->toKeep->contact->phone = str ? + nmem_strdup (p->odr_out->mem, str) : 0; + + str = ZOOM_options_get(p->options, "contact-email"); + req->u.esRequest->toKeep->contact->email = str ? + nmem_strdup (p->odr_out->mem, str) : 0; + + req->u.esRequest->toKeep->addlBilling = 0; + + /* not to keep part ... */ + req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *) + odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep)); + + req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *) + odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem)); + + str = ZOOM_options_get(p->options, "itemorder-setname"); + if (!str) + str = "default"; + req->u.esRequest->notToKeep->resultSetItem->resultSetId = + nmem_strdup (p->odr_out->mem, str); + req->u.esRequest->notToKeep->resultSetItem->item = + (int *) odr_malloc(p->odr_out, sizeof(int)); + + str = ZOOM_options_get(p->options, "itemorder-item"); + *req->u.esRequest->notToKeep->resultSetItem->item = + (str ? atoi(str) : 1); + + req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p); + + return req; +} + +ZOOM_API(void) + ZOOM_package_send (ZOOM_package p, const char *type) +{ + Z_APDU *apdu = 0; + ZOOM_connection c; + if (!p) + return; + c = p->connection; + odr_reset (p->odr_out); + xfree (p->buf_out); + p->buf_out = 0; + if (!strcmp(type, "itemorder")) + { + Z_External *r; + apdu = create_es_package (p, VAL_ITEMORDER); + if (apdu) + { + r = odr_malloc (p->odr_out, sizeof(*r)); + + r->direct_reference = + yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV, + VAL_ITEMORDER); + r->descriptor = 0; + r->which = Z_External_itemOrder; + r->indirect_reference = 0; + r->u.itemOrder = encode_item_order (p); + + apdu->u.extendedServicesRequest->taskSpecificParameters = r; + } + } + if (apdu) + { + if (encode_APDU(p->connection, apdu, p->odr_out) == 0) + { + char *buf; + + ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_PACKAGE); + task->u.package = p; + buf = odr_getbuf(p->odr_out, &p->len_out, 0); + p->buf_out = xmalloc (p->len_out); + memcpy (p->buf_out, buf, p->len_out); + + (p->refcount)++; + if (!c->async) + { + while (ZOOM_event (1, &c)) + ; + } + } + } +} + +ZOOM_API(ZOOM_package) + ZOOM_connection_package (ZOOM_connection c, ZOOM_options options) +{ + ZOOM_package p = (ZOOM_package) xmalloc (sizeof(*p)); + + p->connection = c; + p->odr_out = odr_createmem (ODR_ENCODE); + p->options = ZOOM_options_create_with_parent2 (options, c->options); + p->refcount = 1; + p->buf_out = 0; + p->len_out = 0; + return p; +} + +ZOOM_API(void) + ZOOM_package_destroy(ZOOM_package p) +{ + if (!p) + return; + (p->refcount)--; + if (p->refcount == 0) + { + odr_destroy (p->odr_out); + xfree (p->buf_out); + + ZOOM_options_destroy (p->options); + xfree (p); + } +} + +ZOOM_API(const char *) +ZOOM_package_option_get (ZOOM_package p, const char *key) +{ + return ZOOM_options_get (p->options, key); +} + +ZOOM_API(void) +ZOOM_package_option_set (ZOOM_package p, const char *key, + const char *val) +{ + ZOOM_options_set (p->options, key, val); +} + +static int ZOOM_connection_exec_task (ZOOM_connection c) +{ + ZOOM_task task = c->tasks; + + yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task"); + if (!task) + return 0; + if (c->error != ZOOM_ERROR_NONE || + (!c->cs && task->which != ZOOM_TASK_CONNECT)) + { + ZOOM_connection_remove_tasks (c); + return 0; + } + yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d run=%d", + task->which, task->running); + if (task->running) + return 0; + task->running = 1; + switch (task->which) + { + case ZOOM_TASK_SEARCH: + /* see if search hasn't been sent yet. */ + if (ZOOM_connection_send_search (c)) + return 1; + break; + case ZOOM_TASK_RETRIEVE: + if (send_present (c)) + return 1; + break; + case ZOOM_TASK_CONNECT: + if (do_connect(c)) + return 1; + break; + case ZOOM_TASK_SCAN: + if (send_scan(c)) + return 1; + break; + case ZOOM_TASK_PACKAGE: + if (send_package(c)) + return 1; + } + ZOOM_connection_remove_task (c); + return 0; +} + +static int send_sort_present (ZOOM_connection c) +{ + int r = send_sort (c); + if (!r) + r = send_present (c); + return r; +} + +static int es_response (ZOOM_connection c, + Z_ExtendedServicesResponse *res) +{ + if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE) + return 0; + if (res->diagnostics && res->num_diagnostics > 0) + response_diag(c, res->diagnostics[0]); + if (res->taskPackage && + res->taskPackage->which == Z_External_extendedService) + { + Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService; + Odr_oct *id = taskPackage->targetReference; + + if (id) + ZOOM_options_setl (c->tasks->u.package->options, + "targetReference", id->buf, id->len); + } + return 1; +} + + +static void handle_apdu (ZOOM_connection c, Z_APDU *apdu) +{ + Z_InitResponse *initrs; + + c->mask = 0; + yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which); + switch(apdu->which) + { + case Z_APDU_initResponse: + initrs = apdu->u.initResponse; + if (!*initrs->result) + { + c->error = ZOOM_ERROR_INIT; + } + else + { + char *cookie = + yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo, + VAL_COOKIE, 1, 0); + xfree (c->cookie_in); + c->cookie_in = 0; + if (cookie) + c->cookie_in = xstrdup(cookie); + if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) && + ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3)) + c->support_named_resultsets = 1; + if (c->tasks) + { + assert (c->tasks->which == ZOOM_TASK_CONNECT); + ZOOM_connection_remove_task (c); + } + ZOOM_connection_exec_task (c); + } + if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel)) + { + NMEM tmpmem = nmem_create(); + Z_CharSetandLanguageNegotiation *p = + yaz_get_charneg_record(initrs->otherInfo); + + if (p) + { + char *charset=NULL, *lang=NULL; + int selected; + + yaz_get_response_charneg(tmpmem, p, &charset, &lang, &selected); + yaz_log(LOG_DEBUG, "Target accepted: charset - %s," + "language - %s, select - %d", + charset, lang, selected); + + nmem_destroy(tmpmem); + } + } + break; + case Z_APDU_searchResponse: + handle_search_response (c, apdu->u.searchResponse); + if (!send_sort_present (c)) + ZOOM_connection_remove_task (c); + break; + case Z_APDU_presentResponse: + handle_present_response (c, apdu->u.presentResponse); + if (!send_present (c)) + ZOOM_connection_remove_task (c); + break; + case Z_APDU_sortResponse: + sort_response (c, apdu->u.sortResponse); + if (!send_present (c)) + ZOOM_connection_remove_task (c); + break; + case Z_APDU_scanResponse: + scan_response (c, apdu->u.scanResponse); + ZOOM_connection_remove_task (c); + break; + case Z_APDU_extendedServicesResponse: + es_response (c, apdu->u.extendedServicesResponse); + ZOOM_connection_remove_task (c); + break; + case Z_APDU_close: + if (c->reconnect_ok) + { + do_close(c); + c->tasks->running = 0; + ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT); + } + else + { + c->error = ZOOM_ERROR_CONNECTION_LOST; + do_close(c); + } + break; + default: + c->error = ZOOM_ERROR_DECODE; + do_close(c); + } +} + +static int do_read (ZOOM_connection c) +{ + int r; + Z_APDU *apdu; + ZOOM_Event event; + + event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA); + ZOOM_connection_put_event (c, event); + + yaz_log (LOG_DEBUG, "do_read len=%d", c->len_in); + + r = cs_get (c->cs, &c->buf_in, &c->len_in); + if (r == 1) + return 0; + if (r <= 0) + { + if (c->reconnect_ok) + { + do_close (c); + c->reconnect_ok = 0; + yaz_log (LOG_DEBUG, "reconnect read"); + c->tasks->running = 0; + ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT); + } + else + { + c->error= ZOOM_ERROR_CONNECTION_LOST; + do_close (c); + } + } + else + { + ZOOM_Event event; + odr_reset (c->odr_in); + odr_setbuf (c->odr_in, c->buf_in, r, 0); + event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU); + ZOOM_connection_put_event (c, event); + if (!z_APDU (c->odr_in, &apdu, 0, 0)) + { + c->error = ZOOM_ERROR_DECODE; + do_close (c); + } + else + handle_apdu (c, apdu); + c->reconnect_ok = 0; + } + return 1; +} + +static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out) +{ + int r; + ZOOM_Event event; + + event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA); + ZOOM_connection_put_event (c, event); + + yaz_log (LOG_DEBUG, "do_write_ex len=%d", len_out); + if ((r=cs_put (c->cs, buf_out, len_out)) < 0) + { + if (c->reconnect_ok) + { + do_close (c); + c->reconnect_ok = 0; + yaz_log (LOG_DEBUG, "reconnect write"); + c->tasks->running = 0; + ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT); + return 0; + } + if (c->state == STATE_CONNECTING) + c->error = ZOOM_ERROR_CONNECT; + else + c->error = ZOOM_ERROR_CONNECTION_LOST; + do_close (c); + return 1; + } + else if (r == 1) + { + c->mask = ZOOM_SELECT_EXCEPT; + if (c->cs->io_pending & CS_WANT_WRITE) + c->mask += ZOOM_SELECT_WRITE; + if (c->cs->io_pending & CS_WANT_READ) + c->mask += ZOOM_SELECT_READ; + yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask); + } + else + { + // c->reconnect_ok = 0; + c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT; + yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask); + } + return 0; +} + +static int do_write(ZOOM_connection c) +{ + return do_write_ex (c, c->buf_out, c->len_out); +} + + +ZOOM_API(const char *) +ZOOM_connection_option_get (ZOOM_connection c, const char *key) +{ + return ZOOM_options_get (c->options, key); +} + +ZOOM_API(void) +ZOOM_connection_option_set (ZOOM_connection c, const char *key, + const char *val) +{ + ZOOM_options_set (c->options, key, val); +} + +ZOOM_API(const char *) +ZOOM_resultset_option_get (ZOOM_resultset r, const char *key) +{ + return ZOOM_options_get (r->options, key); +} + +ZOOM_API(void) +ZOOM_resultset_option_set (ZOOM_resultset r, const char *key, + const char *val) +{ + ZOOM_options_set (r->options, key, val); +} + + +ZOOM_API(int) +ZOOM_connection_errcode (ZOOM_connection c) +{ + return ZOOM_connection_error (c, 0, 0); +} + +ZOOM_API(const char *) +ZOOM_connection_errmsg (ZOOM_connection c) +{ + const char *msg; + ZOOM_connection_error (c, &msg, 0); + return msg; +} + +ZOOM_API(const char *) +ZOOM_connection_addinfo (ZOOM_connection c) +{ + const char *addinfo; + ZOOM_connection_error (c, 0, &addinfo); + return addinfo; +} + +ZOOM_API(int) +ZOOM_connection_error (ZOOM_connection c, const char **cp, + const char **addinfo) +{ + int error = c->error; + if (cp) + { + switch (error) + { + case ZOOM_ERROR_NONE: + *cp = "No error"; break; + case ZOOM_ERROR_CONNECT: + *cp = "Connect failed"; break; + case ZOOM_ERROR_MEMORY: + *cp = "Out of memory"; break; + case ZOOM_ERROR_ENCODE: + *cp = "Encoding failed"; break; + case ZOOM_ERROR_DECODE: + *cp = "Decoding failed"; break; + case ZOOM_ERROR_CONNECTION_LOST: + *cp = "Connection lost"; break; + case ZOOM_ERROR_INIT: + *cp = "Init rejected"; break; + case ZOOM_ERROR_INTERNAL: + *cp = "Internal failure"; break; + case ZOOM_ERROR_TIMEOUT: + *cp = "Timeout"; break; + default: + *cp = diagbib1_str (error); + } + } + if (addinfo) + { + if (c->addinfo) + *addinfo = c->addinfo; + else + *addinfo = ""; + } + return c->error; +} + +static int ZOOM_connection_do_io(ZOOM_connection c, int mask) +{ + ZOOM_Event event = 0; + int r = cs_look(c->cs); + yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d", + c, mask, r); + + if (r == CS_NONE) + { + event = ZOOM_Event_create (ZOOM_EVENT_CONNECT); + c->error = ZOOM_ERROR_CONNECT; + do_close (c); + ZOOM_connection_put_event (c, event); + } + else if (r == CS_CONNECT) + { + int ret; + event = ZOOM_Event_create (ZOOM_EVENT_CONNECT); + + ret = cs_rcvconnect (c->cs); + yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret); + if (ret == 1) + { + c->mask = ZOOM_SELECT_EXCEPT; + if (c->cs->io_pending & CS_WANT_WRITE) + c->mask += ZOOM_SELECT_WRITE; + if (c->cs->io_pending & CS_WANT_READ) + c->mask += ZOOM_SELECT_READ; + ZOOM_connection_put_event (c, event); + } + else if (ret == 0) + { + ZOOM_connection_put_event (c, event); + ZOOM_connection_send_init (c); + c->state = STATE_ESTABLISHED; + } + else + { + c->error = ZOOM_ERROR_CONNECT; + do_close (c); + ZOOM_connection_put_event (c, event); + } + } + else + { + if (mask & ZOOM_SELECT_READ) + do_read (c); + if (c->cs && (mask & ZOOM_SELECT_WRITE)) + do_write (c); + } + return 1; +} + +ZOOM_API(int) +ZOOM_connection_last_event(ZOOM_connection cs) +{ + if (!cs) + return ZOOM_EVENT_NONE; + return cs->last_event; +} + +ZOOM_API(int) +ZOOM_event (int no, ZOOM_connection *cs) +{ + int timeout = 5000; +#if HAVE_SYS_POLL_H + struct pollfd pollfds[1024]; + ZOOM_connection poll_cs[1024]; +#else + struct timeval tv; + fd_set input, output, except; +#endif + int i, r, nfds; + int max_fd = 0; + + for (i = 0; ioptions, "timeout", -1); + if (this_timeout != -1 && this_timeout < timeout) + timeout = this_timeout; +#if HAVE_SYS_POLL_H + if (mask) + { + short poll_events = 0; + + if (mask & ZOOM_SELECT_READ) + poll_events += POLLIN; + if (mask & ZOOM_SELECT_WRITE) + poll_events += POLLOUT; + if (mask & ZOOM_SELECT_EXCEPT) + poll_events += POLLERR; + pollfds[nfds].fd = fd; + pollfds[nfds].events = poll_events; + pollfds[nfds].revents = 0; + poll_cs[nfds] = c; + nfds++; + } +#else + if (mask & ZOOM_SELECT_READ) + { + FD_SET (fd, &input); + nfds++; + } + if (mask & ZOOM_SELECT_WRITE) + { + FD_SET (fd, &output); + nfds++; + } + if (mask & ZOOM_SELECT_EXCEPT) + { + FD_SET (fd, &except); + nfds++; + } +#endif + } + if (timeout >= 5000) + timeout = 30; + + if (!nfds) + return 0; + +#if HAVE_SYS_POLL_H + r = poll (pollfds, nfds, timeout * 1000); + for (i = 0; imask) + { + int mask = 0; + if (pollfds[i].revents & POLLIN) + mask += ZOOM_SELECT_READ; + if (pollfds[i].revents & POLLOUT) + mask += ZOOM_SELECT_WRITE; + if (pollfds[i].revents & POLLERR) + mask += ZOOM_SELECT_EXCEPT; + if (mask) + ZOOM_connection_do_io(c, mask); + } + else if (r == 0 && c->mask) + { + ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT); + /* timeout and this connection was waiting */ + c->error = ZOOM_ERROR_TIMEOUT; + do_close (c); + ZOOM_connection_put_event(c, event); + } + } +#else + tv.tv_sec = timeout; + tv.tv_usec = 0; + yaz_log (LOG_DEBUG, "select start"); + r = select (max_fd+1, &input, &output, &except, &tv); + yaz_log (LOG_DEBUG, "select stop, returned r=%d", r); + for (i = 0; imask) + { + /* no timeout and real socket */ + if (FD_ISSET(fd, &input)) + mask += ZOOM_SELECT_READ; + if (FD_ISSET(fd, &output)) + mask += ZOOM_SELECT_WRITE; + if (FD_ISSET(fd, &except)) + mask += ZOOM_SELECT_EXCEPT; + if (mask) + ZOOM_connection_do_io(c, mask); + } + if (r == 0 && c->mask) + { + ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT); + /* timeout and this connection was waiting */ + c->error = ZOOM_ERROR_TIMEOUT; + do_close (c); + yaz_log (LOG_DEBUG, "timeout"); + ZOOM_connection_put_event(c, event); + } + } +#endif + for (i = 0; i +#include +#include + +#include "zoom-p.h" + +ZOOM_API(ZOOM_options) +ZOOM_options_create_with_parent (ZOOM_options parent) +{ + return ZOOM_options_create_with_parent2(parent, 0); +} + +ZOOM_API(ZOOM_options) +ZOOM_options_create (void) +{ + return ZOOM_options_create_with_parent (0); +} + + +ZOOM_API(ZOOM_options) +ZOOM_options_create_with_parent2 (ZOOM_options parent1, ZOOM_options parent2) +{ + ZOOM_options opt = (ZOOM_options) xmalloc (sizeof(*opt)); + + opt->refcount = 1; + opt->callback_func = 0; + opt->callback_handle = 0; + opt->entries = 0; + opt->parent1= parent1; + if (parent1) + (parent1->refcount)++; + opt->parent2= parent2; + if (parent2) + (parent2->refcount)++; + return opt; +} + + +void ZOOM_options_addref (ZOOM_options opt) +{ + (opt->refcount)++; +} + +ZOOM_API(ZOOM_options_callback) +ZOOM_options_set_callback ( + ZOOM_options opt, + ZOOM_options_callback callback_func, + void *callback_handle) +{ + ZOOM_options_callback callback_old; + + assert (opt); + callback_old = opt->callback_func; + opt->callback_func = callback_func; + opt->callback_handle = callback_handle; + return callback_old; +} + +ZOOM_API(void) +ZOOM_options_destroy (ZOOM_options opt) +{ + if (!opt) + return; + (opt->refcount)--; + if (opt->refcount == 0) + { + struct ZOOM_options_entry *e; + + ZOOM_options_destroy (opt->parent1); + ZOOM_options_destroy (opt->parent2); + e = opt->entries; + while (e) + { + struct ZOOM_options_entry *e0 = e; + xfree (e->name); + xfree (e->value); + e = e->next; + xfree (e0); + } + xfree (opt); + } +} + +ZOOM_API(void) +ZOOM_options_setl (ZOOM_options opt, const char *name, const char *value, + int len) +{ + struct ZOOM_options_entry **e; + + e = &opt->entries; + while (*e) + { + if (!strcmp((*e)->name, name)) + { + xfree ((*e)->value); + (*e)->value = 0; + if (value) + { + (*e)->value = xmalloc (len+1); + memcpy ((*e)->value, value, len); + (*e)->value[len] = '\0'; + } + return; + } + e = &(*e)->next; + } + *e = (struct ZOOM_options_entry *) xmalloc (sizeof(**e)); + (*e)->name = xstrdup (name); + (*e)->value = 0; + if (value) + { + (*e)->value = xmalloc (len+1); + memcpy ((*e)->value, value, len); + (*e)->value[len] = '\0'; + } + (*e)->next = 0; +} + +ZOOM_API(void) +ZOOM_options_set (ZOOM_options opt, const char *name, const char *value) +{ + ZOOM_options_setl (opt, name, value, value ? strlen(value): 0); +} + +ZOOM_API(const char *) +ZOOM_options_get (ZOOM_options opt, const char *name) +{ + const char *v = 0; + if (!opt) + return 0; + if (opt->callback_func) + v = (*opt->callback_func)(opt->callback_handle, name); + if (!v) + { + struct ZOOM_options_entry *e; + for (e = opt->entries; e; e = e->next) + if (!strcmp(e->name, name)) + { + v = e->value; + break; + } + } + if (!v) + v = ZOOM_options_get(opt->parent1, name); + if (!v) + v = ZOOM_options_get(opt->parent2, name); + return v; +} + +ZOOM_API(int) +ZOOM_options_get_bool (ZOOM_options opt, const char *name, int defa) +{ + const char *v = ZOOM_options_get (opt, name); + + if (!v) + return defa; + if (!strcmp (v, "1") || !strcmp(v, "T")) + return 1; + return 0; +} + +ZOOM_API(int) +ZOOM_options_get_int (ZOOM_options opt, const char *name, int defa) +{ + const char *v = ZOOM_options_get (opt, name); + + if (!v || !*v) + return defa; + return atoi(v); +} + +ZOOM_API(void) +ZOOM_options_set_int(ZOOM_options opt, const char *name, int value) +{ + char s[40]; + + sprintf (s, "%d", value); + ZOOM_options_set (opt, name, s); +} diff --git a/zutil/zoom-p.h b/zutil/zoom-p.h new file mode 100644 index 0000000..b6cf2ae --- /dev/null +++ b/zutil/zoom-p.h @@ -0,0 +1,157 @@ +/* + * Private C header for ZOOM C. + * $Id: zoom-p.h,v 1.1 2002-09-16 18:45:14 adam Exp $ + */ +#include +#include +#include +#include +#include + +typedef struct ZOOM_Event_p *ZOOM_Event; + +struct ZOOM_query_p { + Z_Query *query; + Z_SortKeySpecList *sort_spec; + int refcount; + ODR odr; +}; + +#define STATE_IDLE 0 +#define STATE_CONNECTING 1 +#define STATE_ESTABLISHED 2 + +#define ZOOM_SELECT_READ 1 +#define ZOOM_SELECT_WRITE 2 +#define ZOOM_SELECT_EXCEPT 4 + +struct ZOOM_connection_p { + COMSTACK cs; + char *host_port; + int error; + char *addinfo; + int state; + int mask; + int reconnect_ok; + ODR odr_in; + ODR odr_out; + char *buf_in; + int len_in; + char *buf_out; + int len_out; + char *proxy; + char *charset; + char *lang; + char *cookie_out; + char *cookie_in; + char *client_IP; + int async; + int support_named_resultsets; + int last_event; + ZOOM_task tasks; + ZOOM_options options; + ZOOM_resultset resultsets; + ZOOM_Event m_queue_front; + ZOOM_Event m_queue_back; +}; + + +struct ZOOM_options_entry { + char *name; + char *value; + struct ZOOM_options_entry *next; +}; + +struct ZOOM_options_p { + int refcount; + void *callback_handle; + ZOOM_options_callback callback_func; + struct ZOOM_options_entry *entries; + ZOOM_options parent1; + ZOOM_options parent2; +}; + +typedef struct ZOOM_record_cache_p *ZOOM_record_cache; + +struct ZOOM_resultset_p { + Z_Query *r_query; + Z_SortKeySpecList *r_sort_spec; + ZOOM_query search; + int refcount; + int size; + int start; + int count; + int piggyback; + char *setname; + ODR odr; + ZOOM_record_cache record_cache; + ZOOM_options options; + ZOOM_connection connection; + ZOOM_resultset next; +}; + +struct ZOOM_record_p { + ODR odr; + WRBUF wrbuf_marc; + Z_NamePlusRecord *npr; +}; + +struct ZOOM_record_cache_p { + struct ZOOM_record_p rec; + char *elementSetName; + char *syntax; + int pos; + ZOOM_record_cache next; +}; + +struct ZOOM_scanset_p { + int refcount; + ODR odr; + ZOOM_options options; + ZOOM_connection connection; + Z_AttributesPlusTerm *termListAndStartPoint; + Z_AttributeSetId *attributeSet; + Z_ScanResponse *scan_response; +}; + +struct ZOOM_package_p { + int refcount; + ODR odr_out; + ZOOM_options options; + ZOOM_connection connection; + char *buf_out; + int len_out; +}; + +struct ZOOM_task_p { + int running; + int which; + union { +#define ZOOM_TASK_SEARCH 1 + struct { + ZOOM_resultset resultset; + } search; +#define ZOOM_TASK_RETRIEVE 2 + struct { + int start; + ZOOM_resultset resultset; + int count; + } retrieve; +#define ZOOM_TASK_CONNECT 3 +#define ZOOM_TASK_SCAN 4 + struct { + ZOOM_scanset scan; + } scan; +#define ZOOM_TASK_PACKAGE 5 + ZOOM_package package; + } u; + ZOOM_task next; +}; + +struct ZOOM_Event_p { + int kind; + ZOOM_Event next; + ZOOM_Event prev; +}; + +void ZOOM_options_addref (ZOOM_options opt);